| Overall Statistics |
|
Total Trades 200 Average Win 42.26% Average Loss -8.74% Compounding Annual Return -88.414% Drawdown 100.000% Expectancy -0.324 Net Profit -99.982% Sharpe Ratio 0.528 Loss Rate 88% Win Rate 12% Profit-Loss Ratio 4.84 Alpha -2.482 Beta 230.934 Annual Standard Deviation 2.371 Annual Variance 5.623 Information Ratio 0.522 Tracking Error 2.371 Treynor Ratio 0.005 Total Fees $2348.32 |
import numpy as np
import decimal as d
import math
class BasicTemplateAlgorithm(QCAlgorithm):
def Initialize(self):
self.symbol = "EURUSD"
self.SetStartDate(2013,01,01) #Set Start Date
self.SetEndDate(2016,12,31) #Set End Date
self.SetCash(100000) #Set Strategy Cash
self.SetBrokerageModel(BrokerageName.FxcmBrokerage)
self.AddSecurity(SecurityType.Forex, self.symbol, Resolution.Hour)
#Indicators in strategy
self.atr_value = self.ATR(self.symbol, 14, MovingAverageType.Exponential, Resolution.Hour)
#Candlestick patterns in strategy
self.hammer_pattern = self.CandlestickPatterns.Hammer(self.symbol, Resolution.Hour)
self.doji_pattern = self.CandlestickPatterns.Doji(self.symbol, Resolution.Hour)
ema_slow = 20
self.slow = self.EMA(self.symbol, ema_slow, Resolution.Hour)
self.var_bb = self.BB(self.symbol, 20, 2, MovingAverageType.Exponential, Resolution.Hour)
#Plotting
self.should_i_plot = 0 #Change to 1 if you want to plot.
if self.should_i_plot == 1:
stockPlot = Chart('Trade Plot')
# On the Trade Plotter Chart we want 3 series: trades and price:
stockPlot.AddSeries(Series('Price', SeriesType.Candle, 0))
self.AddChart(stockPlot)
def OnData(self, data):
#Install positions size algo depending on portfolio size
#Welcome Zijian, you can do this with SetHoldings("Symbol", Fraction); this will calculate the required order size and place a market order.
#If you want to do this manually you can use CalculateOrderQuantity(symbol, targetFraction) which will return the number of shares you need to purchase.
#Long, Entry Condition 1, Hammer formation below bb middleband.
if self.hammer_pattern.Current.Value > 0 and self.Securities[self.symbol].Price < self.var_bb.MiddleBand.Current.Value:
posSize = self.CalculateOrderQuantity(self.symbol, 1)
self.Log('Hammer below middleband, BUY >> {0}'.format(self.Securities[self.symbol].Price))
self.Log('Position size >> {0}'.format(posSize))
#d.getcontext().prec = 4
stopPrice = d.Decimal(self.Securities[self.symbol].High)
limitPrice = d.Decimal(stopPrice) + d.Decimal(0.0015)
newTicket = self.StopLimitOrder(self.symbol, posSize, stopPrice, limitPrice)
#Stoploss, Condition 1.
#d.getcontext().prec = 4
tradePrice = self.Securities[self.symbol].Price
stopPrice = tradePrice - self.atr_value.Current.Value
newTicket = self.StopMarketOrder(self.symbol, posSize*(-1), stopPrice)
#Trailing Stop, Condition 1.
#round(2.665, 2)
#d.getcontext().prec = 5
#>>> Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_DOWN)
#Decimal('7.32')
#>>> Decimal('7.325').quantize(Decimal('1.'), rounding=ROUND_UP)
#Decimal('8')
#Long, Entry Condition 2, doji below lowerband
#if self.doji_pattern.Current.Value > 0 and self.Securities[self.symbol].Price < self.var_bb.LowerBand.Current.Value:
# self.SetHoldings(self.symbol, 1.0, True)
# self.Log('doji below lowerband, BUY >> {0}'.format(self.Securities[self.symbol].Price))
#Plot the asset price.
if self.should_i_plot == 1:
self.lastPrice = self.Portfolio[self.symbol].Price
self.Plot('Asset price', 'Price', self.lastPrice)
#if not self.Portfolio.Invested:
# self.SetHoldings(self.symbol, 1)