Time manipulation within Days

 

Hello,

Please, I am finding it difficult to repeat the box drawn on "x" amount of days, then also change the time to the current time in my plot, Using print function it is showing my time is 1970.01.01. Am still learning and this is the first code am writing with mql 5.

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[])
  {
//Checking and calculating the number of calculated bars
   if (Period() > PERIOD_H1)
      return 0;
 
//--- the time difference (x-axis) range
  int count = End - Offset;

//---- checking the number of bars for sufficiency for calculation
   if(rates_total< count) return 0;

//--- Setting buffer arrays as elements to be used in the calculation
   ArraySetAsSeries(time, true);

//--- Array for storing the dates and time
datetime iTime[], dates[];

int nDays = 5; // Number of days for the rectangle to be drawn

for (int j=0; j<nDays; j++)
{
   CopyTime(NULL, PERIOD_D1, 0, nDays, dc);
   ArraySetAsSeries(dc, true);
   MqlDatetTime dt;
   TimeToStruct (dc[j], dt);
   
   for(int i=0; i<= count && !IsStopped(); i++)
        {
          if (TimeHour (time[i]) == End)
          {
            time1 = time[i];
            time2 = time1 + 3600 * count;
          }
        }
   

}

     
}

The Value of the days is changing but getting the time to change(from 00:00) on those days is what am finding difficult. The Rectangle is drawn only on the recent day. Thanks

Documentation on MQL5: Common Functions / Print
Documentation on MQL5: Common Functions / Print
  • www.mql5.com
Data of double type are shown with the accuracy of up to  16 digits after a decimal point, and can be output either in traditional or in scientific format, depending on what entry will be more compact. Data of float type are output with 5 digits after a decimal point. To output real numbers with another accuracy or in a predefined format, use...
 
   CopyTime(NULL, PERIOD_D1, 0, nDays, dc);
   ArraySetAsSeries(dc, true);
  1. Why are you calling those nDays times? Once is enough.

  2. Don't use NULL.
    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not. OrderSend does not. On MT5 only № 2.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].

  3. Check your return codes. On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum 2019.09.03
              OnCalculate during weekend MT5 - General - MQL5 programming forum
 
William Roeder:
  1. Why are you calling those nDays times? Once is enough.

  2. Don't use NULL.
    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not. OrderSend does not. On MT5 only № 2.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].

  3. Check your return codes. On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum 2019.09.03
              OnCalculate during weekend MT5 - General - MQL5 programming forum
Thanks,
I have changed the NULL, for the nDays, I changed it to one but only the previous date was obtained . I also tried using an increment of "j", the dates were corrected but checking for a specific time(eg draw a rectangle between "04:00-08:00" for 02.06.2020, getting it to draw another for 01.06.2020 - nDays) is the problem.

Please, If am using "ObjectCreate ()" function, can it draw all the rectangles without using a buffer or plot property.

Thanks

 
Trapheal:

Hello,

Please, I am finding it difficult to repeat the box drawn on "x" amount of days, then also change the time to the current time in my plot, Using print function it is showing my time is 1970.01.01. Am still learning and this is the first code am writing with mql 5.

The Value of the days is changing but getting the time to change(from 00:00) on those days is what am finding difficult. The Rectangle is drawn only on the recent day. Thanks

What I have learnt now that am done, It may help someone confused someday.

1) You do not really buffers to plot or draw a rectangle as in my case above.

2) Buffers are  plotted in pairs of (0,1) (2,3) {as seen when you use the command below}. When only one buffer is defined, the buffer together with the origin line are used to plot your data.

//--- What it does is assign your buffer to a number
SetIndexBuffer();

3) When using, to create, more than one object eg Rectangle (OBJ_RECTANGLE), you need to program a unique name for each, if not only one would be created (I struggled here). 

//--- Creating of graphical Objects
ObjectCreate();

4) To get the object to be created in previous date, use a time function such that the , start and end period (time1 and time2) are added to the current bar time iTime[0], with that you would have less headache.

Goodluck