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

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 kevinenergy, 02-17-2023, 12:42 PM
                      117 responses
                      2,766 views
                      1 like
                      Last Post jculp
                      by jculp
                       
                      Started by Mongo, Today, 11:05 AM
                      5 responses
                      15 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by SightCareAubetter, Today, 12:55 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post SightCareAubetter  
                      Started by traderqz, Today, 12:06 AM
                      8 responses
                      16 views
                      0 likes
                      Last Post traderqz  
                      Started by SightCareAubetter, Today, 12:50 PM
                      0 responses
                      2 views
                      0 likes
                      Last Post SightCareAubetter  
                      Working...
                      X