Skip to content

Functions

Summary

This class contains valuable functions that apply to DataSeries.

Signature

1
public static class Functions

Namespace

cAlgo.API

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 SimpleMovingAverage sma;
 protected override void Initialize()
 {
     sma = Indicators.SimpleMovingAverage(source, period);
 }
 public override void Calculate(int index)
 {
     // IsRising returns true if the current value is greater
     // than the previous value in the data series
     if (Functions.IsRising(sma.Result))
     {
         //Do something
     }
     // IsFalling returns true if the current value is less
     // than the previous value in the data series
     else if(Functions.IsFalling(sma.Result))
     {
         // Do something else
     }
     else // sma is level
     {
         Do something else
     }
 }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 using cAlgo.API;
 using cAlgo.API.Indicators;
 namespace cAlgo
 {
     // This sample shows how to use the data series functions
     [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class DataSeriesFunctionsSample : Indicator
     {
         private SimpleMovingAverage _smaFast, _smaSlow;
         protected override void Initialize()
         {
             _smaFast = Indicators.SimpleMovingAverage(Bars.ClosePrices, 9);
             _smaSlow = Indicators.SimpleMovingAverage(Bars.ClosePrices, 20);
         }
         public override void Calculate(int index)
         {
             if (_smaFast.Result.HasCrossedAbove(_smaSlow.Result, 1))
             {
                 // Fast MA crossed above slow MA
             }
             if (_smaFast.Result.HasCrossedBelow(_smaSlow.Result, 1))
             {
                 // Fast MA crossed below slow MA
             }
             if (_smaFast.Result.Maximum(10) > _smaSlow.Result.Maximum(10))
             {
                 // Fast MA last 10 values maximum is larger than slow MA last 10 values
             }
             if (_smaFast.Result.Minimum(10) < _smaSlow.Result.Minimum(10))
             {
                 // Fast MA last 10 values minimum is smaller than slow MA last 10 values
             }
             if (_smaFast.Result.IsFalling() && _smaSlow.Result.IsRising())
             {
                 // Fast MA is falling and slow MA is raising
                 // IsFalling and IsRising compares last two values of the data series
             }
             if (_smaFast.Result.Sum(10) > _smaSlow.Result.Sum(10))
             {
                 // Fast MA last 10 values sum is larger than slow MA last 109 values sum
             }
         }
     }
 }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
 import clr
 clr.AddReference("cAlgo.API")
 from cAlgo.API import *
 class Test():
     def initialize(self):
         self.sma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 14)
     def calculate(self, index):
         # IsRising returns true if the current value is greater
         # than the previous value in the data series
         if Functions.IsRising(self.sma.Result):
             # Do something
             pass
         # IsFalling returns true if the current value is less
         # than the previous value in the data series
         elif Functions.IsFalling(self.sma.Result):
             # Do something else
             pass
         # sma is level
         else:
             # Do something else
             pass
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
 import clr
 clr.AddReference("cAlgo.API")
 from cAlgo.API import *
 class FunctionsSample():
     def initialize(self):
         self.fastSma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 9)
         self.slowSma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 20)
     def calculate(self, index):
         if Functions.HasCrossedAbove(self.fastSma.Result, self.slowSma.Result, 1):
             api.Print("Fast MA crossed above slow MA")
         if Functions.HasCrossedBelow(self.fastSma.Result, self.slowSma.Result, 1):
             api.Print("Fast MA crossed below slow MA")
         if Functions.Maximum(self.fastSma.Result, 10) > Functions.Maximum(self.slowSma.Result, 10):
             api.Print("Fast MA last 10 values maximum is larger than slow MA last 10 values")
         if Functions.Minimum(self.fastSma.Result, 10) < Functions.Minimum(self.slowSma.Result, 10):
             api.Print("Fast MA last 10 values minimum is smaller than slow MA last 10 values")
         # IsFalling and IsRising compares last two values of the data series
         if Functions.IsFalling(self.fastSma.Result) and Functions.IsRising(self.slowSma.Result):
             api.Print("Fast MA is falling and slow MA is raising")
         if Functions.Sum(self.fastSma.Result, 10) > Functions.Sum(self.slowSma.Result, 10):
             api.Print("Fast MA last 10 values sum is larger than slow MA last 109 values sum")

Methods

IsRising

Summary

