TradingView
FFriZz
8 de Feb. de 2023 11:35

FrizLabz_Time_Utility_Methods 

DOGEUSDT Perpetual Swap ContractOKX

Descripción

Library "FrizLabz_Time_Utility_Methods"

Some time to index and index to time helper methods made them for another library thought I would try to make
them as methods

UTC_helper(utc)
  UTC helper function this adds the + to the positive utc times, add "UTC" to the string
and can be used in the timezone arg of for format_time()
  Parameters:
    utc: (int) | +/- utc offset
  Returns: string | string to be added to the timezone paramater for utc timezone usage

bar_time(bar_amount)
  from a time to index
  Parameters:
    bar_amount: (int) | default - 1)
  Returns: int bar_time

time_to_index(_time)
  from time to bar_index
  Parameters:
    _time: (int)
  Returns: int time_to_index | bar_index that corresponds to time provided

time_to_bars_back(_time)
  from a time quanity to bar quanity for use with [historical ref].
  Parameters:
    _time: (int)
  Returns: int bars_back | yeilds the amount of bars from current bar to reach _time provided

bars_back_to_time(bars_back)
  from bars_back to time
  Parameters:
    bars_back
  Returns: int | using same logic as [historical ref.] this will return the
time of the bar = to the bar that corresponds to [x] bars_back

index_time(index)
  bar_index to UNIX time
  Parameters:
    index: (int)
  Returns: int time | time in unix that corrresponds to the bar_index

to_utc(time_or_index, timezone, format)
  method to use with a time or bar_index variable that will detect if it is an index or unix time
and convert it to a printable string
  Parameters:
    time_or_index: (int) required) | time in unix or bar_index
    timezone: (int) required) | utc offset to be appled to output
    format: (string) | default - "yyyy-MM-dd'T'HH:mm:ssZ") | the format for the time, provided string is
default one from str.format_time()
  Returns: string | time formatted string

GET(line)
  Gets the location paramaters of a Line
  Parameters:
    line: (line)
  Returns: tuple [int x1,float y1,int x2,float y2]

GET(box)
  Gets the location paramaters of a Box
  Parameters:
    box: (box)
  Returns: tuple [int left,float top,int right,float bottom]

GET(label)
  Gets the location paramaters and text of a Label
  Parameters:
    label: (label)
  Returns: tuple [int x,float y,string _text]

GET(linefill)
  Gets line 1 and 2 from a Linefill
  Parameters:
    linefill: (linefill)
  Returns: tuple [line line1,line line2]

Format(line, timezone)
  converts Unix time in time or index params to formatted time
and returns a tuple of the params as string with the time/index params formatted
  Parameters:
    line: (line) | required
    timezone: (int) | default - na
  Returns: tuple [string x1,string y1,string x2,string y2]

Line(x1, y1, x2, y2, extend, color, style, width)
  similar to line.new() with the exception
of not needing to include y2 for a flat line, y1 defaults to close,
and it doesnt require xloc.bar_time or xloc.bar_index, if no x1
  Parameters:
    x1: (int) default - time
    y1: (float) default - close
    x2: (int) default - last_bar_time/last_bar_index | not required for line that ends on current bar
    y2: (float) default - y1 | not required for flat line
    extend: (string) default - extend.none | extend.left, extend.right, extend.both
    color: (color) default - chart.fg_color
    style: (string) default - line.style_solid | line.style_dotted, line.style_dashed,
line.style_arrow_both, line.style_arrow_left, line.style_arrow_right
    width
  Returns: line

Box(left, top, right, bottom, extend, border_color, bgcolor, text_color, border_width, border_style, txt, text_halign, text_valign, text_size, text_wrap)
  similar to box.new() but only requires top and bottom to create box,
auto detects if it is bar_index or time used in the (left) arg. xloc.bar_time and xloc.bar_index are not used
args are ordered by purpose | position -> colors -> styling -> text options
  Parameters:
    left: (int) default - time
    top: (float) required
    right: (int) default - last_bar_time/last_bar_index | will default to current bar index or time
