alpacahq/alpaca-backtrader-api

Cannot purchase Long and create a trailing stop

alexanderward opened this issue · 0 comments

I'm trying to buy for a long position with a trailing stop.

Source snippet:

valid_for_minutes = 1
buy_long = self.buy(exectype=exectype,
                                    price=price,
                                    size=data.long_size,
                                    valid=timedelta(minutes=valid_for_minutes),
                                    transmit=False)
data.long_order = self.sell(exectype=bt.Order.StopTrail,
                            parent=buy_long,
                            size=data.long_size,
                            trailamount=trail_amount)

Traceback:

  File "..\backtrader_v1\strategies\base.py", line 191, in purchase
    data.long_order = self.sell(exectype=bt.Order.StopTrail,
  File "C:\Projects\Personal\Trading\trader\backtrader_v1\venv\lib\site-packages\backtrader\strategy.py", line 963, in sell
    return self.broker.sell(
  File "C:\Projects\Personal\Trading\trader\backtrader_v1\venv\lib\site-packages\alpaca_backtrader_api\alpacabroker.py", line 323, in sell
    return self._transmit(order)
  File "C:\Projects\Personal\Trading\trader\backtrader_v1\venv\lib\site-packages\alpaca_backtrader_api\alpacabroker.py", line 275, in _transmit
    parent, stopside = self.opending.pop(pref)
ValueError: not enough values to unpack (expected 2, got 1)

Requirements.txt

alpaca-backtrader-api==0.15.0
setuptools==59.5.0

Long works fine alone, bracket orders work fine, but when I use the transmit=False and use the non-transmitted purchase as a parent I get this error. I'm basing my code on the example: https://www.backtrader.com/blog/posts/2018-02-01-stop-trading/stop-trading/ under the AutoStopOrStopTrail class. The ManualStopOrStopTrail class works fine because it doesn't leverage the parent and transmit=False feature, but it delays the submitting the trailing stop by a minute...so it's not optimal.