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

Using EMA on an indicator

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

    Using EMA on an indicator

    Hello Support.

    I'm trying to develop an indicator with the following charatceristics:
    Must use data from NYSE TICK.
    Must Calculate EMA n period of the High's and Low's, and then use it to perform
    calculations.

    I have an error on calculate avgH and avgL using EMA.

    Thnak you for your help.

    The code is the following (lines in red need help).

    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// Plot a cumulative tick using only high's and Low's above or bellow their EMA's
    /// </summary>
    [Description("")]
    public class CumulativeTick : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int nBars = 20; // Default setting for nBars

    // User defined variables (add any user defined variables below)
    private double tickH = 0;
    private double tickL = 0;
    private double avgH = 0;
    private double avgL = 0;
    private double bull = 0;
    private double bear = 0;
    private double cTick = 0;
    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    This instruction is right to catch the NYSE TICK data?
    Add("^TICK", Bars.Period.Id, Bars.Period.Value);

    Add(new Plot(Color.MediumBlue, PlotStyle.Line, "CTick"));

    CalculateOnBarClose = false;
    DisplayInDataBox = false;
    Overlay = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CurrentBar < 1)
    return;

    tickH = High[0];
    tickL = Low[0];

    Error
    // avgH = EMA(High[0],20);
    // avgL = EMA(Low[0],nBars);


    bull=0;
    if (tickH > avgH)
    bull = tickH - avgH;

    bear=0;
    if (tickL < avgL)
    bear = tickL - avgL;

    if (Bars.FirstBarOfSession)
    CTick.Set(0);
    else
    CTick.Set(CTick[1] + bull + bear);
    }

    #region Properties

    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries CTick
    {
    get { return Values[0]; }
    }

    [Description("Numbers of bars used for calculations")]
    [GridCategory("Parameters")]
    public int BarsPeriod
    {
    get { return nBars; }
    set { nBars = Math.Max(1, value); }
    }
    #endregion

    }
    }

    #2
    Your Add() ing looks correct, but you would need to calculate on the secondary series for the EMA using Highs and Lows



    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertrand.

      Excuseme my english, is very bad.

      Thank you for your quick response.

      Then...

      To calc a 20 periods EMA of Hihgs and Lows, instructions will be:

      avgH = EMA(Highs[0][20],20);
      avgL = EMA(Lows[0][20],20);

      Can you modify instructions if are no ok, please?

      Thank you very much.

      Comment


        #4
        No worries Federico, the updated code from you looks good. You might need to run a filter depending on which BarsInProgress calls the OnBarUpdate() as well :

        BertrandNinjaTrader Customer Service

        Comment


          #5
          The updated code generate the following error message:

          Argument 1: Can not convert from 'double' to 'NinjaTrader.Data.IDataSeries'

          Thanks.

          Comment


            #6
            Sorry, please try this Federico to calculate a 20 bar EMA on this High / Low of the added Tick index series, your second series in the script :

            avgH = EMA(Highs[1], 20)[0];
            avgL = EMA(Lows[1], 20)[0];
            Last edited by NinjaTrader_Bertrand; 08-08-2011, 06:23 AM.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              I get This error:

              Cannot implicitly convert type 'NinjaTrader.indicator.EMA' to 'double'

              Thanks

              Comment


                #8
                Please see my edit below, if you need a double value from the dataseries object the EMA would return please apply indexing to it for the barsago value you would like, so [0] being the most recent one.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  No error message display now.

                  Thank you very much.

                  Comment


                    #10
                    Hello all.

                    Pleas someone can tell me what is bad in this code?

                    This indicator should do the following actions:

                    1.- Take data from the NYSE TICK index.
                    2.- Calculate a EMA of Highs and Lows.
                    3.- Sum only those highs that are above their EMA and lows that are bellow their EMA.
                    4.- Plot a line.

                    Problems:

                    1.- No plot anything.

                    2.- When line Add("^TICK", Bars.Period.Id, Bars.Period.Value); is present and put indicator on a chart an <unknown> parameter is showed like in the image attached.

                    Code:

                    protected override void Initialize()
                    {
                    // I want to take data from NYSE TICK with the same type period and the same value period that chart.
                    Add("^TICK", Bars.Period.Id, Bars.Period.Value);

                    Add(new Plot(Color.MediumBlue, PlotStyle.Line, "CTick"));

                    CalculateOnBarClose = false;
                    DisplayInDataBox = false;
                    Overlay = false;
                    }

                    /// <summary>
                    /// Called on each bar update event (incoming tick)
                    /// </summary>
                    protected override void OnBarUpdate()
                    {
                    if (CurrentBar < 1)
                    return;

                    tickH = High[0];
                    tickL = Low[0];

                    avgH = EMA(Highs[1],Periodos)[0];
                    avgL = EMA(Lows[1],Periodos)[0];

                    bull=0;
                    if (tickH > avgH)
                    bull = tickH - avgH;

                    bear=0;
                    if (tickL < avgL)
                    bear = tickL - avgL;

                    if (Bars.FirstBarOfSession)
                    {
                    CTick.Set(0);
                    }
                    else
                    {
                    CTick.Set(CTick[1] + bull + bear);
                    }
                    }

                    #region Properties

                    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                    public DataSeries CTick
                    {
                    get { return Values[0]; }
                    }

                    [Description("Numbers of bars used for calculations")]
                    [Category("Parameters")]
                    public int Periodos
                    {
                    get { return nBars; }
                    set { nBars = Math.Max(1, value); }
                    }
                    #endregion
                    Attached Files

                    Comment


                      #11
                      Hi Federico,

                      Please open your Output Window. You should be able to see the error message in runtime right as you get into the Indicators dialogue window there.

                      You cannot use Bars.Period.Id or Bars.Period.Value inside Initialize(). You need to know what you want to add and add it with something like PeriodType.Minute and a numeric value.

                      So the line should instead be something like

                      Add("^TICK", PeriodType.Minute, 1);
                      Josh P.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by judysamnt7, 03-13-2023, 09:11 AM
                      4 responses
                      59 views
                      0 likes
                      Last Post DynamicTest  
                      Started by ScottWalsh, Today, 06:52 PM
                      4 responses
                      36 views
                      0 likes
                      Last Post ScottWalsh  
                      Started by olisav57, Today, 07:39 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post olisav57  
                      Started by trilliantrader, Today, 03:01 PM
                      2 responses
                      21 views
                      0 likes
                      Last Post helpwanted  
                      Started by cre8able, Today, 07:24 PM
                      0 responses
                      10 views
                      0 likes
                      Last Post cre8able  
                      Working...
                      X