ประเภทพารามิเตอร์ที่ปรับแต่งได้ เนื่องจาก C# เป็นภาษาที่มีการกำหนดประเภทข้อมูลอย่างเข้มงวด จึงจำเป็นต้องระบุประเภทข้อมูลเมื่อประกาศตัวแปรและคุณสมบัติของคลาสใน cBot อินดิเคเตอร์ และปลั๊กอิน ในทางตรงกันข้าม Python มีการกำหนดประเภทข้อมูลแบบไดนามิก ดังนั้นประเภทของตัวแปรจึงถูกอนุมานโดยอัตโนมัติ อย่างไรก็ตาม เมื่อพัฒนา cBot โดยใช้ cTrader API ประเภทเชิงแนวคิดเดียวกันจะถูกเก็บรักษาไว้เพื่อความสอดคล้องระหว่างทั้งสองภาษา
cTrader Algo API ไม่อนุญาตให้ใช้ชนิดข้อมูลทั้งหมดเป็นพารามิเตอร์ที่ปรับแต่งได้ และนี่คือเหตุผลที่นักพัฒนาอัลกอริทึมจำเป็นต้องเข้าใจและนำทางผ่านชนิดข้อมูลที่รองรับอย่างระมัดระวัง
หมายเหตุ
Python cBots, อินดิเคเตอร์ และปลั๊กอินใช้พารามิเตอร์ที่ปรับแต่งได้ซึ่งประกาศในไฟล์ .cs ของพวกเขา
กรณีการใช้งานพารามิเตอร์และ UI cTrader รองรับเฉพาะประเภทพารามิเตอร์เหล่านี้ โดยมีกรณีการใช้งานหลักและองค์ประกอบ UI ที่เกี่ยวข้องแสดงในตารางด้านล่าง
C# Python กรณีการใช้งาน องค์ประกอบ UI int int ปริมาณคำสั่ง, จำนวนแท่ง, จำนวนช่วงเวลา ฯลฯ ฟิลด์ป้อนตัวเลข (พร้อมตัวเพิ่ม/ลด) double float ค่าราคา, ปริมาณคำสั่ง ฯลฯ ฟิลด์ป้อนตัวเลข (พร้อมตัวเพิ่ม/ลด) string str ข้อความที่กำหนดเอง, ป้ายกำกับตำแหน่ง ฯลฯ ฟิลด์ป้อนข้อความ bool bool กลไกการป้องกัน, อนุญาตการเทรด, อนุญาตอีเมล ฯลฯ เมนูแบบเลื่อนลง ใช่/ไม่ DataSeries api . DataSeries แหล่งที่มาของราคาตลาด ฯลฯ รายการแบบเลื่อนลง TimeFrame api . TimeFrame กรอบเวลาที่เลือก ฯลฯ ตัวเลือกช่วงเวลา enum Enum การจัดตำแหน่งการวาดบนกราฟ, ระดับความเสี่ยงส่วนบุคคล ฯลฯ รายการแบบเลื่อนลง Color Color การวาดบนกราฟ, สีของเครื่องมือวิเคราะห์ทางเทคนิค, องค์ประกอบที่กำหนดเอง ฯลฯ ตัวเลือกสี DateTime DateTime รับวันที่และเวลาที่มีการกำหนดประเภทอย่างเข้มงวดในโซนเวลาของอัลกอ ตัวเลือกวันที่และเวลา DateOnly DateOnly รับวันที่ที่มีการกำหนดประเภทอย่างเข้มงวด ตัวเลือกวันที่ TimeSpan TimeSpan รับช่วงเวลาหรือเวลาของวันที่มีการกำหนดประเภทอย่างเข้มงวด ตัวเลือกเวลา Symbol Symbol รับสัญลักษณ์เดี่ยวที่มีการกำหนดประเภทอย่างเข้มงวด ตัวเลือกสัญลักษณ์ Symbol [] Symbol [] รับสัญลักษณ์หลายตัวที่มีการกำหนดประเภทอย่างเข้มงวดในอาร์เรย์ ตัวเลือกสัญลักษณ์หลายตัว Enum [] Enum [] รับค่าหลายค่าของประเภท Enum ที่มีการกำหนดประเภทอย่างเข้มงวดในอาร์เรย์ ตัวเลือกค่า Enum หลายค่า TimeFrame [] TimeFrame [] รับค่า TimeFrame หลายค่าที่มีการกำหนดประเภทอย่างเข้มงวดในอาร์เรย์ ตัวเลือกช่วงเวลาหลายช่วง
คำเตือน
คุณอาจไม่สามารถใช้ประเภทพารามิเตอร์บางอย่างข้างต้นได้หากคุณใช้ cTrader หรือ Algo API เวอร์ชันเก่า
ตัวอย่างเช่น UI ของ cTrader แสดงประเภท bool, double, int ของ C# และประเภท bool, float, int ของ Python ดังต่อไปนี้
ตัวอย่างสามตัวอย่างถัดไปแสดงประเภทข้อมูล DataSeries, enum แบบกำหนดเอง, string ของ C# และ api.DataSeries, Enum, str ของ Python (ซึ่งเราให้โค้ดเต็มไว้ในคู่มือนี้ด้วย)
ดังที่แสดงด้านล่าง ประเภทพารามิเตอร์ Color ของ C# และ Color ของ Python แสดงด้วยตัวเลือกสี
สุดท้าย UI ของข้อมูล TimeFrame ของ C# และ api.TimeFrame ของ Python สะท้อนตัวเลือกช่วงเวลาที่มีอยู่ในกราฟการเทรดภายในแอปพลิเคชัน Trade
ตัวอย่าง cBot ป้ายกำกับตำแหน่งเป็นพารามิเตอร์ string ของ C# และ str ของ Python ใน cBot ต่อไปนี้
ประเภทข้อมูล DataSeries, int, bool ของ C# และ api.DataSeries, int, bool ของ Python แสดงตัวอย่างในอัลกอริทึมด้านล่าง
C# Python
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 using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using cAlgo.API ;
using cAlgo.API.Collections ;
using cAlgo.API.Indicators ;
using cAlgo.API.Internals ;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SamplecBotReferenceSMA : Robot
{
[Parameter("Source")]
public DataSeries Source { get ; set ; }
[Parameter("SMA Period", DefaultValue = 14)]
public int SmaPeriod { get ; set ; }
[Parameter("Enable Trade", DefaultValue = true)]
public bool EnableTrade { get ; set ; }
private SimpleMovingAverage sma ;
protected override void OnStart ()
{
sma = Indicators . SimpleMovingAverage ( Source , SmaPeriod );
}
protected override void OnTick ()
{
double currentSMA = sma . Result . LastValue ;
double currentPrice = Symbol . Bid ;
if ( EnableTrade )
{
if ( currentPrice > currentSMA )
{
ExecuteMarketOrder ( TradeType . Buy , Symbol , 1000 , "Buy Order" );
}
else if ( currentPrice < currentSMA )
{
ExecuteMarketOrder ( TradeType . Sell , Symbol , 1000 , "Sell Order" );
}
}
Print ( "Current Price: {0}, Current SMA: {1}" , currentPrice , currentSMA );
}
}
}
หมายเหตุ
Python cBots ใช้พารามิเตอร์ที่ปรับแต่งได้ซึ่งประกาศในไฟล์ .cs ของพวกเขา
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import clr
clr . AddReference ( "cAlgo.API" )
from cAlgo.API import *
from robot_wrapper import *
class SamplecBotReferenceSMA ():
def on_start ( self ):
self . sma = api . Indicators . SimpleMovingAverage ( api . Source , api . SmaPeriod )
def on_tick ( self ):
currentSMA = self . sma . Result . LastValue
currentPrice = api . Symbol . Bid
if api . EnableTrade :
if currentPrice > currentSMA :
api . ExecuteMarketOrder ( TradeType . Buy , api . Symbol , 1000 , "Buy Order" )
elif currentPrice < currentSMA :
api . ExecuteMarketOrder ( TradeType . Sell , api . Symbol , 1000 , "Sell Order" )
api . Print ( "Current Price: {0} , Current SMA: {1} " . format ( currentPrice , currentSMA ))
cBot นี้ใช้ Color เป็นพารามิเตอร์เพื่อกำหนดการแสดงผลข้อความในพื้นที่กราฟ:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using cAlgo.API ;
using cAlgo.API.Collections ;
using cAlgo.API.Indicators ;
using cAlgo.API.Internals ;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
[Parameter(DefaultValue = "Yellow")]
public Color TextColor { get ; set ; }
protected override void OnBar ()
{
Chart . DrawStaticText ( "static" , "cBot running!" , VerticalAlignment . Center , HorizontalAlignment . Left , TextColor );
}
}
}
ในตัวอย่างด้านล่าง ประเภทข้อมูล double ของ C# และ float ของ Python ทำหน้าที่เป็นพารามิเตอร์เพื่อป้อนปริมาณคำสั่งในล็อต cBot ดำเนินการคำสั่งซื้อ Market หลังจากมีแท่งสีแดงสามแท่งติดต่อกัน
ในตัวอย่างด้านล่าง ประเภทข้อมูล DateTime, DateOnly และ TimeSpan ทำหน้าที่เป็นพารามิเตอร์
C# Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 using System ;
using cAlgo.API ;
namespace cAlgo.Robots ;
[Robot(AccessRights = AccessRights.None)]
public class TimeParameters : Robot
{
[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 OnStart ()
{
Print ( $"DateTimeParameter: {DateTimeParameter:o}" );
Print ( $"DateOnlyParameter: {DateOnlyParameter:o}" );
Print ( $"TimeSpanParameter: {TimeSpanParameter}" );
}
}
หมายเหตุ
Python cBots ใช้พารามิเตอร์ที่ปรับแต่งได้ซึ่งประกาศในไฟล์ .cs ของพวกเขา
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 using System ;
using cAlgo.API ;
namespace cAlgo.Robots ;
[Robot(AccessRights = AccessRights.None)]
public class TimeParameters : Robot
{
[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 ; }
}
ใช้พารามิเตอร์ใน Python cBot:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import clr
clr . AddReference ( "cAlgo.API" )
# Import cAlgo API types
from cAlgo.API import *
# Import trading wrapper functions
from robot_wrapper import *
class TimeParameters ():
def on_start ( self ):
print ( f "DateTimeParameter: { api . DateTimeParameter } " )
print ( f "DateOnlyParameter: { api . DateOnlyParameter } " )
print ( f "TimeSpanParameter: { api . TimeSpanParameter } " )
หมายเหตุ
ค่าพารามิเตอร์ DateTime จะถูกแปลงโดยอัตโนมัติจากโซนเวลาของแพลตฟอร์มของผู้ใช้เป็นโซนเวลาของอัลกอ ซึ่งช่วยลดความจำเป็นในการแปลงด้วยตนเอง
ในตัวอย่างด้านล่าง ประเภทข้อมูล Symbol ทำหน้าที่เป็นพารามิเตอร์
ในตัวอย่างด้านล่าง เราใช้ประเภทพารามิเตอร์ Enum, Symbol และ TimeFrame แบบหลายค่า ซึ่งแสดงด้วยประเภทอาร์เรย์ของ C#
C# Python
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 using System ;
using cAlgo.API ;
using cAlgo.API.Internals ;
using System.Linq ;
namespace cAlgo.Robots ;
[Robot(AccessRights = AccessRights.None)]
public class MultiValueParametersTest : Robot
{
[Parameter(DefaultValue = "EURUSD,GBPUSD")]
public Symbol [] SymbolsParameter { get ; set ; }
[Parameter(DefaultValue = "Sell")]
public TradeType [] TradeTypesParameter { get ; set ; }
[Parameter(DefaultValue = "Daily,Hour")]
public TimeFrame [] TimeFramesParameter { get ; set ; }
protected override void OnStart ()
{
Print ( $"Selected symbols are: {string.Join(" , ", SymbolsParameter.Select(symbol => symbol.Name))}" );
Print ( $"Selected trade types are: {string.Join(" , ", TradeTypesParameter.Select(tradeType => tradeType.ToString()))}" );
Print ( $"Selected time frames are: {string.Join(" , ", TimeFramesParameter.Select(timeFrame => timeFrame.ToString()))}" );
}
}
หมายเหตุ
Python cBots ใช้พารามิเตอร์ที่ปรับแต่งได้ซึ่งประกาศในไฟล์ .cs ของพวกเขา
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 using System ;
using cAlgo.API ;
using cAlgo.API.Internals ;
namespace cAlgo.Robots ;
[Robot(AccessRights = AccessRights.None)]
public class MultiValueParametersTest : Robot
{
[Parameter(DefaultValue = "EURUSD,GBPUSD")]
public Symbol [] SymbolsParameter { get ; set ; }
[Parameter(DefaultValue = "Sell")]
public TradeType [] TradeTypesParameter { get ; set ; }
[Parameter(DefaultValue = "Daily,Hour")]
public TimeFrame [] TimeFramesParameter { get ; set ; }
}
ใช้พารามิเตอร์ใน Python cBot:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import clr
clr . AddReference ( "cAlgo.API" )
# Import cAlgo API types
from cAlgo.API import *
# Import trading wrapper functions
from robot_wrapper import *
class MultiValueParametersTest ():
def on_start ( self ):
print ( f "Selected symbols are: { [ symbol . Name for symbol in api . SymbolsParameter ] } " )
print ( f "Selected trade types are: { [ str ( tradeType ) for tradeType in api . TradeTypesParameter ] } " )
print ( f "Selected time frames are: { [ str ( timeFrame ) for timeFrame in api . TimeFramesParameter ] } " )
ตัวอย่างอินดิเคเตอร์ โค้ดอินดิเคเตอร์ต่อไปนี้แสดงให้เห็นถึงวิธีการใช้พารามิเตอร์ TimeFrame:
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 using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using cAlgo.API ;
using cAlgo.API.Collections ;
using cAlgo.API.Indicators ;
using cAlgo.API.Internals ;
namespace cAlgo
{
[Indicator(AccessRights = AccessRights.None)]
public class NewIndicator4 : Indicator
{
private Bars _hourlyTimeFrameBars ;
private Bars _targetTimeFrameBars ;
[Parameter("Chosen Time Frame")]
public TimeFrame TargetTimeFrame { get ; set ; }
[Output("Main")]
public IndicatorDataSeries Result { get ; set ; }
protected override void Initialize ()
{
_hourlyTimeFrameBars = MarketData . GetBars ( TimeFrame . Hour );
_targetTimeFrameBars = MarketData . GetBars ( TargetTimeFrame );
}
public override void Calculate ( int index )
{
Result [ index ] = _hourlyTimeFrameBars . HighPrices [ index ] - _targetTimeFrameBars . HighPrices [ index ];
}
}
}
มีอินดิเคเตอร์แบบสนุกๆ (ทดสอบตาบอดสี) ที่มีตัวเลือกการมองเห็นสีแบบ enum (เช่น ปกติ ตาบอดสี และโทนสีเทา) สำหรับผู้ใช้ในการกำหนดสีของเส้นแนวนอนที่วาดบนกราฟ
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 using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using cAlgo.API ;
using cAlgo.API.Collections ;
using cAlgo.API.Indicators ;
using cAlgo.API.Internals ;
namespace cAlgo
{
public enum ColorVision
{
Normal ,
Colorblind ,
Greyscale
}
[Indicator(AccessRights = AccessRights.None)]
public class ColorblindTest : Indicator
{
[Parameter("Color Vision", DefaultValue = ColorVision.Normal)]
public ColorVision ColorVision { get ; set ; }
public override void Calculate ( int index ) {}
protected override void Initialize ()
{
Color lineColor = Color . Green ;
switch ( ColorVision )
{
case ColorVision . Normal :
lineColor = Color . Red ;
break ;
case ColorVision . Colorblind :
lineColor = Color . Yellow ;
break ;
case ColorVision . Greyscale :
lineColor = Color . White ;
break ;
}
var trendLine = Chart . DrawHorizontalLine ( "line" , Bars . HighPrices . Maximum ( 10 ), lineColor );
}
}
}
สรุปได้ว่า การเลือกประเภทข้อมูลที่ถูกต้องสำหรับตัวแปรที่ประกาศและคุณสมบัติของคลาส จะช่วยให้คุณสามารถสร้าง cBot และอินดิเคเตอร์ที่สามารถจัดการกับงานที่ไม่ใช่มาตรฐานได้