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

Buy/sell and P&L indicators plotted incorrectly



  • Hi all.

    I am running a backtest strategy on 2 data feeds in Cerebro, and have the following problems.

    • In the 2 data feed version, the default Buy/Sell indicators (i.e. the ones in stdstats) are NOT in the correct location, i.e. indicators are NOT on the price line. They are in right location for 1 data feed version.

    • In the 2 data feed version, the P&L dots are all blue (regardless if profit, or loss), whereas with 1 data feed, P&L dots are blue (if profit) and red (if loss)..

    • There are new dark green, downward-facing triangles in the 2 data feed version.

    2 data feed version:

    alt text

    1 data feed version:

    alt text

    Here is the relevant portions of my code. How could I fix these 3 issues? Thanks!!

    data = btfeeds.GenericCSVData(
      name='8545',
      dataname='csv/8545_20190801 to 20201009.csv',
      nullvalue=0.0,
      dtformat=('%m/%d/%y %H:%M'),
      datetime=0, # this, and the following correspond to which "column" datetime, open, etc appear in .csv file
      open=1,
      high=2,
      low=3,
      close=4,
      volume=5,
      openinterest=-1,
      headers=True,
      timeframe=bt.TimeFrame.Minutes #needed for intraday minute data, see https://community.backtrader.com/topic/1704/genericcsvdata-does-not-take-time-only-takes-date/8
      #tz=pytz.timezone('America/Toronto') # List of all timezones here: https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568
    )
    
    data2 = btfeeds.GenericCSVData(
      name='901',
      dataname='csv/901_20190801 to 20201009.csv',
      nullvalue=0.0,
      dtformat=('%m/%d/%y %H:%M'),
      datetime=0, # this, and the following correspond to which "column" datetime, open, etc appear in .csv file
      open=1,
      high=2,
      low=3,
      close=4,
      volume=5,
      openinterest=-1,
      headers=True,
      timeframe=bt.TimeFrame.Minutes #needed for intraday minute data, see https://community.backtrader.com/topic/1704/genericcsvdata-does-not-take-time-only-takes-date/8
      #tz=pytz.timezone('America/Toronto') # List of all timezones here: https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568
    )
    
    #===================================
    # Execute backtest and strategy
    #===================================
    # Load strategy .py files
    module = importlib.import_module('strategies.' + strategyName, strategyName) # Assuming the strategy .py files are put in the "strategies" folder
    strategy = getattr(module, strategyName)
    
    # Load commission .py files
    module = importlib.import_module('commission_schemes.' + commissionName, commissionName) # Assuming the commission .py files are put in the "commission_schemes" folder
    commissionScheme = getattr(module, commissionName)
    
    # To run strategy
    if __name__ == '__main__': 
    
      #===================================
      # Cerebro
      #===================================
    
      # Create a cerebro entity:
      # If you don't want the default 3 plots plotted (price, buy/sell P&L, capital), set stdstats=False. See: https://www.backtrader.com/docu/observers-and-statistics/observers-and-statistics/
      # cerebro = bt.Cerebro(stdstats=False)    
        cerebro = bt.Cerebro()
    
    
      # Add a strategy or Optimize a strategy.
        cerebro.addstrategy(strategy) 
        
        # Add the Data Feed to Cerebro:
        cerebro.adddata(data)
        cerebro.adddata(data2)
    
        # Calculate num of stocks to buy / sell every transaction
        cerebro.addsizer(bt.sizers.PercentSizer, percents=strategy.params.percents)
    
        # Set our desired cash start
        cerebro.broker.setcash(startingBalance)
    
        # Commissions:
        # commissions can be used in 2 ways: (1) importing a commission scheme, (2) using default commission scheme
        # 1. Importing:
        cerebro.broker.addcommissioninfo(commissionScheme())  
        # 2. default commission schemes:    
        # commissionPercentage: 0.1% ... divide by 100 to remove the %
        #cerebro.broker.setcommission(commission=commissionPercentage)
        #cerebro.broker.addcommission(commission=commissionPercentage)
    
    
        strategyResults = cerebro.run()
    
        # Plot graph
        cerebro.plot()
    
        # Store results in variable, export to CSV
        print(strategyResults[0].broker.getvalue())
    


  • @genkisushijai said in Buy/sell and P&L indicators plotted incorrectly:

    In the 2 data feed version, the default Buy/Sell indicators (i.e. the ones in stdstats) are NOT in the correct location, i.e. indicators are NOT on the price line. They are in right location for 1 data feed version.

    it was number of topics on the forum with this issue. i can advice to play with barplot and bardist parameters Docs - Observers Refs- BuySell

    @genkisushijai said in Buy/sell and P&L indicators plotted incorrectly:

    In the 2 data feed version, the P&L dots are all blue (regardless if profit, or loss), whereas with 1 data feed, P&L dots are blue (if profit) and red (if loss)..

    in case of several data feeds the color means the data feed, but not profit or loss. Blue markers are markers for 1st data feed.

    @genkisushijai said in Buy/sell and P&L indicators plotted incorrectly:

    There are new dark green, downward-facing triangles in the 2 data feed version.

    this is second data feed markers



  • @ab_trader said in Buy/sell and P&L indicators plotted incorrectly:

    @genkisushijai said in Buy/sell and P&L indicators plotted incorrectly:

    In the 2 data feed version, the default Buy/Sell indicators (i.e. the ones in stdstats) are NOT in the correct location, i.e. indicators are NOT on the price line. They are in right location for 1 data feed version.

    it was number of topics on the forum with this issue. i can advice to play with barplot and bardist parameters Docs - Observers Refs- BuySell

    Thanks for the response.

    I have tried changing the barplot and bardist to different values (ex: barplot = False, bardist = 0.00, etc). Changing barplot and bardist doesn't fix my problem though (the Buy/Sell indicators are still in the wrong location for 2 data feed version).

    Do you know any other ways to fix this issue?

    @genkisushijai said in Buy/sell and P&L indicators plotted incorrectly:

    In the 2 data feed version, the P&L dots are all blue (regardless if profit, or loss), whereas with 1 data feed, P&L dots are blue (if profit) and red (if loss)..

    in case of several data feeds the color means the data feed, but not profit or loss. Blue markers are markers for 1st data feed.

    @genkisushijai said in Buy/sell and P&L indicators plotted incorrectly:

    There are new dark green, downward-facing triangles in the 2 data feed version.

    this is second data feed markers

    Not sure what you mean (what markers do those dark green triangles represent?), can you pls clarify?



  • @genkisushijai said in Buy/sell and P&L indicators plotted incorrectly:

    Not sure what you mean (what markers do those dark green triangles represent?), can you pls clarify?

    Blue markers represent profits/losses for trades made on the 1st data feed, green markers represent profits/losses for trades made on the 2nd data feed. Markers located above zero line represent profits, markers located below zero line represent losses. Hope it is clear this time.



  • @ab_trader Thanks ab_trader.

    Does anyone else know how to fix the Buy/Sell indicators in incorrect position, in the 2 data feed scenario?



  • @backtrader any advice you may have?



  • @genkisushijai said in Buy/sell and P&L indicators plotted incorrectly:

    @ab_trader Thanks ab_trader.

    Does anyone else know how to fix the Buy/Sell indicators in incorrect position, in the 2 data feed scenario?

    pass oldtrades=True to cerebro



  • @hghhgghdf-dfdf said in Buy/sell and P&L indicators plotted incorrectly:

    @genkisushijai said in Buy/sell and P&L indicators plotted incorrectly:

    @ab_trader Thanks ab_trader.

    Does anyone else know how to fix the Buy/Sell indicators in incorrect position, in the 2 data feed scenario?

    pass oldtrades=True to cerebro

    Hi, thanks for the suggestion. I tried this but same result, the Buy/Sell indicators are in incorrect position in 2 data feed scenario

    Any other potential fixes?



  • Try oldbuysell=True with cerebro. According to docs -

    If stdstats is True and observers are getting automatically added, this switch controls the main behavior of the BuySell observer

    • False: use the modern behavior in which the buy / sell signals are plotted below / above the low / high prices respectively to avoid cluttering the plot
    • True: use the deprecated behavior in which the buy / sell signals are plotted where the average price of the order executions for the given moment in time is. This will of course be on top of an OHLC bar or on a Line on Cloe bar, difficulting the recognition of the plot.


  • @ab_trader Hey I tried this as well, but no luck... any other advice you may have? thanks again :)


Log in to reply
 

});