Retrieve Trading Sessions Date & Time

Questions about MultiCharts .NET and user contributed studies.
victoryong
Posts: 24
Joined: 15 Sep 2013
Has thanked: 6 times
Been thanked: 3 times

Retrieve Trading Sessions Date & Time

Postby victoryong » 11 Feb 2014

Hi,
Is there method to retrieve trading session's date & time period for chart resolution lower than daily chart?

e.g. Broker's trading data is based on New York trading time, open at 5pm closes next day at 4.59pm. I'm currently at 5min timeframe chart and the bar time is 8pm and date is 11 Feb 2014. Basically, I want to retrieve trading opening and closing date & time i.e. Opening: 11 Feb 2014 5pm and Closing 12 Feb 2014 4.59pm via coding.


Cheers.
Victor

User avatar
Henry MultiСharts
Posts: 9132
Joined: 25 Aug 2011
Has thanked: 1260 times
Been thanked: 2929 times

Re: Retrieve Trading Sessions Date & Time

Postby Henry MultiСharts » 11 Feb 2014

Hello Victor,

In order to get the session settings of the instrument please check the SessionObject Members.

victoryong
Posts: 24
Joined: 15 Sep 2013
Has thanked: 6 times
Been thanked: 3 times

Re: Retrieve Trading Sessions Date & Time

Postby victoryong » 11 Feb 2014

Hi Henry,
The SessionObjects does not have date, only dayofweek, timespan and a bool to whether it endofsession. where can I retrieve the trading date?


Cheers.
Hello Victor,

In order to get the session settings of the instrument please check the SessionObject Members.

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Retrieve Trading Sessions Date & Time

Postby MidKnight » 11 Feb 2014

Session definitions do not have a date and it probably makes no sense for a session to have a date. Session is a template for trading days, but not trading days them self. I hope that makes sense. To get a trading date, you just use Bars.Time[0]

With kind regards,
MK

victoryong
Posts: 24
Joined: 15 Sep 2013
Has thanked: 6 times
Been thanked: 3 times

Re: Retrieve Trading Sessions Date & Time

Postby victoryong » 12 Feb 2014

Hi MK,
Thanks. I'm aware of Bars.Time[0] date. I'm interested in retrieving start trading date and end trading date. The SessionObject definitions include start and end trading time, and it does make sense to have start and end trading date in SessionObject, forex runs 24/5 or 24/7, start and end date for each trading session are different. With the date, application logic can use it to determine whether the logic is running on the correct trading session date/time. The logic to have session time in SessionObject would be the same for session date.

It does not makes sense for session to include session start and end time without start and end date.


Cheers.
Victor
Session definitions do not have a date and it probably makes no sense for a session to have a date. Session is a template for trading days, but not trading days them self. I hope that makes sense. To get a trading date, you just use Bars.Time[0]

With kind regards,
MK

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Retrieve Trading Sessions Date & Time

Postby MidKnight » 12 Feb 2014

I respectfully disagree. Having a date makes no sense to me. All that is needed is a TIME and DAY. Maybe you could give an example of your suggested session definition with a date for forex session. I run many forex symbols and have no issue with how the current session definitions are.

With kind regards,
MK

victoryong
Posts: 24
Joined: 15 Sep 2013
Has thanked: 6 times
Been thanked: 3 times

Re: Retrieve Trading Sessions Date & Time

Postby victoryong » 12 Feb 2014

;-) Maybe you have gotten use to the limitation of MC .Net and found ways around it? E.g. do you use barsback or do you use date to retrieve information from previous bars? In MC .Net, there is no in-built method to retrieve previous data using dates, users have to use barsback. Typically, users will use barsback, but what happens if the strategy required dates to retrieve previous info? And in typical case, users will just develop strategies around limitations of MC .Net, does that mean that MC .Net do not need to improve to provide in-built method to retrieve data based on date? Ok, this is just an example to explain what I've mentioned but I respect your opinion on the session trading dates.

I can use other ways too, but a more direct, dynamic and structured way of coding would be to use the session trading dates based on brokerage.


Henry,
Do you guys have method to retrieve the session trading dates?


Cheers.
Victor

I respectfully disagree. Having a date makes no sense to me. All that is needed is a TIME and DAY. Maybe you could give an example of your suggested session definition with a date for forex session. I run many forex symbols and have no issue with how the current session definitions are.

