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

Sell/Buy signal labels are missing in the plotting of forex data



  • Hello!
    I am using the 1-minutes forex csv data for backtesting, odd thing observed is that the buy/sell icons are missing in the plotting.
    using daily csv data from yahoo is fine, so looks there is some tricky issue in the csv part.
    Could anybody please help me with the issue?

    the code is

    from datetime import datetime
    import backtrader as bt
    
    class SmaCross(bt.SignalStrategy):
        def __init__(self):
            sma1, sma2 = bt.ind.SMA(period=1), bt.ind.SMA(period=60)
            crossover = bt.ind.CrossOver(sma1, sma2)
            self.signal_add(bt.SIGNAL_LONG, crossover)
    
    
    if __name__ == '__main__':
        # Create a cerebro entity
        cerebro = bt.Cerebro()
    
        # Add a strategy
        #cerebro.addstrategy(MyStrategy, period=15)
    
        cerebro.addstrategy(SmaCross)
    
        # sh: 000001.SS
        # BT: BTC-USD
        # SP500: ^GSPC
        #data0 = bt.feeds.YahooFinanceData(dataname='BTC-USD', fromdate=datetime(2018, 1, 1),
        #                                  todate=datetime(2019, 6, 1), decimals=5)
    
        data0 = bt.feeds.GenericCSVData(
        dataname='./eurusd-1m/DAT_ASCII_EURUSD_M1_201904.csv',
    
        fromdate=datetime(2019, 4, 1),
        todate=datetime(2019, 4, 10),
    
        nullvalue=0.0,
    
        dtformat=('%Y%m%d %H%M%S'),
        #tmformat=('%H:%M:%S'),
    
        datetime=0,
        time=-1,
        high=2,
        low=3,
        open=1,
        close=4,
        volume=-1,
        openinterest=-1,
        timeframe=bt.TimeFrame.Minutes,
        compression = 1,
        separator=';',
        decimals=5,
        headers=False
        )
    
        cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes,
                              compression=30)
       
        # Set our desired cash start
        cerebro.broker.setcash(1000000.0)
    
        # Add a FixedSize sizer according to the stake
        #cerebro.addsizer(bt.sizers.FixedSize, stake=2)
        cerebro.addsizer(bt.sizers.PercentSizer, percents=90)
    
        # Set the commission - 0.1% ... divide by 100 to remove the %
        cerebro.broker.setcommission(commission=0.0005)
    
        # Print out the starting conditions
        print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    
        # Run over everything
        cerebro.run()
    
        # Print out the final result
        print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
        cerebro.plot(style='bar')
    

    The CSV data looks as following:

    20190401 000000;1.123210;1.123240;1.123200;1.123240;0
    20190401 000100;1.123230;1.123230;1.123140;1.123190;0
    20190401 000200;1.123210;1.123310;1.123200;1.123310;0
    20190401 000300;1.123330;1.123360;1.123310;1.123340;0
    20190401 000400;1.123330;1.123330;1.123250;1.123250;0
    

    The plotting:
    0_1560927695451_Screen Shot 2019-06-19 at 09.00.57.png


  • administrators

    The Sell icons are in the same position as this

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

    i.e. (at the top)



  • @backtrader said in Sell/Buy signal icons are missing in the plot:

    position as this

    Wow, thanks for your quick reply!

    The "buy/sell Icons" i meant is as following (in the black circle that i drew):
    0_1560931792157_Screen Shot 2019-06-19 at 10.04.44.png

    but they are missing in first picture (1 minutes csv data).
    any idea?



  • @backtrader Hi sorry for the format issue, it's my first post and I have fixed it.
    Could you have a look again?

    Thanks!



  • it seems that it is because of the data in forex csv file: the number is too small,
    it will work if i change the price to bigger values, such as from "000000;1.123210;1.123240;1.123200;1.123240;0"
    to "000000;3210;3240;3200;3240;0".

    so it looks a bug in backtrader when handling precision of the price.



  • anyone could help?


  • administrators

    @meng-xiaofeng said in Sell/Buy signal labels are missing in the plotting of forex data:

    so it looks a bug in backtrader when handling precision of the price.

    Indeed, it must be a bug in backtrader, what else? (hopefully Nespresso and George Clooney won't mind)

    @meng-xiaofeng said in Sell/Buy signal labels are missing in the plotting of forex data:

    Hi sorry for the format issue, it's my first post and I have fixed it.
    Could you have a look again?

    The problem is you didn't understand the message.

    • Rule #1 for Algorithmic Trading: Read (and read more before looking for non-existent bugs)

    • Rule #2 for Algorithmic Trading: Always go back to Rule #1

    You have a problem with the BuySell observer.

    • Go to the documentation: https://www.backtrader.com/docu/observers-reference/

      See that it has a barplot / bardist parameters

    • Notice that in the default configuration where you see the red/green triangles the parameters are there as (True, 0.015), i.e.: plot above the max/min and do it 1.5% above the price.

    Notice how in the chart where you don't see them ... the configuration is the same. That means, literally

    • **The red/green (sell/buy) ** triangles are well above (at the top) and well below (at the bottom) of your chart, because you supply prices with a minimal variation which doesn't allow them to fit in the scale.

    You can then use Rule #1 and Rule #2 again and see the plotting documentation if you want to let the scale be modified by all elements in the chart and not be dominated by the price.



  • Now I got your idea! didn't realise there is such buy/sell observer.
    now i know that the backtrader has a very flexilbe design and love it more.

    Just a remark: can't the Sell/buy observer be a bit smarter so that it can always plot properly?

    Thanks!


  • administrators

    @meng-xiaofeng said in Sell/Buy signal labels are missing in the plotting of forex data:

    can't the Sell/buy observer be a bit smarter so that it can always plot properly?

    I think you under-appreciate the complexity of having something plotted automatically and handling all possible scenarios and use cases. If your data lacks variability, the problem is on your side.

    You can

    • Configure the observer to plot with less distance (or no distance at all)
    • Configure the chart to allow all elements to influence the scale as pointed out above

    You may also ask for Artificial Intelligence in the Observer ... but it won't probably happen.


Log in to reply
 

});