Skip to content

Plot indicators dynamically computed in next() #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sidsidsidsid opened this issue Jun 14, 2020 · 3 comments
Closed

Plot indicators dynamically computed in next() #87

sidsidsidsid opened this issue Jun 14, 2020 · 3 comments
Labels
question Not a bug, but a FAQ entry top-choice

Comments

@sidsidsidsid
Copy link

sidsidsidsid commented Jun 14, 2020

First, great product. I've been looking for something like this for a while. What are your future plans/direction with the product?

Second, my question is an extension of issue #74. I understand how to add indicators. But, is there a way that their results are added to the bt.plot()? For example below; I would like indicator1 and indicator2 plotted. Note: my indicators do not pass in any values created in the init() method.

I thought of wrapping them in the self.I(), but as stated above, they are using data created from data generated in the init(). Is there a way to get c_var and s_var plotted using bt.plot().

def myIndicator1():
   value = ...
   return value

def myIndicator2():
   value = ...
   return value

class SmaCross(Strategy):
    def init(self):
        self.sma1 = self.I(SMA, self.data.Close, self.n1)
        self.sma2 = self.I(SMA, self.data.Close, self.n2)
    
    def next(self):
        c_var = (self.sma1 * 345).std() - 5
        out = myIndicator( c_var)
        s_var = out.std()
        results = myIndicator2(s_var)

Thanks

@kernc
Copy link
Owner

kernc commented Jun 14, 2020

I understand you'd like to define and show an indicator whose values are computed in next() instead of in init().

First of all, it's usually incomparably faster to precompute values in vector form in init().

When the computation can't be simply or clearly vectorized, you can probably do something like:

class SomeStrategy(Strategy):
    def init(self):
       # Pre-allocate and designate an indicator, fill with NaNs
       self.indicator = self.I(lambda: np.repeat(np.nan, len(self.data.Close)))

    def next(self):
       # Compute indicator value at time t
       values = myIndicator(self.data.Close, ...)
       # If myIndicator returns a vector, we are only interested in its last value
       value = values[-1]

       self.indicator[-1] = value

This is from top of the head. Let me know if it works.

@sidsidsidsid
Copy link
Author

Thanks. I believe your answer has given me a better understanding of what you are attempting with the framework. I will try again to place the indicator in the init() and if that fails, then try your recommendation.

Thanks for the help

@kernc kernc changed the title Add indicators to bt.plot() bt.plot() indicators computed in next() Jul 15, 2020
@kernc kernc added the question Not a bug, but a FAQ entry label Jul 15, 2020
@kernc
Copy link
Owner

kernc commented Jul 15, 2020

Hopefully you got it working. 🚀

@kernc kernc closed this as completed Jul 15, 2020
@kernc kernc changed the title bt.plot() indicators computed in next() Plot indicators dynamically computed in next() Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Not a bug, but a FAQ entry top-choice
Projects
None yet
Development

No branches or pull requests

2 participants