3.4 The same robot in cTrader
In cTrader the robot is called a cBot, and the language is C#. Here is the same strategy – shorter and, for many, easier to read.
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class MaCross : Robot
{
[Parameter("Fast EMA", DefaultValue = 10)]
public int FastPeriod { get; set; }
[Parameter("Slow EMA", DefaultValue = 30)]
public int SlowPeriod { get; set; }
[Parameter("Size (lots)", DefaultValue = 0.1)]
public double Lots { get; set; }
[Parameter("Stop loss (pips)", DefaultValue = 30)]
public double StopLossPips { get; set; }
[Parameter("Take profit (pips)", DefaultValue = 60)]
public double TakeProfitPips { get; set; }
private MovingAverage _fast, _slow;
private const string Label = "MaCross";
protected override void OnStart()
{
_fast = Indicators.MovingAverage(Bars.ClosePrices, FastPeriod, MovingAverageType.Exponential);
_slow = Indicators.MovingAverage(Bars.ClosePrices, SlowPeriod, MovingAverageType.Exponential);
}
protected override void OnBar()
{
double fast1 = _fast.Result.Last(1), fast2 = _fast.Result.Last(2);
double slow1 = _slow.Result.Last(1), slow2 = _slow.Result.Last(2);
if (Positions.FindAll(Label, SymbolName).Length > 0) return;
double volume = Symbol.QuantityToVolumeInUnits(Lots);
if (fast2 <= slow2 && fast1 > slow1)
ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, Label, StopLossPips, TakeProfitPips);
else if (fast2 >= slow2 && fast1 < slow1)
ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, Label, StopLossPips, TakeProfitPips);
}
}
}What is simpler in cTrader
- OnBar is called automatically on a new candle – no separate check.
- Stop loss and take profit are given in pips directly in the order.
- Label works like the magic number, and
Positions.FindAll(Label, SymbolName)finds the robot’s own positions. - Volume is given in units.
Symbol.QuantityToVolumeInUnits(0.1)converts 0.1 lot into the right number of units.
Build and start
- Open Algo in the menu on the left in cTrader, and choose the cBots tab.
- Click New, type MaCross, choose C# and From scratch, and click Create.
- Delete the template, paste in the code and press Build (Ctrl+B).
- Click Add instance, choose EUR/USD and 1 hour, and press Start.
AccessRights.None
AccessRights.None means the robot gets no access to files, the internet or other programs on the computer. That is safest. Be sceptical of robots from others that ask for full access (FullAccess).
Try it yourself
Paste the cTrader version into cTrader and run a backtest. Is the result similar to MetaTrader?
Key takeaways
- In cTrader the robot is a cBot written in C#.
- OnBar is called automatically, and the stop loss and take profit are given in pips.
- Label is the equivalent of the magic number in MetaTrader.
How much do you remember from “Build your first robot”?
Recalling what you have read is one of the most effective ways to learn. Pick the answer you think is right – you get the explanation straight away.
1What is the magic number used for?
In cTrader, Label is used for the same thing.
2Which price do you use when the robot buys?
You buy at the ask and sell at the bid.
65% of retail investor accounts lose money when trading CFDs with this provider.