Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Removing stop Loss orders

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Removing stop Loss orders

    Hi friends,
    I'm still learning scripting in NT, so please be patient.
    I am testing a strategy where I would like to make the following (I make the example with an initial LONG order: it's symmetrical for short trades)
    * when I have a crossover in one direction I place a EnterLongStop order with a Stop Loss
    * I keep the order open GTC
    * If a crossover in the opposite direction takes place, I would like to cancel the stop loss and reverse the position with a EnterShortStop order.

    Reading the Managed Orders rules, I discovered that this will not work so easily, because, if a Stop Loss order (let's say, for exiting a Long trade) is active, no EnterShortStop is allowed (conflict between the Long Stop and the Short Entry). I.e., I think I cannot simply use the SetStopLoss() method.

    So: I tried to make the following:
    In the Variables section I have defined my four brave IOrder objects:
    Code:
    private IOrder 	myLongOrder  	= null;
    private IOrder 	myShortOrder 	= null;
    private IOrder 	existsLSL 	= null;
    private IOrder 	existsSSL 	= null;
    Then, I created and OnExecution method which places a conditional stop loss order (that I can "name" and therefore check and delete later) for the stop loss:

    Code:
    protected override void OnExecution(IExecution execution)
    		{
    			if(myLongOrder != null && myLongOrder == execution.Order && myStopLoss > 0)
    			{
    				Print(execution.ToString());
    				double stopDistance = (myLongOrder.AvgFillPrice * myStopLoss/100);
    				double stopLevel    = (myLongOrder.AvgFillPrice - stopDistance);
    				existsLSL			= ExitLongStop(0, true, 10000, stopLevel, "Long Stop Loss", "MA bull crossover");
    			}
    			
    			if(myShortOrder != null && myShortOrder == execution.Order && myStopLoss > 0)
    			{
    				Print(execution.ToString());
    				double stopDistance = (myShortOrder.AvgFillPrice * myStopLoss/100);
    				double stopLevel    = (myShortOrder.AvgFillPrice + stopDistance);
    				existsSSL		= ExitShortStop(0, true, 10000, stopLevel, "Short Stop Loss", "MA bear crossover");
    			}
    		}
    In the OnBarUpdate() method, I have inserted then the controls for managing (and cancelling) the existing pending orders when a new crossover is detected, like that (for the bullish crossover:

    Code:
    // Condition set 1
                if (CrossAbove(SMA(Close, FastMAperiod), SMA(Close, SlowMAperiod), 1))
                {	
    				if(myShortOrder != null) 
    				 {
    					CancelOrder(myShortOrder);
    					//debug
    					//Print("SellStop cancelled");
    					myShortOrder = null;
    				 }
    				
    				if(existsSSL != null)
    				{
    					//Cancelling stop loss in the other direction otherwise, due to Ninja Managed Order methods
    					//it won't be possible to place an opposite-direction order
    					CancelOrder(existsSSL);
    					existsSSL = null;
    				}
    When I try to backtest the system, I get this output:

    Code:
    **NT** Error on calling 'OnBarUpdate' method for strategy 'Luxor1v3SL/b6754a89931b48c3bf9e888b7582d47c': Object reference not set to an instance of an object.
    ???? By debugging (outcommenting) I realize the problem is the call of the "existsSSL" within the OnBarUpdate() method. I was convinced that an IOrder instance was accessible from all the methods of the script.

    Can some mighty guy around give me some hints? Is there a simpler way of making what I need? Why cannot I access and IOrder instance created by the OnExecution method from within the OnBarUpdate()?

    Thanks a lot in advance!

    fsprea

    #2
    Hello fsprea,
    Thanks for writing in and I am happy to assist you.

    You can place an EnterShortStop order if your position is flat. So before you submit an stop order just check the current postion with the below code and see there are no live/pending Set() orders.

    Code:
    if (Position.MarketPosition == MarketPosition.Flat)
    {
       EnterShortStop(...);
    }



    You might be cancelling an null order for which you are getting an null object error. Please change the code CancelOrder(myShortOrder); to
    Code:
    if (myShortOrder != null) CancelOrder(myShortOrder);
    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Hi Joy,
      thank you very much for your reply.
      Unfortunately, surely due to my inability of explaining things correctly, I fear your answer is not helping very much in solving my problem.

      1. Please review my code, and you'll see that I'm checking for existance of the orders before trying to cancel them:
      Code:
      if(myShortOrder != null) 
      {
      	CancelOrder(myShortOrder);
      	//debug
              //Print("SellStop cancelled");
      	myShortOrder = null;
      }
      ...
      2. My problem is exactely that I would like to be able to submit an opposite order when my position is not (yet) flat. The idea is this:
      * I trade a crossover, basically with a SAR (stop and reverse) mechanism
      * When a crossover happens, orders are not placed at market, but as stop orders above/below the current bar high/low.
      * Signals are kept alive (GTC) as soon as
      * they are either executed
      * or a crossover in the direction of the current trade happens.
      * Stop Loss is used, but, if before it has been executed, the "crossover signal" in the opposite direction happens, the stop loss has to be removed and the trade closed by an opposite one being executed.

      So: let's try with an example:
      * bullish crossover
      * a EnterLongStop order is placed with stop = High[0];
      * The order gets executed: I want immediately to place a StopLoss let's say at entry - 1%. I make that by immediately placing a ExitLongStop at (entry-1%); (OnExecution() method).

      * Before the stop loss gets hit, a bearish crossover happens.
      * There: I would like to place a EnterShortStop to the Low[0] of the crossover bar.
      * For making that, as far as I have understood, you have to remove the ExitLongStop, because Managed Orders Rules forbid to have two Short Stop Orders active (the stop loss + the enter short stop).

      So: my problem below was:
      * I have created IOrder instances keeping track of the four possible order types (EnterLong-ShortStop; ExitLong-ShortStop).
      * I submit and keep track of the ExitStop orders within the OnExecution() method
      * When I want to check them within the OnBarUpdate() method I get a warning that I am not referring to valid objects...

      Sorry: I can understand that the whole could seem quite weird. What I'm trying to do is to verify with Ninja the experiments reported in Tomasini&Jaeckle book. I'm not planning to use this system in real trading: it's for backtesting and optimization.

      If you or somebody else would be so nice to follow up, thanks a lot in advance!

      Bye

      fsprea

      Comment


        #4
        Hello fsprea,
        My apologies for the overlook.

        Unfortunately what you are trying to do is not possible with the Managed approach as the internal order handling rules will reject your orders.

        You can however used the unmanaged approach to do what you are trying to achieve. Please refer here for more http://www.ninjatrader.com/support/h...d_approach.htm

        Also please do make sure your broker too supports such order executions.

        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Thanks Joy,
          Yes, this was the conclusion I was getting to while waiting for your answer: go for the unmanaged approach.

          I have seen around that many people are leaning towards unmanaged approach. The managed one seems to be very handy for quite simple strategies. As soon as you want to go a bit deeper you need to take full control. Isn't it?

          Bye and thanks again.

          fsprea

          Comment


            #6
            Hello fsprea,
            Yes, if you need full control, particularly when it comes to complex order submissions, you need to use Unmanaged approach.

            Please let me know if I can assist you any further.
            JoydeepNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            648 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            369 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            108 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            572 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            573 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X