วิธีสร้างอินดิเคเตอร์จุดหมุนและ Fractal
อินดิเคเตอร์และกลยุทธ์หลายอย่างอาศัยวัตถุบนกราฟเพื่อแสดงข้อมูลสำคัญ และ cTrader Algo มีวิธีการ API ที่จำเป็นสำหรับการวาดวัตถุดังกล่าว จุดหมุนและ Fractal โดยเฉพาะจะถูกวาดบนกราฟการเทรดเพื่อช่วยระบุระดับราคาที่สำคัญและจุดเปลี่ยน
ในบทความนี้และวิดีโอที่เกี่ยวข้อง คุณจะได้เรียนรู้วิธีการวาดจุดหมุนและ Fractal บนกราฟ
สร้างอินดิเคเตอร์จุดหมุน
จุดหมุนคือระดับราคาที่คำนวณจากราคาก่อนหน้า จุดดังกล่าวบ่งบอกถึงพื้นที่ที่มีศักยภาพของแนวรับหรือแนวต้าน
เราจะสร้างอินดิเคเตอร์ที่วาดจุดหมุนรายวันบนกราฟ
ใน cTrader Algo ไปที่แท็บ Indicators และคลิก New พิมพ์ชื่อสำหรับอินดิเคเตอร์ใหม่แล้วคลิกปุ่ม Create

แก้ไขอินดิเคเตอร์ในตัวแก้ไขโค้ดเพื่อทำให้เป็นอินดิเคเตอร์แบบซ้อนทับ
| [Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
|
กำหนดและรับแท่งรายวันในเมธอด Initialize()
| Bars _bars;
protected override void Initialize()
{
_bars = MarketData.GetBars(TimeFrame.Daily, SymbolName);
}
|
คำนวณระดับแนวรับและแนวต้านที่แตกต่างกันซึ่งเรียกว่าจุดหมุน ค่าเจ็ดค่าประกอบด้วยจุดหมุน ระดับแนวต้านสามระดับ และระดับแนวรับสามระดับ
| var pivot = (_bars.HighPrices.Last(1) + _bars.LowPrices.Last(1) + _bars.ClosePrices.Last(1)) / 3;
var r1 = 2 * pivot - _bars.LowPrices.Last(1);
var s1 = 2 * pivot - _bars.HighPrices.Last(1);
var r2 = pivot + _bars.HighPrices.Last(1) - _bars.LowPrices.Last(1);
var s2 = pivot - _bars.HighPrices.Last(1) + _bars.LowPrices.Last(1);
var r3 = _bars.HighPrices.Last(1) + 2 * (pivot - _bars.LowPrices.Last(1));
var s3 = _bars.LowPrices.Last(1) - 2 * (_bars.HighPrices.Last(1) - pivot);
|
เนื่องจากจุดหมุนได้ถูกคำนวณแล้ว จึงสามารถวาดบนกราฟได้โดยใช้เส้นแนวโน้ม
| Chart.DrawTrendLine("pivot ", _bars.OpenTimes.LastValue, pivot, _bars.OpenTimes.LastValue.AddDays(1), pivot, Color.White);
Chart.DrawTrendLine("r1 ", _bars.OpenTimes.LastValue, r1, _bars.OpenTimes.LastValue.AddDays(1), r1, Color.Green);
Chart.DrawTrendLine("r2 ", _bars.OpenTimes.LastValue, r2, _bars.OpenTimes.LastValue.AddDays(1), r2, Color.Green);
Chart.DrawTrendLine("r3 ", _bars.OpenTimes.LastValue, r3, _bars.OpenTimes.LastValue.AddDays(1), r3, Color.Green);
Chart.DrawTrendLine("s1 ", _bars.OpenTimes.LastValue, s1, _bars.OpenTimes.LastValue.AddDays(1), s1, Color.Red);
Chart.DrawTrendLine("s2 ", _bars.OpenTimes.LastValue, s2, _bars.OpenTimes.LastValue.AddDays(1), s2, Color.Red);
Chart.DrawTrendLine("s3 ", _bars.OpenTimes.LastValue, s3, _bars.OpenTimes.LastValue.AddDays(1), s3, Color.Red);
|
คุณสามารถคัดลอกโค้ดทั้งหมดด้านล่างนี้:
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 | 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 PivotPointsExample : Indicator
{
Bars _bars;
protected override void Initialize()
{
_bars = MarketData.GetBars(TimeFrame.Daily, SymbolName);
}
public override void Calculate(int index)
{
var pivot = (_bars.HighPrices.Last(1) + _bars.LowPrices.Last(1) + _bars.ClosePrices.Last(1)) / 3;
var r1 = 2 * pivot - _bars.LowPrices.Last(1);
var s1 = 2 * pivot - _bars.HighPrices.Last(1);
var r2 = pivot + _bars.HighPrices.Last(1) - _bars.LowPrices.Last(1);
var s2 = pivot - _bars.HighPrices.Last(1) + _bars.LowPrices.Last(1);
var r3 = _bars.HighPrices.Last(1) + 2 * (pivot - _bars.LowPrices.Last(1));
var s3 = _bars.LowPrices.Last(1) - 2 * (_bars.HighPrices.Last(1) - pivot);
Chart.DrawTrendLine("pivot ", _bars.OpenTimes.LastValue, pivot, _bars.OpenTimes.LastValue.AddDays(1), pivot, Color.White);
Chart.DrawTrendLine("r1 ", _bars.OpenTimes.LastValue, r1, _bars.OpenTimes.LastValue.AddDays(1), r1, Color.Green);
Chart.DrawTrendLine("r2 ", _bars.OpenTimes.LastValue, r2, _bars.OpenTimes.LastValue.AddDays(1), r2, Color.Green);
Chart.DrawTrendLine("r3 ", _bars.OpenTimes.LastValue, r3, _bars.OpenTimes.LastValue.AddDays(1), r3, Color.Green);
Chart.DrawTrendLine("s1 ", _bars.OpenTimes.LastValue, s1, _bars.OpenTimes.LastValue.AddDays(1), s1, Color.Red);
Chart.DrawTrendLine("s2 ", _bars.OpenTimes.LastValue, s2, _bars.OpenTimes.LastValue.AddDays(1), s2, Color.Red);
Chart.DrawTrendLine("s3 ", _bars.OpenTimes.LastValue, s3, _bars.OpenTimes.LastValue.AddDays(1), s3, Color.Red);
}
}
}
|
กด Ctrl+B หรือคลิก Build จากนั้นเพิ่มอินดิเคเตอร์ลงในกราฟโดยคลิก Add instance
คุณควรเห็นจุดหมุนที่วาดบนกราฟ

