@backtrader I think you should consider another event... we should talk about that offline :-) I love events...
Best posts made by crystalet
Latest posts made by crystalet
RE: An issue with plotting in case of multiple datastreams
That's something in your configuration for matplotlib
ok i will have a look and report back on this one.
You need to tell us something more. Why cannot you? There is no indication of an error. Your snippet runs.
the code runs indeed, but the plot fails to be generated... again, will try the above and see.
just wanted to stop by and say thank you already. Have not had time to look at it properly yet
RE: Indicator using multi datafeed: operate only on overlapping bar
@Jacob : did you get this to work? I have the exact same question.
For example (see picture) I have 1 data feed that has hourly bars for hour 7-8 and hour 8-9 and another data feed that has data for hour 8-9 and hours 9-...-14
![alt text]( image url)
I resampled everything to Minutes (with compression 1) - I have to be honest, I still don't fully understand the resampling, which is why I am looking at the output.
The ratio gets calculated from hour 8 till hour 14...
So my questions are
- what is the logic/rules to pick ? I assume it sits there and waits for both feeds to have a bar before it does anything. And then after the second feed drops off, it seems the system thinks it is still present. I can understand the first bit of the logic, but not the second...
- Can you force it to know that if there is no bar, there is no data: I was outputting for every bar, the value of both legs and it gives me this:
2018-05-31T08:00:00, Close Leg1, 19.09 2018-05-31T08:00:00, Close Leg2, 20.49 2018-05-31T09:00:00, Close Leg1, 18.96 2018-05-31T09:00:00, Close Leg2, 20.49 2018-05-31T10:00:00, Close Leg1, 18.96 2018-05-31T10:00:00, Close Leg2, 20.49 2018-05-31T11:00:00, Close Leg1, 18.94 2018-05-31T11:00:00, Close Leg2, 20.49 2018-05-31T12:00:00, Close Leg1, 18.96 2018-05-31T12:00:00, Close Leg2, 20.49 2018-05-31T13:00:00, Close Leg1, 18.87 2018-05-31T13:00:00, Close Leg2, 20.49 2018-05-31T14:00:00, Close Leg1, 18.77 2018-05-31T14:00:00, Close Leg2, 20.49
An issue with plotting in case of multiple datastreams
I am new to backtrader and in my attempt to get familiar with the system, I ran into a strange issue, which might be down to a small oversight or some misconfiguration, but I am hoping to find some help here.
I have prepared a small datafile
Dates,Open,High,Low,Close,Volume,Openinterest 2000-01-04,25.2,25.69,24.71,25.55,1,2 2000-01-05,25.5,25.61,24.86,24.91,1,2 2000-01-06,24.8,25.34,24.51,24.78,1,2 2000-01-07,24.65,25,24.15,24.22,1,2 2000-01-10,24.15,24.75,24.02,24.67,1,2 2000-01-11,24.71,25.8,24.67,25.77,1,2 2000-01-12,25.73,26.55,25.38,26.28,1,2 2000-01-13,26.28,27.12,26.2,26.69,1,2 2000-01-14,26.8,28.1,26.61,28.02,1,2 2000-01-18,28.03,29,27.96,28.85,1,2 2000-01-19,28.8,29.68,28.55,29.54,1,2 2000-01-20,29.25,29.95,28.8,29.66,1,2 2000-01-21,27.85,29.1,27.7,28.2,1,2 2000-01-24,28.23,28.69,27.51,27.83,1,2 2000-01-25,27.78,28.55,27.35,28.28,1,2 2000-01-26,28.29,28.77,27.8,27.84,1,2 2000-01-27,27.78,28.7,27.19,27.32,1,2 2000-01-28,27.22,27.55,26.95,27.22,1,2 2000-01-31,27.1,27.9,26.7,27.64,1,2 2000-02-01,27.6,28.4,27.6,28.22,1,2 2000-02-02,28.22,28.65,27.45,27.55,1,2 2000-02-03,27.56,28.4,27.41,28.03,1,2 2000-02-04,28.17,28.88,27.75,28.82,1,2 2000-02-07,28.71,28.9,28.25,28.45,1,2 2000-02-08,28.44,28.49,27.9,28.02,1,2 2000-02-09,28,28.84,27.71,28.77,1,2
and the python script I am running is:
from __future__ import (absolute_import, division, print_function, unicode_literals) import backtrader as bt class TestStrategy(bt.Strategy): def log(self, txt, dt=None): ''' Logging function for this strategy''' dt = dt or self.datas.datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data dataseries self.datetime = self.datas.datetime self.dataclose = self.datas.close self.dataopen = self.datas.open self.datahigh = self.datas.high self.datalow = self.datas.low def next(self): self.log('Close, %.2f' % self.dataclose) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() DataPath = 'C:\\dev\\datafolder\\' DataFile = 'SampleError.csv' data = bt.feeds.GenericCSVData( dataname = DataPath + DataFile, timeframe=bt.TimeFrame.Days, reverse = False, dtformat ='%Y-%m-%d', datetime = 0, time = -1, open = 1, high = 2, low = 3, volume = 5, close = 4, openinterest = 6, ) cerebro.adddata(data) data2 = cerebro.resampledata(data, timeframe=bt.TimeFrame.Weeks, compression=1 ) #cerebro.adddata(data2) # Add a strategy cerebro.addstrategy(TestStrategy) print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) thestrats = cerebro.run() # (stdstats=False, runonce=False) cerebro.plot()
The output looks like this (and my questions below)
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32 Backend Qt5Agg is interactive backend. Turning interactive mode on. Starting Portfolio Value: 10000.00 2000-01-10, Close, 24.67 2000-01-11, Close, 25.77 2000-01-12, Close, 26.28 2000-01-13, Close, 26.69 2000-01-14, Close, 28.02 2000-01-18, Close, 28.85 2000-01-19, Close, 29.54 2000-01-20, Close, 29.66 2000-01-21, Close, 28.20 2000-01-24, Close, 27.83 2000-01-25, Close, 28.28 2000-01-26, Close, 27.84 2000-01-27, Close, 27.32 2000-01-28, Close, 27.22 2000-01-31, Close, 27.64 2000-02-01, Close, 28.22 2000-02-02, Close, 27.55 2000-02-03, Close, 28.03 2000-02-04, Close, 28.82 2000-02-07, Close, 28.45 2000-02-08, Close, 28.02 2000-02-09, Close, 28.77 2000-02-09, Close, 28.77 Traceback (most recent call last): File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2910, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-2-e33710618050>", line 58, in <module> cerebro.plot() File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 991, in plot start=start, end=end, use=use) File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\plot\plot.py", line 189, in plot self.plotind(None, ptop, subinds=self.dplotsover[ptop]) File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\plot\plot.py", line 452, in plotind plottedline = pltmethod(xdata, lplotarray, **plotkwargs)
So my questions are:
- What is this warning about the interactive backend? And do I care or not care?
- What am I doing wrong that I cannot have this resampled code as a second feed?
I tried also to load another datafile and left a gap in the bars. Same kind of error. Have not tried on a different system (was planning to do this on a fresh PC)
Hope to find some clarity.
RE: How to close current position at the end of backtest?
I am very very new to backtrader, but from my understanding you add a stop method (https://www.backtrader.com/docu/strategy.html?highlight=stop#backtrader.Strategy.stop) to your strategy where you close the position.
without having tried it, the below should do the trick:
def stop(self): targetsize = 0 self.order = self.order_target_size(target=targetsize)