指标的高级操作 本文补充了我们广泛的 指标代码示例列表 。 我们扩展了这些代码示例中提到的一些概念,并讨论了在创建新指标时可以实现的几种高级功能。
云属性 您可能已经在交易图表上看到过透明的云。
您可以通过使用 CloudAttribute 类属性将云添加到指标输出中,如下例所示:
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 using cAlgo.API ;
using cAlgo.API.Indicators ;
using System ;
namespace cAlgo
{
/// <summary>
/// This indicator shows how to make a built-in cTrader indicator multi time frame and how to use cloud attribute
/// </summary>
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None), Cloud("Top", "Bottom", Opacity = 0.2)]
public class BollingerBandsMTFCloudSample : Indicator
{
private BollingerBands _bollingerBands ;
private Bars _baseBars ;
[Parameter("Base TimeFrame", DefaultValue = "Daily")]
public TimeFrame BaseTimeFrame { get ; set ; }
[Parameter("Source", DefaultValue = DataSeriesType.Close)]
public DataSeriesType DataSeriesType { get ; set ; }
[Parameter("Periods", DefaultValue = 14, MinValue = 0)]
public int Periods { get ; set ; }
[Parameter("Standard Deviation", DefaultValue = 2, MinValue = 0)]
public double StandardDeviation { get ; set ; }
[Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
public MovingAverageType MaType { get ; set ; }
[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 ; }
protected override void Initialize ()
{
_baseBars = MarketData . GetBars ( BaseTimeFrame );
var baseSeries = GetBaseSeries ();
_bollingerBands = Indicators . BollingerBands ( baseSeries , Periods , StandardDeviation , MaType );
}
public override void Calculate ( int index )
{
var baseIndex = _baseBars . OpenTimes . GetIndexByTime ( Bars . OpenTimes [ index ]);
Main [ index ] = _bollingerBands . Main [ baseIndex ];
Top [ index ] = _bollingerBands . Top [ baseIndex ];
Bottom [ index ] = _bollingerBands . Bottom [ baseIndex ];
}
private DataSeries GetBaseSeries ()
{
switch ( DataSeriesType )
{
case DataSeriesType . Open :
return _baseBars . OpenPrices ;
case DataSeriesType . High :
return _baseBars . HighPrices ;
case DataSeriesType . Low :
return _baseBars . LowPrices ;
case DataSeriesType . Close :
return _baseBars . ClosePrices ;
default :
throw new ArgumentOutOfRangeException ( "DataSeriesType" );
}
}
}
public enum DataSeriesType
{
Open ,
High ,
Low ,
Close
}
}
CloudAttribute 构造函数接受两个输出线名称。 之后,它会自动绘制云以匹配这两个输出之间的空间。
您还可以使用 Opacity 属性设置云的不透明度。 要设置云的颜色,请使用 FirstColor 属性。 默认情况下,云的颜色将与指标输出中第一条线的颜色匹配。
使用颜色 Algo API 包括 Color enum ,可用于配置 Chart 对象、图表控件和输出的颜色。
Color enum 的值代表常用颜色。 您可以使用它们而无需处理十六进制或 ARGB 颜色代码。
在自定义输出时,您可以使用 LineColor 字符串属性设置其颜色。 如下所示,它接受命名颜色和十六进制颜色代码。
图表对象 图表控件 输出
_ = Chart . DrawStaticText ( "NamedColor" , "This is text using Color class color name properties" , VerticalAlignment . Center , HorizontalAlignment . Center , Color . Red );
_ = Chart . DrawStaticText ( "HexadecimalColor" , "This is text using Hexadecimal color" , VerticalAlignment . Bottom , HorizontalAlignment . Center , Color . FromHex ( "#FF5733" ));
_ = Chart . DrawStaticText ( "ARGBColor" , "This is text using ARGB color" , VerticalAlignment . Top , HorizontalAlignment . Center , Color . FromArgb ( 255 , 200 , 100 , 60 ));
_ = Chart . DrawStaticText ( "ParsedNameColor" , "This is text using color name by parsing it from string" , VerticalAlignment . Center , HorizontalAlignment . Left , Color . FromName ( "Yellow" ));
1
2
3
4
5
6
7
8
9
10
11
12
13 var stackPanel = new StackPanel
{
Orientation = Orientation . Vertical ,
HorizontalAlignment = HorizontalAlignment . Center ,
VerticalAlignment = VerticalAlignment . Center
};
stackPanel . AddChild ( new TextBlock { Text = "Red Color property" , BackgroundColor = Color . Red });
stackPanel . AddChild ( new TextBlock { Text = "Hexadecimal Color code" , BackgroundColor = Color . FromHex ( "#FF5733" ) });
stackPanel . AddChild ( new TextBlock { Text = "ARGB Color" , BackgroundColor = Color . FromArgb ( 200 , 100 , 40 , 80 ) });
stackPanel . AddChild ( new TextBlock { Text = "Color Name" , BackgroundColor = Color . FromName ( "Green" ) });
Chart . AddControl ( stackPanel );
[Output("Hexadecimal", LineColor = "#FF5733", PlotType = PlotType.Line, Thickness = 1)]
public IndicatorDataSeries Hexadecimal { get ; set ; }
[Output("Name", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
public IndicatorDataSeries Name { get ; set ; }
注意
颜色也可以作为自定义参数进行处理。 要允许用户选择自定义颜色(例如,指标绘制的线条颜色),请声明一个 Color 类型的参数。
[Parameter("Drawing Color", DefaultValue = "#f54242")]
public Color DrawingColor { get ; set ; }
在下面的示例中,我们创建了一个简单的(高减去低)指标,该指标在初始化时会在其附加的交易图表上显示文本。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 using cAlgo.API ;
using cAlgo.API.Indicators ;
using cAlgo.API.Collections ;
using cAlgo.API.Internals ;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class HighMinusLowColor : Indicator
{
[Parameter("Text Color", DefaultValue="#f54242")]
public Color TextColor { get ; set ; }
public override void Initialize ()
{
var staticText = Chart . DrawStaticText ( "static" , "This text shows how color parameters work" , VerticalAlignment . Center , HorizontalAlignment . Center , TextColor );
}
}
}
输出类型 自定义指标有几种不同的输出类型:
Line - 一条连续的线。 DiscontinuousLine - 一条非连续的线,适用于指标并不总是有计算值的情况。 Points - 每个柱状图的点或圆点。 Histogram - 一系列垂直的柱状图。 使用此类型时,请将指标的 IsOverlay 属性设置为 false 。 要设置输出类型,请使用 PlotType 属性,如下所示。
[Output("Line", PlotType = PlotType.Line)]
public IndicatorDataSeries Line { get ; set ; }
[Output("Discontinuous Line", PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries DiscontinuousLine { get ; set ; }
[Output("Points", PlotType = PlotType.Points)]
public IndicatorDataSeries Points { get ; set ; }
[Output("Histogram", PlotType = PlotType.Histogram)]
public IndicatorDataSeries Histogram { get ; set ; }
枚举参数 enum 类型用于创建具有多个预定义选项的参数,用户可以从这些选项中进行选择。 我们在以下示例中使用 enum 类型:
public enum Options
{
First ,
Second ,
Third ,
Fourth
}
[Parameter("Options", DefaultValue = Options.Third)]
public Options OptionsParameter { get ; set ; }
如果将此参数添加到指标或 cBot 中,您将看到一个多选项交互菜单,允许您选择指定的 enum 值之一。
处理时间 平台和服务器时间 您可以通过访问 Server . Time 或 Server . TimeInUtc 属性来获取当前服务器时间。
var currentServerTimeInIndicatorTimeZone = Server . Time ;
var currentServerTimeInUtc = Server . TimeInUtc ;
Server . Time 属性表示您的指标或 cBot 时区中的当前时间,该时区通过 TimeZone 属性建立。
柱状图开盘时间 使用 Bars . OpenTime 集合获取柱状图的开盘时间。 时区将基于您在 TimeZone 类属性中指定的时区。
public override void Calculate ( int index )
{
var barTime = Bars . OpenTimes [ index ];
}
平台时间偏移 使用 Application . UserTimeOffset 属性获取用户平台的时区。 这主要用于将您的指标时间转换为用户的时区。
var userPlatformTimeOffset = Application . UserTimeOffset ;
Application . UserTimeOffset 属性返回一个 TimeSpan 对象,表示用户在 cTrader 平台中设置的与 UTC 时间相比的时间偏移。
您还可以选择在用户更改其平台时间偏移时收到通知。 为此,请使用 Application . UserTimeOffsetChanged 事件。
protected override void Initialize ()
{
Application . UserTimeOffsetChanged += Application_UserTimeOffsetChanged ;
}
private void Application_UserTimeOffsetChanged ( UserTimeOffsetChangedEventArgs obj )
{
var platformTimeOffset = obj . UserTimeOffset ;
}
通过参数获取时间 cTrader 支持专用的日期和时间参数类型,允许您获取强类型的日期和时间值作为算法的输入,而不是使用 string 参数类型。
通过使用新的日期和时间参数类型,您可以在算法时区中获取值,并具有内置的最小值和最大值验证以及完整的优化支持,就像其他参数类型一样。
要通过自定义参数获取特定时间值,您可以使用以下 C# 类型:
DateTime DateOnly TimeSpan 以下是实现此操作的示例:
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 using cAlgo.API ;
using System ;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Sample : Indicator
{
[Parameter("DateTime Parameter", MinValue = "1970-01-01T00:00:00", MaxValue = "2025-11-01T00:00:00", DefaultValue = "2025-01-01T10:00:00")]
public DateTime DateTimeParameter { get ; set ; }
[Parameter("DateOnly Parameter", MinValue = "1970-01-01", MaxValue = "2025-11-01", DefaultValue = "2025-01-01")]
public DateOnly DateOnlyParameter { get ; set ; }
[Parameter("TimeSpan Parameter", MinValue = "00:00:00", MaxValue = "23:59:59", DefaultValue = "04:10:20")]
public TimeSpan TimeSpanParameter { get ; set ; }
protected override void Initialize ()
{
Print ( $"DateTimeParameter: {DateTimeParameter:o}" );
Print ( $"DateOnlyParameter: {DateOnlyParameter:o}" );
Print ( $"TimeSpanParameter: {TimeSpanParameter}" );
}
public override void Calculate ( int index )
{
}
}
}