TradersReality

Traders_Reality_Lib

Library "Traders_Reality_Lib"
This library contains common elements used in Traders Reality scripts

calcPvsra(pvsraVolume, pvsraHigh, pvsraLow, pvsraClose, pvsraOpen, redVectorColor, greenVectorColor, violetVectorColor, blueVectorColor, darkGreyCandleColor, lightGrayCandleColor)
  calculate the pvsra candle color and return the color as well as an alert if a vector candle has apperared.
Situation "Climax"
Bars with volume >= 200% of the average volume of the 10 previous chart TFs, or bars
where the product of candle spread x candle volume is >= the highest for the 10 previous
chart time TFs.
Default Colors: Bull bars are green and bear bars are red.
Situation "Volume Rising Above Average"
Bars with volume >= 150% of the average volume of the 10 previous chart TFs.
Default Colors: Bull bars are blue and bear are violet.
  Parameters:
    pvsraVolume: the instrument volume series (obtained from request.sequrity)
    pvsraHigh: the instrument high series (obtained from request.sequrity)
    pvsraLow: the instrument low series (obtained from request.sequrity)
    pvsraClose: the instrument close series (obtained from request.sequrity)
    pvsraOpen: the instrument open series (obtained from request.sequrity)
    redVectorColor: red vector candle color
    greenVectorColor: green vector candle color
    violetVectorColor: violet/pink vector candle color
    blueVectorColor: blue vector candle color
    darkGreyCandleColor: regular volume candle down candle color - not a vector
    lightGrayCandleColor: regular volume candle up candle color - not a vector
@return

adr(length, barsBack)
  Parameters:
    length: how many elements of the series to calculate on
    barsBack: starting possition for the length calculation - current bar or some other value eg last bar
@return adr the adr for the specified lenght

adrHigh(adr, fromDo)
  Calculate the ADR high given an ADR
  Parameters:
    adr: the adr
    fromDo: boolean flag, if false calculate traditional adr from high low of today, if true calcualte from exchange midnight
@return adrHigh the position of the adr high in price

adrLow(adr, fromDo)
  Parameters:
    adr: the adr
    fromDo: boolean flag, if false calculate traditional adr from high low of today, if true calcualte from exchange midnight
@return adrLow the position of the adr low in price

splitSessionString(sessXTime)
  given a session in the format 0000-0100:23456 split out the hours and minutes
  Parameters:
    sessXTime: the session time string usually in the format 0000-0100:23456
@return

calcSessionStartEnd(sessXTime, gmt)
  calculate the start and end timestamps of the session
  Parameters:
    sessXTime: the session time string usually in the format 0000-0100:23456
    gmt: the gmt offset string usually in the format GMT+1 or GMT+2 etc
@return

drawOpenRange(sessXTime, sessXcol, showOrX, gmt)
  draw open range for a session
  Parameters:
    sessXTime: session string in the format 0000-0100:23456
    sessXcol: the color to be used for the opening range box shading
    showOrX: boolean flag to toggle displaying the opening range
    gmt: the gmt offset string usually in the format GMT+1 or GMT+2 etc
@return void

drawSessionHiLo(sessXTime, show_rectangleX, show_labelX, sessXcolLabel, sessXLabel, gmt, sessionLineStyle)
  Parameters:
    sessXTime: session string in the format 0000-0100:23456
    show_rectangleX: show the session high and low lines
    show_labelX: show the session label
    sessXcolLabel: the color to be used for the hi/low lines and label
    sessXLabel: the session label text
    gmt: the gmt offset string usually in the format GMT+1 or GMT+2 etc
    sessionLineStyle: the line stile for the session high low lines
@return void

calcDst()
  calculate market session dst on/off flags
@return indicating if DST is on or off for a particular region

timestampPreviousDayOfWeek(previousDayOfWeek, hourOfDay, gmtOffset, oneWeekMillis)
  Timestamp any of the 6 previous days in the week (such as last Wednesday at 21 hours GMT)
  Parameters:
    previousDayOfWeek: Monday or Satruday
    hourOfDay: the hour of the day when psy calc is to start
    gmtOffset: the gmt offset string usually in the format GMT+1 or GMT+2 etc
    oneWeekMillis: the amount if time for a week in milliseconds
@return the timestamp of the psy level calculation start time

getdayOpen()
  get the daily open - basically exchange midnight
@return the daily open value which is float price

newBar(res)
  new_bar: check if we're on a new bar within the session in a given resolution
  Parameters:
    res: the desired resolution
