Problems with SymbolSelect()...

 

Hi,

I'm having a few issues with the SymbolSelect() function which I hope you can help me with. From my understanding of things, it's not possible to get any information on a symbol that's not the chart symbol, unless that symbol has been added to the MarketWatch window (is this correct? Please let me know if that's not the case...). So I use the SymbolSelect() function to add the Symbol to the MarketWatch window and then proceed to obtain information on that symbol. For instance: 

SymbolSelect(symbol, true);

double bidPrice = NormalizeDouble(SymbolInfoDouble(symbol, SYMBOL_BID), _Digits);

double halfSpread = NormalizeDouble((SymbolInfoInteger(symbol, SYMBOL_SPREAD)/2.0)*_Point, _Digits);

 

However, when I run this code for the first time, the variables bidPrice and halfSpread return 0. If I run this piece of code a second time (i.e. the Symbol has now been added to the MarketWatch window), the two variables return their proper respective values.

I suspect this is a bit of a timing issue. Once SymbolSelect() completes, MetaTrader appears to need a brief pause in the code before being able to provide data on the selected symbol. However, I'm loath to put a sleep() statement in to force this pause so I'm wondering whether this a bug that needs to be sorted or should I be approaching this a different way?

Thanks in advance for your help with this. 


Documentation on MQL5: Market Info / SymbolSelect
Documentation on MQL5: Market Info / SymbolSelect
  • www.mql5.com
Market Info / SymbolSelect - Documentation on MQL5
 
Oh, and I'm currently using build 742 of MetaTrader.
 

My first guess is that Symbol is not synchronize, but when I check with SymbolIsSynchronized, it keep return false for the next ticks. It only start to return true when I open the symbol chart. 

I agree that this is a timing issue, but not like what you think it is, more like the tick timing issue.

Try this : open only EURUSD chart and attach the code on it and look for GBPUSD data. When the first tick come - the GBPUSD is added to MarketWatch window with blank field. When the second tick come, the GBPUSD data is shown.

Remember this ticks that execute the code are belong to EURUSD and not GBPUSD, and MT never ask for GBPUSD data before. So there may be new EURUSD tick, but the GBPUSD still have the old tick.

I can only guess (again) that MQ deliberately create it this way so we have the latest data.


So you may want to do this in OnInit() or wait for the next tick to get the data.

BTW, speaking about your code, never ever NormalizeDouble price. Why don't you print (with 10 digits or something after decimal point) the original price and the NormalizeDouble one. Some brokers are very sensitive about this that the only thing that save our OrderSend is big slippage :(.

 

 
phi.nuts:

My first guess is that Symbol is not synchronize, but when I check with SymbolIsSynchronized, it keep return false for the next ticks. It only start to return true when I open the symbol chart. 

I agree that this is a timing issue, but not like what you think it is, more like the tick timing issue.

Try this : open only EURUSD chart and attach the code on it and look for GBPUSD data. When the first tick come - the GBPUSD is added to MarketWatch window with blank field. When the second tick come, the GBPUSD data is shown.

Remember this ticks that execute the code are belong to EURUSD and not GBPUSD, and MT never ask for GBPUSD data before. So there may be new EURUSD tick, but the GBPUSD still have the old tick.

I can only guess (again) that MQ deliberately create it this way so we have the latest data.


So you may want to do this in OnInit() or wait for the next tick to get the data.

BTW, speaking about your code, never ever NormalizeDouble price. Why don't you print (with 10 digits or something after decimal point) the original price and the NormalizeDouble one. Some brokers are very sensitive about this that the only thing that save our OrderSend is big slippage :(.

 

Hi,

Thanks again for your prompt answers - much appreciated.

And very interesting to know the details of how some of these things work. It may indeed be a better idea to add any required Symbols to the MarketWatch window in OnInit, as you described.  At least then everything is initialised before it's required.

