Price of close at a specific time.

 

Trying to get the EA to return the closing price at 5pm and store the value in a variable.

This should be done every tick so at 5pm the next day it stores the new close price. Can anyone help?






string five       = "17:00"; 

datetime NY; 

double NYClose;



void OnTick()

  {

     NY = StrToTime(five);

   

   

   if(TimeLocal() == NY)

   {

     NYClose = iClose(NULL,PERIOD_CURRENT,1); 

   }

      

 

    

  }

 

Hi  sheldon_nico2

Couple of points.

Best to run off serverTime where possible, TimeCurrent(); - Daylight savings happens at different times accross the world.

Having time comparitors in OnTick() doesn't work as Ticks are movements in price and can jump specific seconds as your checking against 17:00:00 == 17:00:00

Time comparitor

Use OnTimer set to seconds or just compare Hours Minutes with toggler maybe.

I always add a couple of seconds when dealing with iClose() - even the bar at index 0 has a close value and occationally iClose() will screw up the index when referencing at 01 milliseconds.

I punched out this if it helps.

int OnInit() {
//---
//--- create a timer with a 1 second period
   EventSetTimer(1);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
//---
//--- destroy the timer after completing the work
   EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTimer() {
//---
   double NYClose;
   MqlDateTime Local, NYTime;
   NYTime.hour = 17;
   NYTime.min = 00;
   NYTime.sec = 02;
   if(TimeToStruct(TimeLocal(), Local)) {
      if((Local.hour == NYTime.hour && Local.min == NYTime.min && Local.sec ==  NYTime.sec)) {
         NYClose = iClose(Symbol(), Period(), 1);
         Print("New York Close", NYClose);
      }
   }
}
sheldon_nico2
sheldon_nico2
  • www.mql5.com
Added topic Price of close at a specific time. Trying to get the EA to return the closing price at 5pm and store the value in a variable. This should be done every tick so at 5pm the next day it stores the new close price . Can anyone help? string five       = "17:00";
 
BrianCus:

Hi  sheldon_nico2

Couple of points.

Best to run off serverTime where possible, TimeCurrent(); - Daylight savings happens at different times accross the world.

Having time comparitors in OnTick() doesn't work as Ticks are movements in price and can jump specific seconds as your checking against 17:00:00 == 17:00:00


Use OnTimer set to seconds or just compare Hours Minutes with toggler maybe.

I always add a couple of seconds when dealing with iClose() - even the bar at index 0 has a close value and occationally iClose() will screw up the index when referencing at 01 milliseconds.

I punched out this if it helps.

Works great, Thanks for your time and effort. 
Reason: