Hi,
I’m trying to build a script to exit/liquidate my manual market orders if my stoploss is rejected due to market price above/below the stoploss price. This issue happens in case the market suddenly moves significantly in the other direction when I enter a market order.
I’m trading Nasdaq MNQ using a 20 range chart with an automatic 10 tick stoploss (ATM).
However, the script I tried to build doesn’t work properly.
I finally made my script to compile without compilling errors, but when I back-tested it, my orders didn’t get liquidates/exited when the stoploss my rejected.
Below is my script
Can everyone help on what I need to make this correct?
It’s important to say the script should only exit my positions if the stoploss gets rejected on trade insertion.
#region Using declarations
using System;
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class StopLossRejection : Strategy
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @“Exit position when stop loss gets rejected”;
Name = “StopLossRejection”;
Calculate = Calculate.OnPriceChange;
// To ignore errors for OnOrderUpdate to take action on rejections
RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
// To manage manual orders (trades will be booked manually and are not inserted by this script)
IsUnmanaged = true;
}
}
// Take action when an order's status change
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice,
OrderState orderState, DateTime time, ErrorCode errorCode, string nativeError)
{
// Check if order was rejected
if (orderState == OrderState.Rejected)
{
// Take action on existing orders (exit short position | exit long position)
if (order.OrderAction == OrderAction.Sell || order.OrderAction == OrderAction.BuyToCover)
{
if (Position.MarketPosition == MarketPosition.Long)
{
// Exit position if order is a Long position (Liquidate long positions)
ExitLong(Position.Quantity, Instrument.FullName, "RejectedExitLiquidation");
Print("Long Position liquidated immediately due to rejected exit/stop-loss order.");
}
else if (Position.MarketPosition == MarketPosition.Short)
{
// Exit position if order is a Short position (Liquidate short positions)
ExitShort(Position.Quantity, Instrument.FullName, "RejectedExitLiquidation");
Print("Short Position liquidated immediately due to rejected exit/stop-loss order.");
}
}
}
}
}
}
Thank you so much
I really appreciate if everyone can take a look
Regards,
Thomas