Page 1 of 1

How to get currency conversion rates in API

Posted: Mon Oct 10, 2016 9:47 am
by adamhartley
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

Re: How to get currency conversion rates in API

Posted: Tue Oct 11, 2016 9:48 am
by FX Helper
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;

Re: How to get currency conversion rates in API

Posted: Thu Oct 13, 2016 11:37 am
by adamhartley
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?