Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Stop loss not working

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

    #31
    I’ve been working with Joydeep via email on the code that freezes my machine which uses the Set() method.

    In the meantime, I’m working in parallel on the code that doesn’t freeze, which uses ExitLongStop/ExitShortStop. Now that Bertrand was kind enough to inform me about TraceOrders, I’ve figured out why my stop is not being submitted. I get the following message:

    “A BuyToCover stop order placed at '10/21/2011 8:59:00 AM' has been ignored since the stop price is less than or equal to the close price of the current bar. This is an invalid order and subsequent orders may also be ignored. Please fix your strategy.”

    Now look at the attached image to see the trade. Remember that entry occurs on breakage of a trendline. Black dots are stop levels, yellow profit targets.

    So at 7:35 the strategy goes long. At 8:59, the strategy reverses position and goes short. But look where this happens! It happens near the low of the candle, obviously some time before the close. Yes the close is above the stop level, but so what? When the stop was submitted, the price was down there around the low!

    Here is where the stop and reverse orders are being placed:

    //use this method to listen for stop order execution and execute reversal orders as appropriate
    protected override void OnExecution(IExecution execution)
    {
    if(longStopOrder != null && longStopOrder == execution.Order && shortOnReversal)
    {
    CancelOrder(longTargetOrder);
    }

    if(shortStopOrder != null && shortStopOrder == execution.Order && longOnReversal)
    {
    CancelOrder(shortTargetOrder);
    }

    }

    /// <summary>
    /// Called on each incoming order event
    /// </summary>
    protected override void OnOrderUpdate(IOrder order)
    {
    // When a position is stopped out, the target order is canceled in OnExecution. Check for that condition, and then place entry orders
    if (longTargetOrder != null && longTargetOrder == order && shortOnReversal
    && order.OrderState == OrderState.Cancelled && order.Filled == 0)
    {
    shortStopOrder = ExitShortStop(shortStop, "short_entry");
    shortTargetOrder = ExitShortLimit(shortPT, "short_entry");
    EnterShortStopLimit(shortEntry, shortEntry, "short_entry");
    }

    if (shortTargetOrder != null && shortTargetOrder == order && longOnReversal
    && order.OrderState == OrderState.Cancelled && order.Filled == 0)
    {
    longStopOrder = ExitLongStop(longStop, "long_entry");
    longTargetOrder = ExitLongLimit(longPT, "long_entry");
    EnterLongStopLimit(longEntry, longEntry, "long_entry");
    }
    }
    As far as I can tell, NT is just dropping the ball on this one. My stop loss should be placed immediately after the short position is taken. Can anything be done about this?
    Attached Files

    Comment


      #32
      Hello Style,
      Thanks for writing in and I am replying for Bertrand.

      In backtest, orders are processed only at the close of each bar. Thus if you place an buy stop order above close, then it will be rejected.

      Please use the Print() function to print out the exact stop price you are using to submit the order.

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

      Comment


        #33
        Originally posted by NinjaTrader_Joydeep View Post
        if you place an buy stop order above close, then it will be rejected.
        How can that be true? Look closely at the image attached to my last post. The stop limit entry order to reverse positions is not submitted until the stop loss for the long position fires off near the bottom of the candle at 8:59. If orders are not being submitted until the close of the candle, then how is my reversal limit entry order filling way down there near the low of the candle? It MUST be submitted sometime prior to the close! Otherwise, it the limit order would never fill!

        Comment


          #34
          Hello Style,
          My apologies, it should read if buy stop is placed below or equal the closing price then it will be rejected.

          To assist you further can you send the strategy to support[AT]ninjatrader[DOT]com so that I can test it. Please append Attn:Joydeep in the subject line of the email and give a reference of this thread.

          I look forward to assisting you further.
          JoydeepNinjaTrader Customer Service

          Comment


            #35
            If I was running this strategy live, as opposed to a back test, since my code to submit the buy stop happens in the OnOrderUpdate section, not the OnBarUpdate section, assuming price is currently below my by stop, the order would be submitted, right?

            Comment


              #36
              Hello Style,
              OnOrderUpdate event is triggered only when an order is updated, unlike the OnBarUpate event which is triggered on each incoming tick or on bar close depending on how you set CalculateOnBarClose.

              If you have any particular filter set up which may not be true when the OnOrderUpdate event is triggered then your stop wont get submitted.



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

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by rhyminkevin, Today, 04:58 PM
              3 responses
              48 views
              0 likes
              Last Post Anfedport  
              Started by iceman2018, Today, 05:07 PM
              0 responses
              5 views
              0 likes
              Last Post iceman2018  
              Started by lightsun47, Today, 03:51 PM
              0 responses
              7 views
              0 likes
              Last Post lightsun47  
              Started by 00nevest, Today, 02:27 PM
              1 response
              14 views
              0 likes
              Last Post 00nevest  
              Started by futtrader, 04-21-2024, 01:50 AM
              4 responses
              50 views
              0 likes
              Last Post futtrader  
              Working...
              X