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

SMA Macd ?

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

    SMA Macd ?

    If I went into Edit Ninja script for the Macd indicator , what ,where and how
    could I change it to use simple moving averages . tia

    #2
    Hello,

    Thank you for your post.

    You will want to go to Tools>Edit NinjaScript>Indicator>select MACD>OK.

    When you see the code right click within the code>Save As...>name it something like MACDSMA (or whatever you want to call it).

    Then the easiest way would be to change this code:

    fastEma.Set((2.0 / (1 + Fast)) * Input[0] + (1 - (2.0 / (1 + Fast))) * fastEma[1]);
    slowEma.Set((
    2.0 / (1 + Slow)) * Input[0] + (1 - (2.0 / (1 + Slow))) * slowEma[1]);

    To this code:
    fastEma.Set(SMA(Input, Fast)[0]);

    slowEma.Set(SMA(Input, Slow)[
    0]);

    Then right click>compile.


    DenNinjaTrader Customer Service

    Comment


      #3
      Thanks Ben .
      Much appreciated . Works great !

      Comment


        #4
        also need sma for the avg

        I also need to change the avg calculation to SMA. I guess the following line need to be replaced also with SMA syntax?

        Code:
        [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]
        [SIZE=2][FONT=Courier New][COLOR=#0000ff]double[/COLOR][/FONT][/SIZE][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] macdAvg = ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]2.0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] / ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] + Smooth)) * macd + ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] - ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]2.0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] / ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] + Smooth))) * Avg[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]];[/SIZE][/FONT]
        [/SIZE][/FONT]
        Thank you in advance.

        Originally posted by NinjaTrader_Ben View Post
        Hello,

        Thank you for your post.

        You will want to go to Tools>Edit NinjaScript>Indicator>select MACD>OK.

        When you see the code right click within the code>Save As...>name it something like MACDSMA (or whatever you want to call it).

        Then the easiest way would be to change this code:

        fastEma.Set((2.0 / (1 + Fast)) * Input[0] + (1 - (2.0 / (1 + Fast))) * fastEma[1]);
        slowEma.Set((2.0 / (1 + Slow)) * Input[0] + (1 - (2.0 / (1 + Slow))) * slowEma[1]);

        To this code:
        fastEma.Set(SMA(Input, Fast)[0]);

        slowEma.Set(SMA(Input, Slow)[0]);

        Then right click>compile.

        Comment


          #5
          Hello,


          No changes need to be made. If you make the changes in post #2, the average will be an average using the SMA. Despite the use of "Ema" in the code, if you make the changes in post #2, all of the calculations are using SMA's not EMA's.
          DenNinjaTrader Customer Service

          Comment


            #6
            How would you plot the SMA of the MACD histogram on the histogram. Can someone please help me with this. I attached a picture to show better what I mean.
            Attached Files

            Comment


              #7
              Hi cbratton, this would need custom coding - you can use something like this for a 10 bar SMA of the normal MACD histogram -
              Code:
              [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] macdHistoSMA = SMA(MACD([/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]12[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2], [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]26[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2], [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]9[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]).Diff, [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]10[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2])[[/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]];[/SIZE]
              [/SIZE]
              BertrandNinjaTrader Customer Service

              Comment


                #8
                I figured it out.

                Thanks much.

                Comment


                  #9
                  Originally posted by ygold View Post
                  I also need to change the avg calculation to SMA. I guess the following line need to be replaced also with SMA syntax?

                  Code:
                  [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]
                  [SIZE=2][FONT=Courier New][COLOR=#0000ff]double[/COLOR][/FONT][/SIZE][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] macdAvg = ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]2.0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] / ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] + Smooth)) * macd + ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] - ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]2.0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] / ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] + Smooth))) * Avg[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]];[/SIZE][/FONT]
                  [/SIZE][/FONT]
                  Thank you in advance.
                  You're actually correct. The existing macdAvg is using a weighted method of creating the average which isn't what you want for the MACD SMA. In order to get a simple moving average, you need to change how macdAvg is calculated AND move up "Value.Set(macd);" so the calculation can use it.

                  The code below is in place of the "else" code block. Notice I commented out the old method of calculating macdAvg and also moved Value.Set(macd); above the calculation.

                  Code:
                  fastEma.Set(SMA(Close, Fast)[0]);
                  slowEma.Set(SMA(Close, Slow)[0]);
                                  
                  double macd    = fastEma[0] - slowEma[0];
                  Value.Set(macd);
                                  
                  //double macdAvg    = (2.0 / (1 + Smooth)) * macd + (1 - (2.0 / (1 + Smooth))) * Avg[1];
                  double macdAvg    = SMA(Value, Smooth)[0];
                                  
                  Avg.Set(macdAvg);
                  Diff.Set(macd - macdAvg);
                  I'm guessing after 3+ years you have it figured out already but I'm hoping that anyone else who comes along will be helped by this post.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by timko, Today, 06:45 AM
                  1 response
                  7 views
                  0 likes
                  Last Post gaz0001
                  by gaz0001
                   
                  Started by Waxavi, 04-19-2024, 02:10 AM
                  3 responses
                  41 views
                  0 likes
                  Last Post gaz0001
                  by gaz0001
                   
                  Started by Max238, Today, 01:28 AM
                  2 responses
                  26 views
                  0 likes
                  Last Post NinjaTrader_ChristopherJ  
                  Started by Shansen, 08-30-2019, 10:18 PM
                  25 responses
                  949 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by JonesJoker, 04-22-2024, 12:23 PM
                  8 responses
                  45 views
                  0 likes
                  Last Post JonesJoker  
                  Working...
                  X