Hello !
I have recently started my journey with Quantconnect and I am trying to implement my first, simple strategy. Although I managed to complete (hopefully) the majority of work needed, there is a problem with rolling windows which I can't solve,
The strategy should work as follows:
- calculate sum of 2 securities from 2 days ago
- calculate sum of these securities from 1 day ago
- if the sum of the previous day is higher than from 2 days ago, then open specific positions
- if its lower, open positions in opposite directions
The problem is, I don't know how to “attach” rolling window function to specific securities. As you can see in lines 30-35, I tried to collect closing prices, but do not know how to show the function, closing prices of which security I am looking for.
I would really aprreciate any help/tips :)
class CustomIndexStrategy(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 2, 3)
self.SetCash(100000)
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
self.AUDCHF = self.AddForex("AUDCHF", Resolution.Daily, Market.Oanda)
self.USDZAR = self.AddForex("USDZAR", Resolution.Daily, Market.Oanda)
self.SetBenchmark("SPY")
self.rollingWindow = RollingWindow[TradeBar](2) #rolling window indicator to check prices of last 2 days
def OnData(self,data):
#check if rolling window is ready
if not self.rollingWindows.IsReady:
return
# Add new candles to rolling windows
self.rollingWindow.Add(data["AUDCHF"])
self.rollingWindow.Add(data["USDZAR"])
#Get closing prices from 2 days ago
close_2D_AUDCHF = self.rollingWindow [self.rollingWindow.Count-2].Close
close_2D_USDZAR = self.rollingWindow [self.rollingWindow.Count-2].Close
#get closing prices from 1 day ago
cloes_1D_AUDCHF = self.rollingWindow [self.rollingWindow.Count-1].Close
close_1D_USDZAR = self.rollingWindow [self.rollingWindow.Count-1].Close
#sum of closing prices of AUD and USDZAR from 2 days ago
two_day_Sum = close_2D_AUDCHF + close_2D_USDZAR
#sum of closing prices of AUD and USDZAR from 1 day ago
one_day_Sum = cloes_1D_AUDCHF + close_1D_USDZAR
# check if we have any open positions
if self.Portfolio.Invested:
return
#trading rule 1: if sum of closing prices from 1 day ago is lower than from 2 days ago:
if not self.Portfolio.Invested:
if one_day_Sum < two_day_Sum:
self.MarketOrder("USDZAR", -2000,0.95*self.Securities["AUDCHF"].Close)
self.MarketOrder("AUDUSD", 2000,0.95*self.Securities["AUDCHF"].Close)
#trading rule 2: if sum of closing prices from 1 day ago is higher than from 2 days ago:
if one_day_Sum > two_day_Sum:
self.MarketOrder("USDZAR", 2000, 0.95*self.Securities["AUDCHF"].Close)
self.MarketOrder("AUDUSD", -2000, 0.95*self.Securities["AUDCHF"].Close)
pass
Sebul
Sorry, I meant lines 25-31, not 30-35 😊
Louis Szeto
Hi Sebul
You'll need separate RollingWindow object for each symbol. We recommend storing the RollingWindow for each symbol in a dictionary:
BTW forex data don't have TradeBar data, you might want to use QuoteBar instead. (docs)
Best
Louis
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.
Sebul
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!