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

Accessing data after finished run



  • Inside my strategy, I'm doing some algabraic caluclations and I want to be able to access those as arrays after my backtest run is finished.

    I tried creating an SMA(var ,period=1) but I still don't know how to access the data.

    I assuming it's somewhere in strategy.* but where? Any help?



  • You run your strategy as

    result = cerebro.run()
    

    Then result object contains everything.

    Reference Returning the results section in Cerebro



  • I've read the page though it's not clear to me to get access to the data. The documentation says do it like so.

    thestrats = cerebro.run(tradehistory=True)
    thestrat = thestrats[0]
    

    And then I've figured out how to get price data, trades, and orders

    # raw data
    open = thestrat.data_open.array
    high = thestrat.data_high.array
    ...
    
    # trades
    trades = [str(trade).splitlines() for trade in list(thestrat._trades.values())[0][0]]
    
    # orders
    orders = [str(order).splitlines() for order in thestrat._orders]
    

    But where I'm stuck now is how can I do this same thing with indicators?



  • @algoguy235 did have any luck figuring this out?


  • administrators

    Whereas orders are created by the system, indicators are created by end users. Something that could be my own code

    class MyStrategy(bt.Strategy):
        def __init__(self):
            self.my_smas = [bt.ind.SMA(period=x) for x in range(5, 30, 5)]
    

    Accessing the indicators later is a matter of getting thestrat as shown above and accessing the my_smas attribute (a list containing several Simple Moving Averages)


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.