depending on (left) arg
    bottom: (float) required
    extend: (string) default - extend.none | extend.left, extend.right, extend.both
    border_color: (color) default - chart.fg_color
    bgcolor: (color) default - color.new(chart.fg_color,75)
    text_color: (color) default - chart.bg_color
    border_width: (int) default - 1
    border_style: (string) default - line.style_solid | line.style_dotted, line.style_dashed,
    txt: (string) default - ''
    text_halign: (string) default - text.align_center | text.align_left, text.align_right
    text_valign: (string) default - text.align_center | text.align_top, text.align_bottom
    text_size: (string) default - size.normal | size.tiny, size.small, size.large, size.huge
    text_wrap: (string) default - text.wrap_auto | text.wrap_none
  Returns: box

Label(x, y, txt, yloc, color, textcolor, style, size, textalign, text_font_family, tooltip)
  similar to label.new() but only requires no args to create label,
auto detects if it is bar_index or time used in the (x) arg. xloc.bar_time and xloc.bar_index are not used
args are ordered by purpose | position -> colors -> styling -> text options
  Parameters:
    x: (int) default - time
    y: (float) default - high or low | depending on bar direction
    txt: (string) default - ''
    yloc: (string) default - yloc.price | yloc.price, yloc.abovebar, yloc.belowbar
    color: (color) default - chart.fg_color
    textcolor: (color) default - chart.bg_color
    style: (string) default - label.style_label_down | label.style_none
label.style_xcross,label.style_cross,label.style_triangleup,label.style_triangledown
label.style_flag, label.style_circle, label.style_arrowup, label.style_arrowdown,
label.style_label_up, label.style_label_down, label.style_label_left, label.style_label_right,
label.style_label_lower_left, label.style_label_lower_right, label.style_label_upper_left,
label.style_label_upper_right, label.style_label_center, label.style_square,
label.style_diamond
    size: (string) default - size.normal | size.tiny, size.small, size.large, size.huge
    textalign: (string) default - text.align_center | text.align_left, text.align_right
    text_font_family: (string) default - font.family_default | font.family_monospace
    tooltip: (string) default - na
  Returns: label

Notas de prensa

Library "FrizLabz_Time_Utility_Methods"

UTC_helper(utc)
  UTC helper function this adds the + to the positive utc times, add "UTC" to the string
and can be used in the timezone arg of for format_time()
  Parameters:
    utc: (int) | +/- utc offset
  Returns: string | string to be added to the timezone paramater for utc timezone usage

bar_time(bar_amount)
  from a time to index
  Parameters:
    bar_amount: (int) | default - 1)
  Returns: int bar_time

time_to_index(_time)
  from time to bar_index
  Parameters:
    _time: (int)
  Returns: int time_to_index | bar_index that corresponds to time provided

time_to_bars_back(_time)
  from a time quanity to bar quanity for use with [historical ref].
  Parameters:
    _time: (int)
  Returns: int bars_back | yeilds the amount of bars from current bar to reach _time provided

bars_back_to_time(bars_back)
  from bars_back to time
  Parameters:
    bars_back
  Returns: int | using same logic as [historical ref.] this will return the
time of the bar = to the bar that corresponds to [x] bars_back

index_time(index)
  bar_index to UNIX time
  Parameters:
    index: (int)
  Returns: int time | time in unix that corrresponds to the bar_index

to_utc(time_or_index, timezone, format)
  Parameters:
    time_or_index
    timezone
    format

GET(line)
  Gets the location paramaters of a Box
  Parameters:
    line
  Returns: tuple [int left,float top,int right,float bottom]

GET(box)
  Gets the location paramaters and text of a Label
  Parameters:
    box
  Returns: tuple [int x,float y,string _text]

GET(label)
  Gets line 1 and 2 from a Linefill
  Parameters:
    label
  Returns: tuple [line line1,line line2]

GET(linefill)
  Parameters:
    linefill

Format(line, timezone)
  Parameters:
    line
    timezone

Line(x1, y1, x2, y2, extend, color, style, width)
  Parameters:
    x1
    y1
    x2
    y2
    extend
    color
    style
    width

