Aller au contenu

Comment créer des indicateurs de point pivot et de fractal

De nombreux indicateurs et stratégies s'appuient sur des objets graphiques pour présenter des informations essentielles, et cTrader Algo fournit les méthodes API nécessaires pour dessiner de tels objets. Les points pivots et les fractals, en particulier, sont dessinés sur les graphiques de trading pour aider à identifier les niveaux de cours critiques et les points de retournement.

Dans cet article et la vidéo correspondante, vous apprendrez à dessiner des points pivots et des fractals sur les graphiques.

Créer un indicateur de point pivot

Les points pivots sont des niveaux de cours calculés à partir des cours précédents. Ces points indiquent des zones potentielles de support ou de résistance.

Nous allons créer un indicateur qui dessine des points pivots quotidiens sur un graphique.

Dans cTrader Algo, accédez à l'onglet Indicateurs et cliquez sur Nouveau. Saisissez un nom pour le nouvel indicateur, puis cliquez sur le bouton Créer.

Modifiez l'indicateur dans l'éditeur de code pour en faire un indicateur de superposition.

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

Définissez et obtenez les barres quotidiennes dans la méthode Initialize().

1
2
3
4
5
6
Bars _bars;

protected override void Initialize()
{
    _bars = MarketData.GetBars(TimeFrame.Daily, SymbolName);
}

Calculez les différents niveaux de support et de résistance appelés points pivots. Sept valeurs comprenant le point pivot, trois niveaux de résistance et trois niveaux de support sont calculées.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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);

Puisque les points pivots ont été calculés, ils peuvent être dessinés sur le graphique à l'aide de lignes de tendance.

1
2
3
4
5
6
7
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);

Vous pouvez copier le code complet ci-dessous :

 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);

        }
    }
}

Appuyez sur Ctrl+B ou cliquez sur Générer, puis ajoutez l'indicateur à un graphique en cliquant sur Ajouter une instance.

Vous devriez voir les points pivots dessinés sur le graphique.

Créer un indicateur de fractal

Nous allons développer un indicateur distinct qui trace des fractals sur un graphique. Répétez les étapes de la section précédente et créez un autre indicateur avec un nouveau nom.

Faites de cet indicateur un indicateur de superposition.

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

Dessinez des flèches au-dessus des barres où la valeur haute est supérieure aux deux barres adjacentes de chaque côté dans la méthode Calculate().

1
2
3
4
5
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);
}

Dessinez des flèches vers le haut en dessous des barres où la valeur basse est inférieure aux deux barres adjacentes de chaque côté.

1
2
3
4
5
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);
}

Vous pouvez copier le code complet ci-dessous :

 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);
            }
        }
    }
}

Générez l'indicateur et ajoutez une instance à un graphique.

Vous devriez voir les objets fractals affichés sur les bougies pertinentes du graphique.

Cet article a démontré comment ajouter des points pivots et des fractals aux graphiques, permettant une visualisation appropriée des informations clés.