@return true/false is a new bar for the session has started

toPips(val)
  to_pips Convert value to pips
  Parameters:
    val: the value to convert to pips
@return the value in pips

rLabel(ry, rtext, rstyle, rcolor, valid, labelXOffset)
  a function that draws a right aligned lable for a series during the current bar
  Parameters:
    ry: series float the y coordinate of the lable
    rtext: the text of the label
    rstyle: the style for the lable
    rcolor: the color for the label
    valid: a boolean flag that allows for turning on or off a lable
    labelXOffset: how much to offset the label from the current position

rLabelOffset(ry, rtext, rstyle, rcolor, valid, labelXOffset)
  a function that draws a right aligned lable for a series during the current bar
  Parameters:
    ry: series float the y coordinate of the lable
    rtext: the text of the label
    rstyle: the style for the lable
    rcolor: the color for the label
    valid: a boolean flag that allows for turning on or off a lable
    labelXOffset: how much to offset the label from the current position

rLabelLastBar(ry, rtext, rstyle, rcolor, valid, labelXOffset)
  a function that draws a right aligned lable for a series only on the last bar
  Parameters:
    ry: series float the y coordinate of the lable
    rtext: the text of the label
    rstyle: the style for the lable
    rcolor: the color for the label
    valid: a boolean flag that allows for turning on or off a lable
    labelXOffset: how much to offset the label from the current position

drawLine(xSeries, res, tag, xColor, xStyle, xWidth, xExtend, isLabelValid, labelXOffset, validTimeFrame)
  a function that draws a line and a label for a series
  Parameters:
    xSeries: series float the y coordinate of the line/label
    res: the desired resolution controlling when a new line will start
    tag: the text for the lable
    xColor: the color for the label
    xStyle: the style for the line
    xWidth: the width of the line
    xExtend: extend the line
    isLabelValid: a boolean flag that allows for turning on or off a label
    labelXOffset: how much to offset the label from the current position
    validTimeFrame: a boolean flag that allows for turning on or off a line drawn

drawLineDO(xSeries, res, tag, xColor, xStyle, xWidth, xExtend, isLabelValid, labelXOffset, validTimeFrame)
  a function that draws a line and a label for the daily open series
  Parameters:
    xSeries: series float the y coordinate of the line/label
    res: the desired resolution controlling when a new line will start
    tag: the text for the lable
    xColor: the color for the label
    xStyle: the style for the line
    xWidth: the width of the line
    xExtend: extend the line
    isLabelValid: a boolean flag that allows for turning on or off a label
    labelXOffset: how much to offset the label from the current position
    validTimeFrame: a boolean flag that allows for turning on or off a line drawn

drawPivot(pivotLevel, res, tag, pivotColor, pivotLabelColor, pivotStyle, pivotWidth, pivotExtend, isLabelValid, validTimeFrame, levelStart, pivotLabelXOffset)
  draw a pivot line - the line starts one day into the past
  Parameters:
    pivotLevel: series of the pivot point
    res: the desired resolution
    tag: the text to appear
    pivotColor: the color of the line
    pivotLabelColor: the color of the label
    pivotStyle: the line style
    pivotWidth: the line width
    pivotExtend: extend the line
    isLabelValid: boolean param allows to turn label on and off
    validTimeFrame: only draw the line and label at a valid timeframe
    levelStart: basically when to start drawing the levels
    pivotLabelXOffset: how much to offset the label from its current postion
@return the pivot line series

getPvsraFlagByColor(pvsraColor, redVectorColor, greenVectorColor, violetVectorColor, blueVectorColor, lightGrayCandleColor)
  convert the pvsra color to an internal code
  Parameters:
    pvsraColor: the calculated pvsra color
    redVectorColor: the user defined red vector color
    greenVectorColor: the user defined green vector color
    violetVectorColor: the user defined violet vector color
    blueVectorColor: the user defined blue vector color
    lightGrayCandleColor: the user defined regular up candle color
@return pvsra internal code

