วิธีทำให้กลยุทธ์การเทรดด้วยตนเองเป็นอัตโนมัติ
การเทรดอัตโนมัติหรือการเทรดด้วยอัลกอริทึมมีข้อได้เปรียบหลายประการเหนือการเทรดด้วยตนเอง:
- ระบบอัตโนมัติกำจัดการตัดสินใจทางอารมณ์และดำเนินการเทรดด้วยความแม่นยำ ความเร็ว และประสิทธิภาพที่เพิ่มขึ้น
- อัลกอริทึมใช้กลยุทธ์การเทรดขั้นสูงอย่างสม่ำเสมอในขณะที่มาตรการจัดการความเสี่ยงถูกบังคับใช้อย่างน่าเชื่อถือ
- อัลกอริทึมช่วยให้สามารถทดสอบย้อนหลังกลยุทธ์โดยใช้ข้อมูลในอดีตและกระจายความเสี่ยงในหลายสินทรัพย์และตลาดพร้อมกัน
API ของ cTrader Algo ช่วยให้นักเทรดสามารถทำให้กลยุทธ์ที่ซับซ้อนเป็นอัตโนมัติ ช่วยให้พวกเขาสามารถใช้ประโยชน์จากข้อได้เปรียบมากมายของการเทรดด้วยอัลกอริทึม ในบทความนี้และวิดีโอที่เกี่ยวข้อง คุณจะได้เรียนรู้วิธีทำให้กลยุทธ์ที่ซับซ้อนเป็นอัตโนมัติและเปลี่ยนลำดับของการดำเนินการให้เป็นอัลกอริทึม
ระบุรูปแบบกลยุทธ์ด้วยตนเอง
ในการพัฒนาอัลกอริทึม สมมติว่าเรากำลังเทรดด้วยตนเองโดยใช้รูปแบบ hammer สำหรับการเข้าซื้อและรูปแบบ hanging man สำหรับการเข้าขาย
Hammer เกิดขึ้นในช่วงแนวโน้มขาลงและบ่งชี้ถึงการกลับตัวขาขึ้นที่อาจเกิดขึ้น รูปแบบ hammer ใช้เพื่อระบุจุดเข้าซื้อ

Hanging man ปรากฏในช่วงแนวโน้มขาขึ้นและบ่งชี้ถึงการกลับตัวขาลงที่อาจเกิดขึ้น รูปแบบ hanging man ใช้เพื่อระบุจุดเข้าขาย

พัฒนา cBot เพื่อเทรดกลยุทธ์ด้วยตนเอง
ใน cTrader Algo เราจะสร้างอัลกอริทึมที่ใช้กลยุทธ์ที่อธิบายไว้ในส่วนก่อนหน้านี้
คลิกปุ่ม New ภายใต้แท็บ cBot พิมพ์ Patterns Strategy ในช่องชื่อและคลิก Create

