Getting a price for some time in the past

 
Hi, I want to use for example the bid price for a symbol for 24 hrs ago. How can I get that price? Is the values stored? Can I for instance get a price of 1 week ago?
 
mjdutoit:
Hi, I want to use for example the bid price for a symbol for 24 hrs ago. How can I get that price? Is the values stored? Can I for instance get a price of 1 week ago?
You can get the OHLC prices within a 1 minute time period, you can't get historical tick prices directly with MT4.
 
mjdutoit:
Hi, I want to use for example the bid price for a symbol for 24 hrs ago. How can I get that price? Is the values stored? Can I for instance get a price of 1 week ago?
  • mt4 prices are bid prices only.
  • the prices are not stored in bid & ask formats.
  • the smallest historical data resolution is one_minute.
  • so for your example, you can get to know the Opening_Bid_24Hours ago.
  • code something like iOpen( Symbol(), Period_H1, 24 )
  • you cannot get the ask price unless you make assumptions about the spreads [i.e fixed].
  • you cannot get the second_tick after the opening price with any accuracy worth noting.
  • you can try falling back to smaller timeframe resolutions, example Period_M1
  • the accuracy need usually depends upon what you're trying to accomplish [i.e scalping].
 
thank you very much, will try the iOpen code
 
ubzen:
  • so for your example, you can get to know the Opening_Bid_24Hours ago.
  • code something like iOpen( Symbol(), Period_H1, 24 )

24 H1 bars ago is not necessary the same as 24 hours ago (e.g. over the weekend close.) Especially on smaller timeframes you can have missing bars.

#define HR2400 86400       // 24 * 3600
//int      TimeOfDay(datetime when){  return( when % HR2400          );         }
//datetime DateOfDay(datetime when){  return( when - TimeOfDay(when) );         }
//datetime Today(){                   return(DateOfDay( TimeCurrent() ));       }
//datetime Tomorrow(){                return(Today() + HR2400);                 }
//datetime Yesterday(){               return( iTime(NULL, PERIOD_D1, 1) );      }
//////////////////////////////////////////////////////////////////////////////////////
datetime now = TimeCurrent(),
         H24 = now - HR2400 ;
int      iH1H24= iBarShift(NULL, PERIOD_H1, H24); // 24 Hrs ago or last hour before close.
Print("H1 Open(-24 Hrs) was ", PriceToStr( iOpen(NULL, PERIOD_H1, iH1H24);

int      iH24 = iGetShift(H24);                   // 24 Hrs ago or first hour after open
Print("Chart Open(-24 Hrs) was " PriceToStr( Open[iH24] );
//////////////////////////////////////////////////////////////////////////////////////
string   PriceToStr(double p){   return( DoubleToStr(p, Digits) );            }
int      iGetShift(datetime tm, int p=0){
   if(p == 0)  p = Period();
   //{ At https://www.mql5.com/en/forum/145041 I report that iBarShift doesn't match the
   // documentation. It returned bar 49 hours earlier instead of bar 1 hour
   //} later, (weekend gap.) This routine returns the nearest bar.
   int      iBar     = iBarShift(NULL, p, tm);
   datetime barBeg   = iTime(NULL, p, iBar),
            barEnd   = barBeg + p * 60;         if(barEnd > tm)   return(iBar);
                                                if(iBar <= 0)     return(iBar);
   datetime nxtBeg   = iTime(NULL, p, iBar - 1);
   if(tm - barEnd > nxtBeg - tm) iBar--;
   return(iBar);                                                              }
}
 
so what if i use for instance iOpen( Symbol(), Period_M1, 1440 ) instead of iOpen( Symbol(), Period_D1, 1 ). Would that not give me the price 1 day ago to the nearest minute all the time? Not sure how weekends will effect it?