Problem with sessionstarttime

Questions about MultiCharts and user contributed studies.
Nick
Posts: 490
Joined: 04 Aug 2006
Has thanked: 4 times
Been thanked: 24 times

Problem with sessionstarttime

Postby Nick » 17 Feb 2012

For currencies I have my sessions set up

Sun 22:00 - Mon 07:00
Mon 07:00 - Tues 07:00
etc.

These are London times. I want to reset an indicator on the first bar of a session so used

if time > sessionstarttime(0,1) and time[1] <= sessionstarttime(0,1) then........

Whatever parameters I use for sessionstarttime (e.g. sessionstarttime(1,2)) it returns 22:00.

I worked around the issue by using sessionendtime which returns 07:00.

User avatar
Henry MultiСharts
Posts: 8479
Joined: 25 Aug 2011
Has thanked: 1211 times
Been thanked: 2719 times

Re: Problem with sessionstarttime

Postby Henry MultiСharts » 17 Feb 2012

Hello Nick,

You need to select the same time zone both in Quote manager->Edit symbol->Sessions tab and Format->Instrument->Settings tab.
In this case you will have the same timestamps in the symbol sessions tab, on the chart and in PLE.

Nick
Posts: 490
Joined: 04 Aug 2006
Has thanked: 4 times
Been thanked: 24 times

Re: Problem with sessionstarttime

Postby Nick » 21 Feb 2012

The problem is not to do with exchange or local time (It happens however this is set). The problem is that the function sessionstarttime (and sessionendtime) returns the start of the very first session defined in QM. That is the only value ever returned.

So using my example even if you are processing a bar on Monday it returns the session start time from Sunday. So whichever day of the week the bar you are processing occurs on it returns 22:00.

User avatar
JoshM
Posts: 2084
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1483 times
Contact:

Re: Problem with sessionstarttime

Postby JoshM » 22 Feb 2012

Nick wrote:The problem is not to do with exchange or local time (It happens however this is set). The problem is that the function sessionstarttime (and sessionendtime) returns the start of the very first session defined in QM. That is the only value ever returned.

So using my example even if you are processing a bar on Monday it returns the session start time from Sunday. So whichever day of the week the bar you are processing occurs on it returns 22:00.

That is correct. I had the same problem (with MC7.4) when the first session time of Monday was different from the session times from other days of the weeks. Calling SessionStartTime would return the first session time of Monday, even if it was called on another day of the week.

I "solved" the problem by giving each day the same session start time in the QuoteManager, so that SessionStartTime() would return correct values.

I agree with you here that there seems to be an problem or bug with SessionStartTime.

User avatar
Henry MultiСharts
Posts: 8479
Joined: 25 Aug 2011
Has thanked: 1211 times
Been thanked: 2719 times

Re: Problem with sessionstarttime

Postby Henry MultiСharts » 22 Feb 2012

This is neither a problem nor a bug. This is expected behaviour.

SessionNum - a numerical expression specifying the Session Number.
This value ranges from 1 to the maximum number of sessions present in Quote Manager->Edit symbol->Sessions tab.
Each line is counted starting from the first session.
Attachments
session start-end time.png
session start-end time.png (31.93 KiB) Viewed 489 times
These users thanked the author Henry MultiСharts for the post:
JoshM

Nick
Posts: 490
Joined: 04 Aug 2006
Has thanked: 4 times
Been thanked: 24 times

Re: Problem with sessionstarttime

Postby Nick » 22 Feb 2012

Thanks for the detailed reply Henry.

Odd, I have re-applied the study to a new chart with a newly constructed session template and it seems OK. Guess that means I must have previously mucked something up though was aware how the sessionnumber parameter worked.

A side question is there a function that returns a number representing the session you are in? I want a way to know the start/end time for the current session whichever one it might be.

User avatar
JoshM
Posts: 2084
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1483 times
Contact:

Re: Problem with sessionstarttime

Postby JoshM » 22 Feb 2012

Thanks Henry, I wasn't aware of that and read the help wrong.

Nick wrote:A side question is there a function that returns a number representing the session you are in? I want a way to know the start/end time for the current session whichever one it might be.

Depending on how many sessions you have defined per day, perhaps something like this (with one session per day):

Code: Select all

SessionStartTime(0, DayOfWeek(ComputerDateTime) + 1);

Or replacing the 0 with 1 for a 'regular' session per day.
These users thanked the author JoshM for the post:
Henry MultiСharts

Nick
Posts: 490
Joined: 04 Aug 2006
Has thanked: 4 times
Been thanked: 24 times

Re: Problem with sessionstarttime

Postby Nick » 22 Feb 2012

Yes that might do thanks. I was trying to get something that would work regardless of the instrument.

User avatar
Henry MultiСharts
Posts: 8479
Joined: 25 Aug 2011
Has thanked: 1211 times
Been thanked: 2719 times

Re: Problem with sessionstarttime

Postby Henry MultiСharts » 23 Feb 2012

Nick wrote:A side question is there a function that returns a number representing the session you are in? I want a way to know the start/end time for the current session whichever one it might be.

Unfortunately there is no prebuilt function that returns a number representing the current session you are in.
I believe Josh has suggested a good workaround.

User avatar
JoshM
Posts: 2084
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1483 times
Contact:

Re: Problem with sessionstarttime

Postby JoshM » 02 Jul 2012

