Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Set Stop Loss

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

    Set Stop Loss

    Hello;
    I need help setting stop loss 4 ticks below the low of the entry bar
    I wrote this code but it doesn't work
    SetStopLoss("Entry1", CalculationMode.Ticks, Low[0]- 4, false)

    Could you please help me finding the error!

    #2
    Hello,

    Thank you for the question.

    Could you provide further details on what specifically is not working? Are you seeing an incorrect price or are you seeing something else?

    I look forward to being of further assistance.

    Comment


      #3
      Hello Jesse;
      Thank you for the reply
      When I apply the strategy and enable it, it disabled at once! The strategy is only about this stop loss!

      Comment


        #4
        Hello,

        Thank you for the additional information.

        Generally when a strategy is disabled immediately, this would relate to errors in the logic.

        Can you open the Tools -> Output window and then try running the script again? If errors are reported in the window, please provide those errors. Additionally if you can upload the script it would likely assist in finding the answer. The script would be located in the folder: Documents\NinjaTrader 7\bin\Custom\Strategy

        I look forward to being of further assistance.

        Comment


          #5
          Hello;
          Thank you for the reply;
          No errors are reported in the window. I uploaded the script and I hope that may help you finding the error.
          Attached Files

          Comment


            #6
            Hello,

            Thank you for providing the script.

            I did run this on a chart and was able to see this working.

            I wanted to verify, what instrument are you running this on?
            Also what data connection are you using, is this in Real time you see this disable or are you in one of the other mods like Simulated data feed or Market Replay?

            I don't see anything that sticks out that would not be working correctly unless there was a lack of data. Also, if you had Added items to Initialize like SetProfitTarget, you may need to remove the strategy from the control center Strategies tab and then re add it again to "refresh" it.


            I look forward to being of further assistance.

            Comment


              #7
              Hello Jesse,
              Thank you for the reply,
              It's been working but It seems very strange because the scripts give the same value for stoploss when I put my stoploss Close or Low
              When the script puts stop loss 12 ticks below the Low of signal bar I get the stop loss 12 ticks below Close not Low of this bar.
              What I mean is that the following codes give the same results in action:
              SetStopLoss("TestStopClose", CalculationMode.Ticks,Close[0]+ 12, false);
              SetStopLoss("TestStopLow", CalculationMode.Ticks,Low[0]+ 12, false);


              I upploaded two images of the results and the tow scripts
              Thanks
              Attached Files

              Comment


                #8
                Hello,

                Thank you for the reply.

                I believe this would be the value you are using for the amount of ticks.

                Code:
                  SetStopLoss("TestStopClose", CalculationMode.Ticks,Close[0]+ 12, false);
                A value of Close[0] + 12 would just be the price plus 12 dollars, for example if the price was 2000, it would just be 2000 + 12 or 2012.

                In Initialize, you would be unable to use the Close price, so instead you can use:

                Code:
                 SetStopLoss("TestStopClose", CalculationMode.Ticks, 12, false);
                which would be 12 ticks.

                If you needed an amount of ticks from the close, that would need to instead be moved from Initialize into OnBarUpdate before your order:

                Code:
                SetStopLoss("TestStopClose", CalculationMode.Price,Close[0] - (12 * TickSize), false);
                EnterLongLimit(DefaultQuantity, Close[0], "TestStopClose");
                I look forward to being of further assistance.
                Last edited by NinjaTrader_Jesse; 11-06-2015, 12:41 PM.

                Comment


                  #9
                  Thanks for your reply;
                  I need to write a script which provides stop loss 12 ticks from the Low, not the Close value but I get a stop 12 ticks below the Close
                  I did write the same code but to get stop loss below the low of the signal bar I wrote it as below:
                  SetStopLoss("TestStopClose", CalculationMode.Price,Low[0] - (12 * TickSize), false);
                  EnterLongLimit(DefaultQuantity, Close[0], "TestStopClose");
                  I got the stop loss value 12 ticks below the Close not the Low of OnBarUpdate before the order: !! Why? I don't know

                  Comment


                    #10
                    Hello,

                    The close price would be used for this by default because that is your primary series.

                    If you want to use a different price, you could just use Price instead of Ticks and calculate whatever price you need.

                    This was included as the second sample in my last post, you would substitute Close with Low if you wanted the low series instead:
                    Code:
                    SetStopLoss("TestStopClose", CalculationMode.Price,Low[0] - (12 * TickSize), false);
                    EnterLongLimit(DefaultQuantity, Close[0], "TestStopClose");
                    Code:
                    Low[0] - (12 * TickSize)
                    This would be the low minus 12 ticks and would be a price value, so CalculationMode.Price is required in this case.

                    Please let me know if I may be of further assistance.

                    Comment


                      #11
                      Hello Jesse
                      That's not working! I've been testing it without any progress
                      If I substitute Close with any other series instead I get no stop loss in action.
                      Neither Low[0] nor Low[0] - (12 * TickSize) gives me any stop loss when I test it on Strategy Analyzer, Run Backtest
                      I think we get a software bug here, I don't know, I've already uploaded the results and get the same stop loss values both with Close, Low and High instead.
                      Could you please test it and upload the results!
                      THANKS

                      Comment


                        #12
                        Hello Albaz,

                        Thanks for your post.

                        Can you clarify if you are creating the strategy in the strategy wizard? Would you might to post your latest code so I can review where you are at with your code?

                        Thank-you.

                        Comment


                          #13
                          Hello Jesse;
                          I'm creating the strategy in strategy wizard and because I can't create the stop loss code that I need through the wizard, then I unlock the code to write the following code
                          SetStopLoss("TestStopClose", CalculationMode.Price,Low[0] - (12 * TickSize), false);, and then save it.
                          I upload tow stop loss codes one with price as calculationmode and the other with ticks and the results I get when I ran backtest on Strategy Analyzer
                          The results was:
                          -No stop loss with price as calculationmode
                          -stop loss at the same time of the buy order with ticks as calculationmode
                          Attached Files

                          Comment


                            #14
                            Hello Albaz,

                            Thanks for your reply and posting your code.

                            The entire issue is that you are trying to tell the stop loss to use a price value in the initialize section of the code and that will not work. You cannot use Low[0] in the initialize section because the bars have not loaded so again it will not work the way you have coded it. The purpose of setting the stoploss in the initialize section is to provide a fixed static stop that is immediately available on your very first order and every order afterwards.

                            Please change the SetStopLoss code that you have to CalculationMode.Ticks and then set it for 12 ticks. SetStopLoss (CalculationMode.Ticks, 12); this will allow you to run meaningful tests.

                            For further understanding on how to use SetStopLoss with dynamic values, please review: http://ninjatrader.com/support/forum...ead.php?t=3222

                            Comment


                              #15
                              Hello Paul;
                              So, there is no way to write a code which generate a stop loss, for example, 6 ticks below the low of the entry bar?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              608 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              355 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              105 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              560 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              561 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X