Download MetaTrader 5

Need basic coding help to get me started

To add comments, please log in or register
Publish your article and win recognition from thousands of traders!
RedLineR
21
RedLineR 2014.03.02 11:48 
Hi!

I programmed in C++ 20 years ago when I was still at university. And honestly, either I don't remember much, or the syntax has really changed a lot.

To get me started on the road to recovery I would like to ask for basic coding help.

I want to simply make an exercise indicator that simply plots a horizontal line at the maximum rate achieved during the last 2 years, and the minimum rate for the last 2 years.

And for every new tick that comes in, it should simply check if the new tick is > than the current maximum or not. If it is, the line should simply move to the new maximum.

Same for the minimum. If the new rate is < the current minimum, the horizontal line should be moved to the new minimum.


To give an idea of where/when I'm from.

In the old days, pointers were the only way to manage arrays of indeterminate size. Each element had an memory adress to it, that linked to the next element in memory.

These days it seems arrays can simply be declared and the pointers are sorted out automatically?

Would I be correct if I say, that the simplest way to do this, would be to load the rates in Month timeframe, and somply declare an array with 24 elements.

Then using a for loop I can evaluate the min and max of each of the 24 elements with a temporary one. After 24 iterations, my temporary variables will contain the min and max values.


Then it's a case of plotting a horizontal line at both those variable values.


Is this still how this would be tackled in MQ5?

Would anyone mind just writing me the code. I think after I get a simple thing like this done, I can get the feel for the changes from my old days code, to the new modern day MQ5.


Thanks in advance!

Alain Verleyen
Moderator
30721
Alain Verleyen 2014.03.02 13:12  

Declare some variables :

datetime fromDate;
double   pastmin=DBL_MAX;
double   pastmax=-DBL_MIN;

Initialize it as needed :

fromDate=TimeCurrent()-2*365*24*3600;                          // Approximately 2 years, can be modified for more accuracy

Create your 2 horizontal lines (code not provided).

And in OnCalculate() :

   int limit=(prev_calculated==0) ? 0 : prev_calculated-1;

   for(int i=limit;i<rates_total;i++)
     {
      if(time[i]>=fromDate)
        {
         if(high[i]>pastmax)
            pastmax=high[i];
         if(low[i]<pastmin)
            pastmin=low[i];
        }
     }
    ObjectMove(0,"MaxHLine",0,TimeCurrent(),pastmax);
    ObjectMove(0,"MinHLine",0,TimeCurrent(),pastmin);
RedLineR
21
RedLineR 2014.03.02 13:53  

Awesome thanks!

Exactly what I needed!

Pasi Hakamaki
4684
Pasi Hakamaki 2014.03.04 20:56  

Not directly related to your question, but also remember to use F1 key when you need to quickly find information about some part of the code. Just highlight the part of the code in MetaEditor and hit F1. This was really helpful when I was learning the ropes regarding MQL5.

Regards,

Candles

To add comments, please log in or register