| Overall Statistics |
|
Total Trades 7297 Average Win 0.01% Average Loss -0.02% Compounding Annual Return -2.588% Drawdown 10.400% Expectancy -0.195 Net Profit -10.313% Sharpe Ratio -2.108 Probabilistic Sharpe Ratio 0.000% Loss Rate 59% Win Rate 41% Profit-Loss Ratio 0.95 Alpha -0.021 Beta -0.001 Annual Standard Deviation 0.01 Annual Variance 0 Information Ratio -1.206 Tracking Error 0.115 Treynor Ratio 34.569 Total Fees $0.00 |
import pandas as pd
class ResistanceVerticalInterceptor(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2016, 1, 1) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.assets = ["GBPUSD"]
#some other assets ["EURGBP","GBPJPY", "USDJPY"]
self.count = len(self.assets)
self.risk = 0.1
self.price = {}
self.orderstop = {}
self.stoploss= {}
self.profittaking = {}
# create order tickets for each symbol
self.orderticket = {}
self.stoplossticket = {}
self.takeprofitticket = {}
self.reverseticket = {}
self.holdings = {}
self.SetWarmUp(2)
self.market = Market.Oanda
self.windows = {}
for ticker in self.assets:
symbol = self.AddForex(ticker, Resolution.Hour, self.market).Symbol
self.price[symbol] = 0
self.orderstop[symbol] = 0
self.stoploss[symbol] = 0
self.profittaking[symbol] = 0
self.windows[symbol] = RollingWindow[TradeBar](2)
self.holdings[symbol] = 0
self.orderticket[symbol] = 0
self.stoplossticket[symbol] = 0
self.takeprofitticket[symbol] = 0
self.reverseticket[symbol] = 0
def OnData(self, data):
value = self.Portfolio.TotalPortfolioValue
cash = self.Portfolio.Cash
# go over all tickers
for symbol in self.windows:
# this is needed for OnOrderEvent to cancel the right tickets
#self.symbol = symbol
if data.ContainsKey(symbol) and data.Bars[symbol] != None:
self.windows[symbol].Add(data.Bars[symbol])
self.holdings[symbol] = self.Portfolio[symbol].Quantity
self.price[symbol] = self.Portfolio[symbol].AveragePrice
self.invest = cash*self.risk
if not self.windows[symbol].IsReady:
continue
firstBar = self.windows[symbol][0]
O1 = firstBar.Open #open first candle
C1 = firstBar.Close #close first candle
L1 = firstBar.Low #low first candle
H1 = firstBar.High #high first candle
secondBar = self.windows[symbol][1]
O2 = secondBar.Open #open second candle
C2 = secondBar.Close #close second candle
L2 = secondBar.Low #low second candle
H2 = secondBar.High #high second candle
W = H2 - L2 #range
# if no prior holding
if self.holdings[symbol] == 0:
# go long if first green
if O1 < C1:
# and second contains first
if (L2 < L1) and (H2 > H1):
self.orderstop[symbol] = (H1 + 0.1 * W)
self.stoploss[symbol] = (H1 - 0.2* W)
self.profittaking[symbol] = (H1 + 0.8* W)
# calc how much to buy
self.quantity = int(self.invest/C2)
# stop order
self.orderticket[symbol] = self.MarketOrder(symbol, self.quantity)
# stop loss
self.stoplossticket[symbol] = self.StopMarketOrder(symbol, -self.quantity, self.orderstop[symbol])
# profit taking
self.takeprofitticket[symbol] = self.LimitOrder(symbol, -self.quantity, self.profittaking[symbol])
# message
self.Debug("Going long: " + str(symbol))
# skip to next symbol
continue
# go short if first red
elif O1 > C1:
# and second second contains first
if (L2 < L1) and (H2 > H1):
self.orderstop[symbol] = (L1 - 0.1 * W)
self.stoploss[symbol] = (L1 + 0.2* W)
self.profittaking[symbol] = (L1 - 0.8* W)
# calc how much to buy
#self.quantity = int(self.invest/self.orderstop[symbol] - self.stoploss[symbol])
self.quantity = int(self.invest/C2)
# stop order
self.orderticket[symbol] = self.MarketOrder(symbol, -self.quantity)
# stop loss
self.stoplossticket[symbol] = self.StopMarketOrder(symbol, self.quantity, self.orderstop[symbol])
# profit taking
self.takeprofitticket[symbol] = self.LimitOrder(symbol, self.quantity, self.profittaking[symbol])
# message
self.Debug("Going short: " + str(symbol))
# skip to next symbol
continue
# if long pattern
elif self.holdings[symbol] > 0:
# and then short pattern
if O1 > C1:
# and second second contains first
if (L2 < L1) and (H2 > H1):
self.reverseticket[symbol] = self.Liquidate(symbol)
self.Debug("Reversing long: " + str(symbol))
continue
# if short pattern
elif self.holdings[symbol] < 0:
# and long pattern
if O1 < C1:
# and second contains first
if (L2 < L1) and (H2 > H1):
self.reverseticket[symbol] = self.Liquidate(symbol)
self.Debug("Reversing short: " + str(symbol))
continue
def OnOrderEvent(self, orderEvent):
for symbol in self.windows:
try:
# if stoploss is triggered cancel profit taking
if self.stoplossticket[symbol].Status == OrderStatus.Filled:
self.takeprofitticket[symbol].Cancel
self.Debug("Stoploss triggered: " + str(symbol))
# if profit taking is triggered cancel stoploss
if self.takeprofitticket[symbol].Status == OrderStatus.Filled:
self.stoplossticket[symbol].Cancel
self.Debug("Takeprofit triggered: " + str(symbol))
# if position is reversed
if self.reverseticket[symbol].Status == OrderStatus.Filled:
self.stoplossticket[symbol].Cancel
self.takeprofitticket[symbol].Cancel
self.Debug("Position reversed: " + str(symbol))
except:
self.Debug("No Ticket for Symbol: " + str(symbol))