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

Stoploss not hitting per strategy

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

    Stoploss not hitting per strategy

    Hello,

    I am trying to set the Stop-Loss one tick above the last bar and have the following code.

    Code:
    // Enter Short
    if ((CrossBelow(MACD1.Default, MACD1.Avg, 1)) && TradeTaken == false && Close[0] < Close[1])
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"Short");
    TradeTaken = true;
    }
    SetStopLoss(@"Short", CalculationMode.Price, High[0] + TickSize, false);
    For some reason, the Stop-Loss isn't triggering when it's supposed to and hitting the profit target (see attached). Should it hit the stoploss (marked in the screenshot) instead?
    Attached Files

    #2
    Hello pyraxic,

    Thanks for your post.

    Please check the "log" tab of the control center for any errors related to your strategy.

    Is the screenshot from a live trade, or playback with market replay data, or just a historical trade on the chart or a historical trade in the Strategy analyzer?

    What Calculate setting does the strategy run with? (Calculate.OnBarClose, Calculate.OnEachTick, Calculate.OnPriceChange)

    Do you have any other code that could be setting the stop-loss to another value unexpectedly?
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_PaulH View Post
      Hello pyraxic,

      Thanks for your post.

      Please check the "log" tab of the control center for any errors related to your strategy.

      Is the screenshot from a live trade, or playback with market replay data, or just a historical trade on the chart or a historical trade in the Strategy analyzer?

      What Calculate setting does the strategy run with? (Calculate.OnBarClose, Calculate.OnEachTick, Calculate.OnPriceChange)

      Do you have any other code that could be setting the stop-loss to another value unexpectedly?
      There are no errors in the logs. This is a live simulated trade but Strategy Analyzer has shown similar results in the past. The calculate settings is Calculate.OnEachTick and there is no other code that would be causing the stop loss. I have do have a StopLoss set for the long position but the signal name is different
      Code:
      SetStopLoss(@"Long", CalculationMode.Price, Low[0] - TickSize, false);

      Comment


        #4
        There is another trade that it took this morning with similar behavior for a long position. (see attached)
        Attached Files

        Comment


          #5
          Hello pyraxic,

          Thanks for your reply.

          I would suggest debugging your strategy with print statements to help understand what is happening with the stop price.

          In OnOrderUpdate() you can check to see if the order.Name is "Stop loss" and then print the price it is set to.

          Here is an example:

          protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
          {
          if (order.Name == "Stop loss")
          {
          Print ("stop price is: "+order.StopPrice);
          }
          }


          The print statement sends it output to the new>Ninjascript output window.
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_PaulH View Post
            Hello pyraxic,

            Thanks for your reply.

            I would suggest debugging your strategy with print statements to help understand what is happening with the stop price.

            In OnOrderUpdate() you can check to see if the order.Name is "Stop loss" and then print the price it is set to.

            Here is an example:

            protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
            {
            if (order.Name == "Stop loss")
            {
            Print ("stop price is: "+order.StopPrice);
            }
            }


            The print statement sends it output to the new>Ninjascript output window.
            Would this work for Strategy Analyzer as well? I tried using it in Strategy Analyzer but there was no output. Trying it out in sim trading now.

            Comment


              #7
              Hello pyraxic,

              Thanks for your reply.

              Yes, it will work in the Strategy analyzer as well as live.
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Hmm...it's not providing any output in the NinjaScript Output window. Here is my code. Am I missing anything?

                Code:
                protected override void OnBarUpdate()
                {
                if (CurrentBar < BarsRequiredToTrade)
                return;
                
                if (BarsInProgress != 0)
                return;
                
                if (CurrentBars[0] < 1)
                return;
                
                if (IsFirstTickOfBar){
                TradeTaken = false;
                }
                
                // Enter Long
                if ((CrossAbove(MACD1.Default, MACD1.Avg, 1)) && Close[0] > Close[1] && TradeTaken == false)
                {
                EnterLong(Convert.ToInt32(DefaultQuantity), @"Long");
                TradeTaken = true;
                }
                
                SetStopLoss(@"Long", CalculationMode.Price, Low[0] - TickSize, false);
                
                
                // Enter Short
                if ((CrossBelow(MACD1.Default, MACD1.Avg, 1)) && Close[0] < Close[1] && TradeTaken == false)
                {
                EnterShort(Convert.ToInt32(DefaultQuantity), @"Short");
                TradeTaken = true;
                }
                SetStopLoss(@"Short", CalculationMode.Price, High[0] + TickSize, false);
                }
                
                protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
                {
                if (order.Name == "Stop loss")
                {
                Print ("stop price is: " + order.StopPrice);
                }
                }

                Comment


                  #9
                  Hello pyraxic,

                  Thanks for your reply.

                  Everything looks correct.

                  Please recompile, remove strategy from the chart and then reapply the strategy and enable the strategy.

                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    It gives me output like the following which isn't helpful.

                    stop price is: 3577.5
                    stop price is: 3577.5
                    stop price is: 3577.5
                    stop price is: 3577.5
                    stop price is: 3577.25
                    stop price is: 3577.25
                    stop price is: 3577.25
                    stop price is: 3577.25
                    stop price is: 3577
                    stop price is: 3577
                    stop price is: 3577
                    stop price is: 3577
                    stop price is: 3576.75
                    stop price is: 3576.75
                    stop price is: 3576.75
                    stop price is: 3576.75
                    stop price is: 3576.5
                    stop price is: 3576.5
                    stop price is: 3576.5
                    stop price is: 3576.5
                    stop price is: 3576.25
                    stop price is: 3576.25
                    stop price is: 3576.25
                    stop price is: 3576.25
                    stop price is: 3576
                    stop price is: 3576
                    stop price is: 3576
                    stop price is: 3576
                    stop price is: 3575.75
                    stop price is: 3575.75
                    stop price is: 3575.75
                    stop price is: 3575.75
                    stop price is: 3575.5
                    stop price is: 3575.5
                    stop price is: 3575.5
                    stop price is: 3575.5
                    stop price is: 3575.25
                    stop price is: 3575.25
                    stop price is: 3575.25
                    stop price is: 3575.25
                    stop price is: 3575
                    stop price is: 3575
                    stop price is: 3575
                    stop price is: 3575
                    stop price is: 3575
                    stop price is: 3575
                    stop price is: 3575
                    stop price is: 3578.75
                    stop price is: 3578.75
                    stop price is: 3578.75
                    stop price is: 3578.75
                    stop price is: 3578.75
                    stop price is: 3578.75

                    Comment


                      #11
                      Hello pyraxic,

                      Thanks for your reply.

                      What you can do is to create a class level double type variable and store the stop price into it in OnOrderUpdate. Then in the OnBarUpdate() you can add a print statement to print out the time, the stop price, and the close price (current price) (you can comment out the print statement in OnOrderUpdate() as you will now be printing the same thing except in OnbarUpdate()). This then should help you see what is happening on a per-trade basis, to understand where the price is relative to the stop price as well as what the stop price is.

                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        Thank you, Paul. What is the best way to set stoploss one tick below the last closed bar? I am using
                        Code:
                        SetStopLoss(@"Long", CalculationMode.Price, Low[0] - TickSize, false);
                        but that doesn't seem to be working as expected.

                        Comment


                          #13
                          Hello pyraxic,

                          Thanks for your reply.

                          When you are using Calculate.OnEachTick and using live data, the bar references change so that [0] is the currently forming bar.

                          If you want to use the previous bar then you would change it to [1] which would be the last/just closed bar.
                          Paul H.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by hurleydood, 09-12-2019, 10:45 AM
                          14 responses
                          1,096 views
                          0 likes
                          Last Post Board game geek  
                          Started by cre8able, Yesterday, 04:16 PM
                          1 response
                          16 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Started by cre8able, Yesterday, 04:22 PM
                          1 response
                          14 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by stafe, 04-15-2024, 08:34 PM
                          5 responses
                          28 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by StrongLikeBull, Yesterday, 04:05 PM
                          1 response
                          12 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Working...
                          X