-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
-
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
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 2 replies
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.