Parameter Types As C# is a strongly-typed language, it is necessary to specify data types when declaring variables and class properties in cBots and indicators. The API does not support all data types for parameters, which is why it is essential for algorithm developers to navigate across them.
Parameter Use Cases and UI cTrader supports only these parameter types with the key use cases and related UI elements reflected in the below table.
Data Types Use Cases UI int
Order volume, number of bars, number of periods, etc. Number input field (with stepper) double
Price value, order volume, etc. Number input field (with stepper) string
Custom message, position label, etc. Text input field bool
Protection mechanisms, allow trading, allow email, etc. Dropdown list Yes/No DataSeries
Source of market prices, etc. Dropdown list TimeFrame
Chosen time frame, etc. Timeframe selector enum
Chart drawings alignment, individual risk levels, etc. Dropdown list Color
Chart drawings, colour of technical analysis means, custom elements, etc. Colour picker
For example, the UI of cTrader reflects bool
, double
, and int
parameters as follows.
The next three examples stand for DataSeries
, custom enum
data (for which we also provide the full code in this guide), and string
data.
As shown below, the Color
parameter type is represented by a colour picker.
Finally, the UI of TimeFrame
data duplicates the timeframe of trading charts in the 'Trade ' application.
cBot Examples The position label is a string
parameter in the following cBot.
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 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 NewcBot2 : Robot
{
[Parameter(DefaultValue = "my label")]
public string PositionLabel { get ; set ; }
protected override void OnStart ()
{
ExecuteMarketOrder ( TradeType . Buy , "XAGUSD" , 1000 , PositionLabel );
}
protected override void OnStop ()
{
var positionToFind = Positions . Find ( PositionLabel );
positionToFind . Close ();
}
}
}
DataSeries
, int
, and bool
data are exemplified in the algorithm below.
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 );
}
}
}
This cBot uses Color
data as a parameter to define the text visualisation in the chart area.
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 );
}
}
}
In the below example, double
data type serves as a parameter to input the order volume in lots. The cBot executes a buy market order after three consecutive red bars.
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
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class RedBarsBot : Robot
{
[Parameter("Order Volume (Lots)", DefaultValue = 0.1)]
public double OrderVolume { get ; set ; }
private int redBarsCount = 0 ;
protected override void OnBar ()
{
if ( IsRedBar ())
{
redBarsCount ++ ;
if ( redBarsCount == 3 )
{
PlaceMarketOrder ( TradeType . Buy );
redBarsCount = 0 ;
}
}
else
{
redBarsCount = 0 ;
}
}
private bool IsRedBar ()
{
var currentBar = MarketSeries . Close . Last ( 1 );
var previousBar = MarketSeries . Close . Last ( 2 );
return currentBar < previousBar ;
}
private void PlaceMarketOrder ( TradeType tradeType )
{
var symbol = Symbol ;
var volume = Symbol . QuantityToVolume ( OrderVolume );
ExecuteMarketOrder ( tradeType , symbol , volume );
}
}
}
Indicator Examples The following indicator code illustrates how TimeFrame
parameters are used.
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 ];
}
}
}
There is a playful ‘colour-blind test’ indicator, which offers enum
colour vision options (i.e., normal, colour-blind, and greyscale) for users to determine the colour of a horizontal line drawn on the chart.
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 );
}
}
}
To summarise, by choosing the correct data type for the declared variables and class properties, you will be able to create cBots and indicators that would cope with even non-standard tasks.