Box(left, top, right, bottom, extend, border_color, bgcolor, text_color, border_width, border_style, txt, text_halign, text_valign, text_size, text_wrap)
  Parameters:
    left
    top
    right
    bottom
    extend
    border_color
    bgcolor
    text_color
    border_width
    border_style
    txt
    text_halign
    text_valign
    text_size
    text_wrap

Label(x, y, txt, yloc, color, textcolor, style, size, textalign, text_font_family, tooltip)
  Parameters:
    x
    y
    txt
    yloc
    color
    textcolor
    style
    size
    textalign
    text_font_family
    tooltip

Notas de prensa

v3

Updated:
UTC_helper(utc)
  UTC helper function this adds the + to the positive utc times, add "UTC" to the string
and can be used in the timezone arg of for format_time()
  Parameters:
    utc: (int) +/- utc offset
  Returns: string string to be added to the timezone paramater for utc timezone usage

bar_time(bar_amount)
  from a time to index
  Parameters:
    bar_amount: (int) default - 1)
  Returns: int bar_time

time_to_index(_time)
  from time to bar_index
  Parameters:
    _time: (int)
  Returns: int time_to_index bar_index that corresponds to time provided

time_to_bars_back(_time)
  from a time quanity to bar quanity for use with [historical ref].
  Parameters:
    _time: (int)
  Returns: int bars_back yeilds the amount of bars from current bar to reach _time provided

bars_back_to_time(bars_back)
  from bars_back to time
  Parameters:
    bars_back
  Returns: int using same logic as [historical ref.] this will return the
time of the bar = to the bar that corresponds to [x] bars_back

index_time(index)
  bar_index to UNIX time
  Parameters:
    index: (int)
  Returns: int time time in unix that corrresponds to the bar_index

to_utc(time_or_index, timezone, format)
  method to use with a time or bar_index variable that will detect if it is an index or unix time
and convert it to a printable string
  Parameters:
    time_or_index: (int) required) time in unix or bar_index
    timezone: (int) required) utc offset to be appled to output
    format: (string) default - "yyyy-MM-dd'T'HH:mm:ssZ") the format for the time, provided string is default
one from str.format_time()
  Returns: string time formatted string

GET(linefill)
  Gets line 1 and 2 from a Linefill
  Parameters:
    linefill: (linefill)
  Returns: tuple line line1,line line2

Format(line, timezone)
  converts Unix time in time or index params to formatted time
and returns a tuple of the params as string with the time/index params formatted
  Parameters:
    line: (line) required
    timezone: (int) default - na
  Returns: tuple string x1,string y1,string x2,string y2

Line(x1, y1, x2, y2, extend, color, style, width)
  similar to line.new() with the exception
of not needing to include y2 for a horizontal line, y1 defaults to close,
and it doesnt require xloc.bar_time or xloc.bar_index, if no x1
  Parameters:
    x1: (int) default - time
    y1: (float) default - close
    x2: (int) default - last_bar_time/last_bar_index not required for line that ends on current bar
    y2: (float) default - y1 not required for flat line
    extend: (string) default - extend.none extend.left, extend.right, extend.both
    color: (color) default - chart.fg_color
    style: (string) default - line.style_solid line.style_dotted, line.style_dashed,
line.style_arrow_both, line.style_arrow_left, line.style_arrow_right
    width
  Returns: line object

Box(left, top, right, bottom, extend, border_color, bgcolor, text_color, border_width, border_style, txt, text_halign, text_valign, text_size, text_wrap)
  similar to box.new() but only requires top and bottom to create box,
auto detects if it is bar_index or time used in the (left) arg. xloc.bar_time and xloc.bar_index are not used
args are ordered by purpose position -> colors -> styling -> text options
  Parameters:
    left: (int) default - time
    top: (float) required
    right: (int) default - last_bar_time/last_bar_index will default to current bar index or time depending on (left) arg
    bottom: (float) required
    extend: (string) default - extend.none extend.left, extend.right, extend.both
    border_color: (color) default - chart.fg_color
    bgcolor: (color) default - color.new(chart.fg_color,75)
    text_color: (color) default - chart.bg_color
    border_width: (int) default - 1
    border_style: (string) default - line.style_solid line.style_dotted, line.style_dashed,
    txt: (string) default - ''
    text_halign: (string) default - text.align_center text.align_left, text.align_right
    text_valign: (string) default - text.align_center text.align_top, text.align_bottom
    text_size: (string) default - size.normal size.tiny, size.small, size.large, size.huge
    text_wrap: (string) default - text.wrap_auto text.wrap_none
  Returns: box object

