AddMinutes Function
Posted: Nov 26 2010
From time to time, when analyzing intraday systems, I like to check how different setups perform during different parts of the day. This can be a little tricky to do through optimization though, because, to the best of my knowledge about EasyLanguage, there's no easy way to do time-based arithmetic. So, for example, if you want to see how a particular setup performs every 15 minutes after 12:00 pm, it's hard to tell MultiCharts to add 15 minutes to a particular time period.
Consequently, I came up with the following simple "addMinutes" function. This function takes a submitted time and a submitted number of minutes to add, and then returns the resulting time. If the new time is past midnight, it returns the time for the next day.
Here is the code:
When compiling this code, it should be saved as a function called "AddMinutes" with a return type of 'numeric' and a function storage of 'auto-detect''.
I've tested it on a limited basis and it seems to work fine. Maybe there is an easier way to do this but I don't know what it is. Feel free to play with it and let me know if you have any questions, ideas, bug reports or suggestions.
Consequently, I came up with the following simple "addMinutes" function. This function takes a submitted time and a submitted number of minutes to add, and then returns the resulting time. If the new time is past midnight, it returns the time for the next day.
Here is the code:
Code: Select all
Inputs: RefTime(numeric),RefMinutesToAdd(numeric);
Vars: vOriginalMinutes(0), vOriginalHours(0);
Vars: vTestMinutes(0);
Vars: vTempMinutes(0), vTempHours(0);
Vars: vNewMinutes(0), vNewHours(0);
vOriginalHours = IntPortion(RefTime/100);
vOriginalMinutes = Mod(RefTime,100);
vTestMinutes = vOriginalMinutes + RefMinutesToAdd;
If vTestMinutes <= 59 Then Begin
vNewMinutes = vTestMinutes;
vNewHours = vOriginalHours;
End;
If vTestMinutes > 59 Then Begin
vTempMinutes = Mod(vTestMinutes,60);
vTempHours = IntPortion(vTestMinutes/60);
vNewMinutes = vTempMinutes;
If vNewMinutes > 59 Then Begin
vNewMinutes = vNewMinutes - 60;
vTempHours = vTempHours + 1;
End;
vNewHours = vOriginalHours + vTempHours;
If vNewHours > 23 Then vNewHours = vNewHours - 24;
End;
AddMinutes = (vNewHours * 100) + vNewMinutes;
I've tested it on a limited basis and it seems to work fine. Maybe there is an easier way to do this but I don't know what it is. Feel free to play with it and let me know if you have any questions, ideas, bug reports or suggestions.