Calculating # of minutes/hours until expiry  [SOLVED]

Questions about MultiCharts and user contributed studies.
blquest
Posts: 7
Joined: 09 Nov 2013
Has thanked: 1 time
Been thanked: 1 time

Calculating # of minutes/hours until expiry

Postby blquest » 09 Nov 2013

Hi All,

Thanks for looking at this question. Apologies if it has been covered before, in which case please feel free to refer me to the relevant thread. My question relates to calculating the # of minutes or hours until expiration of a futures contract which I did not find elsewhere on this forum.

I can pull up the expiration date of the contract via the expirationdate function and then calculate the # of business days until expiration, which works for daily charts.

What's the best way to calculate the # of hours or minutes until expiration of a futures contract if i'm working with higher resolution charts?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Calculating # of minutes/hours until expiry

Postby JoshM » 10 Nov 2013

I can pull up the expiration date of the contract via the expirationdate function and then calculate the # of business days until expiration, which works for daily charts.

What's the best way to calculate the # of hours or minutes until expiration of a futures contract if i'm working with higher resolution charts?
I don't know if this is the 'best' way, but this is the way I'd do it:

(1): Calculate time till close today.
Convert the closing time of the instrument to DateTime (see ELTimeToDateTime) and do the same for the current time. Subtract the DateTime of the close from the current DateTime.

For example:

Code: Select all

ELTimeToDateTime(2200) - ELTimeToDateTime(Time)
(2): Calculate the trading hours on the expiration date.
Convert the expiration time to DateTime and subtract the opening hour (in DateTime) of the instrument from this.

For example, if the instrument expires at 15:00 (3 PM) and opens at 8:00:

Code: Select all

ELTimeToDateTime(1500) - ELTimeToDateTime(800)
(3): Calculate the number of business days.
You already used the function for that, so:

Code: Select all

Number of business days - 2 (for the current day and the expiration day, already covered in step 1 and 2 above).
Then:

Code: Select all

Number of business days * (ELTimeToDateTime(closing time) - ELTimeToDateTime(opening time))

Then you need to add (1), (2), and (3) together to get the total amount of DateTime till expiration.

Code: Select all

dateTimeTillExpiration = (1) + (2) + (3)
Then convert this to one minute:

Code: Select all

minutesTillExpiration = dateTimeTillExpiration / ELTimeToDateTime(1)
Or to the number of hours:

Code: Select all

hoursTillExpiration = dateTimeTillExpiration / ELTimeToDateTime(100)
(Btw, this is untested)

blquest
Posts: 7
Joined: 09 Nov 2013
Has thanked: 1 time
Been thanked: 1 time

Re: Calculating # of minutes/hours until expiry

Postby blquest » 10 Nov 2013

Thanks JoshM. This is helpful.

If MC has as comments, or can verify this approach, that would be much appreciated

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

Re: Calculating # of minutes/hours until expiry

Postby TJ » 10 Nov 2013

Thanks JoshM. This is helpful.
If MC has as comments, or can verify this approach, that would be much appreciated
Verification is up to you, the user.
It is not that difficult to test, you have to do it as part of your autotrade coding process anyway. Nobody can do that for you unless you publish your autotrade code as well.

blquest
Posts: 7
Joined: 09 Nov 2013
Has thanked: 1 time
Been thanked: 1 time

Re: Calculating # of minutes/hours until expiry

Postby blquest » 10 Nov 2013

That's correct TJ.

What I'm saying is, if MC has any comments, we are happy to receive them.

blquest
Posts: 7
Joined: 09 Nov 2013
Has thanked: 1 time
Been thanked: 1 time

Re: Calculating # of minutes/hours until expiry  [SOLVED]

Postby blquest » 11 Nov 2013

Thanks JoshM, works exactly as described.


Return to “MultiCharts”