How to identify place where linked contracts in continuous contract?  [SOLVED]

Questions about MultiCharts and user contributed studies.
User avatar
Xurma
Posts: 31
Joined: 24 Nov 2020
Has thanked: 9 times

How to identify place where linked contracts in continuous contract?

Postby Xurma » 21 Dec 2020

As you can see in Crude oil contract the last trading day is changing every month and we don't have the constant date for it.
How to identify the last trading days of each contracts that includes in continuous contract?
Continuous contract have a lot of contracts linked within and I need to find out all dates of the joints.
LTD.jpg
(252.93 KiB) Not downloaded yet

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

Re: How to identify place where linked contracts in continuous contract?

Postby TJ » 22 Dec 2020

Are you aware that most traders skip a contract month with crude oil?

User avatar
Xurma
Posts: 31
Joined: 24 Nov 2020
Has thanked: 9 times

Re: How to identify place where linked contracts in continuous contract?

Postby Xurma » 22 Dec 2020

Are you aware that most traders skip a contract month with crude oil?
Excuse me, but I don't understand your question quiet well. My question was about identifying places where continuous contract was linked


User avatar
Xurma
Posts: 31
Joined: 24 Nov 2020
Has thanked: 9 times

Re: How to identify place where linked contracts in continuous contract?

Postby Xurma » 23 Dec 2020

I have answer from IQfeed, they can not help with this, because they do not store expiration dates and can not help with identification of it.
But if we can't identify this dates - the back tests is not so correct. Because in real trading you have to close all your position before expiration and you can not emulate this with back tests. And backtest results can be better or worse than real time trading.

SUPER
Posts: 646
Joined: 03 Mar 2007
Has thanked: 106 times
Been thanked: 84 times

Re: How to identify place where linked contracts in continuous contract?

Postby SUPER » 23 Dec 2020

from CME website link provided earlier and it states following

"Trading terminates 3 business day prior to the 25th calendar day of the month prior to the contract month. If the 25th calendar day is not a business day, trading terminates 4 business days prior to the 25th calendar day of the month prior to the contract month."

Have a look at the expiration calendar

https://www.cmegroup.com/tools-informat ... -calendar/

User avatar
Xurma
Posts: 31
Joined: 24 Nov 2020
Has thanked: 9 times

Re: How to identify place where linked contracts in continuous contract?

Postby Xurma » 23 Dec 2020

from CME website link provided earlier and it states following

"Trading terminates 3 business day prior to the 25th calendar day of the month prior to the contract month. If the 25th calendar day is not a business day, trading terminates 4 business days prior to the 25th calendar day of the month prior to the contract month."

Have a look at the expiration calendar

https://www.cmegroup.com/tools-informat ... -calendar/
Thank you. It is correct information! Do you know how to calculate this dates in Multicharts?

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: How to identify place where linked contracts in continuous contract?

Postby rrams » 23 Dec 2020

Yes, the rollover date is important information to know if you are trading crude oil futures. Unfortunately that date is determined differently by each data vendor. Sometimes a fixed date and sometimes when more volume arrives in the next contract, or in the case of Interactive Brokers possibly just made up from random information. All is not lost however; as you can calculate the day you want to roll by using a day offset from the native PowerLanguage keyword named ExpirationDate. So your code might look like this:

Code: Select all

vars: DTE(DateToJulian(ExpirationDate)), DaysB4Exp(5), MP(0); if getappinfo(aiStrategyAuto)=1 then MP=MarketPosition_at_Broker else MP=MarketPosition; // Get out if expiring soon if DateToJulian(Date)>=DTE-DaysB4Exp then begin if GetAppInfo(aiRealTimeCalc)=1 then Alert("Expiring contract - Roll!"); if MP>0 then Sell next bar market else if MP<0 then BuyToCover next bar market; end;
You can download the continuous contract from BarChart.com and that has the individual contracts listed if that helps you visualize the dates, but that might not be the same as IQFeed.
qm.txt
(1.59 KiB) Downloaded 81 times

User avatar
Xurma
Posts: 31
Joined: 24 Nov 2020
Has thanked: 9 times

