For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Check the order status of Multiple Stocks

  • @backtrader I found the issue of the key error looping over stocks in the INIT`` part of the strategy... its theif len(d)``` part of the enumerate loop:

    for i, d in enumerate(d for d in self.datas if len(d)):

    if you get rid of if len(d) the INIT works fine, and sets all the parameters to None.

    My question to @backtrader is, what is the purpose of len(d)? I thought it was just checking the length of the dataframe was >0 (i.e. contains data), however all of my dataframes contain data (checked their CSVs) so unsure why I get a key error whenif len(d) is included - and unsure why if len(d) its used at all?

    Could it be that len(d) is the number of days you are in the market? This appears like it may be the case from the tradeanalyzer analyzer output? But if thats the case... why would you use if len(d) in the strategy next() loop?

    Thank you and look forward to your response!!


  • administrators

    If the data feeds have different timeframes not all of them may start delivering at the same time. A 1-minute data feed will deliver faster than the 1-day counterpart.

    The 1-minute data feed may already have delivered 500 bars and the 1-day may still be waiting. Hence the len check to avoid pulling values from a data feed which hasn't still delivered.

    A data feed having values is not the same as a data feed having delivered values.

  • @backtrader but all my data frames are daily time frame?

  • administrators

    That's something you know and need not necessarily be true for other use cases. The code is written in a generic way which can also be applied during for example prenext

Log in to reply