Features of the mql5 language, subtleties and tricks - page 83

 
fxsaber:

Read my answer again. Open the short source code, figure it out, and... Profit!

Of course I opened the source code, of course. Although I didn't specify that I need it all for MT5, but I'm looking for an idea...

Can you tell me, which line solves this problem (hidden idea) - inGetBarSize() function?

#property show_inputs

extern int Width = 1280;
extern int Height = 800;

#import "user32.dll"
  int GetClientRect( int hWnd, int lpRect[] );
#import

#define  BORDER_SIZE 44

int GetBarSize()
{
  int Rect[4], PrevBars, PrevRect, Res = 1;
  int hwnd = WindowHandle(Symbol(), Period());

  GetClientRect(hwnd, Rect);
  PrevRect = Rect[2] - BORDER_SIZE;
  PrevBars = WindowBarsPerChart();

  PrevBars += PrevBars;
    
  while(PrevBars <= PrevRect)
  {
    Res += Res;
    PrevBars += PrevBars;
  }
  
  return(Res);
}

string StrNumToLen( string Num, int Len )
{
  Len -= StringLen(Num);
  
  while (Len > 0)
  {
    Num = "0" + Num;
    Len--;
  }
  
  return(Num);
}

void start()
{
  int Pos = WindowFirstVisibleBar(), Step, i = 1;
  string Directory = Width + "x" + Height + "_" + Symbol() + Period() + " (" +
                     TimeToStr(Time[Pos], TIME_DATE) + "-" +
                     TimeToStr(Time[0], TIME_DATE) + ")/";

  Pos += 2;
  Step = 1 + (Width - BORDER_SIZE) / GetBarSize();
  
  while (Pos >= 0)
  {
    WindowScreenShot(Directory + StrNumToLen(i, 5) + ".gif", Width, Height, Pos);
    i++;
    Pos -= Step;
  }
  
  return;
}
 
Aleksey Vyazmikin:

Of course I opened the source code, of course. Although I didn't specify that I need it all for MT5, but I'm looking for an idea...

Can you tell me, in what line this problem is solved (hidden idea) - in functionGetBarSize()?

int GetBarSize( const ulong Chart = 0 )
{
  return(1 << (int)ChartGetInteger(Chart, CHART_SCALE));
}
 
fxsaber:

I feel like a fool, what does "<<" mean? And what does it have to do with the scale of the graph?

 
Aleksey Vyazmikin:

I feel like a fool, what does "<<" mean?

This is a bitwise shift to the left. The 1 (in binary representation 0000 0001) will be shifted to the left as many times as the value returned by ChartGetInteger. Basically, this is an addition to the power of 2.

The result will be the number of pixels per 1 bar.

 
Ihor Herasko:

This is a bitwise shift to the left. 1 (in binary representation 0000 0001) will be shifted to the left as many times as the value returned by ChartGetInteger. Basically, this is an addition to the power of 2.

The result will be the number of pixels per 1 bar.

Thanks for the clarification, I will try to implement it!

fxsaber:

Thank you!

 

Rosh, thanks a lot for the help!

Forum on trading, automated trading systems and trading strategies testing

Bugs, bugs, questions

Ilyas, 2016.08.24 11:08

The array ("inside") stores allocated - for how many elements the array is allocated.

The logic of working with allocated (conditional code):
ArrayResize(arr,int size,int reserve)
  {
   if(arr.allocated<size)
      if(!ArrayAllocateMemory(arr,size+reserve))  // -> arr.allocated=size+reserve;
         return(-1);
   //---
   CallConstructorsOrDestructors(arr,size);
   //---
   arr.size=size;
   return(size);
  }

Too bad it is not available at all (array wrappers is not an option). Maybe we should add some functionality?

int ArrayAllocated( const void&  array[] );

There is StringBufferLen for strings.

 
Comments not related to this topic have been moved to "Bugs, bugs, questions".
 
Comments not related to this topic have been moved to "Bugs, bugs, questions".
 
string Str = "123.456";

double Num1 = StringToDouble(Str);  
double Num2 = (double)Str; // Быстрее


Much faster this way (Release, not Debug)

double StringToDouble2( const string Str, const uint StartPos = 0 )
{
  const uint Size = StringLen(Str);
  
  bool Sign = false;  
  uint i = StartPos;
  
  while (i < Size)
  {
    const int Digit = Str[i];

    if ((Digit != ' ') && (Digit != '\t') && (Digit != '\n') && (Digit != '\r'))
    {
      if ((Sign = (Digit == '-')) || (Digit == '+'))
        i++;
      
      break;
    }
      
    i++;
  }

  long Res = 0;
  int point = 0;
  
  while (i < Size)
  {
    const int Digit = Str[i];
    
    if (!point && (Digit == '.'))
      point = 1;
    else if (((Digit >= '0') && (Digit <= '9')))
    {
      Res = Res * 10 + Digit - '0';
      
      if (point)
        point *= 10;
    }
    else
      break;
      
    i++;
  }
  
  if (Sign)
    Res = -Res;
  
  return((point > 1) ? Res / (double)point : Res); // Возможна потеря точности при делении
}

When parsing voluminous data, you get a significant speedup.

 
fxsaber:


Much faster this way (Release, not Debug)


Whenparsing large data you get a significant speedup.

Can you provide the benchmark code to demonstrate that ?