Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. Euler Sousa
    3. Posts
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 8
    • Best 2
    • Controversial 0
    • Groups 0

    Posts made by Euler Sousa

    • Bayesian Optimization Framework

      Hello all,

      First I would like to congratulate the authors, contributors, and all the community for the awesome library/content provided.

      Nowadays I'm a Sr. Data Scientist and a beginner in the field of trading investment strategies. After reading the documentation and going over some examples and discussions on the community, I wanted to contribute somehow. I noticed that there are a lot of similarities between Data Science experiments and backtesting trading strategies. One, in particular, is the parameters optimization of the strategy/machine learning model. The most simple approach is a grid search over a list of values for each parameter and their combination. In Data science this approach can be done by using GridsearchCV and I believe cerebro.optstrategy has a similar method.

      Another approach in Data Science that outsmarts grid search is Bayesian Optimization. With that in mind, I created a framework that is available in this repo for optimizing the possible parameters of a given trading strategy using Bayesian optimization.

      Besides backtrader, it was used mainly three libraries:

      • Hydra: The key feature used in this project is the ability to dynamically create a hierarchical configuration by composition and override it through config files and the command line.

      • Bayesian Optimization: This is a constrained global optimization package built upon bayesian inference and gaussian process, that attempts to find the maximum value of an unknown function in as few iterations as possible.

      • backtrader_plotting: Library to add extended plotting capabilities to backtrader. Currently the only available backend is Bokeh.

      This framework produces a series of metadata during the optimization process, which is saved in the directories created automatically by Hydra. This metadata includes a best_iteration.html file showing the Bokeh plot of backtrader_plotting, a main.log file describing step by step of the optimization process, and a STRATEGY_NAME.json file containing the best set of parameters found by Bayesian optimization. After an experiment is concluded the main results are compiled and added to a results.csv file to easily keep tracking of all possibilities that were tried.

      Take a look at the README.md and follow the steps to see these features running with a dummy example prepared.

      I hope this can help some of you!

      Regards,

      posted in General Discussion
      Euler Sousa
      Euler Sousa
    • RE: [FIXED] Ichimoku: Unable to get Future cloud

      I ran some tests and for this case, we should use ago parameters as the starting point in "future" to look backwards, for example :

      print(f"{list(self.Ichimoku.senkou_span_a.get(ago= 3, size=4))}")
      

      which will yield the same result as :

      print(f"{[self.Ichimoku.senkou_span_a[i] for i in range(4)]}")
      

      However, If we pass ago=0 it will get the last 3 values, [0],[-1] ,[-2], which for this particular case is not in our interest.

      I checked the documentation in the Getting slice part and it makes sense.

      Thanks for the help.

      posted in Indicators/Strategies/Analyzers
      Euler Sousa
      Euler Sousa
    • RE: [FIXED] Ichimoku: Unable to get Future cloud

      I made another workaround to achieve it haha ... using Python compreehensive lists :

      print(f"{[self.Ichimoku.senkou_span_a[i] for i in range(26)]}")
      

      please let me know if there is a better way to do it.

      One more time, thanks for the contributions.

      posted in Indicators/Strategies/Analyzers
      Euler Sousa
      Euler Sousa
    • RE: [FIXED] Ichimoku: Unable to get Future cloud

      another quick question is there a way of getting an array of all 26 values? like self.Ichimoku.senkou_span_a[0:26] ?

      posted in Indicators/Strategies/Analyzers
      Euler Sousa
      Euler Sousa
    • RE: [FIXED] Ichimoku: Unable to get Future cloud

      @vladisld, thanks for the reply. It worked here also !

      posted in Indicators/Strategies/Analyzers
      Euler Sousa
      Euler Sousa
    • RE: [FIXED] Ichimoku: Unable to get Future cloud

      @André, here it goes:

      class IchimokuStrategy(bt.Strategy):
          def __init__(self):
              self.ichi= bt.indicators.Ichimoku()
      
          def next(self):
              senkou_a = self._senkou_span_a(tenkan_sen=self.ichi.l.tenkan_sen.get(size=26),
                                             kijun_sen=self.ichi.l.kijun_sen.get(size=26))
              print(f"{senkou_a[25]}")  # pandas.Series are 0 indexed based
          
          @staticmethod
          def _senkou_span_a(tenkan_sen,kijun_sen):        
              # Senkou Span A (Leading Span A): (Conversion Line + Base Line)/2))
              return ((pd.Series(tenkan_sen) + pd.Series(kijun_sen)) / 2)
          
      if __name__ == '__main__':
          cerebro = bt.Cerebro()
          cerebro.adddata(data)
          cerebro.addstrategy(IchimokuStrategy)
          cerebro.run(runonce=True)
      
          cerebro.plot()
      
      posted in Indicators/Strategies/Analyzers
      Euler Sousa
      Euler Sousa
    • RE: [FIXED] Ichimoku: Unable to get Future cloud

      It is clear for me the goal of backtesting engine and the fact that during the next method you have access to the current bar [0], the indicators pre-calculated on the init method, and all their previous values [-1] ...[-n] .

      The particular situation mentioned here is considering Ichimoku indicators, in special senkou_span_a and senkou_span_b, which are calculated by using past bars information (i.e. high, low of 52 and 9 periods before [0]). These two indicators are then projected 26 periods ahead of the current moment (i.e. [26]). For some strategies, it might be useful to have these "future" values to enhance the decision-making process at the current time [0].

      One workaround that I was able to do is getting the past values needed to calculate those two indicators using get() method, for example:

      self.data0.high.get(size=52)
      

      and then calculating the indicators again:

      import pandas as pd  
      
      def tenkan_sen(high,low):
          # Tenkan-sen (Conversion Line): (9-period high + 9-period low)/2))
          nine_period_high = pd.Series(high).rolling(window= 9).max()
          nine_period_low = pd.Series(low).rolling(window= 9).min()
          
          return (nine_period_high + nine_period_low) /2
      
      
      def kijun_sen(high,low):
          # Kijun-sen (Base Line): (26-period high + 26-period low)/2))
          period26_high = pd.Series(high).rolling(window=26).max()
          period26_low = pd.Series(low).rolling(window=26).min()
          return (period26_high + period26_low) / 2
      
      
      def senkou_span_a(tenkan_sen,kijun_sen):
          # Senkou Span A (Leading Span A): (Conversion Line + Base Line)/2))
          return ((pd.Series(tenkan_sen) + pd.Series(kijun_sen)) / 2).shift(26)
      
      
      def senkou_span_b(high,low):
          # Senkou Span B (Leading Span B): (52-period high + 52-period low)/2))
          period52_high = pd.Series(high).rolling(window=52).max()
          period52_low = pd.Series(low).rolling(window=52).min()
          return ((period52_high + period52_low) / 2).shift(26)
      
      

      This nice piece of code was taken from this article.

      I believe that even though this approach is not performatic, it can be useful in cases, for instance, you have some machine learning model for predicting some useful information used in the decision-making process.

      posted in Indicators/Strategies/Analyzers
      Euler Sousa
      Euler Sousa
    • RE: [FIXED] Ichimoku: Unable to get Future cloud

      Hey guys,

      I'm still facing the same situation reported by @André. Does anyone was able to access ichimoku indicators from the "future"?

      ps. The platform is awesome. Congratulations for the amazing job.

      Regards,

      Euler

      posted in Indicators/Strategies/Analyzers
      Euler Sousa
      Euler Sousa
    • 1 / 1