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

วิธีสร้างอินดิเคเตอร์เซสชันการเทรดและแนวรับ/แนวต้าน

API cTrader Algo มี ประเภท ที่จำเป็นสำหรับการสร้างอินดิเคเตอร์ที่วาดสี่เหลี่ยมผืนผ้า เส้น สามเหลี่ยม เมฆ และวัตถุอื่นๆ บนกราฟการเทรด สี่เหลี่ยมผืนผ้า โดยเฉพาะอย่างยิ่ง ถูกใช้อย่างแพร่หลายเพื่อเน้นช่วงเวลาที่สำคัญ โซนการรวมตัว และระดับแนวรับ/แนวต้าน

ในบทความนี้และวิดีโอที่เกี่ยวข้อง คุณจะได้เรียนรู้วิธีการวาดสี่เหลี่ยมผืนผ้าที่แสดงเซสชันการเทรดและระดับแนวรับ/แนวต้านบนกราฟสัญลักษณ์

สร้างอินดิเคเตอร์เซสชันการเทรด

มีเซสชันการเทรดหลักสี่เซสชัน: ซิดนีย์ โตเกียว ลอนดอน และนิวยอร์ก แต่ละเซสชันมีเอกลักษณ์ในแง่ของความผันผวนและระดับกิจกรรม โดยการติดตามเซสชันและการทับซ้อนระหว่างเซสชัน นักเทรดจะรู้ว่าสินทรัพย์เฉพาะถูกเทรดเมื่อใดและสามารถใช้ประโยชน์จากโอกาสได้

ในตัวอย่างนี้ เราต้องการสร้างอินดิเคเตอร์ที่วาดเซสชันการเทรดบนกราฟ

ใน cTrader Algo ไปที่แท็บ Indicators และคลิกปุ่ม New ป้อนชื่อในช่องข้อความแล้วคลิก Create

เริ่มแก้ไขอินดิเคเตอร์ในตัวแก้ไขโค้ด ขั้นแรก ทำให้เป็นอินดิเคเตอร์แบบซ้อนทับ

1
[Indicator(AccessRights = AccessRights.None, IsOverlay = true)]

เขียนโค้ดเพื่อวาดสี่เหลี่ยมผืนผ้าที่แสดงเซสชันลอนดอนบนกราฟ

1
2
3
4
5
6
7
TimeSpan londonOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Greenwich Standard Time")).TimeOfDay;
TimeSpan londonClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Greenwich Standard Time")).TimeOfDay;

Chart.DrawRectangle("London Session ", Server.Time.Date.Add(londonOpen), Chart.TopY, Server.Time.Date.Add(londonClose), Chart.BottomY, 
Color.FromArgb(50, 0, 50, 255)).IsFilled = true;

กด Ctrl+B หรือคลิก Build จากนั้นเพิ่มอินดิเคเตอร์ลงในกราฟโดยคลิก Add instance

สี่เหลี่ยมผืนผ้าเซสชันลอนดอนสามารถเห็นได้บนกราฟ

กลับไปที่ตัวแก้ไขโค้ดและเพิ่มโค้ดสำหรับเซสชันการเทรดที่เหลืออีกสามเซสชัน

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
TimeSpan nyOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")).TimeOfDay;
TimeSpan nyClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")).TimeOfDay;

Chart.DrawRectangle("NY Session ", Server.Time.Date.Add(nyOpen), Chart.TopY, Server.Time.Date.Add(nyClose), Chart.BottomY,
Color.FromArgb(50, 255, 50, 0)).IsFilled = true;

TimeSpan sydneyOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")).TimeOfDay;
TimeSpan sydneyClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")).TimeOfDay;

Chart.DrawRectangle("Sydney Session ", Server.Time.Date.Add(sydneyOpen).AddDays(-1), Chart.TopY, Server.Time.Date.Add(sydneyClose),
Chart.BottomY, Color.FromArgb(50, 50, 255, 0)).IsFilled = true;

TimeSpan tokyoOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time")).TimeOfDay;
TimeSpan tokyoClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time")).TimeOfDay;

Chart.DrawRectangle("Tokio Session ", Server.Time.Date.Add(tokyoOpen), Chart.TopY, Server.Time.Date.Add(tokyoClose), Chart.BottomY,
Color.FromArgb(50, 0, 50, 255)).IsFilled = true;

คุณสามารถคัดลอกโค้ดทั้งหมดด้านล่างนี้:

 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
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
    public class TradingSessions : Indicator
    {
        protected override void Initialize()
        {

        }

        public override void Calculate(int index)
        {
            TimeSpan londonOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("Greenwich Standard Time")).TimeOfDay;
            TimeSpan londonClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("Greenwich Standard Time")).TimeOfDay;

            Chart.DrawRectangle("London Session ", Server.Time.Date.Add(londonOpen), Chart.TopY, Server.Time.Date.Add(londonClose), Chart.BottomY, 
            Color.FromArgb(50, 0, 50, 255)).IsFilled = true;

            TimeSpan nyOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")).TimeOfDay;
            TimeSpan nyClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")).TimeOfDay;

            Chart.DrawRectangle("NY Session ", Server.Time.Date.Add(nyOpen), Chart.TopY, Server.Time.Date.Add(nyClose), Chart.BottomY,
            Color.FromArgb(50, 255, 50, 0)).IsFilled = true;

            TimeSpan sydneyOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")).TimeOfDay;
            TimeSpan sydneyClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time")).TimeOfDay;

            Chart.DrawRectangle("Sydney Session ", Server.Time.Date.Add(sydneyOpen).AddDays(-1), Chart.TopY, Server.Time.Date.Add(sydneyClose),
            Chart.BottomY, Color.FromArgb(50, 50, 255, 0)).IsFilled = true;

            TimeSpan tokyoOpen = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 9, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time")).TimeOfDay;
            TimeSpan tokyoClose = TimeZoneInfo.ConvertTimeToUtc(new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, 18, 0, 0),
            TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time")).TimeOfDay;

            Chart.DrawRectangle("Tokyo Session ", Server.Time.Date.Add(tokyoOpen), Chart.TopY, Server.Time.Date.Add(tokyoClose), Chart.BottomY,
            Color.FromArgb(50, 0, 50, 255)).IsFilled = true;
        }
    }
}

