Advanced - Timestamp and Trading Session

Just like Open, High, Low, Close and BarVolume, Medved Trader provides another indexed variable - Timestamp. It gives you the DateTime for the candle. Again, just like for others, Timestamp (or Timestamp[0]) is for the current candle being processed, Timestamp[1] would be for the timestamp of the previous candle, etc.

 

In order to provide further functionality and flexibility to the paintbar calculation, you can get the trading session parameters for a particular timestamp. Of course, that will only work when the paintbar is applied to an intraday chart. You can use the IsOnHistoricalChart and IsOnIntradayChart calls to determine what kind of chart the paintbar is being drawn on.

 

Note that all the DateTime variables provided are in UTC.

 

To get the trading session, we provide this system variable:

 

public TradingSessionInfo TradingDay;   // this is the current candle's trading day information

 

and function:

 

public TradingSessionInfo GetTradingSessionInfo(DateTime timestamp); // this can get trading day for candle other than the current one

 

You pass it a DateTime (usually the timestamp of a candle) and it returns to you the TradingSessionInfo struct that gives you the following info:

 

Session Parameters

Name

Type

Purpose

DayNumber

int

the # of the day in the list of trading sessions. This is useful if you want to compare it to the DayNumber of another timestamp, to see whether the trading sessions for both timestamps are the same

DayStart

DateTime

the start of day for the session. For example, for NYSE timeframe it would be at midnight.

DayEnd

DateTime

the end of day for the session. For example, for NYSE timeframe it would be at midnight.

SessionStart

DateTime

the start of regular trading session. For example, for NYSE timeframe it would be 9:30 am Eastern.

SessionEnd

DateTime

the end of regular trading session. For example, for NYSE timeframe it would be 4:00 pm Eastern.

NumberOfBreaks

int

Some trading sessions have mid-day breaks - one or two. For example, the Hang Seng exchange has a lunch break from noon to 1pm local time. So, for a Hang Seng timeframe, the NumberOfBreaks would be 1.

BreakStart

DateTime[]

If there are mid-day breaks in the timeframe for the sessions, this would hold the starting times for the breaks. Otherwise (if no breaks), it's null.

BreakEnd

DateTime[]

If there are mid-day breaks in the timeframe for the sessions, this would hold the ending times for the breaks. Otherwise (if no breaks), it's null.

Timestamp-specific Parameters

Name

Type

Purpose

IsAfterHours

Boolean

true if the DateTime with which the TradingSessionInfo was created falls between SessionEnd and DayEnd

IsPreMarket

Boolean

true if the DateTime with which the TradingSessionInfo was created falls between DayStart and SessionStart

OnBreak

int

Returns 1 or 2 if the DateTime falls into first or second mid-day break, 0 otherwise

 

Example code - this would color the paintbar only during pre-market.

 

    if (TradingDay.IsPreMarket)

    {

        SetColor("PreMarket", SysColor.Last);

    }

 

or an equivalent

 

    TradingSessionInfo session = GetTradingSessionInfo(Timestamp);

    

    if (session.IsPreMarket)

    {

        SetColor("PreMarket", SysColor.Last);

    }