1
votes

How to subtract datetime-s accurately, without including weekend times?

Example:

datetime gapOpenTime  = 2015.01.01 00:00;
datetime gapCloseTime = 2015.12.19 00:00;

int      diff         = gapCloseTime - gapOpenTime;

This diff includes also weekend times ( when the market is closed )

How to remove these unwanted times?

1
Hi, PraAnj, how did you like the provided solution? StackOverflow encourages users to reward good ideas or solution by an UpVote -- so feel free to click both on UpVote and on Accept "checker" -- this is the way StackOverflow works. Enjoy the day!user3666197

1 Answers

1
votes

One may use

datetime barTimes[]; 

ArrayCopySeries( barTimes, MODE_TIME, _Symbol, PERIOD_CURRENT ); // Time[] array was sorted in a descending order

int gapOpenSHIFT  = ArrayBsearch( barTimes, gapOpenTime,  WHOLE_ARRAY, 0, MODE_DESCEND );
int gapCloseSHIFT = ArrayBsearch( barTimes, gapCloseTime, WHOLE_ARRAY, 0, MODE_DESCEND );

int diff          = PeriodSeconds( PERIOD_CURRENT ) * ( gapOpenSHIFT - gapCloseSHIFT );