PINE LIBRARY

chrono_utils

Library "chrono_utils"
Collection of objects and common functions that are related to datetime windows session days and time
ranges. The main purpose of this library is to handle time-related functionality and make it easy to reason about a
future bar and see if it is part of a predefined user session and/or inside a datetime window. All existing session
functions I found in the documentation e.g. "not na(time(timeframe, session, timezone))" are not suitable for
strategies, since the execution of the orders is delayed by one bar due to the execution happening at the bar close.
So a prediction for the next bar is necessary. Moreover, a history operator with a negative value is not allowed e.g.
`not na(time(timeframe, session, timezone)[-1])` expression is not valid. Thus, I created this library to overcome
this small but very important limitation. In the meantime, I added useful functionality to handle session-based
behavior. An interesting utility that emerged from this development is data anomaly detection where a comparison
between the prediction and the actual value is happening. If those two values are different then a data inconsistency
happens between the prediction bar and the actual bar (probably due to a holiday or half session day etc..)

exTimezone(timezone)
  exTimezone - Convert extended timezone to timezone string
  Parameters:
    timezone (simple string): - The timezone or a special string
  Returns: string representing the timezone

nameOfDay(day)
  nameOfDay - Convert the day id into a short nameOfDay
  Parameters:
    day (int): - The day id to convert
  Returns: - The short name of the day

today()
  today - Get the day id of this day
  Returns: - The day id

nthDayAfter(day, n)
  nthDayAfter - Get the day id of n days after the given day
  Parameters:
    day (int): - The day id of the reference day
    n (int): - The number of days to go forward
  Returns: - The day id of the day that is n days after the reference day

nextDayAfter(day)
  nextDayAfter - Get the day id of next day after the given day
  Parameters:
    day (int): - The day id of the reference day
  Returns: - The day id of the next day after the reference day

nthDayBefore(day, n)
  nthDayBefore - Get the day id of n days before the given day
  Parameters:
    day (int): - The day id of the reference day
    n (int): - The number of days to go forward
  Returns: - The day id of the day that is n days before the reference day

prevDayBefore(day)
  prevDayBefore - Get the day id of previous day before the given day
  Parameters:
    day (int): - The day id of the reference day
  Returns: - The day id of the previous day before the reference day

tomorrow()
  tomorrow - Get the day id of the next day
  Returns: - The next day day id

normalize(num, min, max)
  normalizeHour - Check if number is inthe range of [min, max]
  Parameters:
    num (int)
    min (int)
    max (int)
  Returns: - The normalized number

normalizeHour(hourInDay)
  normalizeHour - Check if hour is valid and return a noralized hour range from [0, 24]
  Parameters:
    hourInDay (int)
  Returns: - The normalized hour

normalizeMinute(minuteInHour)
  normalizeMinute - Check if minute is valid and return a noralized minute from [0, 59]
  Parameters:
    minuteInHour (int)
  Returns: - The normalized minute

monthInMilliseconds(mon)
  monthInMilliseconds - Calculate the miliseconds in one bar of the timeframe
  Parameters:
    mon (int): - The month of reference to get the miliseconds
  Returns: - The number of milliseconds of the month

barInMilliseconds()
  barInMilliseconds - Calculate the miliseconds in one bar of the timeframe
  Returns: - The number of milliseconds in one bar

method init(this, fromDateTime, toDateTime)
  init - Initialize the time window object from boolean values of each session day
  Namespace types: DateTimeWindow
  Parameters:
    this (DateTimeWindow): - The time window object that will hold the from and to datetimes
    fromDateTime (int): - The starting datetime of the time window
    toDateTime (int): - The ending datetime of the time window
  Returns: - The time window object

method init(this, refTimezone, chTimezone, fromDateTime, toDateTime)
  init - Initialize the time window object from boolean values of each session day
  Namespace types: DateTimeWindow
  Parameters:
    this (DateTimeWindow): - The time window object that will hold the from and to datetimes
    refTimezone (simple string): - The timezone of reference of the 'from' and 'to' dates
    chTimezone (simple string): - The target timezone to convert the 'from' and 'to' dates
    fromDateTime (int): - The starting datetime of the time window
    toDateTime (int): - The ending datetime of the time window
  Returns: - The time window object

