Click or drag to resize

Count

Count calculates number of times the signal was generated during the given period. Commonly it is used to calculate the number of predicate signals in a specified period.

Chart Example

Count 001

Implementation and Usage

To implement counting signals in your strategy you can use of the following constructors:

Count- creates Count indicator with default point period equal to 7.

Count(Int32)- creates Count indicator with point window and allows to set window length.

Count(TimeSpan) - creates Count indicator with time window and allows to set time window length.

The value of the indicator is updated by simple prototype of method Add():

Add(Double)

Add(Double, DateTime)

Use Result property to get the current value.

Example

This example demonstrates counting how many times the Sma line crosses the price chart of Apple Inc. stock price (bar close price) from down to up during the period of 40 points:

C#
 1public partial class InstrumentExecutor : InstrumentExecutorBase
 2{
 3-    #region LocalVariables
 4 
 5     //Sma indicator
 6     Sma sma;
 7     //IsCrossUp indicator
 8     IsCrossUp isCrossUp;
 9     //Count indicator
10     Count count;
11     //chart lines
12     Line smaLine, isCrossUpLine, countLine;
13 
14     #endregion
15
16-    #region Events
17     public override void OnInit()
18     {
19         //init Sma indicator
20         sma = new Sma(10);
21         //init Is Cross Up indicator
22         isCrossUp = new IsCrossUp();
23         //init Count indicator
24         count = new Count(40);
25         //init lines
26         smaLine = Charting.CreateLine("Sma", "", Symbol, Pens.Blue, LineStyle.Line, Charting.PriceChart, 1);
27 
28         isCrossUpLine = Charting.CreateLine("IsCrossUp", "", Symbol, Pens.Green, LineStyle.Line, Charting.PriceChart, 2);
29 
30         countLine = Charting.CreateLine("Count", "", Symbol, Pens.Black, LineStyle.Line, Charting.PriceChart, 3);
31     }
32     public override void OnBarClose()
33     {
34         //update indicators values
35         sma.Add(CurrentPrice, CurrentTime);
36         isCrossUp.Add(sma.SMA, CurrentPrice);
37         count.Add(isCrossUp.CrossUp, CurrentTime);
38         //chart
39         smaLine.Draw(CurrentTime, sma.SMA);
40         isCrossUpLine.Draw(CurrentTime, isCrossUp.CrossUp);
41         countLine.Draw(CurrentTime, count.Result);
42     }
43     #endregion
44}