Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

MACD Entry Level as StopLoss

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

    MACD Entry Level as StopLoss

    Hi,

    How to code the entry MACD value as StopLoss in strategy?

    Please check attached image.

    Thanks in advance for help.
    Attached Files

    #2
    fercho, you can do a SetStopLoss() to the current price when you send in your entry order.

    Maybe something like this:
    Code:
    if (macd cross above)
    { 
      EnterLong();
      SetStopLoss(CalculationMode.Price, Close[0]);
    }
    The help guide page for SetStopLoss() can be found here - http://www.ninjatrader.com/support/h...etstoploss.htm.
    AustinNinjaTrader Customer Service

    Comment


      #3
      well you use the price input to set the stoploss, I was talking about the Macd input.

      could be something like this?

      SetStopLoss(CalculationMode.Price, MACD(12, 26, 9)[0]);

      Comment


        #4
        right this will work no problem.

        Make sure you set it just before you EnterLong(). Also make sure to set it in OnBarUpdate() so it updates. You cannot set this in Initialize().

        Let me know if I can be of further assistance.
        BrettNinjaTrader Product Management

        Comment


          #5
          Didn´t work... strategy F5 compiles OK, but when backtest, MACD goes lower than stoploss MACD and nothing happens.

          Comment


            #6
            fercho,

            Are you wanting the StopLoss to stay up to date with the MACD value?

            Or would you like to use the current MACD value at entry as the stop loss, but cancel the stop if the MACD falls below this value?
            MatthewNinjaTrader Product Management

            Comment


              #7
              I would like to use the current MACD value at entry as the stop loss,

              Comment


                #8
                Originally posted by fercho View Post
                Didn´t work... strategy F5 compiles OK, but when backtest, MACD goes lower than stoploss MACD and nothing happens.
                Do you mind posting the code?

                Comment


                  #9
                  Originally posted by fercho View Post
                  I would like to use the current MACD value at entry as the stop loss,
                  This value should be true for your stop based of your discussion.

                  As Koganam asked, would you mind sharing your code so we can check for any issues here?
                  MatthewNinjaTrader Product Management

                  Comment


                    #10
                    Well as I described I used

                    SetStopLoss(CalculationMode.Price, MACD(12, 26, 9)[0]);

                    But has no effect, MACD goes lower stoploss and nothing happens...

                    You asked me and I replied that:
                    I would like to use the current MACD value at entry as the stop loss

                    Thanks for quick reply.

                    Comment


                      #11
                      Originally posted by fercho View Post
                      Well as I described I used

                      SetStopLoss(CalculationMode.Price, MACD(12, 26, 9)[0]);

                      But has no effect, MACD goes lower stoploss and nothing happens...

                      You asked me and I replied that:
                      I would like to use the current MACD value at entry as the stop loss

                      Thanks for quick reply.
                      Hello,

                      I think there may have been some confusion from earlier on what you are attempting to do.

                      With the current snippet you provided, this would be placing the the order at the MACD value which would not be a valid price.

                      You would need to create a condition for when the MACD crosses over and then create the stop loss based off the current value, similar to Austin's first reply:

                      Code:
                      if (macd cross above)
                      { 
                        EnterLong();
                      }
                      You would then need to create an exit condition if the MACD crosses below a specific level:

                      Code:
                      if (macd cross below)
                      {
                         ExitLong();
                      }
                      MatthewNinjaTrader Product Management

                      Comment


                        #12
                        again...

                        Please see image attached.

                        1 - Condition for EnterLong: CrossAbove(Macd(12, 26, 9), Macd(12, 26, 9).Avg, 1)

                        2 - Tell the Strategy that "remembers" MACD Value for StopLoss.

                        3 - If MACD goes down through StopLoss setted in "2", ExitLong.
                        Attached Files

                        Comment


                          #13
                          Originally posted by fercho View Post
                          Well as I described I used

                          SetStopLoss(CalculationMode.Price, MACD(12, 26, 9)[0]);

                          But has no effect, MACD goes lower stoploss and nothing happens...

                          You asked me and I replied that:
                          I would like to use the current MACD value at entry as the stop loss

                          Thanks for quick reply.
                          That code is setting the MACD value as the StopLoss value, meaning essentially that you could be setting the StopLoss value to a negative value. In fact, your original picture does show that the code will set the StopLoss to a negative value.

                          You instead want to code a condition on the MACD, which if satisfied allows you to grab the corresponding price data, and use it as a stop loss.

                          Code:
                          if (CrossAbove(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1)) 
                               SetStopLoss("", CalculationMode.Price, Low[0], false);
                          You may or may not want to add a condition for MACD to be negative, etc.

                          As the code snippet I showed was probably your entry condition anyway, it just means that you have to set your StopLoss just before your entry, after the entry conditions are met.
                          Last edited by koganam; 07-26-2011, 11:29 AM.

                          Comment


                            #14
                            [QUOTE=koganam;247696]That code is setting the MACD value as the StopLoss value, meaning essentially that you could be setting the StopLoss value to a negative value. In fact, your original picture does show that the code will set the StopLoss to a negative value.

                            Yes I want to set as StopLoss or as NinjaTrader let me do it. Maybe StopLoss function can´t because it is a negative value, but maybe some code could do the job instead, that's I'm asking for. With my little code experience It will be hard to figure out.

                            Thanks for your understand.

                            Comment


                              #15
                              [QUOTE=fercho;247712]
                              Originally posted by koganam View Post
                              That code is setting the MACD value as the StopLoss value, meaning essentially that you could be setting the StopLoss value to a negative value. In fact, your original picture does show that the code will set the StopLoss to a negative value.

                              Yes I want to set as StopLoss or as NinjaTrader let me do it. Maybe StopLoss function can´t because it is a negative value, but maybe some code could do the job instead, that's I'm asking for. With my little code experience It will be hard to figure out.

                              Thanks for your understand.
                              I have given you the necessary code in the post above. If you are having difficulty integrating it into your entry condition, post the entire code, and we may be able to help.

                              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