Checks if the last value in a data series is greater than the previous.

Signature

1
public static bool IsRising(DataSeries series)

Parameters

Name Type Description
series DataSeries Input data series

Return Value

bool

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 SimpleMovingAverage sma;
 public override void Calculate(int index)
 {
     if (Functions.IsRising(sma.Result))
     {
         //Do something
     }
     //May be invoked as an extension method
     if (sma.Result.IsRising())
     {
         //Do something
     }
 }
1
2
3
4
5
6
 def initialize(self):
     self.sma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 14)
 def calculate(self, index):
     if Functions.IsRising(self.sma.Result):
         # Do something
         pass

IsFalling

Summary

Checks if the last value in a data series is less than the previous

Signature

1
public static bool IsFalling(DataSeries series)

Parameters

Name Type Description
series DataSeries Input data series

Return Value

bool

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 SimpleMovingAverage sma;
 public override void Calculate(int index)
 {
     if (Functions.IsFalling(sma.Result))
     {
         //Do something
     }
     // May also be invoked as an extension method
     if (sma.Result.IsFalling())
     {
         //Do something
     }
 }
1
2
3
4
5
6
 def initialize(self):
     self.sma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 14)
 def calculate(self, index):
     if Functions.IsFalling(self.sma.Result):
         # Do something
         pass

Maximum

Summary

Finds the maximum value in a data series for a given period.

Signature

1
public static double Maximum(DataSeries series, int period)

Parameters

Name Type Description
series DataSeries Input data series
period int Input period

Return Value

double

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 public override void Calculate(int index)
 {
     if(Functions.Maximum(sma.Result,20) > Bars.ClosePrices[index])
     {
         //Do something
     }
     // May be invoked as an extension method
     if (sma.Result.Maximum(20) > Bars.ClosePrices[index])
     {
         //Do something
     }
 }
1
 var maxHigh = Bars.HighPrices.Maximum(periods);
1
2
3
4
5
6
 def initialize(self):
     self.sma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 14)
 def calculate(self, index):
     if Functions.Maximum(self.sma.Result,20) > api.Bars.ClosePrices[index]:
         # Do something
         pass
1
 maxHigh = api.Bars.HighPrices.Maximum(periods)

Minimum

Summary

Finds the minimum of a data series for a given period.

Signature

1
public static double Minimum(DataSeries series, int period)

Parameters

Name Type Description
series DataSeries Input data series
period int Input period

Return Value

double

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 public override void Calculate(int index)
 {
     if(Functions.Minimum(sma.Result, 20) > Bars.ClosePrices[index])
     {
         //Do something
     }
     // May be invoked as an extension method
     if (sma.Result.Minimum(20) > Bars.ClosePrices[index])
     {
         //Do something
     }
 }
1
 var minLow = Bars.LowPrices.Minimum(periods);
1
2
3
4
5
6
 def initialize(self):
     self.sma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 14)
 def calculate(self, index):
     if Functions.Minimum(self.sma.Result,20) > api.Bars.ClosePrices[index]:
         # Do something
         pass
1
 minLow = api.Bars.LowPrices.Minimum(periods)

GetExtremum

Signature

1
private static double GetExtremum(DataSeries series, int period, Func<double, double, bool> extremumCondition)

Parameters

Name Type Description
series DataSeries
period int
extremumCondition Func

Return Value

double

HasCrossedAbove (2)

HasCrossedAbove (1 of 2)

Summary

Returns true, if crossingSeries has crossed above crossedSeries, over the specified Period.

Remarks

HasCrossedAbove will compare the crossing data series to the crossed data series starting from thecurrent value of the series going back the specified period.If period is zero only the current bar values will be compared.If period is one the current bar values will be compared as well as the previous.e.g. Functions.HasCrossedAbove(sma.Result, Bars.ClosePrices, 0)will only compare the current values which are not completed until the close of the bar.It is not uncommon that the function will return true and by the end of the bar the twoseries will uncross.

Signature

1
public static bool HasCrossedAbove(DataSeries crossingSeries, DataSeries crossedSeries, int period)

Parameters

Name Type Description
crossingSeries DataSeries Crossing data series
crossedSeries DataSeries Crossed data series
period int Period for which to check for crossing

Return Value