With kind regards,
MK

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Retrieve Trading Sessions Date & Time

Postby MidKnight » 12 Feb 2014

It is not that I'm used to how MC.net do it. Every charting package of the more than 1/2 dozen that I have used define a symbol with just time and day - never date.

If you gave an example of a session definition for forex that uses DATES, maybe some one could give you some answers. I cannot see how a date definition would even work.

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1503 times
Contact:

Re: Retrieve Trading Sessions Date & Time

Postby JoshM » 13 Feb 2014

(...)
Do you guys have method to retrieve the session trading dates?
(...)
If you have more than five days of historical data on your chart, you could store the session start and session end times for every historical day (Mon - Fri) in an array. Then determine some day in the future (for example 30 days from now), look up which weekday that is, and then pull the right times from the array.

* * *

How do you guys retrieve the session start and end times, btw? Perhaps I've overlooked it, but I could not find it.

User avatar
Henry MultiСharts
Posts: 9132
Joined: 25 Aug 2011
Has thanked: 1260 times
Been thanked: 2929 times

Re: Retrieve Trading Sessions Date & Time

Postby Henry MultiСharts » 13 Feb 2014

In MC .Net, there is no in-built method to retrieve previous data using dates, users have to use barsback. Typically, users will use barsback, but what happens if the strategy required dates to retrieve previous info? And in typical case, users will just develop strategies around limitations of MC .Net, does that mean that MC .Net do not need to improve to provide in-built method to retrieve data based on date? Ok, this is just an example to explain what I've mentioned but I respect your opinion on the session trading dates.
Victor, what particular limitation are you referring to? What is your test case and what is your final goal?

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Retrieve Trading Sessions Date & Time

Postby MidKnight » 13 Feb 2014

(...)
Do you guys have method to retrieve the session trading dates?
(...)
If you have more than five days of historical data on your chart, you could store the session start and session end times for every historical day (Mon - Fri) in an array. Then determine some day in the future (for example 30 days from now), look up which weekday that is, and then pull the right times from the array.

* * *

How do you guys retrieve the session start and end times, btw? Perhaps I've overlooked it, but I could not find it.
I just use the SessionObject class accessed through Bars.Sessions

victoryong
Posts: 24
Joined: 15 Sep 2013
Has thanked: 6 times
Been thanked: 3 times

Re: Retrieve Trading Sessions Date & Time

Postby victoryong » 13 Feb 2014

Hi JoshM,
Here is how session object are retrieve,

Code: Select all

IROList<PowerLanguage.SessionObject> session = Bars.Sessions;
Your idea is good, I'm actually working in the same line as your idea but rather than retrieving x days I only want current trading session date/time.


Regards,
Victor Yong
(...)
Do you guys have method to retrieve the session trading dates?
(...)
If you have more than five days of historical data on your chart, you could store the session start and session end times for every historical day (Mon - Fri) in an array. Then determine some day in the future (for example 30 days from now), look up which weekday that is, and then pull the right times from the array.

* * *

How do you guys retrieve the session start and end times, btw? Perhaps I've overlooked it, but I could not find it.

victoryong
Posts: 24
Joined: 15 Sep 2013
Has thanked: 6 times
Been thanked: 3 times

Re: Retrieve Trading Sessions Date & Time

Postby victoryong » 13 Feb 2014

Hi Henry,
MC's limitation wise, I'm speaking in general. But in specific, what irritate me the most is inability to backtest unmanaged orders, that limitation does not make sense the most. I would have expect that being able to code C# we would be able to do much more advance stuff/coding. In this particular aspect, MC is taking one step forward, two steps back (referring being able manage my own position but unable to do backtesting with unmanaged orders, which is one of the core attraction to MC's platform).

I hope there are plans to revamp the engine, to enable unmanaged orders backtesting in the near future. I believe many experience programmers would prefer such route compared to using managed orders. It would be great to have control over our orders/position with more structured coding from backtesting all the way to production level.

