如何绘制安德鲁斯叉线
安德鲁斯叉线是一种流行的技术分析工具,有时与其他指标一起使用,以识别趋势市场中的潜在支撑和阻力位。 这些水平是成功交易策略的关键基础,为交易者提供了清晰度、结构和决策信心。
本文及其相应的视频将演示如何创建一个在图表上绘制安德鲁斯叉线的 cBot,并编写代码使叉线随着图表的变化自动更新。
在图表上绘制安德鲁斯叉线
安德鲁斯叉线由一条中线(中心线)和两条平行线组成,形成一个通道。 这些对象帮助交易者可视化趋势、预测价格走势并识别潜在的交易入场和出场点。
安德鲁斯叉线使用三个关键点在价格图表上绘制:一个高点、一个低点以及随后的高点或低点。 价格在该通道内的运动通常在上线和下线之间振荡,而中线则作为回归点。
我们将创建一个绘制上升趋势安德鲁斯叉线的 cBot,这通常需要两个低点和一个高点。 下降趋势的安德鲁斯叉线则相反:需要两个高点和一个低点。
在 cTrader Algo 中,点击 新建 按钮创建一个 cBot 并为其输入名称,然后点击 创建。

安德鲁斯叉线需要六个参数来存储以下内容:高点和两个低点的索引、两个最低价格和一个最高价格。 声明这六个参数。
| private int _highIndex;
private int _lowIndex1;
private int _lowIndex2;
private double _max;
private double _min1;
private double _min2;
|
检测叉线点所需的两个最低价格和最高价格。
| _max = Bars.HighPrices.Maximum(50);
_min1 = Bars.LowPrices.Minimum(50);
_min2 = Bars.LowPrices.Minimum(10);
|
cTrader Algo 允许您通过提供绝对时间或相对柱索引的方法来定义时间位置。 用于在图表上绘制叉线的方法引入了相关的重写。 在这种情况下,我们选择了相对柱索引。
编写代码以检测高点和低点的索引。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | for (int i = 0; i <= 50; i++)
{
if (Bars.HighPrices.Last(i) == _max)
_highIndex = Bars.Count - i - 1;
}
for (int i = 0; i <= 50; i++)
{
if (Bars.LowPrices.Last(i) == _min1)
_lowIndex1 = Bars.Count - i - 1;
}
for (int i = 0; i <= 10; i++)
{
if (Bars.LowPrices.Last(i) == _min2)
_lowIndex2 = Bars.Count - i - 1;
}
|
在图表上绘制叉线。
| Chart.DrawAndrewsPitchfork("AndrewsPitchfork", _lowIndex1, _min1, _highIndex, _max, _lowIndex2, _min2, 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 | using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
public class AndrewsPitchfork : Robot
{
private int _highIndex;
private int _lowIndex1;
private int _lowIndex2;
private double _max;
private double _min1;
private double _min2;
protected override void OnStart()
{
_max = Bars.HighPrices.Maximum(50);
_min1 = Bars.LowPrices.Minimum(50);
_min2 = Bars.LowPrices.Minimum(10);
for (int i = 0; i <= 50; i++)
{
if (Bars.HighPrices.Last(i) == _max)
_highIndex = Bars.Count - i - 1;
}
for (int i = 0; i <= 50; i++)
{
if (Bars.LowPrices.Last(i) == _min1)
_lowIndex1 = Bars.Count - i - 1;
}
for (int i = 0; i <= 10; i++)
{
if (Bars.LowPrices.Last(i) == _min2)
_lowIndex2 = Bars.Count - i - 1;
}
Chart.DrawAndrewsPitchfork("AndrewsPitchfork", _lowIndex1, _min1, _highIndex, _max, _lowIndex2, _min2, Color.Red);
}
protected override void OnTick()
{
// Handle price updates here
}
protected override void OnStop()
{
// Handle cBot stop here
}
}
}
|
要构建 cBot,请使用 Ctrl+B 快捷键或点击 构建。
添加 cBot 的本地实例:点击 添加实例,选择 本地 选项,然后点击 添加实例。

点击实例并导航到 回测 选项卡。 选择回测的时间周期,启用 可视化模式 选项,然后点击播放图标。
您应该会看到 安德鲁斯叉线 绘制在图表上。

更新安德鲁斯叉线
返回 Algo 应用程序并编写 cBot 代码,以便在图表变化时更新叉线。
创建一个新方法来绘制叉线,并将原始逻辑移至该方法中。
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 | private void DrawAndrewsPitchfork()
{
_max = Bars.HighPrices.Maximum(50);
_min1 = Bars.LowPrices.Minimum(50);
_min2 = Bars.LowPrices.Minimum(10);
for (int i = 0; i <= 50; i++)
{
if (Bars.HighPrices.Last(i) == _max)
_highIndex = Bars.Count - i - 1;
}
for (int i = 0; i <= 50; i++)
{
if (Bars.LowPrices.Last(i) == _min1)
_lowIndex1 = Bars.Count - i - 1;
}
for (int i = 0; i <= 10; i++)
{
if (Bars.LowPrices.Last(i) == _min2)
_lowIndex2 = Bars.Count - i - 1;
}
if (_lowIndex1 < _highIndex && _highIndex < _lowIndex2)
Chart.DrawAndrewsPitchfork("AndrewsPitchfork", _lowIndex1, _min1, _highIndex, _max, _lowIndex2, _min2, Color.Red);
}
|
在 OnBar() 方法中调用新方法。
| protected override void OnBar()
{
DrawAndrewsPitchfork();
}
|
由于只需要上升趋势设置,编写条件逻辑以验证最小值和最大值是否按正确顺序排列。
| if (_lowIndex1 < _highIndex && _highIndex < _lowIndex2)
Chart.DrawAndrewsPitchfork("AndrewsPitchfork", _lowIndex1, _min1, _highIndex, _max, _lowIndex2, _min2, 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 | using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
public class AndrewsPitchfork : Robot
{
private int _highIndex;
private int _lowIndex1;
private int _lowIndex2;
private double _max;
private double _min1;
private double _min2;
protected override void OnStart()
{
DrawAndrewsPitchfork();
}
private void DrawAndrewsPitchfork()
{
_max = Bars.HighPrices.Maximum(50);
_min1 = Bars.LowPrices.Minimum(50);
_min2 = Bars.LowPrices.Minimum(10);
for (int i = 0; i <= 50; i++)
{
if (Bars.HighPrices.Last(i) == _max)
_highIndex = Bars.Count - i - 1;
}
for (int i = 0; i <= 50; i++)
{
if (Bars.LowPrices.Last(i) == _min1)
_lowIndex1 = Bars.Count - i - 1;
}
for (int i = 0; i <= 10; i++)
{
if (Bars.LowPrices.Last(i) == _min2)
_lowIndex2 = Bars.Count - i - 1;
}
if (_lowIndex1 < _highIndex && _highIndex < _lowIndex2)
Chart.DrawAndrewsPitchfork("AndrewsPitchfork", _lowIndex1, _min1, _highIndex, _max, _lowIndex2, _min2, Color.Red);
}
protected override void OnBar()
{
DrawAndrewsPitchfork();
}
protected override void OnStop()
{
// Handle cBot stop here
}
}
}
|
再次构建 cBot 并进行回测。
提高可视化模式的速度并点击 播放 图标。
这次,您应该会看到叉线随着图表的变化而不断重新绘制。

摘要
本文解释了如何在符号图表上绘制安德鲁斯叉线,并编写代码使叉线随着图表的变化而更新。