Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetStopLoss question

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

    SetStopLoss question

    Hi,
    I am trying to set the stoploss using Low[0] at the time of entry signal. Therefore I created a variable LowStopLoss as a Double and set LowStopLoss = Low[0] when the conditions are met.

    The stop definition is >
    SetStopLoss("", CalculationMode.Price, LowStopLoss, false);

    It does not work, but when I put number (as an integer) it works fine.

    Thanks for help

    Petr

    #2
    Hello Petr,
    Thanks for your post.

    Are there any errors on the Logs tab of your Control Center when it isn't working?

    Where is SetStopLoss() inside your strategy? Can you provide a larger sample?
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Yes, there is "Unhandled exception: There was an error generating the XML document." I just did few tests and it really seems to get down to dot vs. comma problem in numbers. I have a new laptop with Win10 using NT8 now and I don't recall what I did on previous computer to avoid it :-(.

      Comment


        #4
        see the larger sample:



        LowStopLoss = 0;
        }
        else if (State == State.Configure)
        {
        }
        else if (State == State.DataLoaded)
        {
        EMA1 = EMA(Close, 34);
        ROC1 = ROC(EMA1, 34);
        CCI1 = CCI(Close, 20);
        EMA2 = EMA(Close, 170);
        ROC2 = ROC(EMA2, 170);
        CCI2 = CCI(Close, 50);
        EMA1.Plots[0].Brush = Brushes.Green;
        CCI1.Plots[0].Brush = Brushes.LightSkyBlue;
        EMA2.Plots[0].Brush = Brushes.Red;
        CCI2.Plots[0].Brush = Brushes.Goldenrod;
        AddChartIndicator(EMA1);
        AddChartIndicator(CCI1);
        AddChartIndicator(EMA2);
        AddChartIndicator(CCI2);
        SetStopLoss("", CalculationMode.Price, LowStopLoss, false);
        }
        }

        protected override void OnBarUpdate()
        {
        if (BarsInProgress != 0)
        return;

        if (CurrentBars[0] < 4)
        return;

        // Set 1
        if (Position.MarketPosition == MarketPosition.Flat)
        {
        LowStopLoss = 0;
        }

        // Set 2
        if ((ROC1[0] > 2)
        && (CrossAbove(CCI1, 0, 1))
        && (ROC2[0] > 2)
        && (CCI2[0] > CCI2[1])
        && (CCI2[1] < CCI2[4])
        && (CCI2[1] >= -100)
        && (CCI2[0] <= 100)
        && (Position.MarketPosition == MarketPosition.Flat))
        {
        EnterLong(Convert.ToInt32(DefaultQuantity), "");
        LowStopLoss = Low[0];
        Print(Times[0][0].ToString() + ":" + LowStopLoss.ToString());

        Comment


          #5
          Petr,

          I suspect that these errors are not related. Is there any way you could provide more of your code?

          You can also write in to PlatformSupport(AT)NinjaTrader(DOT)com and we can work on this together through email.
          Josh G.NinjaTrader Customer Service

          Comment


            #6
            OK, I will do it the suggested way.
            Petr

            Comment


              #7
              Petr,

              Thanks for sending in your code.

              It looks like you are trying to dynamically set your stop loss and that will not be possible. I will add a vote on your behalf to the active feature request to implement this functionality. The tracking number for that request is SFT-882.

              In the mean time, you will need to unlock the code and use SetStopLoss() inside OnBarUpdate() to set your stop loss to LowStopLoss.

              Please let me know if you have any questions.
              Josh G.NinjaTrader Customer Service

              Comment


                #8
                Actually I am not trying to change it on every bar update. Only when I get the order signal. I though this is how I can use SetStopLoss() in the Builder.

                Any idea why I get the error message? See my full code file in the email I sent you.

                Thanks

                Petr

                Comment


                  #9
                  Petr,

                  Your stop loss is not going to be what you expect it to be based on the code you sent me. You need to set that inside OnBarUpdate since LowStopLoss is not static. This is noted in our help guide here: https://ninjatrader.com/support/help...etstoploss.htm

                  I will email you in regards to the error message. I do not believe these two things are related.
                  Josh G.NinjaTrader Customer Service

                  Comment


                    #10
                    Josh,
                    I though that only SetTrailStop() method must be used inside OnBarUpdate. Can you give an example when and how I would use SetStopLoss() in Builder instead of SetTrailStop(). Would it be something like SetStopLoss("", CalculationMode.Price, Low[1], false);?

                    I will send you the requested files via email later today.

                    Thank you

                    Petr

                    Comment


                      #11
                      Domek_69,

                      There will be no way to put SetStopLoss() inside OnBarUpdate() with the Strategy Builder.

                      Your snippet:

                      Code:
                      SetStopLoss("", CalculationMode.Price, Low[1], false);
                      is valid and could work if you unlocked your code.

                      Josh G.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Yesterday, 05:17 AM
                      0 responses
                      54 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      130 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      72 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      44 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      49 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X