Skip to content

Commit e7e19af

Browse files
author
Shlomi Kushchi
authored
Merge pull request #106 from alpacahq/enable_trailing_stop_orders
Enable trailing stop orders
2 parents 5d7c79e + 62f69c2 commit e7e19af

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

alpaca_backtrader_api/alpacastore.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ def get_value(self):
709709
bt.Order.Limit: 'limit',
710710
bt.Order.Stop: 'stop',
711711
bt.Order.StopLimit: 'stop_limit',
712+
bt.Order.StopTrail: 'trailing_stop',
712713
}
713714

714715
def broker_threads(self):
@@ -766,7 +767,7 @@ def order_create(self, order, stopside=None, takeside=None, **kwargs):
766767
okwargs['side'] = 'buy' if order.isbuy() else 'sell'
767768
okwargs['type'] = self._ORDEREXECS[order.exectype]
768769
okwargs['time_in_force'] = "gtc"
769-
if order.exectype != bt.Order.Market:
770+
if order.exectype not in [bt.Order.Market, bt.Order.StopTrail]:
770771
okwargs['limit_price'] = str(order.created.price)
771772

772773
if order.exectype in [bt.Order.StopLimit, bt.Order.Stop]:
@@ -785,6 +786,18 @@ def order_create(self, order, stopside=None, takeside=None, **kwargs):
785786
if stopside or takeside:
786787
okwargs['order_class'] = "bracket"
787788

789+
if order.exectype == bt.Order.StopTrail:
790+
if order.trailpercent and order.trailamount:
791+
raise Exception("You can't create trailing stop order with "
792+
"both TrailPrice and TrailPercent. choose one")
793+
if order.trailpercent:
794+
okwargs['trail_percent'] = order.trailpercent
795+
elif order.trailamount:
796+
okwargs['trail_price'] = order.trailamount
797+
else:
798+
raise Exception("You must provide either trailpercent or "
799+
"trailamount when creating StopTrail order")
800+
788801
okwargs.update(**kwargs) # anything from the user
789802

790803
self.q_ordercreate.put((order.ref, okwargs,))

requirements/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
backtrader==1.9.76.123
2-
alpaca-trade-api==0.49.1
2+
alpaca-trade-api==0.50.1
33
trading_calendars==1.11.10

0 commit comments

Comments
 (0)