ข้ามไปที่เนื้อหา

การดำเนินการขั้นสูงกับอินดิเคเตอร์

บทความนี้เสริมรายการตัวอย่างโค้ดอินดิเคเตอร์ที่ครอบคลุมของเรา เราขยายแนวคิดบางอย่างที่กล่าวถึงในตัวอย่างโค้ดเหล่านี้และอภิปรายเกี่ยวกับคุณสมบัติขั้นสูงหลายอย่างที่คุณสามารถใช้งานเมื่อสร้างอินดิเคเตอร์ใหม่

แอตทริบิวต์ cloud

คุณอาจเคยเห็นเมฆโปร่งใสบนแผนภูมิการเทรด

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 รับชื่อเส้นผลลัพธ์สองเส้น หลังจากนั้น มันจะวาดเมฆโดยอัตโนมัติเพื่อให้ตรงกับพื้นที่ระหว่างผลลัพธ์สองเส้นนี้

คุณยังสามารถตั้งค่าความทึบของเมฆโดยใช้คุณสมบัติ Opacity ในการตั้งค่าสีของเมฆ ใช้คุณสมบัติ FirstColor โดยค่าเริ่มต้น สีของเมฆจะตรงกับสีของเส้นแรกในผลลัพธ์ของอินดิเคเตอร์

ทำงานกับสี

Algo API รวมถึง enum Color ที่สามารถใช้เพื่อกำหนดค่าสีสำหรับวัตถุ Chart การควบคุมแผนภูมิ และผลลัพธ์

ค่าของ enum Color แทนสีที่ใช้บ่อย คุณสามารถใช้มันได้โดยไม่ต้องจัดการกับรหัสสีแบบเลขฐานสิบหกหรือ ARGB

เมื่อปรับแต่งผลลัพธ์ คุณสามารถตั้งค่าสีของพวกมันโดยใช้คุณสมบัติสตริง LineColor ดังที่แสดงด้านล่าง มันยอมรับทั้งชื่อสีและรหัสสีแบบเลขฐานสิบหก

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 จำเป็นสำหรับการสร้างพารามิเตอร์ที่มีตัวเลือกที่กำหนดไว้ล่วงหน้าหลายตัวเลือกให้ผู้ใช้เลือก เราใช้ประเภท 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 แสดงเวลาปัจจุบันในโซนเวลาของอินดิเคเตอร์หรือ cBot ของคุณที่กำหนดผ่านคุณสมบัติ TimeZone

เวลาเปิดแท่งเทียน

ใช้คอลเลกชัน 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 ส่งคืนออบเจกต์ TimeSpan ที่แสดงถึงออฟเซ็ตเวลาที่ผู้ใช้ตั้งค่าในแพลตฟอร์ม cTrader ของพวกเขาเมื่อเทียบกับเวลา UTC

คุณยังสามารถเลือกรับการแจ้งเตือนเมื่อผู้ใช้เปลี่ยนออฟเซ็ตเวลาของแพลตฟอร์มได้ หากต้องการทำเช่นนั้น ให้ใช้อีเวนต์ 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 รองรับประเภทพารามิเตอร์วันที่และเวลาเฉพาะที่ช่วยให้คุณสามารถรับค่าวันที่และเวลาที่มีการพิมพ์แบบเข้มงวดเป็นอินพุตสำหรับ algo ของคุณแทนที่จะใช้ประเภทพารามิเตอร์ string

การใช้ประเภทพารามิเตอร์วันที่และเวลาใหม่ช่วยให้คุณได้รับค่าในโซนเวลา algo ของคุณพร้อมการตรวจสอบความถูกต้องของค่าต่ำสุดและสูงสุดในตัว และการรองรับการเพิ่มประสิทธิภาพอย่างเต็มรูปแบบ เช่นเดียวกับประเภทพารามิเตอร์อื่นๆ

หากต้องการรับค่าเวลาที่แน่นอนผ่านพารามิเตอร์ที่ปรับแต่งได้ คุณสามารถใช้ประเภท 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