For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

BuySell Indicator shown on 2 data series



  • Currently, I have 2 data series at my cerebro.plot(). I would like to show BuySell signals in both of the plots. However, buysell signals are assigned to the first plot only.

    cerebro = bt.Cerebro()
    
    cerebro.adddata(data0)
    cerebro.addstrategy(TestStrategy)
    
    data1 = data0.clone()
    data1.addfilter(bt.filters.HeikinAshi)
    cerebro.adddata(data1)
    
    1. I tried different combinations of kwargs in plotinfo of buysell.py

      plotinfo = dict(plot=True, subplot=False, plotlinelabels=True)
      
    2. I also tried to include the buysell signal into the second data series by adding the observers into the data series like indicators.
      self.buysell = bt.observers.BuySell(self.datas[1])

    but the expected result is not found.

    Please advise.



  • There are charts and samples with multiple data feeds and the BuySell observer shows up for each data feed.

    Your code actually seems to be missing the most important part: how cerebro is actually invoked

    @PeterCHK said in BuySell Indicator shown on 2 data series:

    self.buysell = bt.observers.BuySell(self.datas[1])
    

    I believe the only canonical way to add Observers is via cerebro and not adding them like Indicators.



  • Thank you Paska!

    I am a newbie at the platform. Please advise the links of relevant charts and samples if possible.

    This is how I initiate the cerebro in a full form.

    if name == 'main':

    modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
    datapath = os.path.join(os.path.dirname(modpath), './datas/700-201712-STK.csv')
    
        # Create a Data Feed
    data0 = bt.feeds.YahooFinanceCSVData(
        dataname=datapath,
        reverse=False)
    
    cerebro = bt.Cerebro(stdstats=False)
    cerebro.addobserver(bt.observers.Broker)
    cerebro.addobserver(bt.observers.Trades)
    cerebro.addobserver(bt.observers.BuySell)
    
    cerebro.adddata(data0)
    
    cerebro.addstrategy(TestStrategy)
    data1 = data0.clone()
    data1.addfilter(bt.filters.HeikinAshi)
    cerebro.adddata(data1)
    
    cerebro.broker.setcash(10000.0)
    
    cerebro.broker.setcommission(commission=0.0)
    
    cerebro.run(runonce=True, preload=True)
    
    
    startdate = datetime.date(2007, 1, 1)
    enddate = datetime.date(2017, 12, 1)
    
    cerebro.plot(numfigs=1, style='ohlc', start=startdate, end=enddate, rowsmajor=3)


  • @PeterCHK Use cerebro.addobservermulti(bt.observers.BuySell) instead of cerebro.addobserver(bt.observers.BuySell)



  • @oalexandere Thanks! I simply change cerebro.addobserver(bt.observers.BuySell) to cerebro.addobservermulti(bt.observers.BuySell).

    It does not show any changes. Do I need to input any **kwargs to direct the datas to data1?

    It seems buy() and sell() are using the data0 as reference.



  • @PeterCHK said in BuySell Indicator shown on 2 data series:

    It seems buy() and sell() are using the data0 as reference.

    If you don't tell them to buy/sell something different, the default will be bought/sold.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.