跳转至

如何在 cTrader 指标中添加云层

cTrader Algo 使你能在图表上的线条之间绘制云层。 这些云层使你能快速识别图表上的关键区域并检测市场行为的变化。

在本文及其对应的视频中,我们将向你展示如何在指标中添加和自定义云层。

添加指标

典型的 Bollinger Bands 指标由一条上轨、一条下轨和中间的简单移动平均线组成。 为了增强指标的外观,我们计划在轨道之间填充云层。

要创建该指标,请打开 Algo 应用并导航到 Indicators 选项卡。 点击 New 按钮,输入指标名称,例如 "Bollinger Bands Cloud",然后点击 Create 按钮。

我们现在可以修改指标代码。 我们将使我们的示例成为一个重叠指标。

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

然后我们添加绘制 Bollinger Bands 所需的三条输出线。

1
2
3
4
5
6
7
8
[Output("Main", LineColor = "Yellow", PlotType = PlotType.Line, Thickness = 1)]
public IndicatorDataSeries Main { get; set; }

[Output("Top", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
public IndicatorDataSeries Top { get; set; }

[Output("Bottom", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
public IndicatorDataSeries Bottom { get; set; }

我们声明我们的 Bollinger Bands 指标。

1
private BollingerBands _bollingerBands;

初始化指标。

1
_bollingerBands = Indicators.BollingerBands(Bars.ClosePrices, 20, 2, MovingAverageType.Simple);

Calculate() 方法中用指标值填充线条。

1
2
3
Main[index] = _bollingerBands.Main[index];
Top[index] = _bollingerBands.Top[index];
Bottom[index] = _bollingerBands.Bottom[index];

您可以复制以下完整代码:

 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
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 BollingerBandsCloud : Indicator
    {

        [Output("Main", LineColor = "Yellow", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries Main { get; set; }

        [Output("Top", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries Top { get; set; }

        [Output("Bottom", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries Bottom { get; set; }

        private BollingerBands _bollingerBands;


        protected override void Initialize()
        {
            _bollingerBands = Indicators.BollingerBands(Bars.ClosePrices, 20, 2, MovingAverageType.Simple);
        }

        public override void Calculate(int index)
        {
            Main[index] = _bollingerBands.Main[index];
            Top[index] = _bollingerBands.Top[index];
            Bottom[index] = _bollingerBands.Bottom[index];
        }
    }
}

点击 Build 或使用 Ctrl+B 快捷键来构建指标。

EURUSD 交易品种添加一个指标实例。

你应该在图表上看到一个典型的 Bollinger Bands 指标。

在 Bollinger Bands 中添加云层

现在,我们回到代码编辑器,然后着手在轨道之间添加云层。

为了实现我们的目标,我们向现有指标添加 Cloud 属性。 Cloud 属性指示指标在 TopBottom 线之间绘制云层。

1
[Cloud("Top", "Bottom", Opacity = 0.2)]

你可以复制下面完整的修改代码:

 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
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)]
    [Cloud("Top", "Bottom", Opacity = 0.2)]

    public class BollingerBandsCloud : Indicator
    {

        [Output("Main", LineColor = "Yellow", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries Main { get; set; }

        [Output("Top", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries Top { get; set; }

        [Output("Bottom", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries Bottom { get; set; }

        private BollingerBands _bollingerBands;

        protected override void Initialize()
        {
            _bollingerBands = Indicators.BollingerBands(Bars.ClosePrices, 20, 2, MovingAverageType.Simple);
        }

        public override void Calculate(int index)
        {
            Main[index] = _bollingerBands.Main[index];
            Top[index] = _bollingerBands.Top[index];
            Bottom[index] = _bollingerBands.Bottom[index];
        }
    }
}

我们重新构建算法,然后检查我们的指标以查看发生了什么变化。

你应该在轨道之间看到一个红色的云层。 如果你更改 Top 线的颜色,云层颜色将自动更新。

我们回到代码编辑器,并将 Cloud 属性更改为仅在 Main(黄色)线之前绘制云层。

1
[Cloud("Top", "Main", Opacity = 0.2)]

重新构建指标,然后检查它以确认其新外观。

在 MA 交叉中添加云层

我们将创建一个新指标,并用它来演示如何在图表上添加不同颜色的云层。 我们打算开发一个移动平均线(MA)交叉指标,绿色云层表示上升趋势,红色云层表示下降趋势。

重复上一节的步骤以创建一个新指标。 这次,指标名称应为 "MA Cloud"。

我们通过将其 IsOverlay 属性设置为 True 来开始修改新指标。

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

添加所需的 Cloud 属性。

1
[Cloud("Fast", "Slow", Opacity = 0.2)]

添加输出线并声明移动平均线。

1
2
3
4
5
6
7
8
[Output("Fast", LineColor = "Green", PlotType = PlotType.Line, Thickness = 1)]
public IndicatorDataSeries Fast { get; set; }

[Output("Slow", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
public IndicatorDataSeries Slow { get; set; }

SimpleMovingAverage _fastMA;
SimpleMovingAverage _slowMA;

重复上一个示例(Bollinger Bands)中的步骤。 初始化移动平均线并将结果值传递给线条,以便它们在图表上显示。

1
2
3
4
5
_fastMA = Indicators.SimpleMovingAverage(Bars.ClosePrices, 7);
_slowMA = Indicators.SimpleMovingAverage(Bars.ClosePrices, 13);

Fast[index] = _fastMA.Result[index];
Slow[index] = _slowMA.Result[index];

您可以复制以下完整代码:

 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
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)]
    [Cloud("Top", "Bottom", Opacity = 0.2)]

    public class BollingerBandsCloud : Indicator
    {

        [Output("Main", LineColor = "Yellow", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries Main { get; set; }

        [Output("Top", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries Top { get; set; }

        [Output("Bottom", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries Bottom { get; set; }

        private BollingerBands _bollingerBands;

        protected override void Initialize()
        {
            _bollingerBands = Indicators.BollingerBands(Bars.ClosePrices, 20, 2, MovingAverageType.Simple);
        }

        public override void Calculate(int index)
        {
            Main[index] = _bollingerBands.Main[index];
            Top[index] = _bollingerBands.Top[index];
            Bottom[index] = _bollingerBands.Bottom[index];
        }
    }
}

构建指标并添加一个实例以在图表上查看指标。

云层颜色在每次移动平均线交叉(相交)时发生变化。

本文向你展示了如何在 cTrader 中为指标添加有用的云层。

Image title