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

How to add WMA formula into main code?

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

    How to add WMA formula into main code?

    Hi

    I'm trying to add Weighted Moving average into my code and got abit stuck. I don't know how to proceed. If anyone out there can provide a few tips, it would be greatly appreciated.

    Here is the partial code:

    private void SetPlot(int index,MATYPE matype, int period){
    if(matype == MATYPE.EMA)
    Values[index].Set(Ema(index,period));
    else if (matype == MATYPE.SMA)
    Values[index].Set(Sma(index,period));
    else if (matype == MATYPE.WMA)
    Values[index].Set(Wma(index,period));
    }
    private double Sma(int index, int period){
    double value;

    if (CurrentBar == 0) value = (Input[0]);
    else{
    double last = Values[index][1] * Math.Min(CurrentBar, period);

    if (CurrentBar >= period)
    value = ((last + Input[0] - Input[period]) / Math.Min(CurrentBar, period));
    else
    value = ((last + Input[0]) / (Math.Min(CurrentBar, period) + 1));
    }

    return value;
    }

    private double Ema(int index, int period){
    return CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + period)) + (1 - (2.0 / (1 + period))) * Values[index][1];
    }

    private double Wma(int index, int period){

    // here is where i am stuck..........................

    thanks

    #2
    wxtrox7, you would need to add the WMA calculation logic now in your custom method to have it custom calculated inside your strategy / indicator script and not have it rely on an external call.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      I got this from the NT default WMA code

      private double Wma(int index, int period){
      double value;

      if (CurrentBar == 0) value = (Input[0]);
      else{
      int back = Math.Min(Period - 1, CurrentBar);
      double val = 0;
      int weight = 0;
      for (int idx = back; idx >=0; idx--)
      {
      val += (idx + 1) * Input[back - idx];
      weight += (idx + 1);
      }
      Value.Set(val / weight);

      Since i took the example from another code, not sure how to rewrite this into my code.

      THanks

      Comment


        #4
        You would need to code this as your custom WMA method then in your script, the return value for it would be the (val / weight). Depending on if you want to pass in a custom series, you would need to give the method an IDataSeries input parameter as well to calculate with.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Originally posted by wxtrox7 View Post
          I got this from the NT default WMA code

          private double Wma(int index, int period){
          double value;

          if (CurrentBar == 0) value = (Input[0]);
          else{
          int back = Math.Min(Period - 1, CurrentBar);
          double val = 0;
          int weight = 0;
          for (int idx = back; idx >=0; idx--)
          {
          val += (idx + 1) * Input[back - idx];
          weight += (idx + 1);
          }
          Value.Set(val / weight);

          Since i took the example from another code, not sure how to rewrite this into my code.

          THanks
          Replace
          Code:
          Value.Set(val / weight);
          with
          Code:
          return (val / weight);
          and make sure that you have completed the closure of your brackets.

          Comment


            #6
            pass value to variable help

            Hi,

            I am trying to find correct syntax for this.

            Value.Set(val / weight); // see previous post on WMA

            Code:
            double myWMA0 = Value.Set(val / weight)[0];
            double myWMA1 = Value.Set(val / weight)[1];
            I got an error from these lines. What is the proper syntax for this code to pass the value to variable from Value.Set?

            Thanks,
            -traderjh
            Last edited by traderjh; 10-24-2013, 01:45 PM. Reason: clarification

            Comment


              #7
              What are you trying to access here traderjh? The WMA values could also be directly called by calling the indicator method in your script.

              double myWMA0 = WMA(8)[0];
              double myWMA1 = WMA(8)[1];
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Hi Bertrand,

                Thank you for your reply. I am trying to use the prices to display by DrawTextFixed from WMA Indicator in secondary timeframe and also to pass to condition section.

                I am thinking to incorporate part of WMA logic in indicator into strategy to work with all timeframe prices. My struggle is that the strategy gets all pointed to primary timeframe only. I am trying to mimic 2+ WMA indicators into my strategy's plot lines(StrategyPlot). I will appreciate your suggestions.

                Thanks,
                -traderjh

                Comment


                  #9
                  One more question, Bertrand,

                  In the code you just posted:
                  double myWMA0 = WMA(8)[0];
                  double myWMA1 = WMA(8)[1]; << Is this reference to previous bar or 2nd timeframe?

                  If not 2nd timeframe, what is code for the previous bar?

                  Thanks again.
                  Last edited by traderjh; 10-25-2013, 01:18 PM. Reason: clarification

                  Comment


                    #10
                    Originally posted by traderjh View Post
                    double myWMA1 = WMA(8)[1]; << Is this reference to previous bar or 2nd timeframe?

                    That would be previous bar.

                    2nd time frame would be:

                    Code:
                    double myWMA1 = WMA(BarsArray[1], 8)[x];
                    where x is the barsAgo value.

                    You can also use:

                    Code:
                    double myWMA1 = WMA(Close[b]s[/b], 8)[x];
                    MatthewNinjaTrader Product Management

                    Comment


                      #11
                      Thanks Matthew.

                      I need to make sure that I understand correctly.

                      Does BarsArray[x] consist Close price?

                      Comment


                        #12
                        Yes, it would be the Close price.
                        MatthewNinjaTrader Product Management

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by geddyisodin, Yesterday, 05:20 AM
                        7 responses
                        45 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by gbourque, Today, 06:39 AM
                        2 responses
                        5 views
                        0 likes
                        Last Post gbourque  
                        Started by cre8able, Yesterday, 07:24 PM
                        1 response
                        13 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by cocoescala, 10-12-2018, 11:02 PM
                        6 responses
                        939 views
                        0 likes
                        Last Post Jquiroz1975  
                        Started by cmtjoancolmenero, Yesterday, 03:58 PM
                        1 response
                        18 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Working...
                        X