สร้างอินดิเคเตอร์ใหม่แล้วตรวจสอบกราฟเพื่อดูเซสชันการเทรดทั้งหมดที่แสดงอยู่ที่นั่น

สร้างอินดิเคเตอร์แนวรับ/แนวต้าน

เราจะสร้างอินดิเคเตอร์ที่พล็อตโซนแนวต้านและแนวรับบนกราฟโดยใช้ Fractal

สร้างอินดิเคเตอร์ใหม่โดยใช้ขั้นตอนเดียวกัน แต่กำหนดชื่อที่แตกต่างกัน

เริ่มการปรับเปลี่ยนโดยตั้งค่า IsOverlay เป็น true

1
[Indicator(AccessRights = AccessRights.None, IsOverlay = true)]

โซนแนวต้าน Fractal จะถูกวาดในตำแหน่งที่ค่าสูงสุดสูงกว่าแท่งข้างเคียงสองแท่งในแต่ละด้าน โซนคือพื้นที่ระหว่างราคาสูงสุดและราคาปิดของแท่งเทียน Fractal โซนแนวต้านช่วยระบุพื้นที่ที่แนวโน้มขาขึ้นอาจหยุดและกลับตัว ซึ่งส่งผลให้เกิดโอกาสในการเทรด

เขียนโค้ดที่วาดโซนแนวต้าน

1
2
3
4
5
6
if (Bars.HighPrices[index - 2] > Bars.HighPrices[index - 1] && Bars.HighPrices[index - 2] > Bars.HighPrices[index] &&
Bars.HighPrices[index - 2] > Bars.HighPrices[index - 3] && Bars.HighPrices[index - 2] > Bars.HighPrices[index - 4])
{
    Chart.DrawRectangle(Bars.OpenTimes[index - 2].ToString(), Bars.OpenTimes[index - 2], Bars.HighPrices[index - 2], Bars.OpenTimes[index - 2].AddDays(1),
    Bars.ClosePrices[index - 2], Color.FromArgb(50, Color.Green)).IsFilled = true;
}

ต่างจากโซนแนวต้าน โซนแนวรับช่วยระบุพื้นที่ที่แนวโน้มขาลงอาจหยุดและกลับตัว โซนดังกล่าวอาจถูกตีความเป็นสัญญาณซื้อ

เขียนโค้ดที่วาดโซนแนวรับ

1
2
3
4
5
6
if (Bars.LowPrices[index - 2] < Bars.LowPrices[index - 1] && Bars.LowPrices[index - 2] < Bars.LowPrices[index] &&
Bars.LowPrices[index - 2] < Bars.LowPrices[index - 3] && Bars.LowPrices[index - 2] < Bars.LowPrices[index - 4])
{
    Chart.DrawRectangle(Bars.OpenTimes[index - 2].ToString(), Bars.OpenTimes[index - 2], Bars.ClosePrices[index - 2], Bars.OpenTimes[index - 2].AddDays(1),
    Bars.LowPrices[index - 2], Color.FromArgb(50, Color.Red)).IsFilled = true;
}

คุณสามารถคัดลอกโค้ดทั้งหมดด้านล่างนี้:

 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
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
    public class SupportResistanceExample : Indicator
    {
        protected override void Initialize()
        {

        }

        public override void Calculate(int index)
        {
            if (Bars.HighPrices[index - 2] > Bars.HighPrices[index - 1] && Bars.HighPrices[index - 2] > Bars.HighPrices[index] &&
            Bars.HighPrices[index - 2] > Bars.HighPrices[index - 3] && Bars.HighPrices[index - 2] > Bars.HighPrices[index - 4])
            {
                Chart.DrawRectangle(Bars.OpenTimes[index - 2].ToString(), Bars.OpenTimes[index - 2], Bars.HighPrices[index - 2], Bars.OpenTimes[index - 2].AddDays(1),
                Bars.ClosePrices[index - 2], Color.FromArgb(50, Color.Green)).IsFilled = true;
            }

            if (Bars.LowPrices[index - 2] < Bars.LowPrices[index - 1] && Bars.LowPrices[index - 2] < Bars.LowPrices[index] &&
            Bars.LowPrices[index - 2] < Bars.LowPrices[index - 3] && Bars.LowPrices[index - 2] < Bars.LowPrices[index - 4])
            {
                Chart.DrawRectangle(Bars.OpenTimes[index - 2].ToString(), Bars.OpenTimes[index - 2], Bars.ClosePrices[index - 2], Bars.OpenTimes[index - 2].AddDays(1),
                Bars.LowPrices[index - 2], Color.FromArgb(50, Color.Red)).IsFilled = true;
            }
        }
    }
}

สร้างอินดิเคเตอร์และเพิ่มอินสแตนซ์ลงในกราฟ คุณควรเห็นโซนแนวรับและแนวต้านบนกราฟ

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