Skip to main content

Trading Sessions Provider

TradingSessionsProvider is responsible for getting selected instrument's trading schedule.

DXcharts React requests sessions up to +14 days from Date.now() for a correct work, so make sure you've provided enough.

There are 4 types of trading sessions supported:

type SessionType = 'NO_TRADING' | 'PRE_MARKET' | 'REGULAR' | 'AFTER_MARKET';

TradingSessions incorporates from and to timestamps, which is a range of time when the session is active, and type of the session.

const tradingSessions: TradingSession[] = [
{
// as you can see here, NO_TRADING session is defined as from 12:00 AM to 8:00 AM on 10 Jul
from: 1720569600000, // GMT: Wednesday, July 10, 2024 12:00:00 AM
to: 1720598400000, // GMT: Wednesday, July 10, 2024 8:00:00 AM
type: 'NO_TRADING',
},
];

What you should do is just to define instrument's trading sessions one by one, one after another, like:

const tradingSessions: TradingSession[] = [
{
// as you can see here, you define NO_TRADING session from 12:00 AM to 8:00 AM on 10 Jul
from: 1720569600000, // GMT: Wednesday, July 10, 2024 12:00:00 AM
to: 1720598400000, // GMT: Wednesday, July 10, 2024 8:00:00 AM
type: 'NO_TRADING',
},
{
// after that obviously PRE_MARKET sessions starts from 8:00 AM to 12:00 PM
from: 1720598400000, // GMT: Wednesday, July 10, 2024 8:00:00 AM
to: 1720612800000, // GMT: Wednesday, July 10, 2024 12:00:00 PM
type: 'PRE_MARKET',
},
{
// then REGULAR session starts from 12:00 PM to 8:00 PM
from: 1720612800000, // GMT: Wednesday, July 10, 2024 12:00:00 PM
to: 1720641600000, // GMT: Wednesday, July 10, 2024 8:00:00 PM
type: 'REGULAR',
},
{
// and AFTER_MARKET then starts from 8:00 PM Jul 10 to 12:00 AM Jul 11
from: 1720641600000, // GMT: Wednesday, July 10, 2024 8:00:00 PM
to: 1720656000000, // GMT: Thursday, July 11, 2024 12:00:00 AM
type: 'AFTER_MARKET',
},
{
// and so on...
from: 1720656000000,
to: 1720684800000,
type: 'NO_TRADING',
},
];

With tradable weekends and holidays the situation is the same - just define TradingSession in the same format for them.

How it works

/** Copyright ©2024 Devexperts LLC.
All rights reserved. Any unauthorized use will constitute an infringement of copyright.
In case of any questions regarding types of use, please contact legal@devexperts.com.
This notice must remain intact.
**/
import { generateTradingSessions } from '@dx-private/dxchart5-react/dist/utils/generator/trading-sessions-generator.utils';
const MOCK_TIME_ZONE = 'America/New_York';
/**
* Creates mock implementation of {@link TradingSessionsProvider}.
*/
export const createMockTradingSessionsProvider = () => {
const sessionsCache = new Map();
const timezonesCache = new Map();
// NOTE: chart requests sessions up to +14 days from Date.now() for a correct work,
// so make sure you've provided enough sessions.
const MOCK_TRADING_SESSIONS = generateTradingSessions();
const requestSession = (tradingHours) => {
const cached = sessionsCache.get(tradingHours);
if (cached) {
return cached;
}
const req = new Promise(resolve => setTimeout(() => resolve(MOCK_TRADING_SESSIONS), 1000));
sessionsCache.set(tradingHours, req);
return req;
};
const requestTimezone = (tradingHours) => {
const cached = timezonesCache.get(tradingHours);
if (cached) {
return cached;
}
const req = new Promise(resolve => setTimeout(() => resolve(MOCK_TIME_ZONE), 1000));
timezonesCache.set(tradingHours, req);
return req;
};
/**
* `options.tradingHours` and `options.symbol` properties are taken by `dxcharts-react` from the information
* about `Instrument` you've provided via `SymbolSuggestProvider`.
*
* @example
* options.tradingHours = "NewYorkUS(rt=0300;0=p04000930r09301600a16002000)";
* options.symbol = "AAPL";
*/
const getTimeZone = (options) => {
if (options.tradingHours === undefined) {
return Promise.resolve('UTC');
}
const tradingHours = options.tradingHours;
return requestTimezone(tradingHours);
};
const generateSessions = (from, to, options) => {
const tradingHours = options.tradingHours;
return tradingHours
? requestSession(tradingHours).then((sessions) => {
return sessions.filter(session => session.to >= from && session.from <= to);
})
: Promise.resolve([]);
};
return {
generateSessions,
getTimeZone,
};
};

Indicators that require Trading Session Provider

The following indicators require TradingSessionsProvider to work correctly:

VWAP (Volume Weighted Average Price)

Uses Trading Session Provider to calculate cumulative sum within a trading session.

HistoricalVolatility

Uses Trading Session Provider to get the start and end times of regular trading sessions from data.sessions.

PivotPoints

Uses Trading Session Provider to get previous aggregation data.

API reference

TradingSessionsProvider

To change the way trading sessions work

TradingSessionsProvider.generateSessions
TradingSessionsProvider.generateSessions(from: number, to: number, options: GenerateSessionOptions): Promise<TradingSession[]>

Generates trading sessions

Parameters
from: number
timestamp
to: number
timestamp
options: GenerateSessionOptions
see
Returns
Promise<TradingSession[]>
TradingSessionsProvider.getTimeZone
TradingSessionsProvider.getTimeZone(options: GenerateSessionOptions): Promise<string>

Returns timezone for specified symbol and trading hours

Parameters
options: GenerateSessionOptions
Returns
Promise<string>