method init(this, sun, mon, tue, wed, thu, fri, sat)
  init - Initialize the session days object from boolean values of each session day
  Namespace types: SessionDays
  Parameters:
    this (SessionDays): - The session days object that will hold the day selection
    sun (bool): - Is Sunday a trading day?
    mon (bool): - Is Monday a trading day?
    tue (bool): - Is Tuesday a trading day?
    wed (bool): - Is Wednesday a trading day?
    thu (bool): - Is Thursday a trading day?
    fri (bool): - Is Friday a trading day?
    sat (bool): - Is Saturday a trading day?
  Returns: - The session days objectfrom_chart

method init(this, unixTime)
  init - Initialize the object from the hour and minute of the session time in exchange timezone (syminfo.timezone)
  Namespace types: SessionTime
  Parameters:
    this (SessionTime): - The session time object with the hour and minute of the time of the day
    unixTime (int): - The unix time
  Returns: - The session time object

method init(this, hourInDay, minuteInHour)
  init - Initialize the object from the hour and minute of the session time in exchange timezone (syminfo.timezone)
  Namespace types: SessionTime
  Parameters:
    this (SessionTime): - The session time object with the hour and minute of the time of the day
    hourInDay (int): - The hour of the time
    minuteInHour (int): - The minute of the time
  Returns: - The session time object

method init(this, hourInDay, minuteInHour, refTimezone)
  init - Initialize the object from the hour and minute of the session time
  Namespace types: SessionTime
  Parameters:
    this (SessionTime): - The session time object with the hour and minute of the time of the day
    hourInDay (int): - The hour of the time
    minuteInHour (int): - The minute of the time
    refTimezone (string): - The timezone of reference of the 'hour' and 'minute'
  Returns: - The session time object

method init(this, startTime, endTime)
  init - Initialize the object from the start and end session time in exchange timezone (syminfo.timezone)
  Namespace types: SessionTimeRange
  Parameters:
    this (SessionTimeRange): - The session time range object that will hold the start and end time of the daily session
    startTime (SessionTime): - The time the session begins
    endTime (SessionTime): - The time the session ends
  Returns: - The session time range object

method init(this, startTimeHour, startTimeMinute, endTimeHour, endTimeMinute, refTimezone)
  init - Initialize the object from the start and end session time
  Namespace types: SessionTimeRange
  Parameters:
    this (SessionTimeRange): - The session time range object that will hold the start and end time of the daily session
    startTimeHour (int): - The time hour the session begins
    startTimeMinute (int): - The time minute the session begins
    endTimeHour (int): - The time hour the session ends
    endTimeMinute (int): - The time minute the session ends
    refTimezone (string)
  Returns: - The session time range object

method init(this, days, timeRanges)
  init - Initialize the user session object from session days and time range
  Namespace types: UserSession
  Parameters:
    this (UserSession): - The user-defined session object that will hold the day and the time range selection
    days (SessionDays): - The session days object that defines the days the session is happening
    timeRanges (SessionTimeRange[]): - The array of all the session time ranges during a session day
  Returns: - The user session object

method to_string(this)
  to_string - Formats the time window into a human-readable string
  Namespace types: DateTimeWindow
  Parameters:
    this (DateTimeWindow): - The time window object with the from and to datetimes
  Returns: - The string of the time window

method to_string(this)
  to_string - Formats the session days into a human-readable string with short day names
  Namespace types: SessionDays
  Parameters:
    this (SessionDays): - The session days object with the day selection
  Returns: - The string of the session day short names

method to_string(this)
  to_string - Formats the session time into a human-readable string
  Namespace types: SessionTime
  Parameters:
    this (SessionTime): - The session time object with the hour and minute of the time of the day
  Returns: - The string of the session time

method to_string(this)
  to_string - Formats the session time into a human-readable string
  Namespace types: SessionTimeRange
  Parameters:
    this (SessionTimeRange): - The session time range object with the start and end time of the daily session
  Returns: - The string of the session time

method to_string(this)
  to_string - Formats the user session into a human-readable string
  Namespace types: UserSession
  Parameters:
    this (UserSession): - The user-defined session object with the day and the time range selection
  Returns: - The string of the user session

