如何绘制斐波纳契回调线
斐波纳契回调线是一种流行的技术分析工具,用于识别资产价格的潜在支撑和阻力位。 通过识别支撑和阻力位,交易者可以在交易时机、风险管理、市场趋势对齐以及最终提高盈利机会方面获得战略优势。
在本文及其对应的视频中,您将学习如何创建一个 cBot,该 cBot 在图表上绘制斐波纳契回调线并根据回调线开仓。
在图表上绘制斐波纳契回调线
斐波纳契回调线实现了斐波纳契数列,即每个数字都是前两个数字之和的数列。
示例
这是一个斐波纳契数列的示例:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
斐波纳契回调线水平是从斐波纳契数列中得出的连续数字的比率。 这些是技术分析和交易中常用的比率:0.236, 0.382, 0.5, 0.618 和有时使用的 0.786。
我们将创建一个在图表上绘制斐波纳契回调线的 cBot。 cBot 代码中将提供四个参数,分别代表起始价格、起始时间、结束价格和结束时间。
在 cTrader Algo 中,点击 新建 按钮以创建 cBot 并输入名称,例如“斐波纳契回调策略”。

声明四个重要变量。
| private double _max;
private double _min;
private int _highIndex;
private int _lowIndex;
|
编写 cBot 代码以检测最后 50 根柱的最低和最高价格。
| _max = Bars.HighPrices.Maximum(50);
_min = Bars.LowPrices.Minimum(50);
|
cTrader Algo 允许用户通过绝对时间或相对柱索引定义时间位置。 我们将使用相对柱索引来绘制斐波纳契回调线,并编写代码以检测最高和最低价格的索引。
| 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) == _min)
_lowIndex = Bars.Count - i - 1;
}
|
最后,可以编写在图表上绘制斐波纳契回调线的代码。 最高和最低价格的顺序决定了斐波纳契回调线的方向。
| if (_highIndex > _lowIndex)
Chart.DrawFibonacciRetracement("fibo", _lowIndex, _min, _highIndex, _max, Color.Yellow);
else
Chart.DrawFibonacciRetracement("fibo", _highIndex, _max, _lowIndex, _min, Color.Yellow);
|
您可以复制以下完整代码:
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 | 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 FibonacciRetracementStrategy : Robot
{
private double _max;
private double _min;
private int _highIndex;
private int _lowIndex;
protected override void OnStart()
{
_max = Bars.HighPrices.Maximum(50);
_min = Bars.LowPrices.Minimum(50);
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) == _min)
_lowIndex = Bars.Count - i - 1;
}
if (_highIndex > _lowIndex)
Chart.DrawFibonacciRetracement("fibo", _lowIndex, _min, _highIndex, _max, Color.Yellow);
else
Chart.DrawFibonacciRetracement("fibo", _highIndex, _max, _lowIndex, _min, Color.Yellow);
}
protected override void OnTick()
{
// Handle price updates here
}
protected override void OnStop()
{
// Handle cBot stop here
}
}
}
|
要构建 cBot,请使用 Ctrl+B 快捷键或点击 构建。
添加 cBot 的本地实例:点击 添加实例,选择 本地 选项,然后点击 添加实例。

点击实例并导航到 回测 选项卡。 选择回测的时间周期,然后点击 播放 图标。

您应该会看到 斐波纳契回调线 绘制在图表上。
交易斐波纳契回调线
返回代码编辑器以实施基于斐波纳契回调线的简单策略。 该策略的核心是在价格穿越 0.618 斐波纳契水平并预计反弹时开仓买入。
编写在 OnTick() 方法中执行策略的代码。
| protected override void OnTick()
{
if (Positions.Count == 0 && Bid < _min + ((_max - _min) * 0.618))
ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000);
}
|
您可以复制以下完整代码:
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.Robots
{
[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
public class FibonacciRetracementStrategy : Robot
{
private double _max;
private double _min;
private int _highIndex;
private int _lowIndex;
protected override void OnStart()
{
_max = Bars.HighPrices.Maximum(50);
_min = Bars.LowPrices.Minimum(50);
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) == _min)
_lowIndex = Bars.Count - i - 1;
}
if (_highIndex > _lowIndex)
Chart.DrawFibonacciRetracement("fibo", _lowIndex, _min, _highIndex, _max, Color.Yellow);
else
Chart.DrawFibonacciRetracement("fibo", _highIndex, _max, _lowIndex, _min, Color.Yellow);
}
protected override void OnTick()
{
if (Positions.Count == 0 && Bid < _min + ((_max - _min) * 0.618))
ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000);
}
protected override void OnStop()
{
// Handle cBot stop here
}
}
}
|
再次构建 cBot 并进行回测。

您应该会看到每当回调线水平被穿越时,仓位被开立。
总结
本文解释了如何在符号图表上绘制斐波纳契回调线,并实施依赖于回调线的交易策略。