Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. vladisld
    3. Best
    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 12
    • Topics 11
    • Posts 501
    • Best 263
    • Controversial 0
    • Groups 1

    Best posts made by vladisld

    • RE: How to backtest a group of stocks?

      External blog:

      https://backtest-rookies.com/2017/08/22/backtrader-multiple-data-feeds-indicators/

      Internal blog:
      https://www.backtrader.com/blog/posts/2017-04-09-multi-example/multi-example/

      Community post(s):
      https://community.backtrader.com/topic/2032/manage-multiple-assets/8

      This is just one sample, but there are multiple others - try to search.

      posted in General Code/Help
      vladisld
      vladisld
    • RE: Difference between params and p

      Short:

      In your class methods you may use self.p and self.params interchangeably - they are actually the same object.
      (see the 'Parameters' section in the docs: https://www.backtrader.com/docu/concepts/)

      Longer:

      One need to distinguish between the params class variable and the self.params (or self.p for that matter) instance variable.

      params class variable (list of tuples or dict) defines the custom parameters and their default values that will be later used to "magically" create and populate the self.params (or self.p) instance variable

      This self.params (or self.p) instance variable will have members for each tuple originally present in params class variable but their value could be different depending of which parameters where passed during class instantiation ( or have their default values if no matching parameters were passed )

      TLDR:

      The params class variable may be defined in classes that use MetaParams metaclass (or its derivatives) during their instantiation sequences (like Cerebro, Strategy, Analyzer, BrokerBase, Filter, Indicator, an so on and all their inherited classes ).

      During the instantiation the meta-class "hijack" the instantiation sequence and creates additional instance variables (self.p and self.params in particular, but also many others like 'datas', 'data0' and other aliazes for LineSeries inherited classes for example), intializing them in various ways. You may take a look at the with_metaclass method in utils\py3.py for technical details of how this is implemented.

      For example one of such meta-classes is MetaParams class which is particularly responsible for creating the self.p and self.params instance variables ( see its donew method )

      During your strategy (derived from Strategy class which uses MetaParams meta-class) instantiation process a new auto-generated class (let's call it X) will be used to hold the parameter values. For each tuple in the 'params' class variable there will be a member in X class. The instance of this X class will be created, where each member will get its value either from the parameters passed during the Cerebro.addstrategy call or from the default value in the params class variable tuples.

      For example let's assume your strategy params variable defined as:

      class SMACrossOver(bt.Strategy):
          params = (
              ('stake', 1),
              ('period', 30),
          )
      
      

      and you add this strategy to Cerebro using:

      cerebro.addstrategy(SMACrossOver, period=15)
      
      

      The values for 'self.p' members inside your strategy will be:

      self.p.stake == 1  # from default value in `params` class variable
      self.p.period == 15 # from parameters to cerebro.addstrategy
      
      posted in General Discussion
      vladisld
      vladisld
    • RE: SMA Crossever strategy not showing expected results

      Yes sir. This is Backtrader framework support forum.

      There are other general algo and non-algo trading forums out there:

      https://www.reddit.com/r/algotrading/
      https://www.elitetrader.com/et/

      and many others - just google it.

      posted in General Code/Help
      vladisld
      vladisld
    • RE: The plotting is a bit odd,i have set the compression and the timeframe.

      Probably the same issue as in the following post:

      https://community.backtrader.com/topic/3306/signals-stop-showing-after-a-certain-date/2

      Note: Please do not post your code as image. See the top of the page for the instructions for posting code in the text form.

      posted in General Code/Help
      vladisld
      vladisld
    • RE: How can I inherit params from a parent class and append on the child class?

      @André-Tavares said in How can I inherit params from a parent class and append on the child class?:

      params.update({

      No need to update the params dict. The parameters from the base classes are fused automagically by metaclass during the instantiation. You may take a look at the following post as well: https://community.backtrader.com/topic/2379/difference-between-params-and-p/2

      posted in Indicators/Strategies/Analyzers
      vladisld
      vladisld
    • RE: Problem with multiple datafeeds

      Few things:

      • Positions are maintained by the broker per data.

      • position property of the Strategy class just returns the position of the data[0]. If you need to get the position for the different data, the getposition(data=...) could be used.

      Now the problem in the above code seems to be related to the following logic in the next method:

              if not self.position:
                  self.log('Buy Created, %.2f ' % (self.dataCloseLong[0]))
                  self.order = self.buy(data = self.datas[0],size=1)
              else:
                  self.log('Sell Created, %.2f' % self.dataCloseShort[0])
                  self.order = self.sell(data = self.datas[1],size=1) 
      

      If we have no position in data[0] then the 'buy' will be issued. Once this 'buy' is completed - the position will be established in data[0].

      The next time the next will be called, the position in data[0] is present - so the 'sell' is issued for data[1] ( different data, right ). So the position in data[0] will never be changed from now on.

      Please correct me if I'm wrong.

      posted in General Code/Help
      vladisld
      vladisld
    • RE: How to use two CommissionInfo inside a strategy?

      @soulmachine said in How to use two CommissionInfo inside a strategy?:

      Since data has only one name, so it's not possible to apply multiple commission fees?

      You're probably right. AFAIK a single commission could be specified per data name or default. As @run-out correctly mentioned - you may develop your custom commission scheme.

      posted in Indicators/Strategies/Analyzers
      vladisld
      vladisld
    • RE: SMA Crossever strategy not showing expected results

      Although such a strategy could be easily implemented using the Backtrader framework, the code above has no relation to the framework supported in this forum.

      I suggest you start with Backtrader Quickstart Guide and Simple Sample strategy and
      continue from there.

      posted in General Code/Help
      vladisld
      vladisld
    • RE: Multicore optimization cause performance degradation.

      @yacc2000 said in Multicore optimization cause performance degradation.:

      Is preload=False and Runonce=False cause multicore optimization not working?

      It will work, just much slower. Preloading the data is one of the technics to allow running the optimization at full speed. Technically it means that pre-loaded data's memory is shared between multiple working processes instead of being reloaded to the internal buffers for each run of the engine (for each permutation of optimized parameters).

      As in my answer to your previous post - the framework wasn't designed to dynamically change the data feeds during the backtest and/or optimization. You may fight against the framework ( wish you luck), change your design ( by preloading all your data feeds once) or just try a different framework

      posted in Indicators/Strategies/Analyzers
      vladisld
      vladisld
    • RE: If I add two strategies, how to manage the positions of each strategy

      @the-world AFAIK position is maintained by a broker per data and there is just a single broker per Cerebro engine. So in order for multiple strategies to have an independent positions, either they need to work on different datas or they need to be run using different engine instances.

      posted in General Discussion
      vladisld
      vladisld
    • RE: How to add ML algorithm in backtrader backtest?

      Take a look at the following posts:

      https://community.backtrader.com/topic/102/machine-learning-backtrader
      https://community.backtrader.com/topic/2237/machine-learning-integration

      There are other similar posts, just search.

      posted in General Code/Help
      vladisld
      vladisld
    • RE: Passing Timeframe to Rolling Return Analyzer

      @elektor said in Passing Timeframe to Rolling Return Analyzer:

      timeframe=52

      timeframe should have values from the following list:

       TimeFrame.Ticks
       TimeFrame.MicroSeconds
       TimeFrame.Seconds
       TimeFrame.Minutes
       TimeFrame.Days
       TimeFrame.Weeks
       TimeFrame.Months
       TimeFrame.Years
       TimeFrame.NoTimeFrame
      

      so in your case:

      timeframe=TimeFrame.Weeks, compression=52

      posted in Indicators/Strategies/Analyzers
      vladisld
      vladisld
    • RE: How to backtest a group of stocks?

      In this case, I'd just run the whole Cerebro logic for each stock separately ( in a loop) - at least that's what I'm doing in my project.

      Probably others may have better advice.

      posted in General Code/Help
      vladisld
      vladisld
    • RE: Cash management and analyzers

      @hoflz5 try to use 'size' parameter instead of 'amount'. Docs here:https://www.backtrader.com/docu/strategy/

      posted in Indicators/Strategies/Analyzers
      vladisld
      vladisld
    • RE: Getting error running Optimization on data from IB

      Short:
      I'm not sure the IBData datafeed is supported for optimization with multiple CPUs (it may work on single CPU though).

      Detail:

      During the optimization with multiple CPU involved, Backtrader will use the process pool to run the optimization for each parameter permutation in parallel.
      For this to work the Cerebro instance will need to be copied to each worker process (it is not done if only single CPU is used).
      This is implemented by using pickle mechanism, which is used to serialize the Cerebro instance (together with all the objects connected to it - like data feeds, analyzers ... ).
      The problem with IBData data feed is that it is using some objects that can't be serialized (ex. locks, sockets, ...).
      Usually, for optimization purposes one may use the data feeds that support preloading of data bars without keeping any sync/connection objects alive (ex. CSV or Database based datafeeds)

      posted in General Code/Help
      vladisld
      vladisld
    • RE: Threading Error in cerebro.run(maxcpus=...)

      Please try to set the stdstats parameter to False:

      bt.Cerebro(maxcpus = ..., stdstats = False))
      

      This will disable the standard observers that are automatically added to your strategy by default. More here

      Also see the following posts:

      https://community.backtrader.com/topic/1252/optimization-error-when-multiple-data-feed/3
      https://community.backtrader.com/topic/2265/exception-when-running-optimize-with-optreturn-false/6

      posted in General Code/Help
      vladisld
      vladisld
    • RE: Fixed Holding Period: Selling After 10 Bars

      it seems that you are using the same bought_bar and bars_since_purchase variables for all data feeds instead of keeping those attributes per data feed.

      Something like:

      class FixedExitStrategy(bt.Strategy):
      
          def __init__(self):
              pass
      
          def start(self):
              self.bought_bars={d:0 for d in self.getdatanames()}
              self.bought_sizes={d:0 for d in self.getdatanames()}
      
          def next(self):
      
              for i, d in enumerate(self.getdatanames()):
                  data = self.getdatabyname(d)
                  pos = self.getposition(data=data)
      
                  if not pos.size:
                      if data.signal[0] != data.signal[-1]:
                          if data.signal[0] == 1:
                              self.bought_sizes[d] = data.position_size[0]
                              self.buy(data=data, size=self.bought_sizes[d])
                              self.bought_bars[d] = len(self)
      
                  if pos.size:
                      if (len(self) - self.bought_bars[d]) == 10:
                          self.close(data=data, size=self.bought_sizes[d])
      

      And I'm not sure where the self.getdatabyname(d).position_size is coming from - sorry.

      posted in General Code/Help
      vladisld
      vladisld
    • RE: can BT be used to backtest multiple tickers?

      Please take a look at the following post: https://backtest-rookies.com/2017/08/22/backtrader-multiple-data-feeds-indicators/

      Is it what you was looking for?

      posted in General Code/Help
      vladisld
      vladisld
    • 1
    • 2
    • 3
    • 4
    • 5
    • 13
    • 14
    • 1 / 14