method to_string(this)
  to_string - Formats the bar into a human-readable string
  Namespace types: Bar
  Parameters:
    this (Bar): - The bar object with the open and close times
  Returns: - The string of the bar times

method to_string(this)
  to_string - Formats the chart session into a human-readable string
  Namespace types: ChartSession
  Parameters:
    this (ChartSession): - The chart session object that contains the days and the time range shown in the chart
  Returns: - The string of the chart session

method get_size_in_secs(this)
  get_size_in_secs - Count the seconds from start to end in the given timeframe
  Namespace types: DateTimeWindow
  Parameters:
    this (DateTimeWindow): - The time window object with the from and to datetimes
  Returns: - The number of seconds inside the time widow for the given timeframe

method get_size_in_secs(this)
  get_size_in_secs - Calculate the seconds inside the session
  Namespace types: SessionTimeRange
  Parameters:
    this (SessionTimeRange): - The session time range object with the start and end time of the daily session
  Returns: - The number of seconds inside the session

method get_size_in_bars(this)
  get_size_in_bars - Count the bars from start to end in the given timeframe
  Namespace types: DateTimeWindow
  Parameters:
    this (DateTimeWindow): - The time window object with the from and to datetimes
  Returns: - The number of bars inside the time widow for the given timeframe

method get_size_in_bars(this)
  get_size_in_bars - Calculate the bars inside the session
  Namespace types: SessionTimeRange
  Parameters:
    this (SessionTimeRange): - The session time range object with the start and end time of the daily session
  Returns: - The number of bars inside the session for the given timeframe

method from_chart(this)
  from_chart - Initialize the session days object from the chart
  Namespace types: SessionDays
  Parameters:
    this (SessionDays): - The session days object that will hold the day selection
  Returns: - The user session object

method from_chart(this)
  from_chart - Initialize the session time range object from the chart
  Namespace types: SessionTimeRange
  Parameters:
    this (SessionTimeRange): - The session time range object that will hold the start and end time of the daily session
  Returns: - The session time range object

method from_chart(this)
  from_chart - Initialize the session object from the chart
  Namespace types: ChartSession
  Parameters:
    this (ChartSession): - The chart session object that will hold the days and the time range shown in the chart
  Returns: - The chart session object

method to_sess_string(this)
  to_sess_string - Formats the session days into a session string with day ids
  Namespace types: SessionDays
  Parameters:
    this (SessionDays): - The session days object
  Returns: - The string of the session day ids

method to_sess_string(this)
  to_sess_string - Formats the session time into a session string
  Namespace types: SessionTime
  Parameters:
    this (SessionTime): - The session time object with the hour and minute of the time of the day
  Returns: - The string of the session time

method to_sess_string(this)
  to_sess_string - Formats the session time into a session string
  Namespace types: SessionTimeRange
  Parameters:
    this (SessionTimeRange): - The session time range object with the start and end time of the daily session
  Returns: - The string of the session time

method to_sess_string(this)
  to_sess_string - Formats the user session into a session string
  Namespace types: UserSession
  Parameters:
    this (UserSession): - The user-defined session object with the day and the time range selection
  Returns: - The string of the user session

method to_sess_string(this)
  to_sess_string - Formats the chart session into a session string
  Namespace types: ChartSession
  Parameters:
    this (ChartSession): - The chart session object that contains the days and the time range shown in the chart
  Returns: - The string of the chart session

method from_sess_string(this, sess)
  from_sess_string - Initialize the session days object from the session string
  Namespace types: SessionDays
  Parameters:
    this (SessionDays): - The session days object that will hold the day selection
    sess (string): - The session string part that represents the days [1-7]
  Returns: - The session days object

method from_sess_string(this, sess)
  from_sess_string - Initialize the session time object from the session string in exchange timezone (syminfo.timezone)
  Namespace types: SessionTime
  Parameters:
    this (SessionTime): - The session time object that will hold the hour and minute of the time
    sess (string): - The session string part that represents the time HHmm
  Returns: - The session time object

method from_sess_string(this, sess, refTimezone)
  from_sess_string - Initialize the session time object from the session string
  Namespace types: SessionTime
  Parameters:
    this (SessionTime): - The session time object that will hold the hour and minute of the time
    sess (string): - The session string part that represents the time HHmm
    refTimezone (simple string): - The timezone of reference of the 'hour' and 'minute'
  Returns: - The session time object

