Download MetaTrader 5

i need help to understand period converter code

To add comments, please log in or register
mrluck012
269
mrluck012  
i'm trying to understand period converter code, but
input int InpPeriodMultiplier=3; // Period multiplier factor
int       ExtHandle;//=-1; tirado
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   datetime time0;
   ulong    last_fpos=0;
   long     last_volume=0;
   int      i,start_pos,periodseconds;
   int      cnt=0;
//---- History header
   int      file_version=401;
   string   c_copyright;
   string   c_symbol=Symbol();
   int      i_period=Period()*InpPeriodMultiplier;
   int      i_digits=Digits;
   int      i_unused[13];
   MqlRates rate;
//---  
   ExtHandle=FileOpenHistory(c_symbol+(string)i_period+".hst",FILE_BIN|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_ANSI);
   if(ExtHandle<0)
      return;
   c_copyright="(C)opyright 2003, MetaQuotes Software Corp.";
   ArrayInitialize(i_unused,0);
//--- write history file header
   FileWriteInteger(ExtHandle,file_version,LONG_VALUE);
   FileWriteString(ExtHandle,c_copyright,64);
   FileWriteString(ExtHandle,c_symbol,12);
   FileWriteInteger(ExtHandle,i_period,LONG_VALUE);
   FileWriteInteger(ExtHandle,i_digits,LONG_VALUE);
   FileWriteInteger(ExtHandle,0,LONG_VALUE);
   FileWriteInteger(ExtHandle,0,LONG_VALUE);
   FileWriteArray(ExtHandle,i_unused,0,13);
//--- write history file
   periodseconds=i_period*60;
   start_pos=Bars-1;    // QUESTION:WHAT STAR_POS MEAN? 
   rate.open=Open[start_pos]; WHAT RATE.OPEN MEAN?
   rate.low=Low[start_pos];  WHAT RATE.LOW MEAN
   rate.high=High[start_pos]; WHAT RATE.HIGH MEAN? 
   rate.tick_volume=(long)Volume[start_pos];  WHAT RATE.TICK VOLUME MEAN?
   rate.spread=0;   WHAT RATE.SPREAD MEAN?
   rate.real_volume=0; WHAT RATE.REAL VOLUME MEAN?
   //--- normalize open time
   rate.time=Time[start_pos]/periodseconds;
   rate.time*=periodseconds;

There are variables, like start_pos, but they on the code are not previousily related to the data type, so they are not int or double, or a function, i don't know what it is, and if 

i seach i found nothing about these words, so what's thedeal about them? 

whroeder1
17122
whroeder1  
 MqlRates rate;

   start_pos=Bars-1;    // QUESTION:WHAT STAR_POS MEAN? 

   rate.open=Open[start_pos]; WHAT RATE.OPEN MEAN?
  1. Starting position. Operate on the oldest bar to the newest. just like any indicator's compute loop.
  2. rate.open is the open variable name in the rate structure. History Data Structure - Data Structures - Standard Constants, Enumerations and Structures - MQL4 Reference
mrluck012
269
mrluck012  

if you search for rate.open in entire documentation, you cant find anything, so what it this, a variable? if it's a variable, its an int, data, double, what this is? your link doesnt explain 

what rate. means, could you explain me more about it and what it does? 

whroeder1
17122
whroeder1  
  1. Rate is a variable, so of course your not going to find rate.open any more than your going to find any other variable name used in code. It's type is MqlRates; I gave you the link to it's definition.
  2. The structure contains multiple elements one of which is a double called open.
  3. To access the element open of the variable rate you use the dot notation (rate.dot).
  4. Learn what a structure is. Google it. Same in MQ4/5, C, C++, Java, Ada, etc.
    MqlRates
    Equivalent but with no data typing
    struct MqlRates{
       datetime time;       
       double   open;       
       double   high;       
       double   low;        
       double   close;      
       long     tick_volume;
       int      spread;     
       long     real_volume;
      };
    MqlRates rate;
    PrintFormat("open=%g", rate.open);
    #define MRtime  0
    #define MRopen  1
    #define MRhigh  2
    #define MRlow   3
    #define MRclose 4
    #define MRnTick 5
    #define MRsprd  6
    #define MRlots  7
    #define MRCOUNT 8
    double rate[MRCOUNT];
    PrintFormat("open=%g", rate[MRopen]);
mrluck012
269
mrluck012  

 structure is not described on mql4 book, so as in documentation they don't explain it the way they should, but metatrader

still is the best despite that, i'll look into structure meaning, thanks a lot for the advice

Alain Verleyen
Moderator
29870
Alain Verleyen  
mrluck012:

 structure is not described on mql4 book, so as in documentation they don't explain it the way they should, but metatrader

still is the best despite that, i'll look into structure meaning, thanks a lot for the advice

The book is obsolete in a lot of things, mql4 changed a lot since 2014 and structure were introduced after the book was written.
nicholishen
1546
nicholishen  
whroeder1:
  1. Rate is a variable, so of course your not going to find rate.open any more than your going to find any other variable name used in code. It's type is MqlRates; I gave you the link to it's definition.
  2. The structure contains multiple elements one of which is a double called open.
  3. To access the element open of the variable rate you use the dot notation (rate.dot).
  4. Learn what a structure is. Google it. Same in MQ4/5, C, C++, Java, Ada, etc.
    MqlRates
    Equivalent but with no data typing

A different way to think about a struct is to remember back to when the old-timers (pre-build-600) used to make two-dimensional arrays to store OHLC.

So instead of this:

double rates[][4];
ArrayResize(...);
rates[i][0] = Open[i];
rates[i][1] = High[i];
...

Now you use an array of structs:

MqlRates rates[];
ArrayResize(...);
rates[i].open = Open[i];
rates[i].high = High[i];
...



 

nicholishen
1546
nicholishen  
mrluck012:

 structure is not described on mql4 book, so as in documentation they don't explain it the way they should, but metatrader

still is the best despite that, i'll look into structure meaning, thanks a lot for the advice


Here is one of the best and easiest to understand descriptions of a struct... http://www.learncpp.com/cpp-tutorial/47-structs/

mrluck012
269
mrluck012  

yes but mql4 book is great, because documentation makes a lot of assumptions about your previous knowledge, if you never coded i think its really hard

to lear only by metaquotes pages, the book is a game changer for non programmers, they should care more about make things clear

mrluck012
269
mrluck012  
nicholishen:

Here is one of the best and easiest to understand descriptions of a struct... http://www.learncpp.com/cpp-tutorial/47-structs/


thanks, for the link, i'll look into that

To add comments, please log in or register