AddMinutes Function

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

AddMinutes Function

Postby furytrader » 26 Nov 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:

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;
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.

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: AddMinutes Function

Postby furytrader » 26 Nov 2010

BTW, I noted that there was a small goof in the original posting above which I subsequently fixed. The code should work fine now.

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: AddMinutes Function

Postby sptrader » 27 Nov 2010

It sounds interesting, could you give a simple example of how you would use the function inside a system ? Say a simple MA crossover or BO etc.

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: AddMinutes Function

Postby furytrader » 27 Nov 2010

Sure, let's imagine that you're day-trading the S&P 500. Let's say that you want to trade a certain pattern - for example, if, on a 5-minute bar, you have three consecutive down closes, then buy on a stop above the high of the most recent bar. You could see whether this pattern tends to work best in the morning session, the lunchtime session or in the afternoon. You could run an analysis that examines 30 minute windows.

So, your code would be something like this:

Code: Select all

Inputs: WatchIncrements(0);
Vars: StartWatchTime(930), EndWatchTime(0);

StartWatchTime = AddMinutes(StartWatchTime,WatchIncrements);
EndWatchTime = AddMinutes(StartWatchTime,30);

If Time > StartWatchTime and Time <= EndWatchTime and C < C[1] and C<[1] < C[2] and C[2] < C[3] Then Buy Next Bar At High STOP;

// Enter exit criteria here
You could then run an optimization where you analyze values for "WatchIncrements" from 0 to 360 minutes, in 30 minute increments. Therefore, you could analyze the efficacy of this pattern from 9:30 am through 3:30 pm and see in what times of the day this pattern seems to work the best.
Last edited by furytrader on 27 Nov 2010, edited 1 time in total.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: AddMinutes Function

Postby TJ » 27 Nov 2010

Cool.

When I look at your first post, I was wondering why you did not use ELTime conversions.
Your example explains that.

Very nice work. Thanks for sharing.

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: AddMinutes Function

Postby sptrader » 27 Nov 2010

Thanks for the great explanation and example !

bomberone1
Posts: 310
Joined: 02 Nov 2010
Has thanked: 26 times
Been thanked: 23 times

Re: AddMinutes Function

Postby bomberone1 » 21 Dec 2010

Is it possible to ask seconds and millisecond?
I'd like to find where during a trading hours days the battern works well, are there any suggestions?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: AddMinutes Function

Postby TJ » 23 Dec 2011

All you need to know about Dates and Times:


EasyLanguage Reference Guide

CHAPTER: 2 - The Basic EasyLanguage Elements
Manipulating Dates and Times .... pg. 15

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: AddMinutes Function

Postby TJ » 30 Dec 2011

There is a function included with MultiCharts called CalcTime,
and CalcTime_s for the sub-minute analysis.



CalcTime (Function)

The CalcTime function adds and subtracts minutes from a reference time. The reference time is a numeric value in 24-hour military time format: HHMM (Hour Hour Minute Minute).

For example, 10:15am = 1015, or 1345 = 1:45pm

When working with hour and minutes, it is sometimes difficult to add or subtract some number of minutes with an HHMM time. For example, if you wanted to subtract 15 minutes from noon you might try 1200 - 15, which would result in an answer of 1185, which is not a valid time. CalcTime solves this by returning the correct and valid time of 1145.

Syntax
CalcTime( RefTime, MinuteChange )

Returns (Integer)
A numeric value that represents the time in 24-hour format for the current bar.

RefTime
Specifies the chart time in 24-hour time format to use as the reference time for the calculation, entered in HHMM format. (Examples: 0930, 1245, 1600)

MinuteChange
Sets the minutes to be added (positive value) or subtracted (negative value).


Remarks
EasyLanguage does not see time and date as unique data formats but sees them and treats them as normal numeric values.

Example
Assigns to Value1 the calculated time 15 minutes from market close and then plots a PaintBar on every bar after that time:

Code: Select all

Value1 = CalcTime( 1600, -15 );

if Time >= Value1 then

PlotPB( High, Low, "CalcTime" );
See Also
CalcDate


Return to “User Contributed Studies and Indicator Library”