Re: How to identify place where linked contracts in continuous contract?

Postby Xurma » 24 Dec 2020

Yes, the rollover date is important information to know if you are trading crude oil futures. Unfortunately that date is determined differently by each data vendor. Sometimes a fixed date and sometimes when more volume arrives in the next contract, or in the case of Interactive Brokers possibly just made up from random information. All is not lost however; as you can calculate the day you want to roll by using a day offset from the native PowerLanguage keyword named ExpirationDate. So your code might look like this:

Code: Select all

vars: DTE(DateToJulian(ExpirationDate)), DaysB4Exp(5), MP(0); if getappinfo(aiStrategyAuto)=1 then MP=MarketPosition_at_Broker else MP=MarketPosition; // Get out if expiring soon if DateToJulian(Date)>=DTE-DaysB4Exp then begin if GetAppInfo(aiRealTimeCalc)=1 then Alert("Expiring contract - Roll!"); if MP>0 then Sell next bar market else if MP<0 then BuyToCover next bar market; end;
You can download the continuous contract from BarChart.com and that has the individual contracts listed if that helps you visualize the dates, but that might not be the same as IQFeed.
qm.txt
Hello, thank you for answer. But on continuous contracts ExpirationDate is not working. I need it for backtest. Expiration date is 25 date of every month minus 3 days, if 25 is holiday then -4 days. How to calculate this in MC, I don't know

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: How to identify place where linked contracts in continuous contract?

Postby rrams » 26 Dec 2020

Hi Xurma,
Please be aware that the CME settlement date is not the same as the rollover date that your data provider uses for continuous contracts. If you cannot backtest on each individual contract month in Portfolio Trader and must use a continuous contract, then you will have to construct your own or find out what method they use to roll.
If you just want to re-create the formula for crude oil settlement, then the basic code would look like this:

Code: Select all

vars: WD(0), MD(0); // Current day. 0-Sunday, 6-Saturday WD=DayOfWeek(D); // Current date. days are 1-31 MD=DayOfMonth(D); // Roll 20th-22nd depending on weekends if MD=20 and (WD=3 or WD=4 or WD=5) then print("Rolling on 3 days prior: ", DateToString(DateTime)) // In 4 days the 25th falls on a weekend else if MD=21 and (WD=2 or WD=3) then print("Rolling on 4 days prior: ", DateToString(DateTime)) // 25th in 3 days and not on Saturday or Sunday else if MD=22 and (WD=1 or WD=2) then print("Rolling on 3 days prior: ", DateToString(DateTime));
This code only checks for weekends; the full solution requires checking for holidays also and is non-trivial so you would probably have to pay a programmer to provide that. But first make sure what you need.

User avatar
Xurma
Posts: 31
Joined: 24 Nov 2020
Has thanked: 9 times

Re: How to identify place where linked contracts in continuous contract?  [SOLVED]

Postby Xurma » 28 Dec 2020

Hi Xurma,
Please be aware that the CME settlement date is not the same as the rollover date that your data provider uses for continuous contracts. If you cannot backtest on each individual contract month in Portfolio Trader and must use a continuous contract, then you will have to construct your own or find out what method they use to roll.
If you just want to re-create the formula for crude oil settlement, then the basic code would look like this:

Code: Select all

vars: WD(0), MD(0); // Current day. 0-Sunday, 6-Saturday WD=DayOfWeek(D); // Current date. days are 1-31 MD=DayOfMonth(D); // Roll 20th-22nd depending on weekends if MD=20 and (WD=3 or WD=4 or WD=5) then print("Rolling on 3 days prior: ", DateToString(DateTime)) // In 4 days the 25th falls on a weekend else if MD=21 and (WD=2 or WD=3) then print("Rolling on 4 days prior: ", DateToString(DateTime)) // 25th in 3 days and not on Saturday or Sunday else if MD=22 and (WD=1 or WD=2) then print("Rolling on 3 days prior: ", DateToString(DateTime));
This code only checks for weekends; the full solution requires checking for holidays also and is non-trivial so you would probably have to pay a programmer to provide that. But first make sure what you need.
Thank you, really good solution!


Return to “MultiCharts”