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