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

Help Changing MACD from Ema to Sma

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

    Help Changing MACD from Ema to Sma

    Hi All,

    I posted this awhile back and thought to try again. I'm reaching out to see if anyone has modified a MACD from Exponential to Simple? I would like to be able to switch the Fast, Slow, and Smooth to either Ema or Sma. I tried tweaking the MACD that comes with NT8, but not sure I did anything.

    I turned the slowEma to slowSma, but again not sure that worked correctly. Also, I have no idea where to even start with the Smooth making it either Ema or Sma.

    Thanks in advance.
    -Shaun

    #2
    Hello schaun_oh, and thank you for your question. I am leaving this question on the forums in case a member of the community would like to create this indicator. This said, if you would like to try your hand at creating this indicator yourself, we can compare the EMA's source to the MACD's source. When we do so we see that both contain lines of code like the following :

    Code:
    [FONT=Courier New]// @EMS.cs
    constant1 = 2.0 / (1 + Period);
    constant2 = 1 - (2.0 / (1 + Period));
    // ...
    Value[0] = (CurrentBar == 0 ? Input[0] : Input[0] * constant1 + constant2 * Value[1]);[/FONT]
    Code:
    [FONT=Courier New]// @MACD.cs
    constant1    = 2.0 / (1 + Fast);
    constant2    = (1 - (2.0 / (1 + Fast)));
    // ...
    if (CurrentBar == 0)
    {    
        fastEma[0]        = input0;
    // ...
    }
    else
    {
        double fastEma0    = constant1 * input0 + constant2 * fastEma[1];
    // ...
        fastEma[0]        = fastEma0;
    }[/FONT]
    We can also see that the SMA indicator has this code :

    Code:
    [FONT=Courier New]
                    if (CurrentBar == 0)
                        Value[0] = Input[0];
                    else
                    {
                        double last = Value[1] * Math.Min(CurrentBar, Period);
    
                        if (CurrentBar >= Period)
                            Value[0] = (last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period);
                        else
                            Value[0] = ((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));
                    }[/FONT]
    Therefore, you can probably convert this indicator by

    • open the MACD indicator and SMA indicator in the NS editor
    • in the MACD window, right click, "Save As", and give this a new name
    • in the MACD copy, removing the constants section
    • keep the if (CurrentBar == 0) section as is
    • in the SMA window (changes here will not be saved), add curly braces to the SMA's if (CurrentBar >= Period) / else closes, e.g.

    Code:
    [FONT=Courier New]                    if (CurrentBar >= Period)[/FONT]
    [FONT=Courier New]                    {[/FONT]
    [FONT=Courier New]                        Value[0] = (last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period);[/FONT]
    [FONT=Courier New]                    }[/FONT]
    [FONT=Courier New]                    else[/FONT]
    [FONT=Courier New]                    {[/FONT]
    [FONT=Courier New]                        Value[0] = ((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));[/FONT]
    [FONT=Courier New]                    }[/FONT]

    • replace Value[0] with fastEma[0] and Period with Fast in the same code
    • repeat the sections this leaves you with for slow, value, and avg
    • (optional) rename Ema -> Sma
    • copy your modified code into your new MACD indicator
    • undo your changes to the SMA indicator and close this window
    • test your new MACD indicator
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hi Jessica P,

      Thanks for taking the time out to write up this response. It may be above my pay scale, however I will at least give it a try. I'll let you know if I crash and burn.

      Stay tuned

      Best,
      -Shaun

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by DJ888, 04-16-2024, 06:09 PM
      6 responses
      18 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by Jon17, Today, 04:33 PM
      0 responses
      1 view
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      6 views
      0 likes
      Last Post Javierw.ok  
      Started by timmbbo, Today, 08:59 AM
      2 responses
      10 views
      0 likes
      Last Post bltdavid  
      Started by alifarahani, Today, 09:40 AM
      6 responses
      41 views
      0 likes
      Last Post alifarahani  
      Working...
      X