Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to evaluate with dynamic parameter #452

Unanswered
malakeel asked this question in Q&A
Discussion options

I like to obtain and test the value of a function using dynamic parameter. For example :

rsi_dynamic_length = lambda: ... # logic to calculate RSI length using cycle indicators .. or a def function()
rsi = self.I( talib.RSI , close , timeperiod = rsi_dynamic_length)

I was not able to achieve this, so I was wondering if this is even possible or someone have a work around

You must be logged in to vote

Replies: 2 comments 2 replies

Comment options

Define it as Strategy variable and it will become a parameter that can be modified externally at each run. Then you can use the optimization with a range or list of values to evaluate this parameter for a selected Strategy result. Or because the function run accepts a dictionary with parameters that will update current Strategy parameters, you can loop the run with a range of parameters and collect the stat results for further analysis.

You must be logged in to vote
1 reply
Comment options

A Strategy variable is a constructor argument. Is this what you mean ? I am not sure I want to modify it manually, but let's assume this is what I want. When you run a strategy, you don't have access to the loop for which the variable can be externally controlled.

can you please kindly provide a quick example.

Comment options

from backtesting import Strategy
from backtesting.lib import crossover
from backtesting import Backtest
import talib
class YourStrategy(Strategy):
 rsi_dynamic_length = 50
 
 def init(self):
 self.rsi = self.I( talib.RSI , self.data.Close, timeperiod = self.rsi_dynamic_length)
 
 def next(self):
 #... self.rsi ...
# ... get dataframe in df ...
bt = Backtest(df, YourStrategy, cash=10000, commission=0)
# option 1, run loop through optimize with a given criteria
stats = bt.optimize(rsi_dynamic_length=range(5, 50, 5),maximize='Equity Final [$]')
print(stats)
# option 2, clean run loop
for rsi_dynamic_length in range(5, 50, 5):
 stats = bt.run({'rsi_dynamic_length':rsi_dynamic_length})
 print(stats)

If you need details for the result from each loop of optimize you can request heatmap as shown in the documentation.

You must be logged in to vote
1 reply
Comment options

Thank you a lot @AGG2017 for taking the time and efforts to reply. This is not exactly what I was looking for. I was hoping to experiment the cycles indicators and use them for dynamic periods like Hilbert's Transform. The most important thing is to be able to plot them. I might be able to work around this limitation by precalculating the indicator I need like (talib.HT_DCPERIOD), and store this in a dataframe, or just iterate as in option 2. But this feels like a hack to me. In all cases, thank you a lot for your help. I will take this as a starting point, and re-try to base my work on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /