Back

How do I create a RollingWindow for decimals in Python?

I created a brand new algorithm and followed the documentation to create a RollingWindow of decimals.  Here is my code:

from decimal import Decimal

class ParticleMultidimensionalShield(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2018, 10, 8) # Set Start Date
self.SetCash(100000) # Set Strategy Cash

self.closeWindow = RollingWindow[Decimal](4)


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
'''

# if not self.Portfolio.Invested:
# self.SetHoldings("SPY", 1)

However, running this code returns an error:

During the algorithm initialization, the following exception has occurred: TypeError : type(s) expected
at Initialize in main.py:line 13
TypeError : type(s) expected

What's the problem?

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.


Hi Joseph.

In the method Initialize(), add a security using AddEquity:

# In Initialize
self.spy = self.AddEquity("SPY", Resolution.Daily)

The rolling arrays are updated by adding a new element using the Add method:

# In OnData
if data.ContainsKey("SPY"):
# Add SPY bar close in the rolling window
self.closeWindow.Add(data["SPY"].Close)

I've attached a backtest for you reference.

 

0


@Halldor,

Thanks for your response.

I added a security and copy/pasted the OnData from your backtest but it did not resolve the problem.

However, your backtest gave me a clue about the problem.

Actually, the problem stems from the import statement at the beginning of my code:

from decimal import Decimal

According to the documentation for RollingWindow in Python, we need to import Decimal.  It's even in the code snippet on the documentation page and labeled as "important":

from decimal import Decimal # Important: import decimal.Decimal class
self.closeWindow = RollingWindow[Decimal](4)
self.tradeBarWindow = RollingWindow[TradeBar](2)
self.quoteBarWindow = RollingWindow[QuoteBar](2)

But this actually breaks the code.  I think it's a good idea for QC to update this part of the documentation as it just led me to broken code and confusion.

Regards

0

Hi Joseph,

You're correct, and thank's for pointing this out and we've updated the documentation. It's a legacy from before updated LEAN so that decimal was compatible across C# and Python without the need for importing Decimal. Glad to hear the code is working for you now!

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.


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