| Overall Statistics |
|
Total Trades 26 Average Win 0.01% Average Loss -0.03% Compounding Annual Return -0.108% Drawdown 0.200% Expectancy -0.747 Net Profit -0.231% Sharpe Ratio -0.758 Probabilistic Sharpe Ratio 0.000% Loss Rate 83% Win Rate 17% Profit-Loss Ratio 0.52 Alpha -0.001 Beta 0.001 Annual Standard Deviation 0.001 Annual Variance 0 Information Ratio -0.911 Tracking Error 0.147 Treynor Ratio -2.111 Total Fees $0.00 |
import pandas as pd
class ResistanceVerticalInterceptor(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018, 1, 1) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.assets = ["GBPUSD", "EURUSD"]
#some other assets ["EURGBP","GBPJPY", "USDJPY"]
self.count = len(self.assets)
self.risk = 0.2
self.symbols = {}
self.states = {}
self.price = {}
self.orderstop = {}
self.stoploss= {}
self.profittaking = {}
self.SetWarmUp(2)
self.market = Market.Oanda
self.windows = {}
for ticker in self.assets:
symbol = self.AddForex(ticker, Resolution.Hour, self.market).Symbol
self.states[symbol] = 0
self.price[symbol] = 0
self.orderstop[symbol] = 0
self.stoploss[symbol] = 0
self.profittaking[symbol] = 0
self.windows[symbol] = RollingWindow[TradeBar](2)
def OnData(self, data):
value = self.Portfolio.TotalPortfolioValue
cash = self.Portfolio.Cash
# go over all tickers
for symbol in self.windows:
if data.ContainsKey(symbol) and data.Bars[symbol] != None:
self.windows[symbol].Add(data.Bars[symbol])
self.holdings = self.Portfolio[symbol].Quantity
self.price[symbol] = self.Portfolio[symbol].AveragePrice
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 self.states[symbol] != 0:
# if long pattern
if self.states[symbol] == 1:
# and short pattern
if O1 > C1:
# and second second contains first
if (L2 < L1) and (H2 > H1):
self.Liquidate(symbol)
self.states[symbol] = 0
self.Debug("Liquidating long")
# if short pattern
if self.states[symbol] == -1:
# and long pattern
if O1 < C1:
# and second contains first
if (L2 < L1) and (H2 > H1):
self.Liquidate(symbol)
self.states[symbol] = 0
self.Debug("Liquidating short")
else:
self.Debug("Holding")
if self.states[symbol] == 0:
# go long if first green
if O1 < C1:
# and second contains first
if (L2 < L1) and (H2 > H1):
#self.SetHoldings(symbol, cash*self.risk)
self.orderstop[symbol] = (H1 + 0.1 * W)
self.stoploss[symbol] = (H1 - 0.2* W)
self.profittaking[symbol] = (H1 + 0.8* W)
# stop order
self.StopMarketOrder(symbol, cash*self.risk, self.orderstop[symbol])
# stoploss
self.StopMarketOrder(symbol, -cash*self.risk, self.stoploss[symbol])
# profit taking
self.LimitOrder(symbol, -cash*self.risk, self.profittaking[symbol])
self.states[symbol] = 1
self.Debug("Buy")
# go short if first red
if 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)
# stop order
self.StopMarketOrder(symbol, -cash*self.risk, self.orderstop[symbol])
# stoploss
self.StopMarketOrder(symbol, cash*self.risk, self.stoploss[symbol])
# profit taking
self.LimitOrder(symbol, cash*self.risk, self.profittaking[symbol])
self.states[symbol] = -1
self.Debug("Sell")