Currency Converter¶
In algo 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 cTrader.
Currency Conversion in One Minute!
- The
Convert (double value, Asset from, Asset to
andConvert(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 newAssetConverter
property of theAlgo
class! - The
Asset
class includes its ownConvert(Asset to, double value)
andConvert(string to, double value)
methods that can be easily invoked from anyAsset
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¶
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 thefrom
asset to theto
asset.double Convert(double value, string from, string to)
. Converts the specified value from the asset the name of which matches thefrom
string to the asset the name of which matches theto
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 |
|
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 theto
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 theto
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 |
|
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.
Currency Conversion in Backtesting¶
cTrader 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 optimisation 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/optimisation results are historically accurate.