kernc / backtesting.py

:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.
https://kernc.github.io/backtesting.py/
GNU Affero General Public License v3.0
5.53k stars 1.07k forks source link

Its hard to optimize things #471

Open mablue opened 3 years ago

mablue commented 3 years ago

Expected Behavior

Optimizing and adding new indicators Is hard and dosnt sense! For example whats that "I":

Self.I(...)

Why we need it I want to use ta-lib instade of that! How can I do it? I think 🤔 its not have good docs or maybe I can not read good If we use ccxt than how we can implement it instade of goog data?! Cuz them have defferences in keys and also data will split to 5 place and than we can use it but for example ta.Mfi will use full of goog with lowercase keys( 'Open' to 'open') And etc etc make it difficult to work. Maybe its good to remove all examples and let user to add his data and indicators normally. I Need to make an environment for gym open ai but Its going very hard to me ;(

AGG2017 commented 3 years ago

You don't need to use the internal indicator representation function if you don't have time to understand how it works and how it can be used with ANY external TA library. It is good to be used only if you plan to plot the result of these indicators with the support for that already done. You can use any other indicators directly with current index from self._broker._i. I also does not like many things and decided to rewrite the whole logic to support options and many price sources for different securities at the same time, etc. You are always free to use it as it is, to request modifications or to do them by yourself.

mablue commented 3 years ago

Now if I want to add ta.SMA I will add it inside self.I? Or not? I dk if we can add things with ta why we use other internal one indicator calculator lib todo that? You can remove every other thing from your project instade of adding more things to focus just on backtesting and optimizing. Cuz there is much beautifull indicator generators like ta or ta-lib or finta to calculate indicators or reading data as well but in github we have not a good working "one file one class and one function" strategy backtesting and optimizing and maybe bot(in future) tool for use. I love this lib cuz its simple. I can use it to hyperopt things but it have not a document to show a unic and standard way to use ta indicators. Or/also an easy way to optimize things...

AGG2017 commented 3 years ago

You have to invest time to understand the examples from the documentation. It is very simple to use any of the external ta libs you mentioned. Even I gave you a way to index any other unsupported indicators by using the absolute index from self._broker._i Two ways to go:

  1. To use external indicator by self.I where it will be available to function next always with index -1 for the current value.
  2. To use external indicator already calculated as standard data frame and in function next to use its absolute position value given by the broker absolute index for accessing the current value.

What of these two options is not clear enough?

mablue commented 3 years ago

You mean if i use talib indicators should use talib.abstract.SMA(...).shift(-1) instade of next function? Please add something me examples to docs for using it I can learn just from examples...;(

Maybe im wrong but I think its not simple to undrestanding I need some examples to use other indicators and a simple code to optimize the indicators on it... For example if i calculate sma(14) indicator inside strategy. How can optimize it with an easy way for example with a function like that:

#global level
Strategy.Optimize()
# Inside strategy class: 
Import talib.abstract as ta
sma_timerange = opt(2,100,2)
Dataframe['sma']= ta.sma(sma_timerange)

#Optimization class: 
Def opt(min,max,step):
  return random.choice(range(min,max,step))
AGG2017 commented 3 years ago

Here is one simple example to use external ta lib like finta (worst case because it uses different names for the columns): https://github.com/peerchemist/finta/blob/master/examples/backtest.ipynb For the optimizations check this simple manual here: https://github.com/kernc/backtesting.py/blob/master/doc/examples/Quick%20Start%20User%20Guide.ipynb The way it works is just to start bt.optimize instead of bt.run and to provide the parameters. In the strategy you have to use the same name as class variables like sma_len = 200 and then, inside your init where you will calculate the indicator you have to use self.sma_len instead of the constant 200. Ouside the optimize will execute run with all list or ranges for all combination of parameters you need. It's just simple as that.

mablue commented 3 years ago

thanks I'll check this links ;) yes its usefull but I sometimes think I should use for example self.I or etc also when I was read docs of optimzation its very hard to undrestand and very complex codes will write. or maybe the examples are very hard and complex...