Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 106

 
-Aleks-:

Please advise on a solution to the rounding problem!

I need to get rid of the decimal point without a remainder - rounding and exponentiation do not solve the whole problem - what should I do?

For example it was 1.44430 and I need 144430.

Part of the code - as is

NormalizeDouble(Low[1],Digits)*MathPow(10,(Digits+1)*1-1)

Simply divide by _Point.
 
Alexey Viktorov:
Just divide by _Point.

Wrong result prints, the original is 161188 (1.61188) your method 161187 print gives 1.6119 (why rounding up does it do when print Low[1] if five decimal places?), my version is 161188.

But if we complicate the problem.


long Calc=
NormalizeDouble(Close[1],Digits)*MathPow(10,(Digits+1)*3-1)+
NormalizeDouble(High[1],Digits)*MathPow(10,(Digits+1)*2-1)+
NormalizeDouble(Low[1],Digits)*MathPow(10,(Digits+1)*1-1);

That is, the last part of the number 161184 - ie divergence of 4 units.

Your Variant of this expression produces the same value

long CalcX=
NormalizeDouble(Close[1],Digits)*MathPow(10,(Digits+1)*3-1)+
NormalizeDouble(High[1],Digits)*MathPow(10,(Digits+1)*2-1)+
Low[1]/Point;

Any idea what the error is and how to fix it?

 
-Aleks-:

Wrong result prints, the original is 161188 (1.61188) your method 161187 print gives 1.6119 (why rounding up does it do when print Low[1] if five decimal places?), my version is 161188.

But if we complicate the problem.


long Calc=
NormalizeDouble(Close[1],Digits)*MathPow(10,(Digits+1)*3-1)+
NormalizeDouble(High[1],Digits)*MathPow(10,(Digits+1)*2-1)+
NormalizeDouble(Low[1],Digits)*MathPow(10,(Digits+1)*1-1);

That is, the last part of the number 161184 - ie divergence of 4 units.

Your variant in this expression gives the same value

long CalcX=
NormalizeDouble(Close[1],Digits)*MathPow(10,(Digits+1)*3-1)+
NormalizeDouble(High[1],Digits)*MathPow(10,(Digits+1)*2-1)+
Low[1]/Point;

Any idea what the error is and how to fix it?

Run it like this.

/********************Script program start function*******************/
void OnStart()
{
   string i = DoubleToString(SymbolInfoDouble(_Symbol, SYMBOL_BID)/_Point, 0);
   Print(i);
   Print(SymbolInfoDouble(_Symbol, SYMBOL_BID)/_Point);
}/*******************************************************************/
 
Alexey Viktorov:
Run it like this.

/********************Script program start function*******************/
void OnStart()
{
   string i = DoubleToString(SymbolInfoDouble(_Symbol, SYMBOL_BID)/_Point, 0);
   Print(i);
   Print(SymbolInfoDouble(_Symbol, SYMBOL_BID)/_Point);
}/*******************************************************************/

Yes, the string variable gets the correct number (preliminarily), but zeros 161188.00000000 are added, how do I get rid of them?

 
-Aleks-:

Yes, the string variable gets the correct number (preliminarily), but zeros 161188.00000000 are added, how do I get rid of them?

There are no zeros in my code. Look at the way it's written.
 
Alexey Viktorov:
There are no zeros in my code. Look how it is written.

Thank you - I missed a zero.

This is the construction.

string CalcX=
DoubleToString(Close[1]/_Point,0)+
DoubleToString(High[1]/_Point,0)+
DoubleToString(Low[1]/_Point,0);
Print("CalcX=",CalcX);

Now I need to decompose these numbers back into their components.

When I try to convert the string to a number, I again get the wrong number 161184 instead of 161188

long testX = StringToDouble(CalcX);
Print("testX=",testX);

I should probably cut the string, but how can I do it optimally?


 
-Aleks-:

Yes, the string variable gets the correct number (preliminarily), but zeros 161188.00000000 are added, how do I get rid of them?

Parse the string using the "." delimiter
 
Artyom Trishkin:
So... show me the pattern. How do you know what's in there?
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Can you tell me how to implement an indicator so that the second coordinate time in the trend line is set by the breakthrough time or its touch. That is, the trend line has its start in a certain place, and the time of the second coordinate is set at discretion, but the line should end where it intersects with a candle. As for example in the indicator shown in the picture
 
-Aleks-:

Thank you - I missed the zero.

It came up with this construction

string CalcX=
DoubleToString(Close[1]/_Point,0)+
DoubleToString(High[1]/_Point,0)+
DoubleToString(Low[1]/_Point,0);
Print("CalcX=",CalcX);

Now I need to decompose these numbers back to their components.

When I try to convert the string to a number, I again get wrong number 161184 instead of 161188

long testX = StringToDouble(CalcX);
Print("testX=",testX);

I should probably cut the string, but how can I do it optimally?


There is no need to cut anything. The conversion to string is just to see the right number. You don't have to do anything for calculations.

Well, if you have an overwhelming desire to make a mess, convert the string back to a number StringToDouble() and multiply by _Point with normalization to the desired number of digits, probably _Digits