Profile Picture

creating indicators

Posted By Henrik 12 Years Ago
Rated 5 stars based on 2 votes.
Posted Monday May 14 2007
johnny (5/11/2007)

1. A simple moving average indicator implementation (implementing the whole calculation in itself - running the input array, adding each value to n previous values and diviving each sum by n).

I've re-written our normal SMA to show a single indicator doing all of it's own calculations.  Please do not take this as an example of a the correct way to write this particular indicator.  For 200 periods it's about 200 times slower than the actual implementation BigGrin

Also, check the results of this, I just hacked i out and have not even compiled it.

using System;

using System.Drawing;

using System.Collections.Generic;

using System.ComponentModel;

using RightEdge.Common;


namespace RightEdge.Indicators


  /// <summary>

  /// Simple Moving Average (SMA) indicator.

  /// </summary>



  Name = "SLOW SMA",

  Description = "Mean value, as calculated over a rolling previous period of fixed length.",

  Id = "SLOW-AE5B81CF-9717-49e7-857A-786D67181B5E",

  HelpText = "Slow SMA")]


  [SeriesInputAttribute("Input", 1, Value = BarElement.Close)]

  public class SlowSMA : SeriesCalculatorBaseWithValues


    private int periods = 50;

    private int validCount = 0;


    // Constructs an Simple Moving Average (SMA) indicator instance.

   [ConstructorArgument("Periods", ConstructorArgumentType.Integer, "50", 1)]

    public SlowSMA(int periods) : base(1)


      if (periods <= 0)


        throw new ArgumentException("Periods must be greater than zero");


      this.periods = periods;


    // Calculates a new value for the series.

    protected override double CalcNewValue(int index)


      double returnValue = double.NaN;


      // We don't start calculating until we have enough

      // values in our array to go back by our number of

      // periods.

      if (validCount >= periods)


        double sum = 0.0;

        // We need to iterate back through the last "periods" bars

        // and take an average. We move back one bar per iteration.

        for(int cnt = 0; cnt < periods; cnt++)


          // Build our big sum

          sum = sum + inputs[0][index - cnt];


        // Divide by periods to get the average.

        returnValue = sum / periods;


      return returnValue;



    // Resets the series.

    protected override void Reset()


      validCount = 0;




Posted Tuesday May 15 2007
johnny (5/11/2007)

Can anyone submit a source code of the folowing? Just to let me understand the idea by example (common sample didn't help).

1. A simple moving average indicator implementation (implementing the whole calculation in itself - running the input array, adding each value to n previous values and diviving each sum by n).

2. Indicator which calculates (again all in itself) a moving average, then calculates moving average of that first moving average it has calculated, then calculates a sum of ma1 and ma2, then compares that sum to its pre-pre-vious value and outputs 1 if current value is greater than preprevious, -1 if less, 0 if equal.

3. Indicator that does the same as above n2, but using MA indicator from n1 to calculate MAs.

4. Some example which runs a loop inside other loop. For example Spearman ranks corellation comparing n values (including current and n-1 previous values) with preceding n-value set.

5. FATL indicator (which is a fast low-pass digital signal filter with pre-calculated weights). Here is it's code for AmiBroker. "Input" means current value in input data series, Ref(Input,-1) means its previous value, Ref(Input,-2) - pre-previous, etc.

Great thanks in advance to anyone who can find time for these.

OK, I've written a bunch of examples for you.  I recommend you look at them in the order you requested them, and read the comments in the source code.  They will contain further explanation.

1.  I've included three SMA indicators.  The first one simply calculates the average every bar.  The next one keeps a running total, which speeds up the calculation.  However, see the comment in the source file about numerical precision issues, which notes that the value won't necessarily be *exactly* the same.  The third SMA sample uses our AverageQueue class which does the calculations the same way as the second sample, but it is encapsulated in an easy-to-use class.

2 and 3.  See the SampleAverageAverage1 and SampleAverageAverage2 indicators.  The first one uses the AverageQueue class to calculate the moving averages.  The second one uses the SMASample1 indicator.  Note that because of the numerical precision issues and the different ways the average is being calculated, the SampleAverageAverage1 and SampleAverageAverage2 indicators will not necessarily always have the same value.  This will occur when the difference between the current value and the pre-previous value is very close to zero anyway.  You may want to change the indicator to return zero when the absolute value of the difference is less than some small number such as 0.00001.  This would likely cause these indicators to always return the same values.

4.  I've done two versions of the Spearman Ranks indicator.  The first one calculates the correlation between two user-supplied series.  The second one calculates the correlation between an input series and a monotonically increasing series.  The second one derives from the first one.  It is kind of tricky setting the rank for each value to the average of the ranks for all items that have that value.  I didn't end up using a nested loop for this.  Instead, I kept a dictionary.  Hopefully the code is still helpful.

5.  See the SampleFATL indicator.  This one is pretty simple.

If you need any further explanation, just let us know.  I think these sample indicators will be useful to other people who are interested in writing their own indicators, so I appreciate that you asked for help with them Smile


SMASample1.cs (707 views, 2.00 KB)
SMASample2.cs (625 views, 1.00 KB)
SMASample3.cs (603 views, 1.00 KB)
SampleAverageAverage1.cs (672 views, 2.00 KB)
SampleAverageAverage2.cs (631 views, 3.00 KB)
SpearmanRanksCorrelation.cs (666 views, 3.00 KB)
SpearmanRanksCorrelation2.cs (546 views, 2.00 KB)
SampleFATL.cs (611 views, 2.00 KB)
Posted Tuesday August 07 2007
Are StdDevQueue and AverageQueue documented somewhere?

Can't find them in Developer's Guide.

Seem to be interesting classes, and an even more interesting quetsion are there more of these friends?

Our Trading System at C2: Topaz

Edited: Tuesday August 07 2007 by DrKoch
Posted Tuesday August 07 2007
It looks like they are not documented.  I have added a bug to add documentation for them.

These classes are in the RightEdge.Indicators namespace (and are in Indicators.dll).  The classes available are AverageQueue, WildersSmoothingQueue, RSIQueue, LinearRegressionQueue, and StdDevQueue.


Posted Tuesday August 07 2007
Great. These are powerful building blocks...

The exact formula in StdDevQueue please?

(I get some minor differences if compared to my own stddev calc...)

Our Trading System at C2: Topaz
Posted Tuesday August 07 2007
Is your stddev calculation recalculating from scratch every bar?  Our version works iteratively, which is faster, but can result in slightly different results due to numerical precision limitations.


Posted Wednesday August 08 2007
I made a quick loop for comparison reasons. I am not after numerical precision but the formula:

1/N * sqrt(...)


1/(N-1) * sqrt(...)


Our Trading System at C2: Topaz
Posted Wednesday August 08 2007
The formula we used was sqrt(1/N * SUM((x[i] - AVG(x))^2)).  We then rearranged it to work iteratively.


Posted Thursday August 09 2007
> The classes available are AverageQueue, WildersSmoothingQueue, RSIQueue, LinearRegressionQueue, and StdDevQueue

These queues are great! Its really fun to create some new indicators with them Smile Smile

Just one thing: I miss a property "Count" or "Length"..

What is the formula of "Wilders Smoothing"? (I wonder...)

Our Trading System at C2: Topaz
Posted Thursday August 09 2007
Previous MA + (1 / periods) * (Price - Previous MA)

Similar Topics

Reading This Topic

2005-2018 © RightEdge Systems