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

how to get the indicator data alone?



  • I am a new user of bt, a very good framework. I need to analyze the indicator's statisics, how do I get the the indicator data alone? anyone knows it? thanks!


  • administrators

    The question alone is probably not clear enough.

    • You have created an indicator and probably stored it in an attribute of the strategy

    Why can't you access the data? Where is the problem?



  • By the sample or test in BT, seems the indicator added into the strategy to calculate values of the indicator. If I donot add the indicator into the strategy, how can i to calculate the values of indicator? for I only care about the values of the indicator to get the statistics of it.


  • administrators

    @mothee said in how to get the indicator data alone?:

    If I donot add the indicator into the strategy, how can i to calculate the values of indicator?

    If you don't add the indicator to a Strategy, no values will be calculated. You are probably looking for something different like the python bindings for ta-lib: https://github.com/mrjbq7/ta-lib

    There is a downloadable and installable file at: https://www.lfd.uci.edu/~gohlke/pythonlibs/



  • Thanks your responds!
    I found I could add the indicator into the cerebro, then call the 'run' function, the indicator values are calculated. but seems the Indicator class is a little complicated, I do not know how can I extract the timeseries of indicator(what member variables keep the values in the object of Indicator?). If the bt could provide some design big pictures for classes, it would be nice.



  • The below test code could get the indicator time series:

    #get the date feed
    data = ...

    cerebro = bt.Cerebro(stdstats=False) # I don't want the default plot objects

    Add the Data Feed to Cerebro

    cerebro.adddata(data)

    cerebro.addindicator(indcls=btind.RSI)
    pars ={'period':60, 'plotname': "FastMA"}
    cerebro.addindicator(indcls=btind.SimpleMovingAverage, **pars )

    cerebro.run()

    strategy = cerebro.runstrats[0][0]

    inds = strategy.getindicators()
    ind = inds[0]

    get the datetime and indicator_data

    st_dtime = strategy.lines.datetime.plot()
    ind_data = ind.array[:]

    get the statistics of time-series of indicator

    ....



  • I think the belw code should work also without the cerebro/strategy object, but failed with some error in data.preload().

    get the date feed

    data = ...

    rsi_ind = btind.RSI(data)
    data.reset()
    data.extend(size=0)
    data._start()
    data.preload()
    indicator._once()


  • administrators

    @mothee said in how to get the indicator data alone?:

    cerebro.addindicator(indcls=btind.RSI)
    

    That adds an indicator into a sample strategy class. It is meant for experimentation purposes.

    You should read the top of the forum. It will help you posting and coding:

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

    The Indicator class it not complex or simple. It is meant to be used withing the backtrader echosystem.

    See: Docs - Platform Concepts - Getting a Slice

    It shows how to get the data (from the current point backwards). You may also access the attribute array of each line of the indicator if you want it all.

    data = indicator_instance.lines[0].array