| Overall Statistics |
|
Total Trades 1469 Average Win 0.07% Average Loss -0.05% Compounding Annual Return -6.345% Drawdown 10.700% Expectancy -0.320 Net Profit -10.537% Sharpe Ratio -3.154 Probabilistic Sharpe Ratio 0.000% Loss Rate 72% Win Rate 28% Profit-Loss Ratio 1.39 Alpha -0.052 Beta -0.008 Annual Standard Deviation 0.017 Annual Variance 0 Information Ratio -1.358 Tracking Error 0.128 Treynor Ratio 6.72 Total Fees $0.00 |
import pandas as pd
class ResistanceVerticalInterceptor(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018, 1, 1) # Set Start Date
self.SetStartDate(2018, 6, 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.2
self.symbols = {}
self.states = {}
self.price = {}
self.orderstop = {}
self.stoploss= {}
self.profittaking = {}
self.SetWarmUp(2)
self.market = Market.Oanda
for i in range(0,len(self.assets)):
self.symbols[self.assets[i]] = self.AddForex(self.assets[i], Resolution.Hour, self.market)
self.states[self.assets[i]] = 0
self.price[self.assets[i]] = 0
self.orderstop[self.assets[i]] = 0
self.stoploss[self.assets[i]] = 0
self.profittaking[self.assets[i]] = 0
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
history = self.History(self.Securities.Keys, 2, Resolution.Hour)
value = self.Portfolio.TotalPortfolioValue
cash = self.Portfolio.Cash
# go over all tickers
for symbol in self.assets:
self.holdings = self.Portfolio[symbol].Quantity
self.price[symbol] = self.Portfolio[symbol].AveragePrice
try:
O1 = history.loc[symbol].open[0] #open first candle
C1 = history.loc[symbol].close[0] #close first candle
L1 = history.loc[symbol].low[0] #low first candle
H1 = history.loc[symbol].high[0] #high first candle
O2 = history.loc[symbol].open[1] #open second candle
C2 = history.loc[symbol].close[1] #close second candle
L2 = history.loc[symbol].low[1] #low second candle
H2 = history.loc[symbol].high[1] #high second candle
W = H2 - L2 #range
except:
self.Log('>> {} >> Missing Data')
return
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")