Label(x, y, txt, yloc, color, textcolor, style, size, textalign, text_font_family, tooltip)
  similar to label.new() but only requires no args to create label,
auto detects if it is bar_index or time used in the (x) arg. xloc.bar_time and xloc.bar_index are not used
args are ordered by purpose position -> colors -> styling -> text options
  Parameters:
    x: (int) default - time
    y: (float) default - high or low depending on bar direction
    txt: (string) default - ''
    yloc: (string) default - yloc.price yloc.price, yloc.abovebar, yloc.belowbar
    color: (color) default - chart.fg_color
    textcolor: (color) default - chart.bg_color
    style: (string) default - label.style_label_down label.style_none
label.style_xcross,label.style_cross,label.style_triangleup,label.style_triangledown
label.style_flag, label.style_circle, label.style_arrowup, label.style_arrowdown,
label.style_label_up, label.style_label_down, label.style_label_left, label.style_label_right,
label.style_label_lower_left, label.style_label_lower_right, label.style_label_upper_left, label.style_label_upper_right,
label.style_label_center, label.style_square, label.style_diamond
    size: (string) default - size.normal size.tiny, size.small, size.large, size.huge
    textalign: (string) default - text.align_center text.align_left, text.align_right
    text_font_family: (string) default - font.family_default font.family_monospace
    tooltip: (string) default - na
  Returns: label

Notas de prensa

v4 bug fixes

Notas de prensa

v5
Changed: index_time() to index_to_time() so that index_to_time() and time_to_index() closer match in names

Removed: GET(), Format(), Line(), Label(), Box()
I'll be releasing a library that has functions that mirror these

Notas de prensa

v6
Fixed display
Comentarios
BobbyBanksX
got it working. thank you very much, @FFriZz.
BobbyBanksX
@NeonTL8, this seems like it may be a big endeavor but perhaps we can maybe even work on it together.

i was hoping for this feature... if i want 60 bars back (on a 1m chart) prior to 9:35am EST on a US stock ticker, we could be produced with the answer of 3:05 pm EST on the day prior.

9:30am is the cash open, 4:00pm is the cash close.

9:30~9:35 on the day of would take care of 5 bars, 55 bars before yesterday's close would be 3:05.

---

currently, when i do any calculations that provide me a timestamp with a unix number that enters into another session period, anchoring does not work.
BobbyBanksX
@NeonTL8, with the extended hours being different from day to day on common equities, the number of bars back from the start of one day and the end of the previous day is dynamic. oof, what a headache.
FFriZz
@NeonTL8, does it work when you have extended hours showing on chart?
I would say if you are wanting to account for the extended and pre session to try and avoid bars back and try to reference with time if you share a snippet of what exactly your trying to do I can help you more
FFriZz
@NeonTL8, thanks m8!
ASIFKERIM
Hi, Is this library can use delay between alerts in seconds
FFriZz
@hjsjshs, No this library wont help with that specifically.. but unix time is in milliseconds so if you have 2 unix times and you find the difference and divide it by 1000 it would be converted to seconds.. you could use the bar_time function to find the unix time length of a bar (how many milliseconds a bar is) and divide it by 1000 and that should be how man seconds the bar is.. then there would be some math to figure out exactly how long you would want.. just remember the unix time is in milliseconds so an example would be

if timenow > time + 1000

this would only work on live bars though but hope this helps ya
liteshi55
@FFriZz, Thanks for your legendary work, Much appreciate it.
I have encountered an issue while adding a library in to my indicator i.e it shows an error can not use global variables in the exported function. do you have any workaround for it?
FFriZz
@liteshi55, yes i noticed this today I will have it fixed tomorrow I think it has to do with my FrizBug library something Pinescript changed but ill fix it soon thank you for the feedback m8
FFriZz
@liteshi55, I think I got it taken care of make sure you change the version to the newest one and let me know if it was the problem you were encountering. Thank you
Más