Click or drag to resize

Resampling

If you want to calculate an indicator on a different time frame, you can use resampling methods.  Every indicator has the ResamplingFactor and ResamplingMethod properties as described in the section General Properties Of Indicators. These properties allow programmer to specify a desired points count frame. For example, if you use minute bars and need to calculate an indicator on a 5-minute bar, you must set ResamplingFactor = 5. The indicator selected will then be calculated on the aggregated 5-minute bars.

For bars indicator new bars are aggregated from existing open, high, low and close prices. Open price of the aggregated bar will be the open price of the first bar for aggregation. Close price of the aggregated bar will be the close price of the last bar. High and low prices are the highest and the lowest price of the bars for aggregation respectively.

For other indicators there are two methods specified by the ResamplingMethod property.

Note Note

for bar indicators ResamplingMethod property is ignored.

If you use close prices for calculating an indicator value, you need to choose ResamplingMethod = LastElement. In this case, last price of a given set will be used.

If you use a typical price or a mean price, you need to choose ResamplingMethod = ArithmeticMean. In this case, average price of all bars in a given set will be used.

As an example, consider two ATRs, one with resampling and the other without. In the first case, we will calculate ATR with 5-minute bars, in the second case with 1-minute bars.

C#
 1using FinAnalysis.TA;
 2using FinAnalysis.Base;
 3Atr atr, atrRes;
 4public override void OnInit()
 5{
 6    //..
 7    // Create new instances
 8    atr = new Atr(30);
 9    atrRes = new Atr(30);
10
11    // Set resampling factor for ATR with resampling
12    atrRes.ResamplingFactor = 5;
13    //..
14}
15public override void OnBarClose()
16{
17    //..
18
19    // Add new data point to the indicators
20    atr.Add(Bars.Current.Open, Bars.Current.High, Bars.Current.Low, Bars.Current.Close, Bars.Current.Volume, CurrentTime);
21
22    atrRes.Add(Bars.Current.Open, Bars.Current.High, Bars.Current.Low, Bars.Current.Close, Bars.Current.Volume, CurrentTime);
23
24    // Get indicator values
25    double AtrValue = atr.ATR;
26    double AtrResValue = atrRes.ATR;
27    //..
28}

Displaying the values of indicators on a chart yields the following:

Resampling 001

where Atr calculated on 1-minute bars is shown in green, and the Atr calculated on 5-minute bars is shown in red.

Code Sample

The example of Resampling usage:

C#
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using FinAnalysis.TA;
 6
 7namespace ResamplingSample
 8{
 9    class ResamplingSample
10    {
11        const int Steps = 32;
12        const int ResamplingFactor = 4;
13
14        static void Main(string[] args)
15        {
16            Random r = new Random();
17
18
19            Ema emaArithmeticMean = new Ema(12);
20            emaArithmeticMean.ResamplingFactor = ResamplingFactor;
21
22            emaArithmeticMean.ResamplingMethod = FinAnalysis.Base.ResamplingType.ArithmeticMean;
23            for (int i = 0; i < Steps; ++i)
24            {
25                emaArithmeticMean.Add(r.NextDouble());
26                Console.WriteLine("Resampling = Arithmetic Mean. Value = {0:0.00}", emaArithmeticMean.EMA);
27            }
28
29            Ema emaLastElement = new Ema(12);
30            emaLastElement.ResamplingFactor = ResamplingFactor;
31            emaLastElement.ResamplingMethod = FinAnalysis.Base.ResamplingType.LastElement;
32            for (int i = 0; i < Steps; ++i)
33            {
34                emaLastElement.Add(r.NextDouble());
35                Console.WriteLine("Resampling = Last Element. Value = {0:0.00}", emaLastElement.EMA);
36            }
37
38        }
39    }
40}