bool

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 public override void Calculate(int index)
 {
     if(Functions.HasCrossedAbove(sma.Result, Bars.ClosePrices, 0))
     {
         //Do something
     }
     // May be invoked as an extension method as well
     if(sma.Result.HasCrossedAbove(Bars.ClosePrices, 0))
     {
         //Do something
     }
 }
1
2
3
4
5
6
 def initialize(self):
     self.sma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 14)
 def calculate(self, index):
     if Functions.HasCrossedAbove(self.sma.Result, api.Bars.ClosePrices, 0):
         # Do something
         pass

HasCrossedAbove (2 of 2)

Summary

Checks if crossingSeries has crossed above value, sometime within the specified period.

Remarks

HasCrossedAbove will compare the crossing data series to the crossed data series starting from thecurrent value of the series going back the specified period.If period is zero only the current bar values will be compared.If period is one the current bar values will be compared as well as the previous.e.g. Functions.HasCrossedAbove(sma.Result, value, 0)will only compare the current simple moving average series valuewhich is not completed until the close of the bar.It is not uncommon that the function will return true and by the end of the bar the series will uncross.

Signature

1
public static bool HasCrossedAbove(DataSeries crossingSeries, double value, int period)

Parameters

Name Type Description
crossingSeries DataSeries Crossing data series
value double Price value to check if crossed
period int Period for which to check for crossing

Return Value

bool

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 public override void Calculate(int index)
 {
     var value = Bars.ClosePrices[index - 1];
     if(Functions.HasCrossedAbove(sma.Result, value, 1)
     {
         //Do something
     }
     // May be invoked as an extension method as well
     if(sma.Result.HasCrossedAbove(Bars.ClosePrices[index-1], 1))
     {
         //Do something
     }
 }
1
2
3
4
5
6
7
 def initialize(self):
     self.sma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 14)
 def calculate(self, index):
     value = api.Bars.ClosePrices[index - 1]
     if Functions.HasCrossedAbove(self.sma.Result, value, 1):
         # Do something
         pass

HasCrossedBelow (2)

HasCrossedBelow (1 of 2)

Summary

Returns true, if crossingSeries has crossed below crossedSeries, over the specified Period.

Remarks

HasCrossedBelow will compare the crossing data series to the crossed data series starting from thecurrent value of the series going back the specified period.If period is zero only the current bar values will be compared.If period is one the current bar values will be compared as well as the previous.e.g. Functions.HasCrossedBelow(sma.Result, Bars.ClosePrices, 0)will only compare the current values which are not completed until the close of the bar.It is not uncommon that the function will return true and by the end of the bar the twoseries will uncross.

Signature

1
public static bool HasCrossedBelow(DataSeries crossingSeries, DataSeries crossedSeries, int period)

Parameters

Name Type Description
crossingSeries DataSeries Crossing data series
crossedSeries DataSeries Crossed data series
period int Period for which to check for crossing

Return Value

bool

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 public override void Calculate(int index)
 {
     if(Functions.HasCrossedBelow(sma.Result, Bars.ClosePrices,0)
     {
         //Do something
     }
     // May be invoked as an extension method
     if(sma.Result.HasCrossedBelow(Bars.ClosePrices, 0))
     {
         //Do something
     }
 }
1
2
3
4
5
6
 def initialize(self):
     self.sma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 14)
 def calculate(self, index):
     if Functions.HasCrossedBelow(self.sma.Result, api.Bars.ClosePrices, 0):
         # Do something
         pass

HasCrossedBelow (2 of 2)

Summary

Checks if crossingSeries has crossed below the value, sometime within the specified period.

Remarks

HasCrossedBelow compares the crossing data series to the value starting from thecurrent value of the series going back the specified period.If period is zero, only the current bar value will be examined.If period is one, the current and previous bar value will be examined.e.g. Functions.HasCrossedAbove(sma.Result, value, 0)will only compare the current simple moving average series valuewhich is not completed until the close of the bar.It is not uncommon that the function will return true and by the end of the bar the series will uncross.

Signature

1
public static bool HasCrossedBelow(DataSeries crossingSeries, double value, int period)

Parameters

Name Type Description
crossingSeries DataSeries Crossing data series
value double Price value to check if crossed
period int Period for which to check for crossing

Return Value

