I am just trying to do a simple strategy for entries with stop loss and take profit. It works well with historical data. However, I get this error in real time after few entries:
Strategy 'TplRiskMBE/284109888': An Exit() method to submit an exit order at '7/01/2023 7:01:00 AM' has been ignored. Please search on the term 'Internal Order Handling Rules that Reduce Unwanted Positions' in the Help Guide for detailed explanation.
Entry Price: 11144.25 SL Price: 11143 TP Price: 11146.5
Exit Profit Long: SubmittedTime: 7/01/2023 7:00:32 AM
Strategy 'TplRiskMBE/284109888': Error on calling 'OnExecutionUpdate' method on bar 5400: Object reference not set to an instance of an object.
Strategy Terminated
Disabling NinjaScript strategy 'TplRiskMBE/284109888'
I use OnExecutionUpdate to determine if the entry order, long or short was filled, then I proceed to adjust the SL and TP through the method below.
private void SetSLTP(Execution execution)
{
var entryPrice = execution.Order.AverageFillPrice;
_entrtPrice = entryPrice;
if (_currentPosition == CurrentPos.Long)
{
_BEpriceTrigger = entryPrice + BeTrigger * TickSize;
_BEprice = entryPrice + _BEticks * TickSize;
_stop = entryPrice - _sl * TickSize;
_profit = entryPrice + _tp * TickSize;
_profitOrder = ExitLongLimit(0, true, _posSize, _profit, ProfitLong, LongPos);
_stopOrder = ExitLongStopMarket(0, true, _posSize, _stop, StopLong, LongPos);
Print("Entry Price: "+ entryPrice +" SL Price: "+_stop + " TP Price: "+_profit);
Print("Exit Profit Long: " + _profitOrder.OrderState + "Time: " + _profitOrder.Time);
Print("Exit SL Long: " + _stopOrder.OrderState + "Time: " + _stopOrder.Time);
//ExitLong()
}
else if (_currentPosition == CurrentPos.Short)
{
_BEpriceTrigger = entryPrice - BeTrigger * TickSize;
_BEprice = entryPrice - _BEticks * TickSize;
_stop = entryPrice + _sl * TickSize;
_profit = entryPrice - _tp * TickSize;
_profitOrder = ExitShortLimit(0, true, _posSize, _profit, ProfitShort, ShortPos);
_stopOrder = ExitShortStopMarket(0, true, _posSize, _stop, StopShort, ShortPos);
Print("Entry Price: " + entryPrice + " SL Price: " + _stop + " TP Price: " + _profit);
Print("Exit Profit Short: " + _profitOrder.OrderState + "Time: " + _profitOrder.Time);
Print("Exit SL Short: " + _stopOrder.OrderState + "Time: " + _stopOrder.Time);
}
}
I think it is not creating the _stopOrder. Initially it is doing it but it fails after few entries. Is it there a better way to do this?
I appreciate your help.

Comment