如何创建枢轴点和分形指标
许多指标和策略依赖于图表对象来呈现关键信息,cTrader Algo提供了绘制此类对象所需的API方法。 特别是枢轴点和分形,它们被绘制在交易图表上,以帮助识别关键价格水平和转折点。
在本文和相应的视频中,您将学习如何在图表上绘制枢轴点和分形。
创建枢轴点指标
枢轴点是根据之前的价格计算出的价格水平。 这些点表示潜在的支撑或阻力区域。
我们将创建一个在图表上绘制每日枢轴点的指标。
在cTrader Algo中,导航到指标选项卡并点击新建。 输入新指标的名称,然后点击创建按钮。

在代码编辑器中修改指标,使其成为重叠指标。
| [Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
|
在Initialize()方法中定义并获取每日K线。
| Bars _bars;
protected override void Initialize()
{
_bars = MarketData.GetBars(TimeFrame.Daily, SymbolName);
}
|
计算被称为枢轴点的不同支撑和阻力水平。 计算七个值,包括枢轴点、三个阻力水平和三个支撑水平。
| 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);
|
您可以复制以下完整代码:
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);
}
}
}
|
按下Ctrl+B或点击构建,然后通过点击添加实例将指标添加到图表中。
您应该看到枢轴点绘制在图表上。

创建分形指标
我们将开发一个在图表上绘制分形的独立指标。 重复上一节的步骤,并使用新名称创建另一个指标。

使该指标成为重叠指标。
| [Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
|
在Calculate()方法中,在每侧的高值高于两个相邻K线的K线上方绘制向上箭头。
| 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);
}
|
在每侧的低值低于两个相邻K线的K线下方绘制向下箭头。
| 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);
}
|
您可以复制以下完整代码:
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);
}
}
}
}
|
构建指标并将实例添加到图表中。
您应该看到分形对象显示在图表上的相关蜡烛上。

本文演示了如何将枢轴点和分形添加到图表中,以便正确可视化关键信息。