In terms of my actual test case, I'm unable to provide the codes. Since there are no methods to retrieve start and end session dates, I'll have to develop it or just code a workaround. But I can give a simple example of where it is needed as well,
e.g. a strategy below 1 day timeframe period that only allows one trade a day. Once the SL is trigger no orders are allow until the next trading day. In this case, it would be just easier to have start and end trading session dates & time, do an if statement to filter out the logic (of course filtering it out with the previous exit orders as well) , that would be simple, and direct. Unfortunately, a simple workaround for the logic is required when developing with MC using C#, I assume that this is the typical approach for many MC .Net users (I might be wrong, I'm new to MC .Net).



Regards,
Victor Yong
In MC .Net, there is no in-built method to retrieve previous data using dates, users have to use barsback. Typically, users will use barsback, but what happens if the strategy required dates to retrieve previous info? And in typical case, users will just develop strategies around limitations of MC .Net, does that mean that MC .Net do not need to improve to provide in-built method to retrieve data based on date? Ok, this is just an example to explain what I've mentioned but I respect your opinion on the session trading dates.
Victor, what particular limitation are you referring to? What is your test case and what is your final goal?

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Retrieve Trading Sessions Date & Time

Postby MidKnight » 14 Feb 2014

The logic expressed above is simple to do. You just need to flag session end boundary by as I had said previously, using either session object or LastBarInSession property. I personally think sesison object approach is cleaner rather than checking if previous bar was the last bar in session, but this is probably personal taste.

Once you have the session boundary determining code done, wrap all your system logic within your if !hasNotTradedToday flag.

victoryong
Posts: 24
Joined: 15 Sep 2013
Has thanked: 6 times
Been thanked: 3 times

Re: Retrieve Trading Sessions Date & Time

Postby victoryong » 14 Feb 2014

Yes, you are correct on the solution. I actually gave an invalid example.. lol.. sessionobject's dayofweek and session time is good enough for the logic.

The logic expressed above is simple to do. You just need to flag session end boundary by as I had said previously, using either session object or LastBarInSession property. I personally think sesison object approach is cleaner rather than checking if previous bar was the last bar in session, but this is probably personal taste.

Once you have the session boundary determining code done, wrap all your system logic within your if !hasNotTradedToday flag.

User avatar
Henry MultiСharts
Posts: 9132
Joined: 25 Aug 2011
Has thanked: 1260 times
Been thanked: 2929 times

Re: Retrieve Trading Sessions Date & Time

Postby Henry MultiСharts » 14 Feb 2014

But in specific, what irritate me the most is inability to backtest unmanaged orders
We are going to add this functionality in MultiCharts 9.1 with the new autotrading/backtesting engine.
Yes, you are correct on the solution. I actually gave an invalid example.. lol.. sessionobject's dayofweek and session time is good enough for the logic.
You can also use DateTime.Now.DayOfWeek to get the current day of week and compare it with the Session object.

victoryong
Posts: 24
Joined: 15 Sep 2013
Has thanked: 6 times
Been thanked: 3 times

Re: Retrieve Trading Sessions Date & Time

Postby victoryong » 14 Feb 2014

Thanks. Good to hear about it.

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1503 times
Contact:

Re: Retrieve Trading Sessions Date & Time

Postby JoshM » 15 Feb 2014

Thanks guys for how to retrieve the session times.

