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?
Calculating # of minutes/hours until expiry [SOLVED]
- 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
I don't know if this is the 'best' way, but this is the way I'd do it: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?
(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)
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)
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).
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)
Code: Select all
minutesTillExpiration = dateTimeTillExpiration / ELTimeToDateTime(1)
Code: Select all
hoursTillExpiration = dateTimeTillExpiration / ELTimeToDateTime(100)
- TJ
- Posts: 7744
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2223 times
Re: Calculating # of minutes/hours until expiry
Verification is up to you, the user.Thanks JoshM. This is helpful.
If MC has as comments, or can verify this approach, that would be much appreciated
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.