updateZones(pvsra, direction, boxArr, maxlevels, pvsraHigh, pvsraLow, pvsraOpen, pvsraClose, transperancy, zoneupdatetype, zonecolor, zonetype, borderwidth, coloroverride, redVectorColor, greenVectorColor, violetVectorColor, blueVectorColor, lightGrayCandleColor)
  a function that draws the unrecovered vector candle zones
  Parameters:
    pvsra: internal code
    direction: above or below the current pa
    boxArr: the array containing the boxes that need to be updated
    maxlevels: the maximum number of boxes to draw
    pvsraHigh: the pvsra high value series
    pvsraLow: the pvsra low value series
    pvsraOpen: the pvsra open value series
    pvsraClose: the pvsra close value series
    transperancy: the transparencfy of the vecor candle zones
    zoneupdatetype: the zone update type
    zonecolor: the zone color if overriden
    zonetype: the zone type
    borderwidth: the width of the border
    coloroverride: if the color overriden
    redVectorColor: the user defined red vector color
    greenVectorColor: the user defined green vector color
    violetVectorColor: the user defined violet vector color
    blueVectorColor: the user defined blue vector color
    lightGrayCandleColor: the user defined regular up candle color

cleanarr(arr)
  clean an array from na values
  Parameters:
    arr: the array to clean
@return if the array was cleaned

calcPsyLevels(oneWeekMillis, showPsylevels, psyType, sydDST)
  calculate the psy levels
4 hour res based on how mt4 does it
mt4 code
int Li_4 = iBarShift(NULL, PERIOD_H4, iTime(NULL, PERIOD_W1, Li_0)) - 2 - Offset;
ObjectCreate("PsychHi", OBJ_TREND, 0, Time, iHigh(NULL, PERIOD_H4, iHighest(NULL, PERIOD_H4, MODE_HIGH, 2, Li_4)), iTime(NULL, PERIOD_W1, 0), iHigh(NULL, PERIOD_H4,
iHighest(NULL, PERIOD_H4, MODE_HIGH, 2, Li_4)));
so basically because the session is 8 hours and we are looking at a 4 hour resolution we only need to take the highest high an lowest low of 2 bars
we use the gmt offset to adjust the 0000-0800 session to Sydney open which is at 2100 during dst and at 2200 otherwize. (dst - spring foward, fall back)
keep in mind sydney is in the souther hemisphere so dst is oposite of when london and new york go into dst
  Parameters:
    oneWeekMillis: a constant value
    showPsylevels: should psy levels be calculated
    psyType: the type of Psylevels - crypto or forex
    sydDST: is Sydney in DST
@return
Release Notes:
v2

Added:
adrHiLo(length, barsBack, fromDO)
  Parameters:
    length: how many elements of the series to calculate on
    barsBack: starting possition for the length calculation - current bar or some other value eg last bar
    fromDO: boolean flag, if false calculate traditional adr from high low of today, if true calcualte from exchange midnight
@return adr, adrLow and adrHigh - the adr, the position of the adr High and adr Low with respect to price

drawSessionHiloLite(sessXTime, show_rectangleX, show_labelX, sessXcolLabel, sessXLabel, gmt, sessionLineStyle, sessXcol)
  Parameters:
    sessXTime: session string in the format 0000-0100:23456
    show_rectangleX: show the session high and low lines
    show_labelX: show the session label
    sessXcolLabel: the color to be used for the hi/low lines and label
    sessXLabel: the session label text
    gmt: the gmt offset string usually in the format GMT+1 or GMT+2 etc
    sessionLineStyle: the line stile for the session high low lines
    sessXcol: - the color for the box color that will color the session
@return void

msToHmsString(ms)
  converts milliseconds into an hh:mm string. For example, 61000 ms to '0:01:01'
  Parameters:
    ms: - the milliseconds to convert to hh:mm
@return string - the converted hh:mm string

countdownString(openToday, closeToday)
  that calculates how much time is left until the next session taking the session start and end times into account. Note this function does not work on intraday sessions.
  Parameters:
    openToday: - timestamps of when the session opens in general - note its a series because the timestamp was created using the dst flag which is a series itself thus producing a timestamp series
    closeToday: - timestamp of when the session closes in general - note its a series because the timestamp was created using the dst flag which is a series itself thus producing a timestamp series
@return a countdown of when next the session opens or 'Open' if the session is open now

countdownStringSyd(openToday, closeToday)
  that calculates how much time is left until the next session taking the session start and end times into account. special case of intraday sessions like sydney
  Parameters:
    openToday: - timestamps of when the session opens in general - note its a series because the timestamp was created using the dst flag which is a series itself thus producing a timestamp series
    closeToday: - timestamp of when the session closes in general - note its a series because the timestamp was created using the dst flag which is a series itself thus producing a timestamp series
@return a countdown of when next the session opens or 'Open' if the session is open now

TR
Pine library

In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in a publication is governed by House Rules.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this library?

Copy the following line and paste it in your script.