Skip to content

How to create trading session and support/resistance indicators

The cTrader Algo API provides the types required to create indicators that draw rectangles, lines, triangles, clouds and other objects on trading charts. Rectangles, in particular, are widely used to highlight important periods, consolidation zones and support/resistance levels.

In this article and the corresponding video, you will learn how to draw rectangles that represent trading sessions and support/resistance levels on a symbol chart.

Create a trading session indicator

There are four major trading sessions: Sydney, Tokyo, London and New York. Each session is unique in terms of volatility and activity levels. By monitoring the sessions and overlaps between them, traders know when specific assets are traded and can capitalise on opportunities.

In this example, we want to create an indicator that draws trading sessions on a chart.

In cTrader Algo, navigate to the Indicators tab and click the New button. Enter a name in the text field and then click Create.

Start modifying the indicator in the code editor. First, make it an overlay indicator.

1
[Indicator(AccessRights = AccessRights.None, IsOverlay = true)]

Write the code to draw a rectangle that indicates the London session on a chart.

1
2
3
4
5
6
7
TimeSpan londonOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Greenwich Standard Time")).TimeOfDay;
TimeSpan londonClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Greenwich Standard Time")).TimeOfDay;

Chart.DrawRectangle("London Session ", Server.Time.Date.Add(londonOpen), Chart.TopY, Server.Time.Date.Add(londonClose), Chart.BottomY, 
Color.FromArgb(50, 0, 50, 255)).IsFilled = true;

Press Ctrl+B or click Build, then add the indicator to a chart by clicking Add instance.

The London session rectangle can be seen on the chart.

Return to the code editor and add code for the remaining three trading sessions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
TimeSpan nyOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")).TimeOfDay;
TimeSpan nyClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")).TimeOfDay;

Chart.DrawRectangle("NY Session ", Server.Time.Date.Add(nyOpen), Chart.TopY, Server.Time.Date.Add(nyClose), Chart.BottomY,
Color.FromArgb(50, 255, 50, 0)).IsFilled = true;

TimeSpan sydneyOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")).TimeOfDay;
TimeSpan sydneyClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")).TimeOfDay;

Chart.DrawRectangle("Sydney Session ", Server.Time.Date.Add(sydneyOpen).AddDays(-1), Chart.TopY, Server.Time.Date.Add(sydneyClose),
Chart.BottomY, Color.FromArgb(50, 50, 255, 0)).IsFilled = true;

TimeSpan tokyoOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time")).TimeOfDay;
TimeSpan tokyoClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time")).TimeOfDay;

Chart.DrawRectangle("Tokio Session ", Server.Time.Date.Add(tokyoOpen), Chart.TopY, Server.Time.Date.Add(tokyoClose), Chart.BottomY,
Color.FromArgb(50, 0, 50, 255)).IsFilled = true;

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
43
44
45
46
47
48
49
50
51
52
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 TradingSessions : Indicator
    {
        protected override void Initialize()
        {

        }

        public override void Calculate(int index)
        {
            TimeSpan londonOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("Greenwich Standard Time")).TimeOfDay;
            TimeSpan londonClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("Greenwich Standard Time")).TimeOfDay;

            Chart.DrawRectangle("London Session ", Server.Time.Date.Add(londonOpen), Chart.TopY, Server.Time.Date.Add(londonClose), Chart.BottomY, 
            Color.FromArgb(50, 0, 50, 255)).IsFilled = true;

            TimeSpan nyOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")).TimeOfDay;
            TimeSpan nyClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")).TimeOfDay;

            Chart.DrawRectangle("NY Session ", Server.Time.Date.Add(nyOpen), Chart.TopY, Server.Time.Date.Add(nyClose), Chart.BottomY,
            Color.FromArgb(50, 255, 50, 0)).IsFilled = true;

            TimeSpan sydneyOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")).TimeOfDay;
            TimeSpan sydneyClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")).TimeOfDay;

            Chart.DrawRectangle("Sydney Session ", Server.Time.Date.Add(sydneyOpen).AddDays(-1), Chart.TopY, Server.Time.Date.Add(sydneyClose),
            Chart.BottomY, Color.FromArgb(50, 50, 255, 0)).IsFilled = true;

            TimeSpan tokyoOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time")).TimeOfDay;
            TimeSpan tokyoClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time")).TimeOfDay;

            Chart.DrawRectangle("Tokyo Session ", Server.Time.Date.Add(tokyoOpen), Chart.TopY, Server.Time.Date.Add(tokyoClose), Chart.BottomY,
            Color.FromArgb(50, 0, 50, 255)).IsFilled = true;
        }
    }
}

Rebuild the indicator and then check the chart to see all the trading sessions represented there.

Create a support/resistance indicator

We will create an indicator that plots resistance and support zones on a chart using fractals.

Create a new indicator using the same steps, but assign it a different name.

Start the modifications by setting IsOverlay to true.

1
[Indicator(AccessRights = AccessRights.None, IsOverlay = true)]

A fractal resistance zone is drawn where the high value is higher than the two adjacent bars on each side. The zone is the area between the high and close prices of a fractal candle. Resistance zones help to identify areas where a bullish trend might stop and reverse, which results in trading opportunities.

Write the code that draws a resistance zone.

1
2
3
4
5
6
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.DrawRectangle(Bars.OpenTimes[index - 2].ToString(), Bars.OpenTimes[index - 2], Bars.HighPrices[index - 2], Bars.OpenTimes[index - 2].AddDays(1),
    Bars.ClosePrices[index - 2], Color.FromArgb(50, Color.Green)).IsFilled = true;
}

Unlike resistance zones, support zones help to identify where a bearish trend might stop and reverse. Such zones may be translated as buy signals.

Write the code that draws a support zone.

1
2
3
4
5
6
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.DrawRectangle(Bars.OpenTimes[index - 2].ToString(), Bars.OpenTimes[index - 2], Bars.ClosePrices[index - 2], Bars.OpenTimes[index - 2].AddDays(1),
    Bars.LowPrices[index - 2], Color.FromArgb(50, Color.Red)).IsFilled = true;
}

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
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 SupportResistanceExample : 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.DrawRectangle(Bars.OpenTimes[index - 2].ToString(), Bars.OpenTimes[index - 2], Bars.HighPrices[index - 2], Bars.OpenTimes[index - 2].AddDays(1),
                Bars.ClosePrices[index - 2], Color.FromArgb(50, Color.Green)).IsFilled = true;
            }

            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.DrawRectangle(Bars.OpenTimes[index - 2].ToString(), Bars.OpenTimes[index - 2], Bars.ClosePrices[index - 2], Bars.OpenTimes[index - 2].AddDays(1),
                Bars.LowPrices[index - 2], Color.FromArgb(50, Color.Red)).IsFilled = true;
            }
        }
    }
}

Build the indicator and add an instance to a chart. You should see the support and resistance zones on the chart.

This article has demonstrated how to create indicators with useful objects, such as rectangles, which can be used to represent trading sessions and support/resistance levels on charts.

Subscribe to our YouTube channel