#### SeriesCalculatorBaseSimple sample (Absolute value example)

Posted By young 11 Years Ago

## SeriesCalculatorBaseSimple sample (Absolute value example)

 Message Posted Tuesday September 04 2007 Hello,I have decided that over the next few days I am going to post some example code of indicators.  There are three different base classes that can be used to write an indicator and I am going to also try to explain when to use each type and why.  First:SeriesCalculatorBaseSimple - Indicators of this sort are useful when you do not need to look backwards into the series.  For example, if you were going to create a indicator that simple doubled the value of every input it would not ever need to look at previous values, just muliply the current value by two.  For this example I have choosen an 'Absolute Value' indicator.   Now the code.`using System;using System.Collections.Generic;using System.Text;using RightEdge.Common; namespace Indicators{  [YYEIndicatorAttribute(System.Drawing.KnownColor.Black,  YYEIndicatorAttribute.EIndicatorGroup.Other,  Name = "Absolute Value",  Description = "Returns the absolute value of the input.",  Id = "uniqueid-0000000",  HelpText = "")]  [Serializable]  [SeriesInputAttribute("Input", 1)]  public class AbsoluteValue : SeriesCalculatorBaseSimple  {    ///     /// Constructs an Absolute Value indicator instance.    ///     public AbsoluteValue()    : base(1)    {    }    ///     /// Returns a series value at the specified index.    ///     /// The index to retrieve series value.    /// A double containing the calculated value.    public override double this[int index]    {      get      {         return Math.Abs(inputs[0][index]);      }    }  }}`Now the runthrough of what everything here does.  [YYEIndicatorAttribute(System.Drawing.KnownColor.Black,  YYEIndicatorAttribute.EIndicatorGroup.Other,  Name = "Absolute Value",  Description = "Returns the absolute value of the input.",  Id = "uniqueid-0000000",  HelpText = "")]This attribute contains a number of parameters that help the UI of rightedge know how to present your indicator.The first paramter is the default drawing color of your indicator. The second is the group, which is used to place your indicator into our treeview of indicators.  Next is the name that is shown in the treeview.  The description is shown on the indicator information page.  Next is a unique id for your indicator.  Suggest you use your company name and then a unique number here.  Internally we use GUIDS for this.  Next is the friendly helptext for the indicator.  Usually good to have a description of what the indicator does, how it does it and when it might be useful.[Serializable]Every indicator needs this attribute.  Don't forget it.[SeriesInputAttribute("Input", 1)]This attribute lets RE know how many inputs are required for this indicator to work.  In our case we accept one since we're just doing an absolute value on the members of a single series.  If we needed two (or more) we would just specify the number here.public class AbsoluteValue : SeriesCalculatorBaseSimpleWe are deriving from SeriesCalulatorBaseSimplere here.    public AbsoluteValue()    : base(1)    {    }Our constructor does not do anything but call the base constructor, passing in the number of series (one, in this case).    public override double this[int index]    {      get      {         return Math.Abs(inputs[0][index]);      }    }Overide the indexer for this class, returning the absolute value of the index of the first series.That's about it for a simple indicator.  Any questions, please ask.  We'd really like to see folks creating indicators and getting up to speed with this. Posted Wednesday September 05 2007 Thanks! Most informative and helpful. Posted Monday November 12 2007 I would like to write an indicator plugin that calculates an exponential moving average of a value.  I would like to do this by using the EMA class that already exists. How would I do this?Thanks Posted Monday November 12 2007 There is a function in IndicatorMath to help with this.  You can calculate EMA that's not tied to BarData.BigBerner (11/12/2007)I would like to write an indicator plugin that calculates an exponential moving average of a value.  I would like to do this by using the EMA class that already exists. How would I do this?Thanks Posted Monday November 12 2007 If you want to actually use one indicator inside another, see the SampleAverageAverage2.cs example attached to this creating indicators post.  Let us know if you have questions.Thanks,Daniel Posted Monday November 12 2007 dplaisted (11/12/2007)If you want to actually use one indicator inside another, see the SampleAverageAverage2.cs example attached to this creating indicators post.  Let us know if you have questions.Thanks,DanielThe SampleAverageAverage2.cs link seems to just take you back to the forum home page. Posted Monday November 12 2007 Try http://www.rightedgesystems.com/forums/Topic128-10-3.aspx.Thanks,Daniel Posted Monday November 12 2007 billb (11/12/2007)There is a function in IndicatorMath to help with this.  You can calculate EMA that's not tied to BarData.BigBerner (11/12/2007)I would like to write an indicator plugin that calculates an exponential moving average of a value.  I would like to do this by using the EMA class that already exists. How would I do this?ThanksCould you explain how to use the IndicatorMath.EMAValue method. I am not sure what "presentvalue" and "previousvalue" refer to. Let's say I wanted to calculate the EMA of today's close - yesterday's close, how would I do this?Thanks. Posted Monday November 12 2007 The EMA is calculated using the previous value's EMA.  The code looks like this:`   double percent = (2.0 / (double)((double)periods + 1.0));   double todaysMA = presentValue * percent;   double yesterdaysMA = previousValue;   return (todaysMA + (yesterdaysMA * (1 - percent)));`If there is no previous EMA, you should start with an SMA.  See the EMA indicator remarks section in the documentation for a good description.If you want to override the existing EMA indicator, I would derive from EMA.  It is based on SeriesCalculatorBaseWithValues so you can override CalcNewValue and Reset.You can also contain an instance of the EMA indicator in your own class and simply manipulate the values returned from that.  If you want to give me more details on what you're trying to accomplish, I can go into it deeper.BigBerner (11/12/2007)billb (11/12/2007)There is a function in IndicatorMath to help with this.  You can calculate EMA that's not tied to BarData.BigBerner (11/12/2007)I would like to write an indicator plugin that calculates an exponential moving average of a value.  I would like to do this by using the EMA class that already exists. How would I do this?ThanksCould you explain how to use the IndicatorMath.EMAValue method. I am not sure what "presentvalue" and "previousvalue" refer to. Let's say I wanted to calculate the EMA of today's close - yesterday's close, how would I do this?Thanks. Posted Monday November 12 2007 BigBerner (11/12/2007)dplaisted (11/12/2007)If you want to actually use one indicator inside another, see the SampleAverageAverage2.cs example attached to this creating indicators post.  Let us know if you have questions.Thanks,DanielThe SampleAverageAverage2.cs link seems to just take you back to the forum home page.You may have tried this before being logged in.  You must be logged in to download attachments.  That should be handled better.