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

EMA Separation Indicator

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

    EMA Separation Indicator

    I am trying to code an indicator that will color a bar either white or red when the separation between the 9 period EMA and the 25 period EMA is greater than or equal to 8 pips.

    The code paints the bars but I am finding when I look back at historic price bars it is sometimes not painting the first bar where the conditions exists but the next bar.

    My code is the following.

    protectedoverridevoid Initialize()
    {
    CalculateOnBarClose =
    true;
    Overlay =
    false;
    PriceTypeSupported =
    false;
    }
    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {
    value =
    0.0008;

    if (CurrentBar < 1)
    return;

    if (EMA (9)[1] - EMA (25) [1] < value && EMA (9)[0] - EMA (25) [0] >= value){
    BarColor = Color.White;
    }
    elseif(EMA (25)[1] - EMA (9) [1] < value && EMA (25)[0] - EMA (9) [0] >= value) {
    BarColor = Color.Red;
    }
    }
    #region Properties
    #endregion
    }
    }

    It should check to see the separation between the two EMA lines, that is EMA 9 minus EMA 25 is greater than or equal to 8 pips for a long and EMA 25 - EMA 9 is greater than or equal to 8 pips for a short. If the previous bar the separation was less than 8 pips and on the current bar the separation is greater than or equal to 8 pips then color the bar.

    The attached screenshot shows my problem. I have attached two files with screenshots. As you can see in the first one at the close of the 15:55 bar the 9 period ema is 1.2983 and the 25 period ema is 1.2975 which is equal to 8 pips. The separation on the bar before is less than 8 pips so this bar should be colored white. As you can see it is coloring the next bar white which closed at 16:00 which also has a separation of greater than 8 pips.

    I can't figure out why the code is not coloring the 15:55 bar white since the separation is 8 pips?

    Thanks for your help.

    Will
    Attached Files

    #2
    Hello,


    This issue could be one or both of two things:

    1) It may be due to the negative value that is produced from the subtraction you are using. It makes the EMA difference always lower than the "value". Try using Math.Abs() to get the absolute value, then to do you comparison. Something like this:
    Math.Abs(EMA(9)[1] - EMA(25)[1]) < value

    2) It may be due to live drawing vs. historical drawing. I recommend adding this code below the code you have:
    if (EMA (9)[2] - EMA (25) [2] < value && EMA (9)[1] - EMA (25) [1] >= value){
    BarColor = Color.White;
    }
    else if(EMA (25)[2] - EMA (9) [2] < value && EMA (25)[1] - EMA (9) [1] >= value) {
    BarColor = Color.Red;
    }

    Effectively this will retroactively change any live drawings that should be corrected so they show up correctly in history.
    DenNinjaTrader Customer Service

    Comment


      #3
      Thanks. I posted the questions here again so that others may benefit.

      I tried adding the Math.Abs calculation but it did not fix the problem. My code was changed to the following

      protectedoverridevoid Initialize()
      {
      CalculateOnBarClose =
      true;
      Overlay =
      false;
      PriceTypeSupported =
      false;
      }
      ///<summary>
      /// Called on each bar update event (incoming tick)
      ///</summary>
      protectedoverridevoid OnBarUpdate()
      {
      value =
      0.0008;
      Val1 = Math.Abs(EMA (
      9)[1] - EMA (25) [1]);
      Val2 = Math.Abs(EMA (
      9)[0] - EMA (25) [0]);


      if (CurrentBar < 1)
      return;
      if ( Val1 < value && Val2 >= value){
      BarColor = Color.White;
      }
      }
      #region Properties
      #endregion
      }
      }

      This code does not color any of the bars and fails to show anything on the chart. It looks straight forward to me so I don't understand why.

      If the 9 Period EMA is 1.2000 and the 25 Period EMA is 1.1992 what value does the Math.Abs return for this calculation? Is is 0.0008 regardless of whether the value is positive or negative?

      Thanks

      Will

      Comment


        #4
        Hello,


        Please use Print(Val1); and Print(Val2); to see what values Val1 and Val2 are being evaluated at.
        DenNinjaTrader Customer Service

        Comment


          #5
          Hi Ben. Where would I place that Print code so that it would give the value the is calculated for each bar.

          Second. Where will I find that value on my chart.

          Thanks

          Will

          Comment


            #6
            Hi Will,

            you posted this most recent code.


            protectedoverridevoid OnBarUpdate()
            {
            value = 0.0008;
            Val1 = Math.Abs(EMA (9)[1] - EMA (25) [1]);
            Val2 = Math.Abs(EMA (9)[0] - EMA (25) [0]);


            if (CurrentBar < 1)
            return;


            Place the current bar and return ahead of the line value= ,like in your earlier post.

            Then you should get your plot image back.

            RJay
            RJay
            NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

            Comment


              #7
              Hello,


              I recommend putting it after your if condition before BarColor so you can see what value is being used at the time the BarColor is being used. This won't be once per bar but it will be better for debugging what is being used for the BarColor condition.

              Also, put your if(CurrentBar < 1) return; right after the OnBarUpdate().

              The value will not be printed to your chart, it will print to Tools>Output window.

              Here is how it would look:

              protectedoverridevoid OnBarUpdate()
              {

              if (CurrentBar < 1)
              return;


              value = 0.0008;
              Val1 = Math.Abs(EMA (
              9)[1] - EMA (25) [1]);
              Val2 = Math.Abs(EMA (
              9)[0] - EMA (25) [0]);


              if ( Val1 < value && Val2 >= value){

              Print("Val1: " + Val1);
              Print("Val2: " + Val2);


              BarColor = Color.White;
              }
              }


              DenNinjaTrader Customer Service

              Comment


                #8
                Thanks for everyones help.

                The indicator is coloring the bars white, still not at what I believe is the right place so I would like to see what values are being used in the calculation.

                I am still new to Ninja and don't understand how the output window under the Tools menu relates to my chart of eur/usd where the indicator is being used. If I have the chart open and I want to see the value of Val1 and Val2 at a certain bar, how would I find it in the output window? What if I have several chart windows open, how does the output window know which chart to get the values from?

                Will

                Comment


                  #9
                  Hello,

                  If you want to see what is being used in the calculation prior to the if condition pull the Print()'s out and place them above the condition. I also added Time[0] so you can see when the value was printed:

                  Print(Time[0] +" Val1: " + Val1);
                  Print(Time[0] +" Val2: " + Val2 );


                  if ( Val1 < value && Val2 >= value){


                  BarColor = Color.White;
                  }
                  }


                  The output window just displays whatever you want when you tell it to from the Print() method. Since you have CalculateOnBarClose = false; in the Initialize() method the OnBarUpdate() will be called every time the bar is updated, not once per bar. If you set the CalculateOnBarClose to true it will only be called once per bar. Also the output window will display what you Print() regardless of the number indicators and charts you have going. Any time Print() is called it will print.
                  DenNinjaTrader Customer Service

                  Comment


                    #10
                    This is great thanks.

                    I have copied that last entry from the output window and it provides the information I was looking for and what I thought could be the problem.

                    The calculation of the Val1 and Val2 is showing to many decimal places. While if I look at the indicator values and can see that it is 8 pips, the calculation is looking at the value of 7.6 pips and won't color the right bar.

                    05/11/2008 3:20:00 PM Val1: 0.000548379616120531
                    05/11/2008 3:20:00 PM Val2: 0.000751952425091407

                    To solve this problem do I need to round the value given to Val1 and Val2 to the instrument tick size so that it is from the above example 0.0005 and 0.0008. Then when the comparison is made to my value it should be correct.

                    Would I use the Instrument.MasterInstrument.Round2TickSize(double price)

                    Thanks

                    Comment


                      #11
                      Yup. You could try using that.

                      Instrument.MasterInstrument.Round2TickSize(Val1)
                      Josh P.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by f.saeidi, Today, 12:14 PM
                      2 responses
                      5 views
                      0 likes
                      Last Post f.saeidi  
                      Started by TradeForge, 04-19-2024, 02:09 AM
                      2 responses
                      28 views
                      0 likes
                      Last Post TradeForge  
                      Started by aprilfool, 12-03-2022, 03:01 PM
                      3 responses
                      327 views
                      0 likes
                      Last Post NinjaTrader_Adrian  
                      Started by giulyko00, Today, 12:03 PM
                      1 response
                      5 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Started by AnnBarnes, Today, 12:17 PM
                      1 response
                      2 views
                      0 likes
                      Last Post NinjaTrader_Zachary  
                      Working...
                      X