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 refer some specific indicator value?



  • For example, I may keep comparing to the first SMA value of the day at following time bars.
    How to do that? Thanks!


  • administrators

    It seems you would have intraday data

    1. When the day changes, store the value of the SMA in an attribute (self.data.datetime.date() gives you a datetime instance)
    2. As long as the day doesn't change, compare what you want with the store value
    3. Go back to step 1 (Mr. Miyagi managed it with 2 rules ...)


  • Sorry, I am still a beginner and don't quite get your point...Where should I do the storing? In the init of strategy class?
    My indicators in init looks like:
    for i, d in enumerate(self.datas):
    self.inds[d] = dict()
    self.inds[d]['dataclose'] = d.close
    self.inds[d]['sma'] = bt.indicators.SimpleMovingAverage(
    d.close, period=self.params.maperiod)
    How can I define a variable to store the very first sma value(at maperiod price)?
    maybe something like: self.inds[d]['firstma'] = .....?


  • administrators

    @forrestchi said in How to refer some specific indicator value?:

    How can I define a variable to store the very first sma value(at maperiod price)?
    maybe something like: self.inds[d]['firstma'] = .....?

    It's a possibility. But the value has to be filled manually by you when the SMA starts producing values. Your options

    • You check all SMAs during prenext and nextstart (because they will first produce a value at different times) and record the values the first time they aren't NaN

    or

    • You subclass the moving average of your choice and specifically store the first value in an attribute when nextstart is called

    See Docs - Operating the Platform to understand when next, prenext and nextstart are called.