Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot purchase Long and create a trailing stop #391

Open
alexanderward opened this issue Apr 1, 2022 · 0 comments
Open

Cannot purchase Long and create a trailing stop #391

alexanderward opened this issue Apr 1, 2022 · 0 comments

Comments

@alexanderward
Copy link

alexanderward commented Apr 1, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant