Create order object for later submission
-
Is there a way to create an Order object to be stored and issued to the broker at a later date?
-
Following documentation leads me to a KeyError with a _ococheck(order).
Traceback (most recent call last): File "run.py", line 205, in <module> main(args) File "run.py", line 78, in main bm.run(**run_kwargs) File "/opt/emeraldmine/backtest.py", line 61, in run runs = self.cerebro.run(stdstats=stdstats) File "/usr/local/lib/python3.7/dist-packages/backtrader/cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "/usr/local/lib/python3.7/dist-packages/backtrader/cerebro.py", line 1298, in runstrategies self._runnext(runstrats) File "/usr/local/lib/python3.7/dist-packages/backtrader/cerebro.py", line 1623, in _runnext self._brokernotify() File "/usr/local/lib/python3.7/dist-packages/backtrader/cerebro.py", line 1360, in _brokernotify self._broker.next() File "/usr/local/lib/python3.7/dist-packages/backtrader/brokers/bbroker.py", line 1221, in next self._try_exec(order) File "/usr/local/lib/python3.7/dist-packages/backtrader/brokers/bbroker.py", line 1073, in _try_exec self._try_exec_limit(order, popen, phigh, plow, pcreated) File "/usr/local/lib/python3.7/dist-packages/backtrader/brokers/bbroker.py", line 912, in _try_exec_limit self._execute(order, ago=0, price=p) File "/usr/local/lib/python3.7/dist-packages/backtrader/brokers/bbroker.py", line 845, in _execute self._ococheck(order) File "/usr/local/lib/python3.7/dist-packages/backtrader/brokers/bbroker.py", line 618, in _ococheck parentref = self._ocos[order.ref] KeyError: 1
-
Could you include your code please?
-
Strategy:
class testStrategy(BaseStrategy): params = dict( period=21, temp=True ) def __init__(self): self.rsi = bt.indicators.RSI_SMA( self.data1.close, period=self.p.period, safediv=True, lowerband=30.0, upperband=70.0) self.cross = bt.indicators.CrossOver(self.rsi, self.line) def next(self): if not self.position: if self.rsi < 30: order = bt.BuyOrder(data=self.data0, price=self.data0.close[0], size=0.01, exectype=bt.Order.Limit) self.broker.submit(order) else: if self.rsi > 70: order = bt.SellOrder(data=self.data0, price=self.data0.close[0], size=0.01, exectype=bt.Order.Limit) self.broker.submit(order)
-
I can't understand what exactly you need.
And the second what is the self.line variable? -
This is not the situation I intend to use this method of submitting orders, however I don't feel comfortable sharing the logic in which it will be used. This was just a simple use case that still demonstrates the error. However the original logic ultimately involves creating a series of orders to be issued at varying times in the future dependant on price action and the like.
The self.line was missing from edits i made to the original file, my apologies i will amend the original sample.
Trying to follow the error chain, i'm not sure exactly how the _ococheck() works, however I cant understand why its searching for a parent reference without any oco code being present.
-
Dumb question, but why can't you just issue the order in the future?
-
@run-out I can, no doubt, but it will involve getting and referencing numerous values from various lines. It became apparent this could all be a lot easy and frankly look like cleaner code done at once and using a queue system to place the orders in to be pulled at the appropriate time.
All in all this isnt a do or die issue however I believe documentation here that implies any situation in which a buy/sell can be issued can also be replaced with submitting an Order object to the broker should be changed to reflect that this method does not work.