インジケーターの高度な操作 この記事は、私たちの広範なインジケーターコードサンプルのリスト を補完します。 これらのコードサンプルで言及されているいくつかの概念を拡張し、新しいインジケーターを作成する際に実装できるいくつかの高度な機能について説明します。
クラウド属性 おそらく、取引チャート上の透明なクラウドを見たことがあるでしょう。
以下の例に示すように、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 コンストラクターは2つの出力ライン名を取ります。 その後、これらの2つの出力の間のスペースに一致するようにクラウドを自動的にプロットします。
Opacity プロパティを使用して、クラウドの不透明度を設定することもできます。 クラウドの色を設定するには、FirstColor プロパティを使用します。 デフォルトでは、クラウドの色はインジケーター出力の最初のラインの色と一致します。
色を扱う Algo APIには、Chart オブジェクト、チャートコントロール、および出力の色を設定するために使用できるColor enum が含まれています。
Color enum の値は、一般的に使用される色を表します。 16進数やARGBカラーコードを扱うことなく、これらを使用できます。
出力をカスタマイズする際、LineColor 文字列プロパティを使用して色を設定できます。 以下に示すように、名前付きカラーと16進数カラーコードの両方を受け入れます。
チャートオブジェクト チャートコントロール 出力
_ = 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 プロパティは、TimeZone プロパティを介して確立されたインジケーターまたはcBotのタイムゾーンにおける現在の時間を表します。
バー開始時間 Bars . OpenTime コレクションを使用して、バーの開始時間を取得します。 タイムゾーンは、TimeZone クラス属性で指定したタイムゾーンに基づきます。
public override void Calculate ( int index )
{
var barTime = Bars . OpenTimes [ index ];
}
プラットフォーム時間オフセット Application . UserTimeOffset プロパティを使用して、ユーザーのプラットフォームのタイムゾーンを取得します。 これは主に、インジケーターの時間をユーザーのタイムゾーンに変換するために使用されます。
var userPlatformTimeOffset = Application . UserTimeOffset ;
Application . UserTimeOffset プロパティは、ユーザーがcTraderプラットフォームで設定したUTC時間との時間オフセットを表すTimeSpan オブジェクトを返します。
ユーザーがプラットフォームの時間オフセットを変更したときに通知を受け取ることもできます。 そのためには、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 )
{
}
}
}