Docs menu

cTrader Pro

Import the C# cBot into cTrader Automate, build it yourself, run a backtest, and what differs from MetaTrader 5.

This feature is part of Pro.

cTrader exports a C# cBot (.cs) for cTrader Automate. It can export 106 of 119 parts. It does not export some instruments. Getting the source is part of Pro.

Build the cBot

  1. In the Builder toolbar, choose cTrader (C#).

    The export buttons in the Builder toolbar with cTrader (C#) selected
    cTrader (C#) selected in the Builder toolbar.
  2. Click Download .cs. The file is saved as EA_Builder.cs.

  3. In cTrader, open the Automate tab and create a new cBot.

  4. Replace the code of the new cBot with the whole content of the file.

  5. Click Build. The Builder does not compile it. The cBot is built on your computer.

The class is named EABuilderStrategy, in the namespace EABuilder. It is written for the cTrader Automate API (the cAlgo.API namespace).

How the cBot decides

OnBar runs when a new bar starts, so the bar that just closed is the latest closed bar. The decision uses only closed bars. It is sent as a market order, which the platform fills at the first tick of the new bar, about the next bar open.

Stop loss and take profit are set on the position right after the fill, as absolute prices that the server holds. Limit and stop orders can still fill inside a bar.

Gaps

The code does not model gaps. A market order is filled by the platform at the first tick of the new bar, at whatever price the platform has then. The price can differ from the previous close after a weekend or news gap, so the entry price and the result can differ from the Miner. When a bar opened beyond a stop, cTrader closed the position at the opening price in all 122 such trades of the comparison runs. The MetaTrader 5 tester closed at the stop level. The comparison runs behind Match check counted gap opens as one of the causes of a difference.

Inputs

SettingWhat it doesValues
TZThe offset in hours from UTC. Server time is the bar open time plus TZ.Number, default 0
LabelPrefixThe label on this cBot’s orders, so it only manages its own positions.Text, default EAB
LotSizeLot size, or the inputs of the money-management part. The cBot turns lots into volume with the symbol’s lot size, rounds down to the volume step and keeps it within the minimum and maximum.Number, the lot size of the strategy (0.01 if it has none)
One input per setting of a partFor example the periods of an indicator or the stop loss in pips.Set in the Builder, editable in cTrader

Run a backtest

  1. In Automate, select the cBot and open the Backtesting tab.
  2. Choose the symbol and timeframe named on the Checked under: line at the top of the file.
  3. Choose a date range and the data source, then start it.

Expect the numbers to differ from the Miner, because spread, commission and swap are what the cTrader account and the tester data charge. The code does not model them. The spread filter reads the live spread.

Results are an approximation of past data. They do not predict future results.

Differences from MetaTrader 5

TopicIn cTrader
Account typeA netting account merges same-side orders into one position and nets opposite orders. The MetaTrader 5 export assumes a hedging account. Test on the account type you will trade.
Trailing stop and break-evenEvaluated at the bar close. MetaTrader 5 does it on every tick.
OCO orderscTrader Automate has no OCO order that was found in its documentation. The second leg is cancelled at the next bar, and both legs can fill inside the same bar.
VolumeVolume series use tick volume, which may not match MetaTrader volumes.
Indicator start valuesEMA and RMA start values differ slightly from MetaTrader. The values converge.
Minimum stop distanceThe code does not adjust SL and TP to the broker’s limit.

Instruments that are not exported

cTrader does not export these instruments, because the platform decides their pip size and the Builder does not guess it. The export is refused with a message. MetaTrader 5 and MetaTrader 4 export them.

InstrumentSymbol in the Miner
GoldXAUUSD
SilverXAGUSD
WTI crude oilLIGHTCMDUSD
Brent crude oilBRENTCMDUSD
S&P 500 indexUSA500IDXUSD
Nasdaq 100 indexUSATECHIDXUSD
Dow Jones indexUSA30IDXUSD
DAX indexDEUIDXEUR
FTSE 100 indexGBRIDXGBP
Nikkei 225 indexJPNIDXJPY

Match status

The header of the .cs file reads Match with the platform tester: not verified, and no match mark is shown for cTrader exports today. Comparison runs on cTrader exist, and most of them matched. See Match check.

If the build fails

  • Check that you replaced the whole code of a new cBot and did not paste inside the old class.
  • Read the first message in the build output.
  • If the export was refused, no file was saved. See Blocks that can’t be exported.

Next steps