Back

Stop Loss and Take Profit filled simultaneously

Hi Team

I am running a code to set up a Take Profit (TP) and Stop Limit (SL) Order. Using second level data in the backtest, it seems there will be time when both the TP and SL will be filled within the same second slice. This is a problem obviously as I basically end up taking a Loss when there may not have been any need for (assumuing for instant that the TP would have been filled first). One way to adress this would be to use Tick data in the back test , but I think this is not possible (would love to be wrong on this and it would be great to see an example in python example if so).

Is there a workaround available to avoid this "simultaneity" problem so that only one of either the SL or the TP gets filled: 

Below is the code I run:

def OnOrderEvent(self, orderEvent):

if orderEvent.Status == OrderStatus.Filled:
order = self.Transactions.GetOrderById(orderEvent.OrderId)
if orderEvent.FillQuantity>0:
# SETTING Take Proit and Stop Loss Prices following Long Stop Order (in OnData section)
self.TP =d.Decimal(orderEvent.FillPrice + d.Decimal(0.00005)).quantize(d.Decimal('1e-5'))
self.SL =d.Decimal(orderEvent.FillPrice - d.Decimal(0.00005)).quantize(d.Decimal('1e-5'))

self.StopLimitOrder(self.pair,-350000, self.TP ,self.TP ,"TP: {0}".format(self.TP))
self.LimitOrder(self.pair ,-350000, self.SL ,"SL: {0}".format(self.SL))
self.TPFILL=0
self.Log("D1: ENTRY FILL->TP/SL CREATED: TP: {0} SL: {1}".format(self.TP,self.SL))

elif orderEvent.FillQuantity<0 and order.Type ==OrderType.StopLimit:
self.Debug("Canceling SL")
self.Transactions.CancelOpenOrders(order.Symbol)

elif orderEvent.FillQuantity<0 and order.Type ==OrderType.Limit:
self.Debug("Canceling TP")
self.Transactions.CancelOpenOrders(order.Symbol)

Example below from that Trade Log:

TimeSymbolPriceQuantityTypeStatus2017-02-03 13:32:06EURUSD1.07707-3500001Filled2017-02-03 13:32:06EURUSD1.07728-3500003Filled

 How would you address this issue?

Update Backtest








Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed