Hello ! 

I post my question again, as last time I used “LEAN” tag in my question, and it was moved to the “Lean Issues” category, not to the “Newest”.

 

So 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 25-31, 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

Author