Plotting StopLoss orders
reynard80 last edited by reynard80
When I use stoploss orders, these are plotted when they are accepted, not when they are actually executed. This results in double buy/sell markers in my plots, when an order is placed with a stoploss. See the screen below (with both long and short orders for opening a position with a stoploss, resulting in a double marker).
Wouldn't it make more sense to actually plot an order when it is executed? The stoploss orders would then just be plotted when the position is closed.
Thanks for the great work on BT!
When I use stoploss orders
A stoploss order doesn't exist. It's a concept you use to try to limit your losses. You implement the concept using the available execution types.
You may wish to share the code you use to create that, else there is no way to know what you mean.
reynard80 last edited by
@backtrader I use a StopTrail or StopTrailLimit order for a stoploss. For example, when a Limit or Market order is completed, a stoploss is set with the following code:
if order.getordername() in ['Market', 'Limit']: # For regular orders self.order = None # Create stoploss order if applicable if order.status == order.Completed: if self.p.stoptype: if self.getposition(data=self.posdata): # Open position (and no open orders, above) # Evaluate postion exit for stoploss possize = self.getposition(data=self.posdata).size if possize > 0: # Long position price = None plimit = None trailpercent = None if self.p.stoptype == bt.Order.StopTrailLimit: price = self.middata.close # Base price for trailing stop plimit = self.middata.close + self.p.limitoffset # Price of limit order trailpercent = self.p.trailpercent # Percentage of trailing stop elif self.p.stoptype == bt.Order.StopTrail: price = self.middata.close # Base price for trailing stop trailpercent = self.p.trailpercent # Percentage of trailing stop stoporderexitkwargs = dict( data=self.compdata, size=self.p.stake, exectype=self.p.stoptype, price=price, plimit=plimit, trailpercent=trailpercent, valid=self.p.valid ) # print('Add stoploss order') self.stoporder = self.sell(**stoporderexitkwargs) self.orderid.append(self.stoporder)