method from_sess_string(this, sess)
  from_sess_string - Initialize the session time range object from the session string in exchange timezone (syminfo.timezone)
  Namespace types: SessionTimeRange
  Parameters:
    this (SessionTimeRange): - The session time range object that will hold the start and end time of the daily session
    sess (string): - The session string part that represents the time range HHmm-HHmm
  Returns: - The session time range object

method from_sess_string(this, sess, refTimezone)
  from_sess_string - Initialize the session time range object from the session string
  Namespace types: SessionTimeRange
  Parameters:
    this (SessionTimeRange): - The session time range object that will hold the start and end time of the daily session
    sess (string): - The session string part that represents the time range HHmm-HHmm
    refTimezone (simple string): - The timezone of reference of the time ranges
  Returns: - The session time range object

method from_sess_string(this, sess)
  from_sess_string - Initialize the user session object from the session string in exchange timezone (syminfo.timezone)
  Namespace types: UserSession
  Parameters:
    this (UserSession): - The user-defined session object that will hold the day and the time range selection
    sess (string): - The session string that represents the user session HHmm-HHmm,HHmm-HHmm:ddddddd
  Returns: - The session time range object

method from_sess_string(this, sess, refTimezone)
  from_sess_string - Initialize the user session object from the session string
  Namespace types: UserSession
  Parameters:
    this (UserSession): - The user-defined session object that will hold the day and the time range selection
    sess (string): - The session string that represents the user session HHmm-HHmm,HHmm-HHmm:ddddddd
    refTimezone (simple string): - The timezone of reference of the time ranges
  Returns: - The session time range object

method nth_day_after(this, day, n)
  nth_day_after - The nth day after the given day that is a session day (true) in the object
  Namespace types: SessionDays
  Parameters:
    this (SessionDays): - The session days object with the day selection
    day (int): - The day id of the reference day
    n (int): - The number of days after
  Returns: - The day id of the nth session day of the week after the given day

method nth_day_before(this, day, n)
  nth_day_before - The nth day before the given day that is a session day (true) in the object
  Namespace types: SessionDays
  Parameters:
    this (SessionDays): - The session days object with the day selection
    day (int): - The day id of the reference day
    n (int): - The number of days after
  Returns: - The day id of the nth session day of the week before the given day

method next_day(this)
  next_day - The next day that is a session day (true) in the object
  Namespace types: SessionDays
  Parameters:
    this (SessionDays): - The session days object with the day selection
  Returns: - The day id of the next session day of the week

method previous_day(this)
  previous_day - The previous day that is session day (true) in the object
  Namespace types: SessionDays
  Parameters:
    this (SessionDays): - The session days object with the day selection
  Returns: - The day id of the previous session day of the week

method get_sec_in_day(this)
  get_sec_in_day - Count the seconds since the start of the day this session time represents
  Namespace types: SessionTime
  Parameters:
    this (SessionTime): - The session time object with the hour and minute of the time of the day
  Returns: - The number of seconds passed from the start of the day until that session time

method get_ms_in_day(this)
  get_ms_in_day - Count the milliseconds since the start of the day this session time represents
  Namespace types: SessionTime
  Parameters:
    this (SessionTime): - The session time object with the hour and minute of the time of the day
  Returns: - The number of milliseconds passed from the start of the day until that session time

method eq(this, other)
  eq - Compare two bars
  Namespace types: Bar
  Parameters:
    this (Bar): - The bar object with the open and close times
    other (Bar): - The bar object to compare with
  Returns: - Whether this bar is equal to the other one

method get_open_time(this)
  get_open_time - The open time object
  Namespace types: Bar
  Parameters:
    this (Bar): - The bar object with the open and close times
  Returns: - The open time object

method get_close_time(this)
  get_close_time - The close time object
  Namespace types: Bar
  Parameters:
    this (Bar): - The bar object with the open and close times
  Returns: - The close time object

method get_time_range(this)
  get_time_range - Get the time range of the bar
  Namespace types: Bar
  Parameters:
    this (Bar): - The bar object with the open and close times
  Returns: - The time range that the bar is in

