Skip to content

Plot SL / TP levels #56

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
jamhot1 opened this issue Mar 29, 2020 · 3 comments
Closed

Plot SL / TP levels #56

jamhot1 opened this issue Mar 29, 2020 · 3 comments
Labels
question Not a bug, but a FAQ entry

Comments

@jamhot1
Copy link

jamhot1 commented Mar 29, 2020

Is there a way to plot Stop loss and Take profit points? or indeed add any such additional plot points for specified data in scatter or line format. Useful for ensuring strategy is operating as per

@kernc
Copy link
Owner

kernc commented Mar 29, 2020

You can think of the SL/TP levels as regular overlay indicators.

If you can precompute them in init(), that's probably the preferred option. But if you can't, an alternative is to define an empty indicator (plotted as scatter plot since line plots don't work well with NaN values), and fill it only gradually in next():

class MyStrategy(Strategy):

    def init(self):
        # Initialize an empty SL/TP-level indicator (filled with NaNs) 
        self.sl_level = self.I(lambda: np.repeat(np.nan, len(self.data)), 
                               name='SL level', scatter=True)

    def next(self):
        # Determine actual SL/TP level
        sl_level = ...
        # Enter position with a contingent SL/TP order attached
        self.buy(sl=sl_level)
        # And mark the computed SL/TP level on the SL indicator plot
        self.sl_level[-1] = sl_level        

Above code is untested, but something to the like should work.

@kernc kernc added the question Not a bug, but a FAQ entry label Mar 29, 2020
@jamhot1
Copy link
Author

jamhot1 commented Mar 30, 2020

Brilliant! Thanks Kernc. I was going along those sort of lines but couldn't quite implement the indicator correctly. I guessed it would have to be some sort of logging or custom indicator.
Could I ask. Are there any forum pages dedicated to this framework? A place for everyone to exchange tips/questions etc? I really like this lib over the others and would love to see it used more widely.

@jamhot1
Copy link
Author

jamhot1 commented Mar 30, 2020

BTW, if anyone else is interested. The NP array needs to be made none immutable by adding the code:

self.sl_level.flags.writeable = True 

Many thanks to the dev for pointing me on the right track!

@kernc kernc closed this as completed Jul 15, 2020
@kernc kernc changed the title Overlay Plotting SL and TP Plot SL / TP levels 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
Projects
None yet
Development

No branches or pull requests

2 participants