I've spend 4+ hours trying to make a function that returns the session begin time using the Session reserved words. Sounds simple enough, but my PowerLanguage skills are not suited to this challenge.

For example, when there are only 5 sessions (Mon, Tue, Wed, Thu, Fri) that all close on the same day, it's easy to get the begin and end time of each sessions when using the DayOfWeek(Date) substitute for the SessionNumber in the QuoteManager. The problems start however when an instrument starts trading on Sunday (like FX pairs).

Then I run into a runtime error since a correction for the DayOfWeek(Date) substitute is needed since DayOfWeek(Date) returns a zero for Sunday, which off course doesn't work since there's no SessionNumber of zero. So I use a work around for this:

Code: Select all

sessionNumber = IFF(SessionStartDay(SessionType, 1) = 0, MinList(DayOfWeek(Date), 1), DayOfWeek(Date));


However, if I then try to get the session begin time and session end time from an instrument with multiple sessions per day (say ES), another string of run time errors pops up. Okay, fair enough so we'll need to make an adjustment for those instruments:

Code: Select all

   Print("SessionCount: ", NumToStr(SessCount, 0), ", SessionNumber: ", NumToStr(sessionNumber, 0), " EndTime: ",
      SessionEndTime(SessionType, SessionNumber * 2), " StartTime: ", SessionStartTime(SessionType, SessionNumber));


At that point I was a little bit confident I got it solved -- until I changed the chart time to Exchange and guess what: the code generated on every symbol again run time errors. So, now I need to build in a check to see if the chart time settings are Local or Exchange. As far as I know, such a thing cannot be retrieved with PowerLanguage. So I'm quite stuck.

Hence my question:
    How can I retrieve the session begin time and session end time for symbols through PowerLanguage?

(And yes, my QuoteManager settings are correct. And yes, I know I can hard code the session begin and end time in a function and use a switch function to return these; but that will not make the code portable.)

SP
Posts: 444
Joined: 06 Feb 2006
Has thanked: 36 times
Been thanked: 280 times

Re: Problem with sessionstarttime

Postby SP » 02 Jul 2012

Maybe SessionCountDay could help:

Code: Select all

variables:
   zaehler( 0 ) ;

for zaehler = 1 to SessionCountDay(0, DayOfWeek(Date))
begin
   print(" SessionNumber #:", NumToStr(zaehler, 0), Spaces(5 - StrLen(NumToStr(zaehler, 0))), 
       SessionStartTime(0, zaehler), " - ", SessionEndTime(0, zaehler));
end ;
These users thanked the author SP for the post:
JoshM

User avatar
JoshM
Posts: 2084
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1483 times
Contact:

Re: Problem with sessionstarttime

Postby JoshM » 02 Jul 2012

SP wrote:Maybe SessionCountDay could help:

Thanks SP :). I wasn't aware of the SessionCountDay function.

For FESX, it works correctly:

Code: Select all

FESX SessionNumber #:1     800.00 - 2200.00


For ES it returns on every bar (Exchange time):

Code: Select all

ES SessionNumber #:1    1700.00 -  830.00

Which is correct for the first session starting Sunday afternoon, but the next sessions in the week start on 15:30 (CT time).

For EURUSD (Spot, MB Trading) it returns nothing since the SessionCountDay function returns zero for this instrument.

I'll will experiment further with adding checks for this behaviour, but I'm afraid it's the same result as I had earlier.

Nick
Posts: 490
Joined: 04 Aug 2006
Has thanked: 4 times
Been thanked: 24 times

Re: Problem with sessionstarttime

Postby Nick » 01 Aug 2012

I decided to revisit this (as I am trading a couple of different instruments to my old faves).

Did anyone crack it so that it works for all instruments be it Spot FX, Globex, or any other exchange round the world??

I thought I had but still unpredictable sometimes when I load up a brand new instrument with different session parameters.

All I want to do is reset an indicator on the transition of the last bar of session x to the first bar of session y!!!!

Nick
Posts: 490
Joined: 04 Aug 2006
Has thanked: 4 times
Been thanked: 24 times

Re: Problem with sessionstarttime

Postby Nick » 01 Aug 2012

Can anyone explain what is the difference between AutoSession an RegularSession is supposed to be. They currently return identical values when used in any session function. The following code illustrates that.

Code: Select all

var: n(0);
once
begin
  for n = 1 to sessioncount (0) begin
    print (n," ",SessionStarttime(0,n)," ",sessionendtime(0,n));
  end;

  print ("_____________");

  for n = 1 to sessioncount (1) begin
    print (n," ",SessionStarttime(1,n)," ",sessionendtime(1,n));
  end;
end;


Edit: Regular Session should only return the sessions with a tick in the checkbox on the session template? It doesn't.

Nick
Posts: 490
Joined: 04 Aug 2006
Has thanked: 4 times
Been thanked: 24 times

Re: Problem with sessionstarttime

Postby Nick » 01 Aug 2012

Im thinking something like this will work in most cases, its still not really satisfactory

Code: Select all

n(0),WeekDaySession(0);
once repeat until WeekDaySession >= 1 begin  // get first weekday session
  n = + 1;
  WeekDaySession = SessionEndDay(1,n);
end;
if time  > sessionendtime(0,WeekDaySession) and time[1] <= sessionendtime(0,WeekDaySession) .....bar is first bar of the first weekday session
These users thanked the author Nick for the post:
PK1


Return to “MultiCharts”