ในเมธอด OnBarClosed() เราใช้ตรรกะสำหรับกลยุทธ์ของเรา การเข้าของเราต้องเป็นไปตามเงื่อนไขสองข้อนี้:
- ราคาปิดตรงกับราคาสูงสุด
- ขนาดแท่งเทียนใหญ่กว่าตัวแท่งเทียนอย่างน้อย 5 เท่า
เราเริ่มด้วยการกำหนดพารามิเตอร์ Volume, StopLoss และ TakeProfit
| [Parameter(DefaultValue = 1000)]
public double Volume { get; set; }
[Parameter(DefaultValue = 10)]
public double StopLoss { get; set; }
[Parameter(DefaultValue = 10)]
public double TakeProfit { get; set; }
|
จากนั้นเราเขียนส่วนย่อยเพื่อดำเนินการเทรดซื้อเมื่อเงื่อนไขที่กำหนดเป็นจริง
| if (Bars.Last(0).Close == Bars.Last(0).High &&
(Bars.Last(0).Close - Bars.Last(0).Open) < (Bars.Last(0).Close - Bars.Last(0).Low) * 0.2)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, InstanceId, StopLoss, TakeProfit);
}
|
| bar = api.Bars.Last(0)
if bar.Close == bar.High and (bar.Close - bar.Open) < (bar.Close - bar.Low) * 0.2:
api.ExecuteMarketOrder(TradeType.Buy, api.SymbolName, api.Volume, api.InstanceId, api.StopLoss, api.TakeProfit)
|
ในทางตรงกันข้าม นี่คือเงื่อนไขที่ต้องเป็นจริงสำหรับการเข้าขายของเรา:
- ราคาปิดตรงกับราคาต่ำสุด
- ขนาดแท่งเทียนใหญ่กว่าตัวแท่งเทียนอย่างน้อย 5 เท่า
ในทำนองเดียวกัน เราเขียนส่วนย่อยเพื่อดำเนินการเทรดขายเมื่อเงื่อนไขที่กำหนดเป็นจริง
| if (Bars.Last(0).Close == Bars.Last(0).Low &&
(Bars.Last(0).Open - Bars.Last(0).Close) < (Bars.Last(0).High - Bars.Last(0).Close) * 0.2)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, InstanceId, StopLoss, TakeProfit);
}
|
| if bar.Close == bar.Low and (bar.Open - bar.Close) < (bar.High - bar.Close) * 0.2:
api.ExecuteMarketOrder(TradeType.Sell, api.SymbolName, api.Volume, api.InstanceId, api.StopLoss, api.TakeProfit)
|
คุณสามารถคัดลอกโค้ดทั้งหมดด้านล่างนี้:
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 | using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
public class PatternsStrategy : Robot
{
[Parameter(DefaultValue = 1000)]
public double Volume { get; set; }
[Parameter(DefaultValue = 10)]
public double StopLoss { get; set; }
[Parameter(DefaultValue = 10)]
public double TakeProfit { get; set; }
protected override void OnStart()
{
}
protected override void OnBarClosed()
{
if (Bars.Last(0).Close == Bars.Last(0).High &&
(Bars.Last(0).Close - Bars.Last(0).Open) < (Bars.Last(0).Close - Bars.Last(0).Low) * 0.2)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, InstanceId, StopLoss, TakeProfit);
}
if (Bars.Last(0).Close == Bars.Last(0).Low &&
(Bars.Last(0).Open - Bars.Last(0).Close) < (Bars.Last(0).High - Bars.Last(0).Close) * 0.2)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, InstanceId, StopLoss, TakeProfit);
}
}
protected override void OnStop()
{
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | import clr
clr.AddReference("cAlgo.API")
from cAlgo.API import *
from robot_wrapper import *
class PatternsStrategy():
def on_start(self):
pass
def on_bar_closed(self):
bar = api.Bars.Last(0)
if bar.Close == bar.High and (bar.Close - bar.Open) < (bar.Close - bar.Low) * 0.2:
api.ExecuteMarketOrder(TradeType.Buy, api.SymbolName, api.Volume, api.InstanceId, api.StopLoss, api.TakeProfit)
if bar.Close == bar.Low and (bar.Open - bar.Close) < (bar.High - bar.Close) * 0.2:
api.ExecuteMarketOrder(TradeType.Sell, api.SymbolName, api.Volume, api.InstanceId, api.StopLoss, api.TakeProfit)
def on_stop(self):
pass
|
ในการสร้าง cBot กด Ctrl+B หรือคลิก Build
ในการทดสอบย้อนหลัง cBot เราเพิ่มอินสแตนซ์ก่อน เลือกตัวเลือก Locally ระบุพารามิเตอร์ที่คุณต้องการ แล้วคลิกปุ่ม Add instance

จากนั้นเราไปที่แท็บ Backtesting ระบุช่วงเวลาสำหรับการทดสอบย้อนหลังและเริ่มการทดสอบย้อนหลัง
เมื่อกระบวนการทดสอบย้อนหลังเสร็จสิ้น เราสามารถตรวจสอบการเทรดได้ คุณจะเห็นว่าเงื่อนไขการเข้าเป็นจริงก่อนที่จะมีการเข้าเทรดแต่ละครั้ง
ด้วยความสำเร็จในการทำให้กลยุทธ์ของเราเป็นอัตโนมัติ เราสามารถใช้ cBot เพื่อทำการเทรดแทนเราได้
ใช้กลยุทธ์การกลับตัวของ RSI
ในตัวอย่างที่สองของเรา เราต้องการใช้กลยุทธ์ที่อิงกับการกลับตัวของตัวบ่งชี้ Relative Strength Index (RSI) ในกลยุทธ์นี้ เราเข้าตำแหน่งโดยคาดหวังว่า RSI จะกลับทิศทางและปฏิบัติตามกฎเหล่านี้:
- เมื่อค่า RSI เคลื่อนที่ต่ำกว่าเกณฑ์การซื้อ RSI เราเข้าตำแหน่งซื้อ
- เมื่อค่า RSI เคลื่อนที่สูงกว่าเกณฑ์การขาย RSI เราเข้าตำแหน่งขาย
มาสร้าง cBot ใหม่ ป้อน RSI Reversal Strategy เป็นชื่อและคลิก Create

เมื่อตัวแก้ไขโค้ดปรากฏขึ้น ให้เพิ่ม System.Linq ที่ด้านบนของโค้ดเป็นการอ้างอิง
มาเพิ่มพารามิเตอร์สองตัวที่จะช่วยให้เราแก้ไขเกณฑ์ก่อนที่เราจะรัน cBot
| [Parameter(DefaultValue = 30)]
public int BuyLevel { get; set; }
[Parameter(DefaultValue = 70)]
public int SellLevel { get; set; }
|
เราประกาศและเริ่มต้นตัวบ่งชี้ความแข็งแกร่งสัมพัทธ์ของเรา
จากนั้นเราใช้เงื่อนไขที่กำหนดตรรกะการเทรดของเรา
คุณสามารถคัดลอกโค้ดทั้งหมดด้านล่างนี้:
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 | using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
public class RSIReversalStrategy : Robot
{
[Parameter(DefaultValue = 30)]
public int BuyLevel { get; set; }
[Parameter(DefaultValue = 70)]
public int SellLevel { get; set; }
private RelativeStrengthIndex _rsi;
protected override void OnStart()
{
_rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 14);
}
protected override void OnBarClosed()
{
if (_rsi.Result.LastValue < BuyLevel)
{
if (Positions.Count == 0)
ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000);
foreach (var position in Positions.Where(p => p.TradeType == TradeType.Sell))
{
position.Close();
}
}
else if (_rsi.Result.LastValue > SellLevel)
{
if (Positions.Count == 0)
ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000);
foreach (var position in Positions.Where(p => p.TradeType == TradeType.Buy))
{
position.Close();
}
}
}
protected override void OnStop()
{
}
}
}
|
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 | import clr
clr.AddReference("cAlgo.API")
from cAlgo.API import *
from robot_wrapper import *
class RSIReversalStrategy():
def on_start(self):
self._rsi = api.Indicators.RelativeStrengthIndex(api.Bars.ClosePrices, 14)
def on_bar_closed(self):
rsi_value = self._rsi.Result.LastValue
if rsi_value < api.BuyLevel:
if api.Positions.Count == 0:
api.ExecuteMarketOrder(TradeType.Buy, api.SymbolName, 1000)
for position in api.Positions:
if position.TradeType == TradeType.Sell:
position.Close()
elif rsi_value > api.SellLevel:
if api.Positions.Count == 0:
api.ExecuteMarketOrder(TradeType.Sell, api.SymbolName, 1000)
for position in api.Positions:
if position.TradeType == TradeType.Buy:
position.Close()
def on_stop(self):
pass
|
มาสร้างกลยุทธ์ของเรา เพิ่มอินสแตนซ์ และทดสอบย้อนหลัง เหมือนกับที่เราทำกับกลยุทธ์ก่อนหน้านี้
เราสามารถตรวจสอบและทบทวนการเทรดบางรายการอีกครั้งเพื่อดูว่าเงื่อนไขการเข้าเป็นจริงก่อนที่จะมีการเข้าเทรด
บทความนี้ได้แสดงวิธีระบุกลยุทธ์ด้วยตนเองและแปลงให้เป็นกลยุทธ์อัตโนมัติ ซึ่งช่วยให้สามารถดำเนินการเทรดด้วย Algo ได้