* * *
(...)
Do you guys have method to retrieve the session trading dates?
(...)
When exploring session information, I came across this (note: I found the 'SessionForDate' idea in Dru's TPO indicator):

Code: Select all

if ((Bars.Status == EBarState.Close) && (Bars.LastBarOnChart))
{
DateTime pastTime = DateTime.Now.AddDays(-3);// or use - TimeSpan.FromDays(3);
var newSession = Bars.Sessions.SessionForDate(pastTime);

Output.WriteLine("Past date: {0}. Session start time: {1}, end time: {2}. Start day: {3} and end day: {4}",
pastTime.ToString("d-M HH:mm:ss"),
newSession.StartTime,
newSession.EndTime,
newSession.StartDay,
newSession.EndDay);
}
Which gives the following output:

Code: Select all

Past date: 12-2 06:27:41. Session start time: 00:00:00, end time: 23:59:00. Start day: Wednesday and end day: Wednesday
Today is 15-2, so minus three days is indeed 12-2, and that was indeed Wednesday.

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1503 times
Contact:

Re: Retrieve Trading Sessions Date & Time

Postby JoshM » 15 Feb 2014

In order to get the session settings of the instrument please check the SessionObject Members.
Is it also possible to retrieve multiple sessions per day? Currently, at least how I do it, is that 'StartTime' gives the time of the first session of the day, and 'EndTime' the time at which the trading day ended. However, some symbols (like ES) have multiple sessions per day.

For example, my ES symbol has two sessions per day. Output however gives:

Code: Select all

Monday, start time: 00:00:00, End time: 23:15:00
Tuesday, start time: 00:00:00, End time: 23:15:00
Wednesday, start time: 00:00:00, End time: 23:15:00
Thursday, start time: 00:00:00, End time: 23:15:00
Friday, start time: 00:00:00, End time: 23:15:00

Code: Select all

for (int i = 0; i < Bars.Sessions.Count; i++)
{
Output.WriteLine("{0}, start time: {1}, End time: {2}",
Bars.Sessions[i].StartDay,
Bars.Sessions[i].StartTime,
Bars.Sessions[i].EndTime);
}

victoryong
Posts: 24
Joined: 15 Sep 2013
Has thanked: 6 times
Been thanked: 3 times

Re: Retrieve Trading Sessions Date & Time

Postby victoryong » 15 Feb 2014

I search and found a code that was develop by Dru,
http://www.multicharts.com/discussion/v ... sionobject

There are some limitation to the codes, but can be used to retrieve current trading session date & time.

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1503 times
Contact:

Re: Retrieve Trading Sessions Date & Time

Postby JoshM » 16 Feb 2014

In order to get the session settings of the instrument please check the SessionObject Members.
Is it also possible to retrieve multiple sessions per day?
To tentatively answer my own question, apparently not. When using the EndOfSession property, which "Returns DateTime end of session (sess) that is nearest to specified DateTime (_dt) in the future.", only the closing time of the last session is returned:

Code: Select all

protected override void CalcBar()
{
Output.WriteLine("{0} - EndOfSession: {1}",
Bars.Time[0].ToString("d-M HH:mm:ss"),
Bars.Sessions[Math.Max((int)Bars.TimeValue.DayOfWeek - 1,0)].EndOfSession(Bars.TimeValue));
}
returns..

Code: Select all

7-2 10:45:00 - EndOfSession: 7-2-2014 16:15:00
7-2 11:00:00 - EndOfSession: 7-2-2014 16:15:00
7-2 11:15:00 - EndOfSession: 7-2-2014 16:15:00
7-2 11:30:00 - EndOfSession: 7-2-2014 16:15:00
7-2 11:45:00 - EndOfSession: 7-2-2014 16:15:00
7-2 12:00:00 - EndOfSession: 7-2-2014 16:15:00
7-2 12:15:00 - EndOfSession: 7-2-2014 16:15:00
7-2 12:30:00 - EndOfSession: 7-2-2014 16:15:00
7-2 12:45:00 - EndOfSession: 7-2-2014 16:15:00
7-2 13:00:00 - EndOfSession: 7-2-2014 16:15:00
7-2 13:15:00 - EndOfSession: 7-2-2014 16:15:00
7-2 13:30:00 - EndOfSession: 7-2-2014 16:15:00
7-2 13:45:00 - EndOfSession: 7-2-2014 16:15:00
7-2 14:00:00 - EndOfSession: 7-2-2014 16:15:00
7-2 14:15:00 - EndOfSession: 7-2-2014 16:15:00
7-2 14:30:00 - EndOfSession: 7-2-2014 16:15:00
7-2 14:45:00 - EndOfSession: 7-2-2014 16:15:00
7-2 15:00:00 - EndOfSession: 7-2-2014 16:15:00
7-2 15:15:00 - EndOfSession: 7-2-2014 16:15:00
7-2 15:45:00 - EndOfSession: 7-2-2014 16:15:00
7-2 16:00:00 - EndOfSession: 7-2-2014 16:15:00
7-2 16:15:00 - EndOfSession: 7-2-2014 16:15:00
9-2 17:15:00 - EndOfSession: 10-2-2014 16:15:00
9-2 17:30:00 - EndOfSession: 10-2-2014 16:15:00
9-2 17:45:00 - EndOfSession: 10-2-2014 16:15:00
9-2 18:00:00 - EndOfSession: 10-2-2014 16:15:00
9-2 18:15:00 - EndOfSession: 10-2-2014 16:15:00
9-2 18:30:00 - EndOfSession: 10-2-2014 16:15:00
9-2 18:45:00 - EndOfSession: 10-2-2014 16:15:00
9-2 19:00:00 - EndOfSession: 10-2-2014 16:15:00
9-2 19:15:00 - EndOfSession: 10-2-2014 16:15:00
9-2 19:30:00 - EndOfSession: 10-2-2014 16:15:00
9-2 19:45:00 - EndOfSession: 10-2-2014 16:15:00
9-2 20:00:00 - EndOfSession: 10-2-2014 16:15:00
9-2 20:15:00 - EndOfSession: 10-2-2014 16:15:00
9-2 20:30:00 - EndOfSession: 10-2-2014 16:15:00
9-2 20:45:00 - EndOfSession: 10-2-2014 16:15:00
9-2 21:00:00 - EndOfSession: 10-2-2014 16:15:00
9-2 21:15:00 - EndOfSession: 10-2-2014 16:15:00
9-2 21:30:00 - EndOfSession: 10-2-2014 16:15:00
9-2 21:45:00 - EndOfSession: 10-2-2014 16:15:00
9-2 22:00:00 - EndOfSession: 10-2-2014 16:15:00
9-2 22:15:00 - EndOfSession: 10-2-2014 16:15:00
9-2 22:30:00 - EndOfSession: 10-2-2014 16:15:00
9-2 22:45:00 - EndOfSession: 10-2-2014 16:15:00
9-2 23:00:00 - EndOfSession: 10-2-2014 16:15:00
9-2 23:15:00 - EndOfSession: 10-2-2014 16:15:00
9-2 23:30:00 - EndOfSession: 10-2-2014 16:15:00
9-2 23:45:00 - EndOfSession: 10-2-2014 16:15:00
10-2 00:00:00 - EndOfSession: 10-2-2014 16:15:00
10-2 00:15:00 - EndOfSession: 10-2-2014 16:15:00
10-2 00:30:00 - EndOfSession: 10-2-2014 16:15:00
10-2 00:45:00 - EndOfSession: 10-2-2014 16:15:00
10-2 01:00:00 - EndOfSession: 10-2-2014 16:15:00
10-2 01:15:00 - EndOfSession: 10-2-2014 16:15:00
10-2 01:30:00 - EndOfSession: 10-2-2014 16:15:00
10-2 01:45:00 - EndOfSession: 10-2-2014 16:15:00
10-2 02:00:00 - EndOfSession: 10-2-2014 16:15:00
10-2 02:15:00 - EndOfSession: 10-2-2014 16:15:00
10-2 02:30:00 - EndOfSession: 10-2-2014 16:15:00
10-2 02:45:00 - EndOfSession: 10-2-2014 16:15:00
10-2 03:00:00 - EndOfSession: 10-2-2014 16:15:00
10-2 03:15:00 - EndOfSession: 10-2-2014 16:15:00
10-2 03:30:00 - EndOfSession: 10-2-2014 16:15:00
10-2 03:45:00 - EndOfSession: 10-2-2014 16:15:00
I have the session template of the symbol set to 'Default' and this shows in the QuoteManager:

(Session 1 from 17:00 to 15:15 and session 2 from 15:30 to 16:15).

Image

Am I doing something wrong?

If not, when is this big limitation removed?* Even the regular MultiCharts allows retrieving times for multiple sessions (i.e. Sess1FirstBarTime, Sess2FirstBarTime).

*: But without code breaking changes if you don't mind me saying. For example, rather add something like 'Bars.Session[index weekday].FirstSessStart' and 'Bars.Session[index weekday].SecondSessStart'.
Attachments
scr.16-02-2014 08.12.09.png
(32.89 KiB) Downloaded 3839 times

User avatar
Henry MultiСharts
Posts: 9132
Joined: 25 Aug 2011
Has thanked: 1260 times
Been thanked: 2929 times

Re: Retrieve Trading Sessions Date & Time

Postby Henry MultiСharts » 18 Feb 2014

Is it also possible to retrieve multiple sessions per day?
JoshM, unfortunately that is not possible at the moment. This functionality will be added in MultiCharts .NET 9.0 Beta1


Return to “MultiCharts .NET”