Issue with BuySell obverver plot from cerebro.plot(). Disappearing with few data points
Lammy last edited by
After a while of trying to trace the source of my missing BuySell indicators, I think the issue is with backtrader.
Consider the following example:
class St(bt.Strategy): def __init__(self): self.dataclose = self.datas.close def next(self): if self.dataclose > 2944: self.order = self.sell() data = bt.feeds.BacktraderCSVData(dataname='data.csv') cerebro = bt.Cerebro() cerebro.adddata(data) cerebro.addstrategy(St) cerebro.run() cerebro.plot()
Date,Open,High,Low,Close,Volume,OpenInterest 2005-01-03,2952.29,2989.61,2946.80,2970.02,0,0 2005-01-04,2969.78,2979.88,2961.14,2971.12,0,0 2005-01-05,2969.00,2969.00,2942.69,2947.19,0,0 2005-01-06,2947.44,2967.65,2947.44,2966.24,0,0 2005-01-07,2965.54,2988.99,2964.48,2979.82,0,0 2005-01-10,2980.30,2986.07,2967.79,2977.21,0,0 2005-01-11,2977.14,2981.51,2943.42,2949.29,0,0 2005-01-12,2948.89,2952.79,2914.00,2924.01,0,0 2005-01-13,2925.60,2943.09,2922.91,2936.32,0,0 2005-01-14,2933.58,2951.77,2925.13,2948.22,0,0 2005-01-17,2948.11,2963.22,2948.11,2963.06,0,0 2005-01-18,2962.28,2965.60,2940.77,2962.50,0,0 2005-01-19,2963.91,2976.33,2957.21,2959.90,0,0 2005-01-20,2957.55,2957.55,2929.78,2937.71,0,0 2005-01-21,2937.27,2944.92,2927.62,2940.87,0,0 2005-01-24,2939.68,2940.02,2916.08,2937.83,0,0 2005-01-25,2937.66,2960.90,2932.56,2958.61,0,0 2005-01-26,2958.31,2967.62,2954.36,2956.43,0,0 2005-01-27,2956.32,2970.81,2948.55,2970.81,0,0 2005-01-28,2969.91,2977.99,2950.68,2955.89,0,0 2005-01-31,2958.07,2987.88,2958.07,2984.75,0,0 2005-02-01,2984.63,3008.85,2982.06,3008.85,0,0 2005-02-02,3009.08,3022.87,3008.92,3021.95,0,0 2005-02-03,3021.94,3023.85,3001.87,3010.39,0,0 2005-02-04,3011.58,3038.31,3011.58,3037.13,0,0
But delete the last 6 dates from data.csv and you will end up with this:
In addition, if my data is on hourly resolution, all BuySell markers disappear, no matter how many hours I include.
Any idea what to do?
I think the issue is with backtrader.
Sorry, but you are wrong. The issue is with
matplotliband with not using the existing possibilities of the platform to try to compensate.
But first: backtrader is NOT a charting platform. The possibility to offer automated charts (at the end of the backtesting) with some customization options is meant as a visual aid.
And it is not a charting platform because backtesting relies on numbers and not on charts.
matplotlibis a great platform and backtrader tries to abstract as much as possible to offer automated charts, not everything is possible.
Change the way the
BuySellobserver shows up by plotting right on the price and not above/below the price. See Docs - Observer Reference
Plot the data allowing other items to change the scale. See
ylimited- Docs - Plotting
In addition, if my data is on hourly resolution
Have you told the platform when loading your data that your data has your expected timeframe/compression?
Lammy last edited by
You are absolutely right, the problem is not with backtrader, but rather with matplotlib. Taking a look at the (excellent) documentation and previous questions have highlighted that for me.
I will post my workaround as soon as I am done.
For others who are stuggeling here, take a look at: