Automatically getting 'yesterday' and 'today'

TheBramble

Legendary member
Messages
8,394
Likes
1,170
I’ve managed to cobble together something that does the job in a very primitive way. (Feel free to adopt, amend and utillise as you wish…).

But is there anyway I can get around having to either edit the swine each day or input the datetime values when I pull it onto the chart?

The extern datetime are the data I want to automate.

The times are always going to be the same: 23:00 ‘yesterday’ and 01:00 ‘today’. SO they can stay hard-coded. If there is a way to have it automatically use ‘yesterday’ and ‘today’ it would be really useful.

Come on, I've done the hard yards.

There has to be a metatrader coding Guru on this site with a better than basic grasp of this stuff who can come and insouciantly wave his coding wand and grab all the credit. Show us your stuff!!!


//---- input parameters
extern datetime xdt_from = D'2009.05.21 23:00';
extern datetime xdt_to = D'2009.05.22 01:00';

//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+

int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
//int counted_bars=IndicatorCounted();
//----
int vi_bar;
int vi_startBar;
int vi_endBar;
double vd_hi = 0;
double vd_lo = 99999999.0;


//delete the lines from the last time :)
ObjectDelete("_start");
ObjectDelete("_end");
ObjectDelete("_hi");
ObjectDelete("_lo");

//find starting and ending bars and mark them with a vline
vi_startBar = ArrayBsearch(Time, xdt_from);
ObjectCreate("_start", OBJ_VLINE, 0, Time[vi_startBar], 0);
vi_endBar = ArrayBsearch(Time, xdt_to);
ObjectCreate("_end", OBJ_VLINE, 0, Time[vi_endBar], 0);

//go on all bars...
for(vi_bar = vi_startBar; vi_bar >= vi_endBar; vi_bar--)
{
//update min/max
vd_hi = MathMax(vd_hi, High[vi_bar]);
vd_lo = MathMin(vd_lo, Low[vi_bar]);
}

//mark the highest and lowest with an hline
ObjectCreate("_hi", OBJ_HLINE, 0, 0, vd_hi);
ObjectCreate("_lo", OBJ_HLINE, 0, 0, vd_lo);

ObjectSet("_hi",OBJPROP_COLOR,BlueViolet);
ObjectSet("_lo",OBJPROP_COLOR,BlueViolet);

ObjectSet("_hi",OBJPROP_WIDTH,2);
ObjectSet("_lo",OBJPROP_WIDTH,2);

//----
return(0);
}
//+------------------------------------------------------------------+
 
datetime dToday = iTime(Symbol(), PERIOD_D1, 0);
datetime dYesterday = iTime(Symbol(), PERIOD_D1, 1);

Hope it helps
 
Thanks, looks promising.

How can I combine that wih the '23:00' and the '01:00' hour values?
 
Thanks, looks promising.

How can I combine that wih the '23:00' and the '01:00' hour values?

Try this ...
datetime dToday = StrToTime(StringConcatenate(TimeToStr(iTime(Symbol(), PERIOD_D1, 0),TIME_DATE), " 01:00"));
datetime dYesterday = StrToTime(StringConcatenate(TimeToStr(iTime(Symbol(), PERIOD_D1, 1),TIME_DATE), " 23:00"));


pogle
 
Certainly getting closer, thanks Pogle, but the 'from' appears to be working with the 00:00 (midnight) bar, not the 23:00 cutoff time hard-coded.

Any ideas?
 
Doh!

There is no data available at 23:00 on Friday night....that's why....(I've amended the 'from' time from 23:00 to 22:59 to handle this issue).

I'm delighted to be as thick as the next man on (increasingly frequent) occasion.

pogle - many thanks. Your code works a treat. You're a star.
 
Top