How to create pivot point and fractal indicators VIDEO
Many indicators and strategies rely on chart objects to present essential information, and cTrader Algo provides the API methods required to draw such objects. Pivot points and fractals, in particular, are drawn on trading charts to help identify critical price levels and turning points.
In this article and the corresponding video, you will learn how to draw pivot points and fractals on charts.
Create a pivot point indicator Pivot points are price levels calculated from previous prices. Such points indicate potential areas of support or resistance.
We will create an indicator that draws daily pivot points on a chart.
In cTrader Algo, navigate to the Indicators tab and click New . Type in a name for the new indicator and then click the Create button.
Modify the indicator in the code editor to make it an overlay indicator.
[Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
Define and get the daily bars in the Initialize()
method.
Bars _bars ;
protected override void Initialize ()
{
_bars = MarketData . GetBars ( TimeFrame . Daily , SymbolName );
}
Calculate the different support and resistance levels known as pivot points. Seven values comprising the pivot point, three resistance levels and three support levels are calculated.
var pivot = ( _bars . HighPrices . Last ( 1 ) + _bars . LowPrices . Last ( 1 ) + _bars . ClosePrices . Last ( 1 )) / 3 ;
var r1 = 2 * pivot - _bars . LowPrices . Last ( 1 );
var s1 = 2 * pivot - _bars . HighPrices . Last ( 1 );
var r2 = pivot + _bars . HighPrices . Last ( 1 ) - _bars . LowPrices . Last ( 1 );
var s2 = pivot - _bars . HighPrices . Last ( 1 ) + _bars . LowPrices . Last ( 1 );
var r3 = _bars . HighPrices . Last ( 1 ) + 2 * ( pivot - _bars . LowPrices . Last ( 1 ));
var s3 = _bars . LowPrices . Last ( 1 ) - 2 * ( _bars . HighPrices . Last ( 1 ) - pivot );
Since the pivot points have been calculated, they can be drawn on the chart using trend lines.
Chart . DrawTrendLine ( "pivot " , _bars . OpenTimes . LastValue , pivot , _bars . OpenTimes . LastValue . AddDays ( 1 ), pivot , Color . White );
Chart . DrawTrendLine ( "r1 " , _bars . OpenTimes . LastValue , r1 , _bars . OpenTimes . LastValue . AddDays ( 1 ), r1 , Color . Green );
Chart . DrawTrendLine ( "r2 " , _bars . OpenTimes . LastValue , r2 , _bars . OpenTimes . LastValue . AddDays ( 1 ), r2 , Color . Green );
Chart . DrawTrendLine ( "r3 " , _bars . OpenTimes . LastValue , r3 , _bars . OpenTimes . LastValue . AddDays ( 1 ), r3 , Color . Green );
Chart . DrawTrendLine ( "s1 " , _bars . OpenTimes . LastValue , s1 , _bars . OpenTimes . LastValue . AddDays ( 1 ), s1 , Color . Red );
Chart . DrawTrendLine ( "s2 " , _bars . OpenTimes . LastValue , s2 , _bars . OpenTimes . LastValue . AddDays ( 1 ), s2 , Color . Red );
Chart . DrawTrendLine ( "s3 " , _bars . OpenTimes . LastValue , s3 , _bars . OpenTimes . LastValue . AddDays ( 1 ), s3 , Color . Red );
You can copy the full code below:
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 using System ;
using cAlgo.API ;
using cAlgo.API.Collections ;
using cAlgo.API.Indicators ;
using cAlgo.API.Internals ;
namespace cAlgo
{
[Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
public class PivotPointsExample : Indicator
{
Bars _bars ;
protected override void Initialize ()
{
_bars = MarketData . GetBars ( TimeFrame . Daily , SymbolName );
}
public override void Calculate ( int index )
{
var pivot = ( _bars . HighPrices . Last ( 1 ) + _bars . LowPrices . Last ( 1 ) + _bars . ClosePrices . Last ( 1 )) / 3 ;
var r1 = 2 * pivot - _bars . LowPrices . Last ( 1 );
var s1 = 2 * pivot - _bars . HighPrices . Last ( 1 );
var r2 = pivot + _bars . HighPrices . Last ( 1 ) - _bars . LowPrices . Last ( 1 );
var s2 = pivot - _bars . HighPrices . Last ( 1 ) + _bars . LowPrices . Last ( 1 );
var r3 = _bars . HighPrices . Last ( 1 ) + 2 * ( pivot - _bars . LowPrices . Last ( 1 ));
var s3 = _bars . LowPrices . Last ( 1 ) - 2 * ( _bars . HighPrices . Last ( 1 ) - pivot );
Chart . DrawTrendLine ( "pivot " , _bars . OpenTimes . LastValue , pivot , _bars . OpenTimes . LastValue . AddDays ( 1 ), pivot , Color . White );
Chart . DrawTrendLine ( "r1 " , _bars . OpenTimes . LastValue , r1 , _bars . OpenTimes . LastValue . AddDays ( 1 ), r1 , Color . Green );
Chart . DrawTrendLine ( "r2 " , _bars . OpenTimes . LastValue , r2 , _bars . OpenTimes . LastValue . AddDays ( 1 ), r2 , Color . Green );
Chart . DrawTrendLine ( "r3 " , _bars . OpenTimes . LastValue , r3 , _bars . OpenTimes . LastValue . AddDays ( 1 ), r3 , Color . Green );
Chart . DrawTrendLine ( "s1 " , _bars . OpenTimes . LastValue , s1 , _bars . OpenTimes . LastValue . AddDays ( 1 ), s1 , Color . Red );
Chart . DrawTrendLine ( "s2 " , _bars . OpenTimes . LastValue , s2 , _bars . OpenTimes . LastValue . AddDays ( 1 ), s2 , Color . Red );
Chart . DrawTrendLine ( "s3 " , _bars . OpenTimes . LastValue , s3 , _bars . OpenTimes . LastValue . AddDays ( 1 ), s3 , Color . Red );
}
}
}
Press Ctrl + B or click Build , then add the indicator to a chart by clicking Add instance .
You should see the pivot points drawn on the chart.
Create a fractal indicator We will develop a separate indicator that plots fractals on a chart. Repeat the steps from the previous section and create another indicator with a new name.
Make this indicator an overlay indicator.
[Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
Draw arrows above bars where the high value is higher than the two adjacent bars on each side in the Calculate()
method.
if ( Bars . HighPrices [ index - 2 ] > Bars . HighPrices [ index - 1 ] && Bars . HighPrices [ index - 2 ] > Bars . HighPrices [ index ] &&
Bars . HighPrices [ index - 2 ] > Bars . HighPrices [ index - 3 ] && Bars . HighPrices [ index - 2 ] > Bars . HighPrices [ index - 4 ])
{
Chart . DrawIcon ( Bars . OpenTimes [ index ]. ToString (), ChartIconType . DownArrow , Bars . OpenTimes [ index - 2 ], Bars . HighPrices [ index - 2 ], Color . Red );
}
Draw up arrows below bars where the low value is lower than the two adjacent bars on each side.
if ( Bars . LowPrices [ index - 2 ] < Bars . LowPrices [ index - 1 ] && Bars . LowPrices [ index - 2 ] < Bars . LowPrices [ index ] &&
Bars . LowPrices [ index - 2 ] < Bars . LowPrices [ index - 3 ] && Bars . LowPrices [ index - 2 ] < Bars . LowPrices [ index - 4 ])
{
Chart . DrawIcon ( Bars . OpenTimes [ index ]. ToString (), ChartIconType . UpArrow , Bars . OpenTimes [ index - 2 ], Bars . LowPrices [ index - 2 ], Color . Green );
}
You can copy the full code below:
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 using System ;
using cAlgo.API ;
using cAlgo.API.Collections ;
using cAlgo.API.Indicators ;
using cAlgo.API.Internals ;
namespace cAlgo
{
[Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
public class FractalsExample : Indicator
{
protected override void Initialize ()
{
}
public override void Calculate ( int index )
{
if ( Bars . HighPrices [ index - 2 ] > Bars . HighPrices [ index - 1 ] && Bars . HighPrices [ index - 2 ] > Bars . HighPrices [ index ] &&
Bars . HighPrices [ index - 2 ] > Bars . HighPrices [ index - 3 ] && Bars . HighPrices [ index - 2 ] > Bars . HighPrices [ index - 4 ])
{
Chart . DrawIcon ( Bars . OpenTimes [ index ]. ToString (), ChartIconType . DownArrow , Bars . OpenTimes [ index - 2 ], Bars . HighPrices [ index - 2 ], Color . Red );
}
if ( Bars . LowPrices [ index - 2 ] < Bars . LowPrices [ index - 1 ] && Bars . LowPrices [ index - 2 ] < Bars . LowPrices [ index ] &&
Bars . LowPrices [ index - 2 ] < Bars . LowPrices [ index - 3 ] && Bars . LowPrices [ index - 2 ] < Bars . LowPrices [ index - 4 ])
{
Chart . DrawIcon ( Bars . OpenTimes [ index ]. ToString (), ChartIconType . UpArrow , Bars . OpenTimes [ index - 2 ], Bars . LowPrices [ index - 2 ], Color . Green );
}
}
}
}
Build the indicator and add an instance to a chart.
You should see fractal objects displayed on the relevant candles on the chart.
This article has demonstrated how to add pivot points and fractals to charts, enabling proper visualisation of key information.
Subscribe to our YouTube channel