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

イントロダクション

この記事は、当社の豊富なインジケーターコードサンプルリストを補完するものです。これらのコードサンプルで言及されているいくつかの概念を詳しく説明し、新しいインジケーターを作成する際に実装できるいくつかの高度な機能について議論します。

「クラウド」属性

おそらく、トレーディングチャートで透明な「クラウド」を見たことがあるでしょう。

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には、Color enum が含まれており、これを使用して Chart オブジェクト、チャートコントロール、および出力の色を設定できます。

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", VerticalAlignm

色はカスタマイズ可能なパラメータとしても扱うことができます。ユーザーがカスタムの色を選択できるようにするには(例:インジケーターによって描かれるラインの色など)、Color 型のパラメータを宣言します。

1
2
[Parameter("描画色", 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。各バーに対して1つの点またはドット。
  • 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 タイプは、ユーザーが選択できる複数の定義済みオプションを持つパラメータを作成するために必要です。以下の例では、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 の値の中から1つを選択できる複数のオプションが表示されるインタラクティブなメニューが表示されます。

時間の操作

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

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

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 プロパティは、UTC 時間に対するユーザーの cTrader プラットフォームで設定された時間オフセットを表す TimeSpan オブジェクトを返します。

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

1
2
3
4
protected override void Initialize()
{
    Application.UserTimeOffsetChanged += Application_UserTimeOffsetChanged;
}

private void Application_UserTimeOffsetChanged(UserTimeOffsetChangedEventArgs obj)
{
    var platformTimeOffset = obj.UserTimeOffset;
}

パラメーターを使った時間の取得

カスタマイズ可能なパラメーターを介して特定の時間値を取得するには、string 型を使用し、この文字列を以下の型のいずれかに解析する必要があります。

  • DateTime
  • DateTimeOffset
  • TimeSpan
  • DateOnly
  • TimeOnly

これは、これがどのように行われるかの一例です。

 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
using cAlgo.API;
using System;
namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Sample : Indicator
    {
        [Parameter("TimeSpan", DefaultValue = "01:40:00")]
        public string TimeSpanString { get; set; }

        [Parameter("DateTime", DefaultValue = "01/01/2000 00:00:00")]
        public string DateTimeString { get; set; }

        [Parameter("DateTimeOffset", DefaultValue = "01/01/2000 00:00:00 +00:00")]
        public string DateTimeOffsetString { get; set; }

        [Parameter("DateOnly", DefaultValue = "01/01/2000")]
        public string DateOnlyString { get; set; }

        [Parameter("TimeOnly", DefaultValue = "13:36:25")]
        public string TimeOnlyString { get; set; }

        protected override void Initialize()
        {
            if (TimeSpan.TryParse(TimeSpanString, out var timeSpan) is false)
            {
                Print("Invalid TimeSpan");
            }
            else
            {
                Print(timeSpan);
            }

            if (DateTime.TryParse(DateTimeString, out var dateTime) is false)
            {
                Print("Invalid DateTime");
            }
            else
            {
                Print(dateTime);
            }

            if (DateTimeOffset.TryParse(DateTimeOffsetString, out var dateTimeOffset) is false)
            {
                Print("Invalid DateTimeOffset");
            }
            else
            {
                Print(dateTimeOffset);
            }

            if (DateOnly.TryParse(DateOnlyString, out var dateOnly) is false)
            {
                Print("Invalid DateOnly");
            }
            else
            {
                Print(dateOnly);
            }

            if (TimeOnly.TryParse(TimeOnlyString, out var timeOnly) is false)
            {
                Print("Invalid TimeOnly");
            }
            else
            {
                Print(timeOnly);
            }
        }

        public override void Calculate(int index)
        {
        }
    }
}
目次

このページについて