Re: your comments on NormalizeDouble() - there seems to be a great deal of confusion out there as to when it should be used and when not.  I've read documents produced by MetQuotes stating that it should be used for OrderSend prices (e.g. https://www.mql5.com/en/articles/100 - there's a line in this article that reads "Also here, we used the NormalizeDouble function for the Bid price, the StopLoss and TakeProfit values, it is good practice to always normalize these prices to the number of digits of currency pair before sending it to the trade server." Another source is the MetaQutoes documentation for NormalizeDouble() (https://www.mql5.com/en/docs/convert/normalizedouble), which states "Calculated values of StopLoss, TakeProfit, and values of open prices for pending orders must be normalized with the accuracy, the value of which can be obtained by Digits()."). Other forum contributions however, state that it shouldn't be used.  Would it be possible for MetaQuotes to write a "once-and-for-all" article on the use of NormalizeDouble or even better, update the NormalizeDouble() documentation with a detailed article as when and when not to use it?

Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
  • 2010.06.09
  • Samuel
  • www.mql5.com
The Expert Advisors programming in MQL5 is simple, and you can learn it easy. In this step by step guide, you will see the basic steps required in writing a simple Expert Advisor based on a developed trading strategy. The structure of an Expert Advisor, the use of built-in technical indicators and trading functions, the details of the Debug mode and use of the Strategy Tester are presented.
 
cowil:

Hi,

Thanks again for your prompt answers - much appreciated.

And very interesting to know the details of how some of these things work. It may indeed be a better idea to add any required Symbols to the MarketWatch window in OnInit, as you described.  At least then everything is initialised before it's required.

Re: your comments on NormalizeDouble() - there seems to be a great deal of confusion out there as to when it should be used and when not.  I've read documents produced by MetQuotes stating that it should be used for OrderSend prices (e.g. https://www.mql5.com/en/articles/100 - there's a line in this article that reads "Also here, we used the NormalizeDouble function for the Bid price, the StopLoss and TakeProfit values, it is good practice to always normalize these prices to the number of digits of currency pair before sending it to the trade server." Another source is the MetaQutoes documentation for NormalizeDouble() (https://www.mql5.com/en/docs/convert/normalizedouble), which states "Calculated values of StopLoss, TakeProfit, and values of open prices for pending orders must be normalized with the accuracy, the value of which can be obtained by Digits()."). Other forum contributions however, state that it shouldn't be used.  Would it be possible for MetaQuotes to write a "once-and-for-all" article on the use of NormalizeDouble or even better, update the NormalizeDouble() documentation with a detailed article as when and when not to use it?

Yes, it's possible.

I'm full aware of what documentation said of NormalizeDouble, Lets take a look at other things, does MetaQuotes make a suggestion where to install MetaTrader 4 or 5 for Windows Vista/7/8 ? No they don't. However from experience, many user make a suggestion to install MetaTrader outside C:\Program Files\... to avoid UAC issues.

From experience, some user never use NormalizeDouble. Like I've said some brokers are sensitive about this.

 
phi.nuts:

Yes, it's possible.

I'm full aware of what documentation said of NormalizeDouble, Lets take a look at other things, does MetaQuotes make a suggestion where to install MetaTrader 4 or 5 for Windows Vista/7/8 ? No they don't. However from experience, many user make a suggestion to install MetaTrader outside C:\Program Files\... to avoid UAC issues.

From experience, some user never use NormalizeDouble. Like I've said some brokers are sensitive about this.

Thanks again for you answers above - much appreciated. I suppose for me, the NormalizeDouble() issue is slightly different to your example above re: the installation directory, in that MetaQuotes recommends installing it in the ...\Program Files\... directory but leaves the final decision up to the user - i.e. the functionality of MetaTrader is not affected by which directory you happen to install it in. I suppose the confusion for me (and a lot of other people) comes from the actual wording in the documentation for NormalizeDouble() - for instance, the NormalizeDouble() documentation states that prices MUST be normalized before submitting an order, which basically implies that the Expert won't work properly if prices aren't normalized. Then we have other people saying to use NormalizeDouble() sparingly and NOT to use it for normalizing prices before an order is placed, as this can cause problems with sensitive brokers. What is the average Expert programmer (most of which probably haven't programmed much before getting into programming Experts) to believe?

Anyway, I appreciate what you're saying and will modify my Expert accordingly.

 
cowil:

Thanks again for you answers above - much appreciated. I suppose for me, the NormalizeDouble() issue is slightly different to your example above re: the installation directory, in that MetaQuotes recommends installing it in the ...\Program Files\... directory but leaves the final decision up to the user - i.e. the functionality of MetaTrader is not affected by which directory you happen to install it in. I suppose the confusion for me (and a lot of other people) comes from the actual wording in the documentation for NormalizeDouble() - for instance, the NormalizeDouble() documentation states that prices MUST be normalized before submitting an order, which basically implies that the Expert won't work properly if prices aren't normalized. Then we have other people saying to use NormalizeDouble() sparingly and NOT to use it for normalizing prices before an order is placed, as this can cause problems with sensitive brokers. What is the average Expert programmer (most of which probably haven't programmed much before getting into programming Experts) to believe?

Anyway, I appreciate what you're saying and will modify my Expert accordingly.

1. Actually, for Vista/7/8, because UAC issue, the functionality of MetaTrader is indeed affected by which directory where it is installed.

2. It always bad idea to NormalizeDouble value of double or float, because their value is not the same with the original anymore. 

Reason: