カスタマイズ可能なパラメータータイプ C#は強く型付けされた言語であるため、cBot、インジケーター、プラグインで変数やクラスプロパティを宣言する際にデータ型を指定する必要があります。 対照的に、Pythonは動的に型付けされるため、変数の型は自動的に推論されます。 ただし、cTrader APIを使用してcBotを開発する場合、両言語間の一貫性を保つために同じ概念的な型が保持されます。
cTrader Algo APIでは、すべてのデータ型をカスタマイズ可能なパラメーターとして使用することはできません。そのため、アルゴリズム開発者はサポートされている型を慎重に理解し、操作することが重要です。
注意
PythonのcBot、インジケーター、およびプラグインは、.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の古いバージョンを使用している場合、上記のパラメータータイプの一部を使用できない場合があります。
例えば、cTrader UIはC#のbool、double、intおよびPythonのbool、float、intタイプを以下のように反映します。
次の3つの例は、C#のDataSeries、カスタムenum、stringおよびPythonのapi.DataSeries、Enum、strデータタイプを示しています(このガイドでは完全なコードも提供しています)。
以下に示すように、C#のColorおよびPythonのColorパラメータータイプはカラーピッカーで表現されます。
最後に、C#のTimeFrameおよびPythonのapi.TimeFrameデータのUIは、Trade アプリケーション内の取引チャートで利用可能な期間オプションを反映しています。
cBotの例 ポジションラベルは、以下のcBotでC#のstringおよびPythonのstrパラメーターです。
C#のDataSeries、int、boolおよびPythonのapi.DataSeries、int、boolデータタイプは、以下のアルゴリズムで例示されています。
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のcBotは、.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 );
}
}
}
以下の例では、C#のdoubleおよびPythonのfloatデータタイプが、ロット単位の注文数量を入力するためのパラメーターとして機能します。 cBotは、3つの連続した赤いバーの後に買いの成行注文を実行します。
以下の例では、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のcBotは、.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データタイプがパラメーターとして機能します。
以下の例では、C#の配列タイプで表現されるマルチEnum、SymbolおよびTimeFrameパラメータータイプを使用しています。
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のcBotは、.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やインジケーターを作成できるようになります。