Back

Novice struggling to use rolling window

Hello All,

I am trying to create a very simple test algorithm which buys one share of "SPY" six trading periods after the price closes at 302.01 (this occurred on 7/26/2019). I am doing this just to try to understand the basics of coding in Python on QuantConnect. Here is my code:

----------------------------------------------------------------------------------

class OptimizedModulatedCoil(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 3, 20)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        self.AddEquity("SPY", Resolution.Daily)


    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
        '''
        self.closeWindow = RollingWindow[float](10)
        self.closeWindow.Add(data["SPY"].Close)
        testParm = self.closeWindow[6]
        self.SetWarmUp(20, Resolution.Daily)
        if not self.Portfolio.Invested and testParm == 302.01:
            self.Buy("SPY", 1)

----------------------------------------------------------------------------------

 

...and here is the error message I keep recieving when I perform a backtest:

 

----------------------------------------------------------------------------------

Runtime Error: ArgumentOutOfRangeException : Index must be between 0 and 0 (entry 6 does not exist yet)
Parameter name: i
Actual value was 6.

----------------------------------------------------------------------------------

 

Any help that the community can offer will be greatly appreciated. I have created algorithms with Trading View's Pine Script language, but I am completely new to Quantconnect and Python.

 

Update Backtest







0

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.


Go to the Bootcamp and take the brand new Rolling Window tutorial.  It will show you that you need to define the RollingWindow in the initialize function, among other things.

1

Update Backtest





0

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.


Loading...

This discussion is closed