Advanced - Looping Functions

Since advanced code is basically straight C#, you can do anything you want in there including using the for loops to look back at some candle or indicator values. In general, that's the most efficient way to do things, but may not be the simplest or the most concise method. To aid those who would want to make it simpler, while sacrificing a little bit of efficiency, here are some helper functions we included:
 

Note: in the functions below, Candle Range is one or two ints. One int means check that many candles starting from current candle and back. Two - int start, int end - means check starting at start candles back and ending at end - inclusive.

 

Expression is any combination of variables, numbers, and Indexed Variables. At least one Indexed Variable has to be referenced.

 

 

NOTE: All indexes inside the looping function's expression are RELATIVE, not absolute

 

That is, if you do Count(10, High>Low[0]), it will NOT count all candles out of the last 10 where the candle high is higher than the current candle's low. It will translate to High[i] > Low[i+0], and thus compare the looped candle's high to its own low.

 

If you really do want to compare the looped candle's high to the current candle's low, you would have to do:

 

var CandleLow = Low[0];

Count(10, High>CandleLow);     

 

 

 

All(Candle Range, Boolean Expression)

Returns true if all the candles in the range satisfy the Expression condition, false otherwise

Any(Candle Range, Boolean Expression)

Returns true if any of the candles in the range satisfy the Expression condition, false otherwise

Count(Candle Range, Boolean Expression)

Returns the number of candles in the range that satisfy the Expression condition

Min(Candle Range, Numeric Expression)

Returns the minimum value for the expression for one of the candles in the range

Max(Candle Range, Numeric Expression)

Returns the maximum value for the expression for one of the candles in the range

Sum(Candle Range, Numeric Expression)

Returns the sum of all the numeric values that expression returns for all candles in the range

Average(Candle Range, Numeric Expression)

Returns the average of all the numeric values that expression returns for all candles in the range

Slope(Candle Range, Numeric Expression)

Returns the slope (delta Y / range) for the numeric value

 

Examples:

 

 

  if (All(0, 2, Close > Open))

     SetScanResult("Last 3 candles up");

 

   if (Count(0, 9, Open > Close[1] && Close > Open && Close[1] > Open[1]) >= 2)

     SetScanResult("2 or more up gaps in last 10 candles");

 

   if (Max(3, Close) == Close)
     SetScanResult("Is highest close in last 3 candles");      

 

   if (Count(1, 10, PBarShape == PBShape.Diamong) >= 2)
     SetScanResult("Has more than 2 diamond shapes in 10 candles preceding current");      

 

 

Note: the functions described above are pre-processed by Medved Trader before compiling into C# to convert them to the proper C# syntax delegate-processing functions that the indexed properties class has.

 

For example,

 

Count(0, 9, Open > Close[1] && Close > Open && Close[1] > Open[1]) >= 2);

 

is converted before compiling into:

 

Open.Count(0, 9, (i, x) => {return Open[i] > Close[i+1] && Close[i] > Open[i] && Close[i+1] > Open[i]) >= 2; } );

 

Of course, those indexed properties' class functions can be used directly as well, and the code in the delegate function can be arbitrarily complex.