Obtain the opening and closing price of a day

To add comments, please log in or register
Ivan9215
63
Ivan9215  

Hi, how are you ? A few days ago I published this subject, but I did not get answers that would help me. Then I made a script that gets the highest price all day and also the lowest, showing the information in message boxes. But I also want to get the opening price of the 0 hours and the closing price of the 23 hours, although I do not know how to do it this time. Here is the code:


//+------------------------------------------------------------------+

//|                                              precioPrincipal.mq4 |

//|                        Copyright 2017, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2017, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict


int numBars,searchH,searchL;

int boxH,boxL,boxT;

double h,l;

string msgH,msgL,msgT;


//+------------------------------------------------------------------+

//| Script program start function                                    |

//+------------------------------------------------------------------+

void OnStart()

  {

//---


   numBars = iBarShift(NULL,0,iTime(NULL,PERIOD_D1,0));

 

   searchH = iHighest(NULL,0,MODE_HIGH,numBars,0);

   searchL = iLowest(NULL,0,MODE_LOW,numBars,0);

   

   if(searchH && searchL != -1)

   {

      h = High[searchH];

      l = Low[searchL];

      msgH = DoubleToString(h) + " high price in the bar " + IntegerToString(searchH);

      msgL = DoubleToString(l) + " low price in the bar " + IntegerToString(searchL);

      msgT = IntegerToString(numBars) + " bars";

      

      boxH = MessageBox(msgH, "High price", MB_OK);

      boxL = MessageBox(msgL, "Low price", MB_OK);

      boxT = MessageBox(msgT, "Total bars",MB_OK);

   }  

   else

   {

      boxH = MessageBox("Error", "Not found", MB_OK);

      boxL = MessageBox("Error", "Not found", MB_OK);

      boxT = MessageBox("Error", "Not found", MB_OK);

   }

  }

//+------------------------------------------------------------------+

Can you help me ?

Trading automático y simulación de estrategias comerciales
Trading automático y simulación de estrategias comerciales
  • www.mql5.com
MQL5 es un lenguaje built-in de estrategias comerciales para el terminal MetaTrader 5. Este lenguaje permite escribir sus propios sistemas automáticos de trading, indicadores técnicos, scripts y bibliotecas de funciones.
Ivan9215
63
Ivan9215  

I forgot to mention that I have the graph with the M15 period. Even so, count the number of bars correctly and get the other data correctly if the period is changed.

nicholi shen
2392
nicholi shen  
Ivan9215:

I forgot to mention that I have the graph with the M15 period. Even so, count the number of bars correctly and get the other data correctly if the period is changed.


void OnStart()
{
   int day_shift = 1; // yesterday
   MqlRates rate[];
   CopyRates(Symbol(),PERIOD_D1,day_shift,1,rate);
   Print("Yesterday: O=",rate[0].open," C=",rate[0].close);
}
Ivan9215
63
Ivan9215  
nicholishen:


Thank you very much, the data is correct. I started doing it with arrays but with your method it is simpler.

William Roeder
20228
William Roeder  

There are two ways.

  1. If you use the D1 chart (CopyRates or iClose, etc) Unless the current chart is the specific pair/TF referenced, you must handle 4066/4073 errors
              Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
    or check your return codes (CopyRates) for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. Alternatively, just manipulate time to get beginning of the day, beginning of yesterday, etc., get the shifts, get the data.
              Draw rectangle around range of bars by hours - MQL4 and MetaTrader 4 - MQL4 programming forum
nicholi shen
2392
nicholi shen  
whroeder1:

There are two ways.

  1. If you use the D1 chart (CopyRates or iClose, etc) Unless the current chart is the specific pair/TF referenced, you must handle 4066/4073 errors
              Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
    or check your return codes (CopyRates) for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. Alternatively, just manipulate time to get beginning of the day, beginning of yesterday, etc., get the shifts, get the data.
              Draw rectangle around range of bars by hours - MQL4 and MetaTrader 4 - MQL4 programming forum

Would something like this not work?

void OnStart()
{
   int day_shift = 1; // yesterday
   MqlRates rate[1]={0};
   MqlDateTime time;
   TimeCurrent(time);
   time.hour=0;
   time.min=0;
   time.sec=0;
   datetime yesterday = StructToTime(time) - PeriodSeconds(PERIOD_D1);
   datetime tdp = yesterday - PeriodSeconds(PERIOD_D1);
   int retries = 10;
   for(int i=0; i<retries && rate[0].time!=yesterday && rate[0].time!=tdp; i++)
   {
      CopyRates(Symbol(),PERIOD_D1,day_shift,1,rate);
      Sleep(10);
   }
   if(rate[0].time!=yesterday && rate[0].time!=tdp)
      Print(__FUNCTION__," Error: ",GetLastError());
      
   Print("Yesterday: O=",rate[0].open," C=",rate[0].close);
}
William Roeder
20228
William Roeder  
  1. nicholishen: Would something like this not work?

    It probably will not. What part of "you must handle 4066/4073 errors or check your return codes (CopyRates)" was unclear?

  2. rate[0].time!=yesterday 
    The LHS will be start of the day (00:00) the RHS will usually be a time. These checks are unnecessary if CopyRates returns one. They are bogus if CopyRates returns an error.
nicholi shen
2392
nicholi shen  

whroeder1:

 What part of "you must handle 4066/4073 errors or check your return codes (CopyRates)" was unclear?

You must be fun at parties.
William Roeder
20228
William Roeder  
nicholishen: You must be fun at parties.

Why do you ask for help and then ignore the answers, thus wasting everyone's time and effort?

nicholi shen
2392
nicholi shen  
whroeder1:

Why do you ask for help and then ignore the answers?


Why do you have to be such a rude curmudgeon when replying to questions?

William Roeder
20228
William Roeder  
nicholishen: Why do you have to be such a rude curmudgeon when replying to questions?

"Wasting everyone's time and effort" is rude and inconsiderate. All I did was point it out. Your protestations and name calling make you curmudgeon. I've now added you to my do not help list, have a nice life.

12
To add comments, please log in or register