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 checking if it will be part of a predefined session and/or inside a datetime window. All existing session
functionality I found in the documentation e.g. "not na(time(timeframe, session, timezone))" are not suitable for
strategy scripts, since the execution of the orders is delayed by one bar, due to the script execution happening at
the bar close. Moreover, a history operator with a negative value that looks forward is not allowed in any pinescript
expression. So, a prediction for the next bar using the bars_back argument of "time()"" and "time_close()" was
necessary. 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 the 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 happened between the prediction bar and the actual bar
(probably due to a holiday, half session day, a timezone change 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 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 session into a human-readable string
  Namespace types: Session
  Parameters:
    this (Session): - The session object with the day and the time range selection
  Returns: - The string of the session

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 object

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 session object from session days and time range
  Namespace types: Session
  Parameters:
    this (Session): - The 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 (array<SessionTimeRange>): - The array of all the session time ranges during a session day
  Returns: - The session object

method init(this, days, timeRanges, names, colors)
  init - Initialize the session object from session days and time range
  Namespace types: SessionView
  Parameters:
    this (SessionView): - The session view object that will hold the session, the names and the color selections
    days (SessionDays): - The session days object that defines the days the session is happening
    timeRanges (array<SessionTimeRange>): - The array of all the session time ranges during a session day
    names (array<string>): - The array of the names of the sessions
    colors (array<color>): - The array of the colors of the sessions
  Returns: - The session object

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 is_bar_included(this, offset_forward)
  is_bar_included - Check if the given bar is between the start and end dates of the window
  Namespace types: DateTimeWindow
  Parameters:
    this (DateTimeWindow): - The time window object with the from and to datetimes
    offset_forward (simple int): - The number of bars forward. Default is 1
  Returns: - Whether the current bar is inside the datetime window

method is_bar_included(this, offset_forward)
  is_bar_included - 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: Session
  Parameters:
    this (Session): - The session with the day and the time range selection
    offset_forward (simple int): - The bar forward to check if it is between the from and to datetimes. Default is 1
  Returns: - Whether the current time is inside the session

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 session into a session string
  Namespace types: Session
  Parameters:
    this (Session): - The session object with the day and the time range selection
  Returns: - The string of the 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 session object from the session string in exchange timezone (syminfo.timezone)
  Namespace types: Session
  Parameters:
    this (Session): - The session object that will hold the day and the time range selection
    sess (string): - The session string that represents the session HHmm-HHmm,HHmm-HHmm:ddddddd
  Returns: - The session time range object

method from_sess_string(this, sess, refTimezone)
  from_sess_string - Initialize the session object from the session string
  Namespace types: Session
  Parameters:
    this (Session): - The session object that will hold the day and the time range selection
    sess (string): - The session string that represents the 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 is_day_included(this, day)
  is_day_included - Check if the given day is inside the session days
  Namespace types: SessionDays
  Parameters:
    this (SessionDays): - The session days object with the day selection
    day (int): - The day to check if it is a trading day
  Returns: - Whether the current day is included in the session days

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

Session
  Session - Object that represents a session
  Fields:
    days (SessionDays): - The map of the trading days
    timeRanges (array<SessionTimeRange>): - The array with all time ranges of the session during the trading days

SessionView
  SessionView - Object that visualize a session
  Fields:
    sess (Session): - The Session object to be visualized
    names (array<string>): - The names of the session time ranges
    colors (array<color>): - The colors of the session time ranges
timesessions

Biblioteka Pine

W prawdziwym duchu TradingView autor opublikował ten kod Pine jako bibliotekę typu open source, aby inni programiści Pine z naszej społeczności mogli go ponownie wykorzystać. Pozdrowienia dla autora! Możesz używać tej biblioteki prywatnie lub w innych publikacjach typu open source, ale ponowne użycie tego kodu w publikacji podlega Zasadom Regulaminu.

Wyłączenie odpowiedzialności