Skip to content

Currency Converter

In automated trading, giving cBots and indicators the ability to autonomously convert currencies using current market rates is essential. In this API guide, we cover how this feature is implemented in the Automate API.

Below, we also summarize the key principles of currency conversion in the Automate API.

Currency Conversion in One Minute!

  • The Convert (double value, Asset from, Asset to and Convert(double value, string from, string to) methods return the exact conversion rate between the specified assets or the assets with the specified names. Access them via the new AssetConverter property of the Algo class!
  • The Asset class includes its own Convert(Asset to, double value) and Convert(string to, double value) methods that can be easily invoked from any Asset object.
  • When backtesting, cTrader will automatically use historical conversion rates! This means that you can always expect historically accurate results and fully evaluate the performance of your cBots.

How Currency Conversion Works in cTrader Automate

The IAssetConverter interface in the cAlgo.API.Internals namespace contains the Convert() method that has the following overloads.

  • double Convert(double value, Asset from, Asset to). Converts the specified value from the from asset to the to asset.
  • double Convert(double value, string from, string to). Converts the specified value from the asset the name of which matches the from string to the asset the name of which matches the to string.

The Algo class now includes the AssetConverter property of the IAssetConverter type. As a result, you can access the two methods above simply by typing the following.

1
AssetConverter.Convert(double value, Asset from, Asset to)

The Asset class also has its own Convert() methods with the following overloads.

  • double Convert(Asset to, double value). Converts the specified value from the asset from which this method is called to the to asset.
  • double Convert(string to, double value). Converts the specified value from the asset from which this method is called to the asset the name of which matches the to string.

Consider the below example in which our cBot converts GBP into JPY and then executes a new market order using the calculated value as the order volume.

 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
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 SampleConversionTestBot : Robot
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        protected override void OnStart()
        {

            Asset baseAssetForNewOrder = Assets.GetAsset("GBP");

            Asset quoteAssetForNewOrder = Assets.GetAsset("JPY");

            double newOrderVolume = baseAssetForNewOrder.Convert(quoteAssetForNewOrder, 1000.00);

            ExecuteMarketOrder(TradeType.Buy, SymbolName, newOrderVolume);

            Print($"New order has been executed with {newOrderVolume} as its volume");
        }


    }
}

If we take a look at the log, we can see that our cBot behaves as expected. At the time when the instance was started, the bid price for GBPJPY equaled 159.619.

How Currency Conversion Works in Backtesting

To avoid any confusion, the Automate API fully supports historical conversion rates in backtesting.

This means that, when you backtest a cBot that uses any of the methods described above, conversion calculations will be performed using historically accurate data rather than the current exchange rates between the specified assets. This feature ensures that the results of backtesting and optimization are as precise as possible given past market conditions.

Note that cTrader can build accurate conversion chains involving a possibly infinite number of trading instruments. As a result, even if your account deposit currency differs from the base and/or quote assets of the symbol on which you would like to perform a backtest, cTrader will handle all the necessary calculations to provide you with precise backtesting results.

In summary, conversion rates constitute another valuable tool for making sure that your cBots/indicators perform exactly as expected. As cTrader supports the use of historical data when converting assets in backtesting, you can be sure that all backtesting/optimization results are historically accurate.


Last update: January 30, 2023