getBarNow()
  getBarNow - Get the current bar object with time and time_close timestamps
  Returns: - The current bar

getFixedBarNow()
  getFixedBarNow - Get the current bar with fixed width defined by the timeframe. Note: There are case like SPX 15min timeframe where the last session bar is only 10min. This will return a bar of 15 minutes
  Returns: - The current bar

method is_in_window(this, win)
  is_in_window - Check if the given bar is between the start and end dates of the window
  Namespace types: Bar
  Parameters:
    this (Bar): - The bar to check if it is between the from and to datetimes of the window
    win (DateTimeWindow): - The time window object with the from and to datetimes
  Returns: - Whether the current bar is inside the datetime window

method is_in_timerange(this, rng)
  is_in_timerange - Check if the given bar is inside the session time range
  Namespace types: Bar
  Parameters:
    this (Bar): - The bar to check if it is between the from and to datetimes
    rng (SessionTimeRange): - The session time range object with the start and end time of the daily session
  Returns: - Whether the bar is inside the session time range and if this part of the next trading day

method is_in_days(this, days)
  is_in_days - Check if the given bar is inside the session days
  Namespace types: Bar
  Parameters:
    this (Bar): - The bar to check if its day is a trading day
    days (SessionDays): - The session days object with the day selection
  Returns: - Whether the current bar day is inside the session

method is_in_session(this, sess)
  is_in_session - Check if the given bar is inside the session as defined by the input params (what "not na(time(timeframe.period, this.to_sess_string())[-1])" should return if you could write it
  Namespace types: Bar
  Parameters:
    this (Bar): - The bar to check if it is between the from and to datetimes
    sess (UserSession): - The user-defined session object with the day and the time range selection
  Returns: - Whether the current time is inside the session

method next_bar(this, offsetBars)
  next_bar - Predicts the next bars open and close time based on the charts session
  Namespace types: ChartSession
  Parameters:
    this (ChartSession): - The chart session object that contains the days and the time range shown in the chart
    offsetBars (simple int): - The number of bars forward
  Returns: - Whether the current time is inside the session

DateTimeWindow
  DateTimeWindow - Object that represents a datetime window with a beginning and an end
  Fields:
    fromDateTime (series int): - The beginning of the datetime window
    toDateTime (series int): - The end of the datetime window

SessionDays
  SessionDays - Object that represent the trading days of the week
  Fields:
    days (map<int, bool>): - The map that contains all days of the week and their session flag

SessionTime
  SessionTime - Object that represents the time (hour and minutes)
  Fields:
    hourInDay (series int): - The hour of the day that ranges from 0 to 24
    minuteInHour (series int): - The minute of the hour that ranges from 0 to 59
    minuteInDay (series int): - The minute of the day that ranges from 0 to 1440. They will be calculated based on hourInDay and minuteInHour when method is called

SessionTimeRange
  SessionTimeRange - Object that represents a range that extends from the start to the end time
  Fields:
    startTime (SessionTime): - The beginning of the time range
    endTime (SessionTime): - The end of the time range
    isOvernight (series bool): - Whether or not this is an overnight time range

UserSession
  UserSession - Object that represents a user-defined session
  Fields:
    days (SessionDays): - The map of the user-defined trading days
    timeRanges (SessionTimeRange[]): - The array with all time ranges of the user-defined session during the trading days

Bar
  Bar - Object that represents the bars' open and close times
  Fields:
    openUnixTime (series int): - The open time of the bar
    closeUnixTime (series int): - The close time of the bar
    chartDayOfWeek (series int)

ChartSession
  ChartSession - Object that represents the default session that is shown in the chart
  Fields:
    days (SessionDays): - A map with the trading days shown in the chart
    timeRange (SessionTimeRange): - The time range of the session during a trading day
    isFinalized (series bool)
timesessions

Bibliothèque Pine

Dans le plus pur esprit TradingView, l'auteur a publié ce code Pine en tant que bibliothèque open-source afin que d'autres programmeurs Pine de notre communauté puissent le réutiliser. Bravo à l'auteur! Vous pouvez utiliser cette bibliothèque en privé ou dans d'autres publications à code source ouvert, mais la réutilisation de ce code dans une publication est régie par nos Règles.


Aussi sur:

Clause de non-responsabilité