Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 1089

 
Money_Maker:

Another question is how to cut off extra zeros?

to make it 0.2.
ps the number of digits after the decimal point will always be used differently

There are up to 8 fractional decimal digits, correct as you need.

// Переводит в строку курс
string KursS(double Ku) {
int k, i;
string S; // 8 дробных цифр для составного курса открытия позиции в MT5
S=DoubleToString(NormalizeDouble(Ku,8),8); // Уберем концевые нули
i=StringLen(S)-1;
if (StringSubstr(S,i,1)!="0") return(S); // Их нет
for (k=i-1;k>0;k--) if (StringSubstr(S,k,1)!="0") {S=StringSubstr(S,0,k+1); break;}
i=StringLen(S)-1; // Уберем концевую точку
if (StringSubstr(S,i,1)==".") S=StringSubstr(S,0,i);
return (S);
} // KursS
 
Money_Maker Thank you) Too bad you can't save the time(
Time can be saved in a horizontal line name
 
boing9267:    Good evening, when running the script there is an error in the logs: uninit reason 0 ... Can you please tell me what is wrong?

Reworked it like this - everything goes

void OnStart()
{
   ObjectCreate(ChartID(),"HLine",OBJ_HLINE,0,0,iHigh(NULL,PERIOD_W1,0));
   ObjectSetInteger(ChartID(),"HLine",OBJPROP_COLOR, Yellow); // Желтый виднее 
   ObjectSetInteger(ChartID(),"HLine",OBJPROP_STYLE,0);   // стиль отображения
   ObjectSetInteger(ChartID(),"HLine",OBJPROP_WIDTH,1);   // толщина линии 
   ObjectSetInteger(ChartID(),"HLine",OBJPROP_BACK,true); // на передний план 

   ObjectCreate(ChartID(),"LLine",OBJ_HLINE,0,0,iLow(NULL,PERIOD_W1,0));
   ObjectSetInteger(ChartID(),"LLine",OBJPROP_COLOR,Blue); 
   ObjectSetInteger(ChartID(),"LLine",OBJPROP_STYLE,0); 
   ObjectSetInteger(ChartID(),"LLine",OBJPROP_WIDTH,1); 
   ObjectSetInteger(ChartID(),"LLine",OBJPROP_BACK,true); 
}
 
Vlad143:    There are up to 8 fractional decimal digits here, correct as you wish.
// Переводит в строку курс    Вот так короче!!!  Строк меньше и в каждой строке прозрачнее
string Kurs(double Ku)
{
  string S="0";   int n=8;     // Количество знаков дробной части
  while(S[StringLen(S)-1]=='0'  && n>=0)
  {
    S=DoubleToString(Ku,n); // n дробных цифр
    n--;
  }
  return (S);
} // Kurs
 
LRA:


Shorter in the source code, longer in the execution. I'm all for fast execution. Run the comparison script, I got 6 seconds and 11 seconds. Blame the number to string operation, it's better to do it once.

#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

// Переводит в строку курс
string KursS(double Ku) {
int k, i;
string S; // 8 дробных цифр для составного курса открытия позиции в MT5
S=DoubleToString(NormalizeDouble(Ku,8),8); // Уберем концевые нули
i=StringLen(S)-1;
if (StringSubstr(S,i,1)!="0") return(S); // Их нет
for (k=i-1;k>0;k--) if (StringSubstr(S,k,1)!="0") {S=StringSubstr(S,0,k+1); break;}
i=StringLen(S)-1; // Уберем концевую точку
if (StringSubstr(S,i,1)==".") S=StringSubstr(S,0,i);
return (S);
} // KursS

// Переводит в строку курс    Вот так короче!!!  Строк меньше и в каждой строке прозрачнее
string Kurs(double Ku)
{
  string S="0";   int n=8;     // Количество знаков дробной части
  while(S[StringLen(S)-1]=='0'  && n>=0)
  {
    S=DoubleToString(Ku,n); // n дробных цифр
    n--;
  }
  return (S);
} // Kurs   

void OnStart() {
  string S;
  datetime TBeg,T1, T2;
  double x;
  int N,i;
  N=10000000; x=0.2;
  TBeg=TimeLocal();
  for (i=0;i<N;i++) S=KursS(x);
  T1=TimeLocal()-TBeg;
  TBeg=TimeLocal();
  for (i=0;i<N;i++) S=Kurs(x);
  T2=TimeLocal()-TBeg;
  Alert ("KursS -",(long)(T1),"сек   Kurs - ",(long)(T2),"сек");
  }
 
LRA:

Reworked it like this - it's going

How do you understand it's going? Are the two horizontal lines being created? There are no errors in my log but lines do not appear after the start of the script

It`s strange, though - it worked and created lines on AUDCAD, but not on AUDCHF and EURUSD...

I think I understand it... It does not appear on charts that already have horizontal lines. If I remove them and run the script, lines will appear. But why do they appear? Question.

 
LRA:
Re-creating a line or even another object with the same name is forbidden
No... I hand-drew those lines, they don't have names.
 

Any object has a name. Right-click on the graphic --> List of objects


 
LRA:
Time can be saved in the name of the horizontal line
Thank you, but the name is occupied by a comment , the trend line is better suited both prices are the same time the first time is 0 and the second is the right one ... this is probably the most reasonable
 
Vlad143:

There are up to 8 fractional decimal digits here, correct as you wish.



Thank you)
Reason: