How do you save RollingWindow of data of multiple securities on 'class Symbol Data'? My objective is to create rolling windows of indicators as well as rolling windows that hold data for multiple securities, and save a reference of this objects on a dictionary.
Here's what i've managed so far, though not completely accurate.
class DeterminedRedOrangeAnt(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2015, 12, 1) # Set Start Date
self.SetEndDate(2020, 12, 1)
self.SetCash(100000) # Set Strategy Cash
#EURUSD", "USDJPY", "GBPUSD", "AUDUSD"
self.Data = {}
for ticker in ["GBPUSD", "EURUSD", "USDJPY", "AUDUSD"]:
symbol = self.AddForex(ticker, Resolution.Hour, Market.FXCM).Symbol
self.Data[symbol] = SymbolData(
self.ADX(symbol, 14, Resolution.Hour),
self.RSI(symbol, 14, Resolution.Hour),
self.closeWindow)
self.SetWarmUp(25, Resolution.Hour)
def OnData(self, data):
if self.IsWarmingUp:
return
for symbol, symbolData in self.Data.items():
lbbclose = self.closeWindow[20]
lbsclose = self.closeWindow[10]
ADX = symbolData.adx.Current.Value
RSI = symbolData.rsi.Current.Value
# Condition to begin if ADX value is greater than 25
if (not ADX > 25):
return
if not self.Portfolio[symbol].Invested:
current_price = data[symbol].Close
if RSI < 50 and current_price < lbbclose and current_price > lbsclose:
self.SetHoldings(symbol, 1)
self.Log(f"{self.Time} Entered Long Position at {current_price}")
if RSI > 50 and current_price > lbbclose and current_price < lbsclose:
self.SetHoldings(symbol, -1)
self.Log(f"{self.Time} Entered Short Position at {current_price}")
class SymbolData:
def __init__(self, adx, rsi, closeW):
self.adx = adx
self.rsi = rsi
self.closeW = closeW
self.adxWindow = RollingWindow[IndicatorDataPoint](2) #setting the Rolling Window for the fast SMA indicator, takes two values
self.adx.Updated += self.AdxUpdated #Updating those two values
self.rsiWindow = RollingWindow[IndicatorDataPoint](2) #setting the Rolling Window for the slow SMA indicator, takes two values
self.rsi.Updated += self.RsiUpdated #Updating those two values
self.closeWindow = RollingWindow[float](21)
self.closeW.Updated += self.CloseUpdated
def AdxUpdated(self, sender, updated):
'''Event holder to update the fast SMA Rolling Window values'''
if self.adx.IsReady:
self.fastSMAWindow.Add(updated)
def RsiUpdated(self, sender, updated):
'''Event holder to update the slow SMA Rolling Window values'''
if self.rsi.IsReady:
self.slowSMAWindow.Add(updated)
def CloseUpdated(self, sender, data):
'''Event holder to update the close Rolling Window values'''
if self.closeW.IsReady:
self.closeWindow.Add(data[symbol].Close)
i have not been able to update data on the rolling windows for the multiple securities.
i would appreciate any and all the help i can get, to get this algorithm running.
Derek Melchin
Hi Samwel,
The data stored in the SymbolData class should only be for 1 security. When we want to access all of the RollingWindows, we can just loop through our SymbolData collection.
See the attached backtest for reference. Note that a consolidator has been added to the algorithm to update the `closeW` RollingWindow.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Samwel Kibet
Hi Derek, Thanks for the help. Much appreciated! I managed to add some exit parameters on the algorithm, so as to increase the number of trades. The algo made 1021 trades within a period of 5 years, and on average 1 trade every two days.
Aside from having a good entry strategy, what other variables should i pay attention to /or adjust to improve the performance of my algorithm? What should i do to reduce the number of trades taken while increasing accuracy of each trade?
Derek Melchin
Hi Samwel,
> what other variables should i pay attention to /or adjust to improve the performance of my algorithm?
To boost the performance of the backtest, we could optimize the `stopLossLevel` and `stopProfitLevel`. In terms of other indicators to use, this Investopedia article offers some ideas worth exploring. For instance, the article points out "The Canadian dollar is heavily correlated with commodities, such as crude oil and metals."
> What should i do to reduce the number of trades taken while increasing accuracy of each trade?
To reduce the number of trades, we could increase the data resolution from hour to daily. In addition, adjusting the entry/exit conditions to incorporate more informative indicators or using the optimizer should increase the accuracy of each trade.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Samwel Kibet
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!