Exit Manual Market Orders on Stoploss Rejection

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

As code for a stretegy, this seems to be missing a lot of the boilerplate code for it to even work. If you’re not a knowledgeable in how to code, you’ll probably need to hire someone to build this for you. Its not possible to wing it. You can find software developers on platforms like upwork, fiverr, and other places.

When hiring someone who can write code for you, keep in mind that you’re basically hiring brain power, that you’re hoping helps turn your ideas into a set of instructions for a computer to execute.

I don’t suppose anyone would spend all of their concious and mental capital to update your computer program for free… but it could happen, it could happen.

1 Like

I am not an expert but it looks like your strategy is trying to exit strategy managed positions, not those manually entered.

Thank you Maverick.
I’m not an expert in coding, only know some basics.
Tried to learn up on Ninjascript, so I gave it a try.

I might be looking into hiring someone for this then

You are probably right.
Well, I tried to use this in order to make this work on manual orders, but apparently I didn’t get it to work

IsUnmanaged = true;

Thank you for your feedback

Above is strategy exit order.