TICK_VALUE for non account currency pair - page 3

 
amrali:

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.

//--- symbol suffix of the current chart symbol
   string symbol_suffix = StringSubstr(Symbol(), 6, StringLen(Symbol())-6);

And the above code is potentially a bug.

 
Thanks Alain for pointing out those bugs. 

Here is another version of the code with corrections as per Alain:

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   const string symbol = "AUDCHF"; //-symbol of interest

//-MQL5 default method for calculating TICK_VALUE:
   const double expected = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE);

//-my method to calculate TICK_VALUE:
   const double result = GetTickValue(symbol);

   Print(symbol, " tickvalue expected: ", expected, " result: ", result);

   if(MathAbs(result - expected) > 1e-08)
      Print(symbol, " test failed!");
   else
      Print(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)
  {
   const string prof = SymbolInfoString(symbol, SYMBOL_CURRENCY_PROFIT);
   const string acc  = AccountInfoString(ACCOUNT_CURRENCY); //-account currency

//--- TickValue (in account currency) = price delta * number of units per lot * profit/account exchange rate
   const double 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-insensitive
   if(StringCompare(curr_prof, curr_acc, false) == 0)
     {
      return (1.0);
     }

//--- symbol suffix of the current chart symbol
   string 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 watch
      double 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 watch
      double 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 MarketWatch
   if(!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);
        }
     }
//--- succeed
   return(true);
  }
//+------------------------------------------------------------------+
TICK_VALUE for non account currency pair
TICK_VALUE for non account currency pair
  • 2021.04.18
  • www.mql5.com
I need to calculate tick value for currecy pair that does not include account currency, but getting different from defult SymbolInfoDouble(symbol...
 

By the way:

https://www.mql5.com/en/forum/365781/

TickValue with EURUSD includes exchange rate?
TickValue with EURUSD includes exchange rate?
  • 2021.03.26
  • www.mql5.com
Hi, The specifications in general are as shown in the picture. The focus should be on Profit-currency, which is USD...
 
Doerk Hilger:

By the way:

https://www.mql5.com/en/forum/365781/

0 answers.

How is that 0 answer ?
 
amrali #:

Try this code to calculate the tick_value:

This is excellent !!!!!!!!!!!! Thank you for your help !!!
Reason: