Bracketorder : Order gets executed at open instead of limit price.
-
Quoting @Robin-Dhillon : "the purpose of a bracket order is as follows:
A mainside order: Basically the set price you want to enter in, could be Limit Price or StopLimit order
A lowside order: basically an order to make sure you get out a trade in the event it goes against your initial analysis
A highside order: When you enter, you want to make sure that you get out in time, given the trade goes in the favorable decision."I'm facing an error where my main side order doesn't get executed at limit price but instead the open of the candle ( where my limit price hits and is under valid days) which contradicts what @Robin-Dhillon said and in general the actual motive of limit orders.
Here's my code:
price = self.datas[0].low[0] valid = datetime.timedelta(minutes = self.params.limmins) self.log('BUY CREATE, %.2f' % price) sl= self.datas[0].high[0] * 1.0001 tgt = price - 2*(sl- price) # Keep track of the created order to avoid a 2nd order os = self.sell_bracket(plimit = price,exectype=bt.Order.Limit, stopprice=sl,stopexec=bt.Order.Stop, limitprice=tgt, limitexec=bt.Order.Limit, valid=valid, stopargs=dict(valid=None),limitargs=dict(valid=None),)
Please suggest me where I'm wrong, Thanks
-
@backtrader any help would be appreciated
-
@devang-bhanushali As the document says :
"Limit
Execution:The price set at order creation if the data touches it, starting with the next price bar.
The order will be canceled if valid is set and the time point is reached
Price Matching:
backtrader tries to provide most realistic execution price for Limit orders.
Using the 4 price spots (Open/High/Low/Close) it can be partially inferred if the requested price can be improved.
For Buy Orders
Case 1:
If the open price of the bar is below the limit price the order executes immediately with the open price. The order has been swept during the opening phase of the session
Case 2:
If the open price has not penetrated below the limit price but the low price is below the limit price, then the limit price has been seen during the session and the order can be executed
The logic is obviously inverted for Sell orders."
The limit order will be executed for the best possible price and since open was the best, it got executed on it.