Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How would I code SMA as a stoploss using

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

    How would I code SMA as a stoploss using

    SetStopLoss()?

    #2
    Hello Kaywai,

    You could plug in the value of the SMA for the price.


    Code:
    SetStopLoss(CalculationMode.Price, SMA(14)[0]);

    http://www.ninjatrader-support.com/H...tStopLoss.html
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      No its wrong for stoploss.
      In Rayn's formula the stoploss can go up and down!

      Baruch

      Comment


        #4
        Originally posted by Baruch View Post
        No its wrong for stoploss.
        In Rayn's formula the stoploss can go up and down!

        Baruch
        You have to use addition variable for that.
        When you enter the trade:
        stopprice = SMA(period)[0];
        SetStopLoss(CalculationMode.Price, stopprice);

        While you are in the trade:

        stopprice = Math.Max(SMA(period)[0], stopprice);
        //for long position; for short position use Math.Min
        SetStopLoss(CalculationMode.Price, stopprice);

        Comment


          #5
          You have to use addition variable for that.
          When you enter the trade:
          stopprice = SMA(period)[0];
          SetStopLoss(CalculationMode.Price, stopprice);
          This is also not correct. For example when you go long and the SMA is above the price!!
          But its easy do it correctly.

          Baruch

          Comment


            #6
            kaywai,

            The answer to your question depends on exactly what you want to do. Do you want a trailing stop, hard stop that doesn't move? If you're taking a long position, is the SMA below the entry price?

            Give more details and you will get a more precise answer.
            mrlogik
            NinjaTrader Ecosystem Vendor - Purelogik Trading

            Comment


              #7
              Originally posted by Baruch View Post
              This is also not correct. For example when you go long and the SMA is above the price!!
              But its easy do it correctly.

              Baruch

              Off course you have to check a lot of things. The strategy usually takes more than 2 lines of code.

              Comment


                #8
                Guys, Thanks for all the answers. It's kinda like a trailing stop. And since I am using SetStopLoss as my method, I was just thinking how I could tweak it automatically. The thought that first came to mind was SMA or more specifically TD Moving Average I. I just didn't know how to write the code for it.

                Comment


                  #9
                  Kay,

                  As long as you ensure the MA price is below your entry price for long when you first call SetStopLoss(), and above for short you can use Ryan's first response for your code.
                  mrlogik
                  NinjaTrader Ecosystem Vendor - Purelogik Trading

                  Comment


                    #10
                    Will do. Thanks mrlogik!

                    Comment


                      #11
                      No problem. Post any coding issues you have and I'll be happy to help.
                      mrlogik
                      NinjaTrader Ecosystem Vendor - Purelogik Trading

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      648 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      369 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      108 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      572 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      573 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X