@Nguyễn-Tài-Nguyên I have the same issue. Its a quedtion for @Ed-Bartosh that I hope can spare some time soon to help us out.
Best posts made by Søren Pallesen
-
RE: Anyone use backtrader to do live trading on Bitcoin exchange?
Latest posts made by Søren Pallesen
-
Help with Volume-Weigthed Moving Average (VWMA) indicator needed
Have anyone done an indicator on this that they would be willing to share? Im interested in both simple and exponential smoothing.
Alternatively, please share some tips on how to approach this given that I have not gotten my head around to indicator development yet and are not experienced in sub-classing/inheritance in Python (with some tips I could likely hack my way through it)
in short any help would be highly appreciated. Thx in advance & Merry Christmas !
-
RE: Indicator based on other indicators with 2 data feeds
@søren-pallesen said in Indicator based on other indicators with 2 data feeds:
@backtrader thanks for the response. Just to be clear there are no problems plotting with multiple data feeds with different timeframes. It breaks when I add calculated indicators on the 2nd feed like:
differ = self.ema_day - self.kama_day self.roc = btind.ROC(differ, period=6)
Adding the same calculated indicator on the first feed works and plots fine.
-
Indicator based on other indicators with 2 data feeds
Getting an error when base an indicator on a calculation of two other indicators from a second data feed:
import backtrader as bt import backtrader.indicators as btind class myStrategy(bt.SignalStrategy): def __init__(self): self.ema_day = btind.EMA(self.data1, period=15) self.kama_day = btind.KAMA(self.data1, period=25, fast=3, slow=30) differ = self.ema_day - self.kama_day self.roc = btind.ROC(differ, period=6) def next(self): pass data = bt.feeds.GenericCSVData( dataname='./data/VXX-15m-2017-10-01us.csv', dtformat='%Y-%m-%d %H:%M:%S', openinterest=-1, timeframe=bt.TimeFrame.Minutes, compression=15 ) data_day = bt.feeds.GenericCSVData( dataname='./data/VXX-1d-2017-10-01us.csv', dtformat='%Y-%m-%d', openinterest=-1, timeframe=bt.TimeFrame.Days, compression=1 ) cerebro = bt.Cerebro() cerebro.addstrategy(myStrategy) cerebro.adddata(data) cerebro.adddata(data_day) cerebro.run() cerebro.plot()
And get this error:
ValueError: x and y must have same first dimension, but have shapes (7542,) and (300,)
Here is the full message:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-1-6c0f97016110> in <module> 34 35 cerebro.run() ---> 36 cerebro.plot() ~\Anaconda3\envs\IBbt\lib\site-packages\backtrader\cerebro.py in plot(self, plotter, numfigs, iplot, start, end, width, height, dpi, tight, use, **kwargs) 989 rfig = plotter.plot(strat, figid=si * 100, 990 numfigs=numfigs, iplot=iplot, --> 991 start=start, end=end, use=use) 992 # pfillers=pfillers2) 993 ~\Anaconda3\envs\IBbt\lib\site-packages\backtrader\plot\plot.py in plot(self, strategy, figid, numfigs, iplot, start, end, **kwargs) 226 subinds=self.dplotsover[ind], 227 upinds=self.dplotsup[ind], --> 228 downinds=self.dplotsdown[ind]) 229 230 cursor = MultiCursor( ~\Anaconda3\envs\IBbt\lib\site-packages\backtrader\plot\plot.py in plotind(self, iref, ind, subinds, upinds, downinds, masterax) 450 xdata = np.array(xdata)[lplotmask] 451 --> 452 plottedline = pltmethod(xdata, lplotarray, **plotkwargs) 453 try: 454 plottedline = plottedline[0] ~\Anaconda3\envs\IBbt\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs) 1715 warnings.warn(msg % (label_namer, func.__name__), 1716 RuntimeWarning, stacklevel=2) -> 1717 return func(ax, *args, **kwargs) 1718 pre_doc = inner.__doc__ 1719 if pre_doc is None: ~\Anaconda3\envs\IBbt\lib\site-packages\matplotlib\axes\_axes.py in plot(self, *args, **kwargs) 1370 kwargs = cbook.normalize_kwargs(kwargs, _alias_map) 1371 -> 1372 for line in self._get_lines(*args, **kwargs): 1373 self.add_line(line) 1374 lines.append(line) ~\Anaconda3\envs\IBbt\lib\site-packages\matplotlib\axes\_base.py in _grab_next_args(self, *args, **kwargs) 402 this += args[0], 403 args = args[1:] --> 404 for seg in self._plot_args(this, kwargs): 405 yield seg 406 ~\Anaconda3\envs\IBbt\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs) 382 x, y = index_of(tup[-1]) 383 --> 384 x, y = self._xy_from_xy(x, y) 385 386 if self.command == 'plot': ~\Anaconda3\envs\IBbt\lib\site-packages\matplotlib\axes\_base.py in _xy_from_xy(self, x, y) 241 if x.shape[0] != y.shape[0]: 242 raise ValueError("x and y must have same first dimension, but " --> 243 "have shapes {} and {}".format(x.shape, y.shape)) 244 if x.ndim > 2 or y.ndim > 2: 245 raise ValueError("x and y can be no greater than 2-D, but have " ValueError: x and y must have same first dimension, but have shapes (7542,) and (300,)
Its a resent install of BT (v1.9.67.122). I'm properly missing something simple ...Any ideas?
-
How does compression work when resampling feed data (...and I have read the docs)
Reading the documentation/examples and studying community posts on resampling I still have a few questions. Mainly related to how compression in the feed relates to compression in resampledata and the specific role of sessionstart/sessionend.
Question 1: Im loading 1 hour bar data. Can I resample from 1 hour bars to 90 minute bars, like this?:
data = bt.feeds.GenericCSVData( dataname='./data/VXX-1h-2016-08-09us.csv', #1 hour bars timeframe=bt.TimeFrame.Minutes, compression=60, sessionstart=datetime.time(9, 30), sessionend=datetime.time(15, 0)) #souce resolution is a higher timeframe than destination resolution - does that work? cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=90)
Question 2: Im loading 15min bar data. Can i resample from 15min bars to 120min bars like this?:
data = bt.feeds.GenericCSVData( dataname='./data/VXX-15m-2016-08-09us.csv', #15 minute bars timeframe=bt.TimeFrame.Minutes, compression=15, sessionstart=datetime.time(9, 30), sessionend=datetime.time(16, 0)) #souce resolution is divisible with destination resolution - is that a requirement? cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=120)
Question 3: Im loading 15min bar data. Can i resample from 15min bars to 50min bars like this?:
data = bt.feeds.GenericCSVData( dataname='./data/VXX-15m-2016-08-09us.csv', #15 minute bars timeframe=bt.TimeFrame.Minutes, compression=15, sessionstart=datetime.time(9, 30), sessionend=datetime.time(16, 0)) #souce resolution is indivisible with destination resolution - does that work? cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=50)
Question 4: Using NASDAQ as an example trading session starts 9.30 and ends 16.00 and my feed data consists of 1 hour bars with first daily bar at 10.00 last daily bar at 15.00. Should sessionstart and sessionend be set:
#the data's trading hours sessionstart=datetime.time(10, 0), sessionend=datetime.time(15, 0)
Or:
#the exchange's actual trading hours sessionstart=datetime.time(9, 30), sessionend=datetime.time(16, 0)
Also if possible please explain the role og sessionstart and sessionend, like what does it do? And should always set them when loading data?
Thx in advance :-)
-
RE: Where to get OHLCV data on crypto currencies?
Thx a bunch for the pointers. Sounds very interesting poling Bitstamp, as im also trading there. Can you share the code you use to get data and construct candles on Bitstamp?
-
RE: Anyone use backtrader to do live trading on Bitcoin exchange?
Anyone have tips for where to get good OHLCV candle data for backtesting on crypto currencies?
Are there any commercial offerings on the market?
-
Where to get OHLCV data on crypto currencies?
Anyone have tips for where to get good OHLCV candle data for backtesting on crypto currencies?
Are there any commercial offerings on the market?
-
RE: Bracket order with a Trailing Stop
This worked fo me (it seems - still have more testing to do) in case anyone is interested ... :-)
mainside = self.buy(transmit=False) lowside = self.sell(price=self.data.close[0]*(1-self.params.sPerc), size=mainside.size, exectype=bt.Order.Stop, transmit=False, parent=mainside) highside = self.sell(price=self.data.close[0]*self.params.lPerc, size=mainside.size, exectype=bt.Order.Limit, transmit=False, parent=mainside) trailside = self.sell(size=mainside.size, exectype=bt.Order.StopTrail, trailpercent=self.params.tPerc, transmit=True, parent=mainside)
Incendently anyone know the differences in doing this with 'OCO' vs the 'Parent' property?
-
RE: dealing with ExchangeError in backtrader ccxt branch
@Ed-Bartosh will this fix get merged into your main CCXT branch?
-
RE: Bracket order with a Trailing Stop
A trailing stop and a (normal) stop works completly different. For example on position bought at $10 a stop at say $9 would protect against a loss. A trailing stop of 20% would ensure that you sold if the price drops 20% at any time of the life span on the position. So if the price increased to $20 and drops to $16 it would be sold with a profit of $6.
A normal stop is static protecting against a loss and a trailingstop dynamic and typically primarily used to lock in a profit while keeping the upside of further increases.