How to get currency conversion rates in API

How to create strategies and indicators
Message
Author
adamhartley
Posts: 5
Joined: Mon Oct 10, 2016 8:41 am

How to get currency conversion rates in API

#1 Postby adamhartley » Mon Oct 10, 2016 9:47 am

In ForexTester what is the currency conversion rate that is used in converting profits in foreign currency back to USD as I want to be able to access this rate from within the API? For example, with my USD account if I am trading GBPJPY then my profits will be in JPY - what rate is used to convert these back into USD? Does it use the relevant USDJPY rate from within the database if it's available (in which case I can just make a call to get this rate via the iClose function) or does it use some other rate and if so then what? What happens if you don't have any USDJPY data in your database, what rate does it use then?

Adam

FX Helper
Posts: 1477
Joined: Mon Apr 01, 2013 3:55 am

Re: How to get currency conversion rates in API

#2 Postby FX Helper » Tue Oct 11, 2016 9:48 am

Hello,

Here is the code that our programmer used in one of his tools, I hope, it will work for you.

function GetSymbolType(): TSymbolType;
begin
result:= st_Other;
end;

// cost of 1 secondary currency unit in USD
function GetSecCurCostInUSD(symbolName: ANSIString): double;
var baseCur: ANSIString;
secondCur: ANSIString;
info: PCurrencyInfo;
begin
baseCur := copy(symbolName, 1, 3);
secondCur := copy(symbolName, 4, 3);

if GetCurrencyInfo(secondCur+'USD', info) then
begin
Result := iClose(secondCur+'USD',1,0);
exit;
end else
if GetCurrencyInfo('USD'+secondCur, info) then
begin
Result := 1/(iClose('USD'+secondCur,1,0));
exit;
end else
if GetCurrencyInfo(baseCur+'USD', info) then
begin
result := iClose(baseCur+'USD', 1, 0) / iClose(symbolName, 1, 0);
exit;
end else
if GetCurrencyInfo('USD'+baseCur, info) then
begin
result := (1/iClose('USD'+baseCur, 1, 0))/iClose(symbolName, 1, 0);
exit;
end;

result := 0;
Print('GetCostInUSD: not enough information');
end;

//calculates point cost for 1 lot order
function GetPointCostUSD: double;
var
info: PCurrencyInfo;
symbolType: TSymbolType;
begin
symbolType := GetSymbolType();
if GetCurrencyInfo(symbol, info) then
begin
case symbolType of
st_Normal: result := info.lot * info.Point;
st_InvertedUSD: result := info.lot * info.Point/iClose(symbol, 1, 0);
st_Other: result := GetSecCurCostInUSD(symbol)*info.lot*info.Point;
end;
end;
end;

adamhartley
Posts: 5
Joined: Mon Oct 10, 2016 8:41 am

Re: How to get currency conversion rates in API

#3 Postby adamhartley » Thu Oct 13, 2016 11:37 am

Thanks for that. I'm inferring from the code there that the answer to my question is that the conversion rates do indeed come from the existing database of rates. I'm still keen to know what rate is used if the relevant currency doesn't exist. For example, if trading GBPJPY and the user doesn't have any USDJPY data loaded then what rate is used to convert the JPY P&L back to USD?


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 13 guests