Here is another version of the code with corrections as per Alain:
//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+voidOnStart()
{
conststring symbol = "AUDCHF"; //-symbol of interest//-MQL5 default method for calculating TICK_VALUE:constdouble expected = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE);
//-my method to calculate TICK_VALUE:constdouble result = GetTickValue(symbol);
Print(symbol, " tickvalue expected: ", expected, " result: ", result);
if(MathAbs(result - expected) > 1e-08)
Print(symbol, " test failed!");
elsePrint(symbol, " test pass OK");
}
//+------------------------------------------------------------------+//output:/*
2021.04.18 19:09:25.257 TickValue (XAUUSD,M5) AUDCHF tickvalue expected: 1.086921078660479 result: 1.086921078660478
2021.04.18 19:09:25.257 TickValue (XAUUSD,M5) AUDCHF test pass OK
*///+------------------------------------------------------------------+//| Calculate the value of one tick per lot in the account currency. |//+------------------------------------------------------------------+double GetTickValue(string symbol)
{
conststring prof = SymbolInfoString(symbol, SYMBOL_CURRENCY_PROFIT);
conststring acc = AccountInfoString(ACCOUNT_CURRENCY); //-account currency//--- TickValue (in account currency) = price delta * number of units per lot * profit/account exchange rateconstdouble tick_value =
SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE)
* SymbolInfoDouble(symbol, SYMBOL_TRADE_CONTRACT_SIZE)
* CrossRate(prof, acc);
if(tick_value == 0)
{
Print(__FUNCTION__, ": Error, cannot get tick value for ", symbol);
}
return (tick_value);
}
//+------------------------------------------------------------------+//| Get the cross-rate for the profit/account symbol |//+------------------------------------------------------------------+double CrossRate(string curr_prof, string curr_acc)
{
//--- compare case-insensitiveif(StringCompare(curr_prof, curr_acc, false) == 0)
{
return (1.0);
}
//--- symbol suffix of the current chart symbolstring symbol_suffix = StringSubstr(Symbol(), 6, StringLen(Symbol())-6);
//---string symbol = curr_prof + curr_acc + symbol_suffix;
if(CheckMarketWatch(symbol))
{
//--- if symbol is in the market watchdouble cross_rate = SymbolInfoDouble(symbol, SYMBOL_BID);
if(cross_rate != 0.0)
{
return (cross_rate);
}
}
//--- Try the inverse symbol
symbol = curr_acc + curr_prof + symbol_suffix;
if(CheckMarketWatch(symbol))
{
//--- if symbol is in the market watchdouble cross_rate = SymbolInfoDouble(symbol, SYMBOL_ASK);
if(cross_rate != 0.0)
{
return (1 / cross_rate);
}
}
//---Print(__FUNCTION__, ": Error, cannot get cross rate for ", curr_prof + curr_acc);
return (0.0);
}
//+------------------------------------------------------------------+//| Checks if symbol is selected in the MarketWatch |//| and adds symbol to the MarketWatch, if necessary |//+------------------------------------------------------------------+bool CheckMarketWatch(string symbol)
{
//--- check if symbol is selected in the MarketWatchif(!SymbolInfoInteger(symbol,SYMBOL_SELECT))
{
if(GetLastError()==ERR_MARKET_UNKNOWN_SYMBOL)
{
//printf(__FUNCTION__+": Unknown symbol '%s'",symbol);return(false);
}
if(!SymbolSelect(symbol,true))
{
printf(__FUNCTION__+": Error adding symbol %d",GetLastError());
return(false);
}
}
//--- succeedreturn(true);
}
//+------------------------------------------------------------------+
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Try this code to calculate the tick_value:
There are several bugs :
1st run (EURCHF not in Market watch)
2021.04.18 12:10:52.579 367457 (AUDCHF,H1) CheckMarketWatch: Unknown symbol 'CHFEUR'
2021.04.18 12:10:52.579 367457 (AUDCHF,H1) CrossRate: Error, cannot get cross rate for CHFEUR
2021.04.18 12:10:52.579 367457 (AUDCHF,H1) GetTickValue: Error, cannot get tick value for AUDCHF
2021.04.18 12:10:52.579 367457 (AUDCHF,H1) AUDCHF tickvalue expected: 0.9070328392101118 result: 0.0
2021.04.18 12:10:52.579 367457 (AUDCHF,H1) AUDCHF test failed! Account currency : EUR Tick size : 1e-05 Contract size : 100000.0 Rate : 0.0
2nd run
2021.04.18 12:11:03.888 367457 (AUDCHF,H1) CheckMarketWatch: Unknown symbol 'CHFEUR'
2021.04.18 12:11:03.888 367457 (AUDCHF,H1) AUDCHF tickvalue expected: 0.9070706154474127 result: 0.9073092835885896
2021.04.18 12:11:03.888 367457 (AUDCHF,H1) AUDCHF test failed! Account currency : EUR Tick size : 1e-05 Contract size : 100000.0 Rate : 0.9073092835885896
Clue : sometimes you need to use Ask and not Bid price.
And the above code is potentially a bug.
Here is another version of the code with corrections as per Alain:
By the way:
https://www.mql5.com/en/forum/365781/
By the way:
https://www.mql5.com/en/forum/365781/
0 answers.
Try this code to calculate the tick_value: