Not able to replace accurate rates

 

Here is the image of the custom symbol that I am trying to create:  


As you can see the values coming are not the values in the DATA section on MT5.

I am trying the following code for that purpose:  

ticks_array[0].time= StructToTime(running);
      ticks_array[0].bid = StringToDouble(s[7]);
      ticks_array[0].ask = StringToDouble(s[8]);
      ticks_array[0].last = StringToDouble(s[5]);
      ticks_array[0].flags= TICK_FLAG_LAST;
      
      ticks_array[Should_change].time_msc = 0; 
      CustomTicksAdd(symbol_custom,ticks_array);
      
      rates_add[0].time = Time_For_Rates;
      rates_add[0].open = StringToDouble(s[2]);
      rates_add[0].high= StringToDouble(s[3]);
      rates_add[0].low = StringToDouble(s[4]);
      rates_add[0].close=StringToDouble(s[5]);
      rates_add[0].spread=(int)((StringToDouble(s[8])-StringToDouble(s[7]))*10000);
      rates_add[0].tick_volume=StringToInteger(s[6])-1;
      rates_add[0].real_volume=0;      
      Print(CustomRatesReplace(symbol_custom,Time_For_Rates,Time_For_Rates,rates_add));

Please let know why is this happening and how i can resolve it.

 
Here is the configuration of the symbol attached.
Files:
 
The reason is because you need to also round the number to the nearest tick for that symbol. 
double string_to_tick(const string number, string symbol=NULL)
{
   if(symbol == NULL)
      symbol = _Symbol;
   double step = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);
   return step * round(double(number) / step);
}

EDIT: I just noticed you said "Custom symbol", and in that case you will need to use normalize double and define the appropriate number of digits. 

rates_add[0].open = NormalizeDouble(double(s[2]), my_digits);
 
rates_add[0].high= StringToDouble(s[3],8);
 
Jozsef Bognar:

StringToDouble only takes a string param. You might as well not use it and use 

double(string_number)

instead.

 
nicholi shen:

StringToDouble only takes a string param. You might as well not use it and use 

instead.

I will try your suggestion dear sir.

 
nicholi shen:
The reason is because you need to also round the number to the nearest tick for that symbol. 

EDIT: I just noticed you said "Custom symbol", and in that case you will need to use normalize double and define the appropriate number of digits. 

But why my program is not assigning the exact values what it is receiving from the API? Is there any issues with the symbol configuration or is the fault of MQL5/MT5?

 
jaffer wilson:

But why my program is not assigning the exact values what it is receiving from the API? Is there any issues with the symbol configuration or is the fault of MQL5/MT5?

Your API, whatever it is, is converting the raw doubles to strings and is not strictly enforcing a specified number of digits. Therefore it is your responsibility as a programmer to do it on your end. 

You also need to make sure that you've specified the number of digits in the custom symbols settings. 
 
nicholi shen:

Your API, whatever it is, is converting the raw doubles to strings and is not strictly enforcing a specified number of digits. Therefore it is your responsibility as a programmer to do it on your end. 

You also need to make sure that you've specified the number of digits in the custom symbols settings. 

Yes sir I did it. You can see that in my configuration settings which I have attached with the comment just below the question.

Reason: