General Properties Of Indicators |
To extend standard features, every indicator has some common properties.
Ready – it is a read only property. You can use it to find out whether the indicator receives adequate values for its calculation. If it is false, the indicator returns a NaN.
Stable - for example, if we assigned 10 for period of Sma indicator, and we received less than 10 values yet, the indicator value will be calculated anyway, but it will be an average of less than 10 values. So the Ready property will be true, but Stable property will be false in this case, as the value of the indicator does not actually comply with parameters we have assigned in the indicator constructor.
In the example below the IndicatorValue variable is assigned only in case of Stable property is true:
1using FinAnalysis.TA; 2using FinAnalysis.Base; 3 4Sma sma = new Sma(14); 5 6public override void OnBarClose() 7{ 8 //... 9 sma.Add(CurrentPrice); 10 if (sma.Stable) 11 { 12 double IndicatorValue = sma.SMA; 13 } 14 //... 15}
HistoryCapacity and HistoryCount - you can get previous values of the indicator using number of previous calculation in square brackets. In the example below the indicator Sma returns double precision floating-point number and you can simply get the necessary previous value by accessing the element with the corresponding index:
1using FinAnalysis.TA; 2using FinAnalysis.Base; 3 4Sma sma = new Sma(); 5 6public override void OnInit() 7{ 8 //... 9 sma.HistoryCapacity = 5; 10 //... 11} 12public override void OnBarClose() 13{ 14 //... 15 sma.Add(CurrentPrice); 16 if (sma.HistoryCount > 3) 17 { 18 double IndicatorValue = sma[3]; 19 } 20 //... 21}
If specified index is greater or equal to HistoryCount, the returned indicator value will be NaN.
HistoryTimePeriod property.
1using FinAnalysis.TA; 2using FinAnalysis.Base; 3 4Sma sma = new Sma(); 5 6public override void OnInit() 7{ 8 //... 9 sma.HistoryTimePeriod = new TimeSpan(1,0,0); 10 //... 11} 12 13public override void OnBarClose() 14{ 15 //... 16 sma.Add(CurrentPrice, CurrentTime); 17 // Get the SMA value for 10 minutes ago 18 double IndicatorValue = sma[CurrentTime.AddMinutes(-10)]; 19 //... 20}
You can access the stored values by specifying the time moment of the indicator calculation in square brackets. Note that if the specified time is outside of the time window, method returns the default value – NaN.
1using FinAnalysis.TA; 2using FinAnalysis.Base; 3 4Sma sma = new Sma(); 5 6public override void OnBarClose() 7{ 8 //... 9 // Add new data point to the indicator 10 sma.Add(CurrentPrice, CurrentTime); 11 if (sma.Ready) 12 { 13 // Values IndValue1 == IndValue2 14 double IndValue1 = sma.SMA; 15 double IndValue2 = sma.Value; 16 } 17 //... 18}
If the indicator returns more than one value, Value property will contain a set of indicator values:
1using FinAnalysis.TA; 2using FinAnalysis.Base; 3 4Bollinger bb = new Bollinger(50, 2); 5 6public override void OnBarClose() 7{ 8 //... 9 // Add new data point to the indicator 10 bands.Add(CurrentPrice); 11 if (bands.Ready) 12 { 13 // Indicator values 14 double IndMidBand = bands.MiddleBand; 15 double IndUppBand = bands.UpperBand; 16 double IndLowBand = bands.LowerBand; 17 18 // or 19 // Indicator values got through Value property 20 double IndValueMidBand = bands.Value.MiddleBand; 21 double IndValueUppBand = bands.Value.UpperBand; 22 double IndValueLowBand = bands.Value.LowerBand; 23 } 24 //... 25}
First – this read only property returns oldest values of indicator.
Last – this read only property returns the set of latest values of indicator.
where:
LastElement – uses last element for calculation of the set defined by ResamplingFactor
ArithmeticMean – uses arithmetic mean for calculation of the set defined by ResamplingFactor
Here is the example of HistoryCapacity and HistoryTimePeriod usage:
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using FinAnalysis.TA; 6 7namespace HistorySample 8{ 9 class HistorySample 10 { 11 public const int Steps = 32; 12 public const int HistoryCapacity = 12; 13 public static TimeSpan HistoryTime = TimeSpan.FromMilliseconds(0xF); 14 15 static void Main(string[] args) 16 { 17 Random r = new Random(); 18 19 Ema emaHistoryPoint = new Ema(0xF); 20 emaHistoryPoint.HistoryCapacity = HistoryCapacity; 21 22 for (int i = 0; i < Steps; ++i) 23 { 24 emaHistoryPoint.Add(r.NextDouble()); 25 for (int h = 0; h < HistoryCapacity; ++h) 26 { 27 double historyValue = emaHistoryPoint[h]; 28 Console.WriteLine("History value at point {0} after {1} steps = {2:0.00}", h, i, historyValue); 29 } 30 } 31 32 33 Ema emaHistoryPeriod = new Ema(0xF); 34 emaHistoryPeriod.HistoryTimePeriod = HistoryTime; 35 36 for (int i = 0; i < Steps; ++i) 37 { 38 emaHistoryPeriod.Add(r.NextDouble(), DateTime.Now); 39 40 for (int h = 0; h < HistoryTime.Milliseconds; ++h) 41 { 42 double historyValue = emaHistoryPeriod[DateTime.Now - TimeSpan.FromMilliseconds(h)]; 43 Console.WriteLine("History value at time {0} millisec after {1} steps = {2:0.00}", h, i, historyValue); 44 45 } 46 } 47 } 48 } 49}