bool

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 public override void Calculate(int index)
 {
     if(Functions.HasCrossedBelow(sma.Result, Bars.ClosePrices[index], 0)
     {
         //Do something
     }
     // May be invoked as an extension method as well
     if(sma.Result.HasCrossedBelow(Bars.ClosePrices[index], 0))
     {
         //Do something
     }
 }
1
2
3
4
5
6
 def initialize(self):
     self.sma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 14)
 def calculate(self, index):
     if Functions.HasCrossedBelow(self.sma.Result, api.Bars.ClosePrices[index], 0):
         # Do something
         pass

HasCrossed (2)

HasCrossed (1 of 2)

Summary

Returns true, if crossingSeries has crossed above crossedSeries, over the specified Period.

Remarks

HasCrossedAbove will compare the crossing data series to the crossed data series starting from thecurrent value of the series going back the specified period.If period is zero only the current bar values will be compared.If period is one the current bar values will be compared as well as the previous.e.g. Functions.HasCrossedAbove(sma.Result, Bars.ClosePrices, 0)will only compare the current values which are not completed until the close of the bar.It is not uncommon that the function will return true and by the end of the bar the twoseries will uncross.

Signature

1
private static bool HasCrossed(DataSeries crossingSeries, DataSeries crossedSeries, int period, Func<double, double, bool> preCrossCondition, Func<double, double, bool> postCrossCondition)

Parameters

Name Type Description
crossingSeries DataSeries Crossing data series
crossedSeries DataSeries Crossed data series
period int Period for which to check for crossing
preCrossCondition Func
postCrossCondition Func

Return Value

bool

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 public override void Calculate(int index)
 {
     if(Functions.HasCrossedAbove(sma.Result, Bars.ClosePrices, 0))
     {
         //Do something
     }
     // May be invoked as an extension method as well
     if(sma.Result.HasCrossedAbove(Bars.ClosePrices, 0))
     {
         //Do something
     }
 }
1
2
3
4
5
6
 def initialize(self):
     self.sma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 14)
 def calculate(self, index):
     if Functions.HasCrossedAbove(self.sma.Result, api.Bars.ClosePrices, 0):
         # Do something
         pass

HasCrossed (2 of 2)

Summary

Returns true, if crossingSeries has crossed below crossedSeries, over the specified Period.

Remarks

HasCrossedBelow will compare the crossing data series to the crossed data series starting from thecurrent value of the series going back the specified period.If period is zero only the current bar values will be compared.If period is one the current bar values will be compared as well as the previous.e.g. Functions.HasCrossedBelow(sma.Result, Bars.ClosePrices, 0)will only compare the current values which are not completed until the close of the bar.It is not uncommon that the function will return true and by the end of the bar the twoseries will uncross.

Signature

1
private static bool HasCrossed(DataSeries crossingSeries, double value, int period, Func<double, double, bool> preCrossCondition, Func<double, double, bool> postCrossCondition)

Parameters

Name Type Description
crossingSeries DataSeries Crossing data series
value double
period int Period for which to check for crossing
preCrossCondition Func
postCrossCondition Func

Return Value

bool

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 public override void Calculate(int index)
 {
     if(Functions.HasCrossedBelow(sma.Result, Bars.ClosePrices,0)
     {
         //Do something
     }
     // May be invoked as an extension method
     if(sma.Result.HasCrossedBelow(Bars.ClosePrices, 0))
     {
         //Do something
     }
 }
1
2
3
4
5
6
 def initialize(self):
     self.sma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 14)
 def calculate(self, index):
     if Functions.HasCrossedBelow(self.sma.Result, api.Bars.ClosePrices, 0):
         # Do something
         pass

Sum

Summary

Calculates the sum of a data series, over the specified period.

Signature

1
public static double Sum(DataSeries series, int period)

Parameters

Name Type Description
series DataSeries DataSeries of which values are summed
period int Period of values that are summed prior to current index

Return Value

double

Examples

1
2
3
4
5
6
 SimpleMovingAverage sma;
 public override void Calculate(int index)
 {
     // The sum of the simple moving average series of the last 20 bars
     var sumSma = Functions.Sum(sma.Result, 20);
 }
1
2
3
4
5
 def initialize(self):
     self.sma = api.Indicators.SimpleMovingAverage(api.Bars.ClosePrices, 14)
 def calculate(self, index):
     # The sum of the simple moving average series of the last 20 bars
     sumSma = Functions.Sum(self.sma.Result, 20)