สร้างอินดิเคเตอร์ Fractal
เราจะพัฒนาอินดิเคเตอร์แยกต่างหากที่พล็อต Fractal บนกราฟ ทำตามขั้นตอนจากส่วนก่อนหน้าและสร้างอินดิเคเตอร์อีกตัวด้วยชื่อใหม่

ทำให้อินดิเคเตอร์นี้เป็นอินดิเคเตอร์แบบซ้อนทับ
| [Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
|
วาดลูกศรเหนือแท่งที่ค่าสูงสุดสูงกว่าแท่งที่อยู่ติดกันสองแท่งในแต่ละด้านในเมธอด Calculate()
| 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.DrawIcon(Bars.OpenTimes[index].ToString(), ChartIconType.DownArrow, Bars.OpenTimes[index - 2], Bars.HighPrices[index - 2], Color.Red);
}
|
วาดลูกศรขึ้นใต้แท่งที่ค่าต่ำสุดต่ำกว่าแท่งที่อยู่ติดกันสองแท่งในแต่ละด้าน
| 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.DrawIcon(Bars.OpenTimes[index].ToString(), ChartIconType.UpArrow, Bars.OpenTimes[index - 2], Bars.LowPrices[index - 2], Color.Green);
}
|
คุณสามารถคัดลอกโค้ดทั้งหมดด้านล่างนี้:
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 | 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 FractalsExample : 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.DrawIcon(Bars.OpenTimes[index].ToString(), ChartIconType.DownArrow, Bars.OpenTimes[index - 2], Bars.HighPrices[index - 2], Color.Red);
}
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.DrawIcon(Bars.OpenTimes[index].ToString(), ChartIconType.UpArrow, Bars.OpenTimes[index - 2], Bars.LowPrices[index - 2], Color.Green);
}
}
}
}
|
สร้างอินดิเคเตอร์และเพิ่มอินสแตนซ์ลงในกราฟ
คุณควรเห็นวัตถุ Fractal ที่แสดงบนแท่งเทียนที่เกี่ยวข้องบนกราฟ

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