Hi folks,
While I was running my strategy today on my sim account to test it I got these errors a few times. I'm using OnEachTick and pasted my entry and sl/tp code below. From reading the error message and previous posts on this forum I understand that the price has moved in the time it takes for the stop loss or tp to get submitted, and that it's no longer valid given current price. My question is how should I go about catching this, closing the open position (if this happens then I'd want to exit to be on the safe side), and then continuing to run the strategy? Do you by chance have any example/sample code that does the above that I can reference? Thank you!
//Buy
if (buySignalPrice > 0 && Close[0] > buySignalPrice && Position.MarketPosition == MarketPosition.Flat && orderBar != CurrentBar)
{
EnterLong(Convert.ToInt32(DefaultQuantity), @"Long");
SetStopLoss(CalculationMode.Price, buySignalPrice);
//calculate stop loss diff between st and close
double risked = Close[0] - buySignalPrice;
double target = (Close[0] + risked) * ProfitTargetRatio;
SetProfitTarget(CalculationMode.Price, target);
Print(string.Format( "{0} | Buy signal: {1} Close: {2} Open: {3}, Target: {4}", Time[0], buySignalPrice, Close[0], Open[0], target));
orderBar = CurrentBar;
}
//Sell
if (sellSignalPrice > 0 && Close[0] < sellSignalPrice && Position.MarketPosition == MarketPosition.Flat && orderBar != CurrentBar)
{
EnterShort(Convert.ToInt32(DefaultQuantity), @"Short");
SetStopLoss(CalculationMode.Price, sellSignalPrice);
//calculate stop loss diff between st and close
double risked = sellSignalPrice - Close[0];
double target = (Close[0] - risked) * ProfitTargetRatio;
SetProfitTarget(CalculationMode.Price, target);
Print(string.Format( "{0} | Buy signal: {1} Close: {2} Open: {3}, Target: {4}", Time[0], sellSignalPrice, Close[0], Open[0], target));
orderBar = CurrentBar;
}

Comment