コンテンツにスキップ

インジケーターの高度な操作

この記事は、私たちの広範なインジケーターコードサンプルのリストを補完します。 これらのコードサンプルで言及されているいくつかの概念を拡張し、新しいインジケーターを作成する際に実装できるいくつかの高度な機能について説明します。

クラウド属性

おそらく、取引チャート上の透明なクラウドを見たことがあるでしょう。

Image title

以下の例に示すように、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進数カラーコードの両方を受け入れます。

1
2
3
4
_ = 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);
1
2
3
4
5
[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型のパラメーターを宣言します。

1
2
[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プロパティを使用します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[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型を使用しています:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public enum Options
{
    First,
    Second,
    Third,
    Fourth
}

[Parameter("Options", DefaultValue = Options.Third)]
public Options OptionsParameter { get; set; }

このパラメーターをインジケーターまたはcBotに追加すると、指定されたenum値のいずれかを選択できるインタラクティブなメニューが表示されます。

時間の操作

プラットフォームとサーバー時間

Server.TimeまたはServer.TimeInUtcプロパティにアクセスすることで、現在のサーバー時間を取得できます。

1
2
var currentServerTimeInIndicatorTimeZone = Server.Time;
var currentServerTimeInUtc = Server.TimeInUtc;

Server.Timeプロパティは、TimeZoneプロパティを介して確立されたインジケーターまたはcBotのタイムゾーンにおける現在の時間を表します。

バー開始時間

Bars.OpenTimeコレクションを使用して、バーの開始時間を取得します。 タイムゾーンは、TimeZoneクラス属性で指定したタイムゾーンに基づきます。

1
2
3
4
public override void Calculate(int index)
{
    var barTime = Bars.OpenTimes[index];
}

プラットフォーム時間オフセット

Application.UserTimeOffsetプロパティを使用して、ユーザーのプラットフォームのタイムゾーンを取得します。 これは主に、インジケーターの時間をユーザーのタイムゾーンに変換するために使用されます。

1
var userPlatformTimeOffset = Application.UserTimeOffset;

Application.UserTimeOffsetプロパティは、ユーザーがcTraderプラットフォームで設定したUTC時間との時間オフセットを表すTimeSpanオブジェクトを返します。

ユーザーがプラットフォームの時間オフセットを変更したときに通知を受け取ることもできます。 そのためには、Application.UserTimeOffsetChangedイベントを使用します。

1
2
3
4
5
6
7
8
9
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)
        {
        }
    }
}

Image title