Traders Reality Main
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Original pattern formation by plasmapug, rise retrace continuation to the upside by infernix, peshocore and xtech5192
// dst auto f-n and psy refactor for dst by xtech5192
// Range daily/weekly Hi/Lo added with research contributions from xtech5192
// Please note while the code is open source and you are free to use it however you like - the 'Traders Reality' name is not - ie if you produce derivatives of this 
// source code you to name those scripts using "Traders Reality", "Pattern Watchers" or any other name that relates to Traders Reality in any way.


 
//Session	            Local Time	                   DST OFF (UCT+0)	DST ON (UTC+0)	DST ON 2022	  DST OFF 2022	DST ON 2023	  DST OFF 2023	  DST ON 2024	DST OFF 2024
//London	            8am-430pm	                   0800-1630	    0700-1530	    March, 27	  October, 30	March, 26	  October, 29	  March, 31	    October, 27
//NewYork	            930am-4pm	                   1430-2100	    1330-2000	    March, 13	  November, 6	March, 12	  November, 5	  March, 10	    November, 3
//Tokyo	                9am-3pm	                       0000-0600	    0000-0600	    N/A	          N/A	        N/A	          N/A	          N/A	        N/A
//HongKong	            930am-4pm	                   0130-0800	    0130-0800	    N/A	          N/A	        N/A	          N/A	          N/A	        N/A
//Sydney (NZX+ASX)	    NZX start 10am, ASX end 4pm	   2200-0600	    2100-0500	    October, 2	  April, 3	    October, 1	  April, 2	      October, 6	April, 7
//EU Brinx	            800am-900am	                   0800-0900	    0700-0800	    March, 27	  October, 30	March, 26	  October, 29	  March, 31	    October, 27
//US Brinx	            900am-10am	                   1400-1500	    1300-1400	    March, 13	  November, 6	March, 12	  November, 5	  March, 10	    November, 3
//Frankfurt	            800am-530pm	                   0700-1630	    0600-1530	    March, 27	  October, 30	March, 26	  October, 29	  March, 31	    October, 27
 

//@version=5
indicator(title = 'Traders Reality Main', shorttitle='TR_MAIN', overlay=true, max_bars_back=300,max_boxes_count=500, max_lines_count=500, max_labels_count=500)
import TradersReality/Traders_Reality_Lib/2 as trLib


// Config
labelOffsetInput = input.int(group='Label offsets', title='General', defval=12, inline='labeloffset1')
pivotOffsetInput = input.int(group='Label offsets', title='Pivots', defval=6, inline='labeloffset1')
adrOffsetInput = input.int(group='Label offsets', title='ADR', defval=12, inline='labeloffset1')
adrOffsetInput50 = input.int(group='Label offsets', title='50% ADR', defval=12, inline='labeloffset1')
rdOffsetInput = input.int(group='Label offsets', title='RD/W', defval=24, inline='labeloffset1')
rdOffsetInput50 = input.int(group='Label offsets', title='50% RD/W', defval=24, inline='labeloffset1')

color redVectorColor = input.color(title='Vector: Red', group='PVSRA Colors', defval=color.red, inline='vectors')
color greenVectorColor = input.color(title='Green', group='PVSRA Colors', defval=color.lime, inline='vectors')
color violetVectorColor = input.color(title='Violet', group='PVSRA Colors', defval=color.fuchsia, inline='vectors')
color blueVectorColor = input.color(title='Blue', group='PVSRA Colors', defval=color.blue, inline='vectors', tooltip='Bull bars are green and bear bars are red when the bar is with volume >= 200% of the average volume of the 10 previous bars, or bars where the product of candle spread x candle volume is >= the highest for the 10 previous bars.\n Bull bars are blue and bear are violet when the bar is with with volume >= 150% of the average volume of the 10 previous bars.')
color regularCandleUpColor = input.color(title='Regular: Up Candle', group='PVSRA Colors', defval=#999999, inline='nonVectors')
color regularCandleDownColor = input.color(title='Down Candle', group='PVSRA Colors', defval=#4d4d4d, inline='nonVectors', tooltip='Bull bars are light gray and bear are dark gray when none of the red/green/blue/violet vector conditions are met.')


showEmas = input.bool(group='EMAs', title='Show EMAs?', defval=true, inline='showemas')
labelEmas = input.bool(group='EMAs', title='EMA Labels?', defval=false, inline='showemas')
oneEmaColor = input.color(group='EMAs', title='EMA Color: 5', defval=color.rgb(254, 234, 74, 0), inline='emacolors')
twoEmaColor = input.color(group='EMAs', title='13', defval=color.rgb(253, 84, 87, 0), inline='emacolors')
threeEmaColor = input.color(group='EMAs', title='50', defval=color.rgb(31, 188, 211, 0), inline='emacolors')
fourEmaColor = input.color(group='EMAs', title='200', defval=color.rgb(255, 255, 255, 0), inline='emacolors')
fiveEmaColor = input.color(group='EMAs', title='800', defval=color.rgb(50, 34, 144, 0), inline='emacolors')
emaCloudColor = input.color(group='EMAs', title='EMA Cloud', defval=color.rgb(155, 47, 174, 60), inline='emacloud')
emaCloudBorderColor = input.color(group='EMAs', title='Border', defval=color.rgb(18, 137, 123, 100), inline='emacloud')

//Daily Pivot Points
showLevelOnePivotPoints = input.bool(group='Pivot Points', title='Show Level: 1 R/S?', defval=false, inline='pivotlevels')
showLevelTwoPivotPoints = input.bool(group='Pivot Points', title='2 R/S?', defval=false, inline='pivotlevels')
showLevelThreePivotPoints = input.bool(group='Pivot Points', title=' 3 R/S?', defval=false, inline='pivotlevels')
showPivotLabels = input.bool(group='Pivot Points', title='Show labels?', defval=true, inline='pivotlevels')
string rsStyleX = input.string(group='Pivot Points', defval='Dashed', title='R/S Levels Line Style', options=['Dotted', 'Dashed', 'Solid'], inline='pivotcolorsRS')
rsStyle = rsStyleX == 'Dotted' ? line.style_dotted : (rsStyleX == 'Dashed' ? line.style_dashed : (rsStyleX == 'Solid' ? line.style_solid : line.style_dashed))

activeM = input.bool(group='Pivot Points', title='Show M levels?', defval=true, inline='mlevels')
showMLabels = input.bool(group='Pivot Points', title='Labels?', defval=true, inline='mlevels')
extendPivots = input.bool(group='Pivot Points', title='Extend lines in both directions?', defval=false)
pivotColor = input.color(group='Pivot Points', title='Colors: Pivot Point', defval=color.rgb(254, 234, 78, 50), inline='pivotcolors')
pivotLabelColor = input.color(group='Pivot Points', title='Pivot Point Label', defval=color.rgb(254, 234, 78, 50), inline='pivotcolors')
mColor = input.color(group='Pivot Points', title='Colors: M Levels', defval=color.rgb(255, 255, 255, 50), inline='pivotcolors1')
mLabelColor = input.color(group='Pivot Points', title='M Levels Label', defval=color.rgb(255, 255, 255, 50), inline='pivotcolors1')
string mStyleX = input.string(group='Pivot Points', defval='Dashed', title='M Levels Line Style', options=['Dotted', 'Dashed', 'Solid'], inline='pivotcolors2')
mStyle = mStyleX == 'Dotted' ? line.style_dotted : (mStyleX == 'Dashed' ? line.style_dashed : (mStyleX == 'Solid' ? line.style_solid : line.style_dashed))


showDayHighLow = input.bool(group="Yesterday's and Last Week's High/low", title='Show Hi/Lo: Daily?', defval=true, inline='highlow')
dailyHiLoColor = color.new(color.blue, 50) //input.color(group="Yesterday's and Last Week's High/low", title='Color', defval=color.new(color.blue, 50), inline='dhighlow')
showWeekHighLow = input.bool(group="Yesterday's and Last Week's High/low", title='Weekly?', defval=true, inline='highlow')
weeklyHiLoColor = color.new(color.green, 60) //input.color(group="Yesterday's and Last Week's High/low", title='Color', defval=color.new(color.green, 60), inline='whighlow')
showDayHighLowLabels = input.bool(group="Yesterday's and Last Week's High/low", title='Show labels?', defval=true, inline='highlow')

showADR = input.bool(group='Average Daily Range - ADR', title='Show ADR?', defval=true, inline='adr')
showADRDO = input.bool(group='Average Daily Range - ADR', title='Use Daily Open (DO) calc?', defval=false, inline='adr', tooltip='Measure the ADR from the daily open. This will make the ADR static throughout the day. ADR is usually measured taking today high and low. Since todays high and low will change throughout the day, some might prefer to have a static range instead.')
showADRLabels = input.bool(group='Average Daily Range - ADR', title='Labels?', defval=true, inline='adr1')
showADRRange = input.bool(group='Average Daily Range - ADR', title='Range label?', defval=false, inline='adr1')
showADR50 = input.bool(group='Average Daily Range - ADR', title='Show 50% ADR?', defval=false, inline='adr1')
aDRRange = input.int(group='Average Daily Range - ADR', title='ADR length (days)?', defval=14, minval=1, maxval=31, step=1, inline='adr2', tooltip="Defaults taken from mt4. This defines how many days back to take into consideration when calculating the ADR")
adrColor = input.color(group='Average Daily Range - ADR', title='ADR Color', defval=color.new(color.silver, 50), inline='adr3')
string adrStyleX = input.string(group='Average Daily Range - ADR', defval='Dotted', title='ADR Line Style', options=['Dotted', 'Dashed', 'Solid'], inline='adr3')
adrStyle = adrStyleX == 'Dotted' ? line.style_dotted : (adrStyleX == 'Dashed' ? line.style_dashed : (adrStyleX == 'Solid' ? line.style_solid : line.style_dotted))

showAWR = input.bool(group='Average Weekly Range - AWR', title='Show AWR?', defval=false, inline='awr')
showAWRWO = input.bool(group='Average Weekly Range - AWR', title='Use Weekly Open (WO) calc?', defval=false, inline='awr', tooltip='Measure the AWR from the weekly open. This will make the AWR static throughout the week. AWR is usually measured taking this weeks high and low. Since this weeks high and low will change throughout the week, some might prefer to have a static range instead.')
showAWRLabels = input.bool(group='Average Weekly Range - AWR', title='Labels?', defval=true, inline='awr1')
showAWRRange = input.bool(group='Average Weekly Range - AWR', title='Range label?', defval=false, inline='awr1')
showAWR50 = input.bool(group='Average Weekly Range - AWR', title='Show 50% AWR?', defval=false, inline='awr1')
aWRRange = input.int(group='Average Weekly Range - AWR', title='AWR length (weeks)?', defval=4, minval=1, maxval=52, step=1, inline='awr2', tooltip="Defaults taken from mt4. This defines how many weeks back to take into consideration when calculating the AWR")
awrColor = input.color(group='Average Weekly Range - AWR', title='AWR Color', defval=color.new(color.orange, 50), inline='awr3')
string awrStyleX = input.string(group='Average Weekly Range - AWR', defval='Dotted', title='AWR Line Style', options=['Dotted', 'Dashed', 'Solid'], inline='awr3')
awrStyle = awrStyleX == 'Dotted' ? line.style_dotted : (awrStyleX == 'Dashed' ? line.style_dashed : (awrStyleX == 'Solid' ? line.style_solid : line.style_dotted))


showAMR = input.bool(group='Average Monthly Range - AMR', title='Show AMR?', defval=false, inline='amr')
showAMRMO = input.bool(group='Average Monthly Range - AMR', title='Use Monthly Open (MO) calc?', defval=false, inline='amr',tooltip='Measure the AMR from the monthly open. This will make the AMR static throughout the month. AMR is usually measured taking this months high and low. Since this months high and low will change throughout the month, some might prefer to have a static range instead.')
showAMRLabels = input.bool(group='Average Monthly Range - AMR', title='Labels?', defval=true, inline='amr1')
showAMRRange = input.bool(group='Average Monthly Range - AMR', title='Range label?', defval=false, inline='amr1')
showAMR50 = input.bool(group='Average Monthly Range - AMR', title='Show 50% AMR?', defval=false, inline='amr1')
aMRRange = input.int(group='Average Monthly Range - AMR', title='AMR length (months)?', defval=6, minval=1, maxval=12, step=1, inline='amr2', tooltip="Defaults taken from mt4. This defines how many months back to take into consideration when calculating the AMR")
amrColor = input.color(group='Average Monthly Range - AMR', title='AMR Color', defval=color.new(color.red, 50), inline='amr3')
string amrStyleX = input.string(group='Average Monthly Range - AMR', defval='Dotted', title='AMR Line Style', options=['Dotted', 'Dashed', 'Solid'], inline='amr3')
amrStyle = amrStyleX == 'Dotted' ? line.style_dotted : (amrStyleX == 'Dashed' ? line.style_dashed : (amrStyleX == 'Solid' ? line.style_solid : line.style_dotted))

showRD = input.bool(group='Range Daily Hi/Lo - RD Hi/Lo', title='Show RD?', defval=false, inline='rd')
showRDDO = input.bool(group='Range Daily Hi/Lo - RD Hi/Lo', title='Use Daily Open (DO) calc?', defval=false, inline='rd',tooltip='Measure the RD from the daily open. This will make the RD static throughout the day. RD is usually measured taking todays high and low. Since today high and low will change throughout the day, some might prefer to have a static range instead.')
showRDLabels = input.bool(group='Range Daily Hi/Lo - RD Hi/Lo', title='Labels?', defval=true, inline='rd1')
showRDRange = input.bool(group='Range Daily Hi/Lo - RD Hi/Lo', title='Range label?', defval=false, inline='rd1')
showRD50 = input.bool(group='Range Daily Hi/Lo - RD Hi/Lo', title='Show 50% RD?', defval=false, inline='rd1')
rdRange = input.int(group='Range Daily Hi/Lo - RD Hi/Lo', title='RD length (days)?', defval=15, minval=1, maxval=31, step=1, inline='rd2', tooltip="Defaults taken from Trader At Home PVSRA documentation. This defines how many days back to take into consideration when calculating the RD")
rdColor = input.color(group='Range Daily Hi/Lo - RD Hi/Lo', title='RD Color', defval=color.new(color.red, 30), inline='rd3')
string rdStyleX = input.string(group='Range Daily Hi/Lo - RD Hi/Lo', defval='Solid', title='RD Line Style', options=['Dotted', 'Dashed', 'Solid'], inline='rd3')
rdStyle = rdStyleX == 'Dotted' ? line.style_dotted : (rdStyleX == 'Dashed' ? line.style_dashed : (rdStyleX == 'Solid' ? line.style_solid : line.style_dotted))

showRW = input.bool(group='Range Weekly Hi/Lo - RW Hi/Lo', title='Show RW?', defval=false, inline='rw')
showRWWO = input.bool(group='Range Weekly Hi/Lo - RW Hi/Lo', title='Use Weekly Open (WO) calc?', defval=false, inline='rw', tooltip='Measure the RW from the weekly open. This will make the RW static throughout the week. RW is usually measured taking this weeks high and low. Since this weeks high and low will change throughout the week, some might prefer to have a static range instead.')
showRWLabels = input.bool(group='Range Weekly Hi/Lo - RW Hi/Lo', title='Labels?', defval=true, inline='rw1')
showRWRange = input.bool(group='Range Weekly Hi/Lo - RW Hi/Lo', title='Range label?', defval=false, inline='rw1')
showRW50 = input.bool(group='Range Weekly Hi/Lo - RW Hi/Lo', title='Show 50% RW?', defval=false, inline='rw1')
rwRange = input.int(group='Range Weekly Hi/Lo - RW Hi/Lo', title='RW length (weeks)?', defval=13, minval=1, maxval=52, step=1, inline='rw2', tooltip="Defaults taken from Trader At Home PVSRA documentation. This defines how many weeks back to take into consideration when calculating the RW")
rwColor = input.color(group='Range Weekly Hi/Lo - RW Hi/Lo', title='RW Color', defval=color.new(color.blue, 30), inline='rw3')
string rwStyleX = input.string(group='Range Weekly Hi/Lo - RW Hi/Lo', defval='Solid', title='RW Line Style', options=['Dotted', 'Dashed', 'Solid'], inline='rw3')
rwStyle = rwStyleX == 'Dotted' ? line.style_dotted : (rwStyleX == 'Dashed' ? line.style_dashed : (rwStyleX == 'Solid' ? line.style_solid : line.style_dotted))

showAdrTable = input.bool(group='ADR/ADRx3/AWR/AMR Table', title='Show ADR Table', inline='adrt', defval=true)
showAdrPips = input.bool(group='ADR/ADRx3/AWR/AMR Table', title='Show ADR PIPS', inline='adrt', defval=true) and showAdrTable
showAdrCurrency = input.bool(group='ADR/ADRx3/AWR/AMR Table', title='Show ADR Currency', inline='adrt', defval=false) and showAdrTable
showRDPips = input.bool(group='ADR/ADRx3/AWR/AMR Table', title='Show RD PIPS', inline='adrt', defval=false) and showAdrTable
showRDCurrency = input.bool(group='ADR/ADRx3/AWR/AMR Table', title='Show RD Currency', inline='adrt', defval=false) and showAdrTable
choiceAdrTable = input.string(group='ADR/ADRx3/AWR/AMR Table', title='ADR Table postion', inline='adrt', defval='top_right', options=['top_right', 'top_left', 'top_center', 'bottom_right', 'bottom_left', 'bottom_center'])
adrTableBgColor = input.color(group='ADR/ADRx3/AWR/AMR Table', title='ADR Table: Background Color', inline='adrtc', defval=color.rgb(93, 96, 107, 70))
adrTableTxtColor = input.color(group='ADR/ADRx3/AWR/AMR Table', title='Text Color', inline='adrtc', defval=color.rgb(31, 188, 211, 0))

/// market boxes and daily open only on intraday
bool show = timeframe.isminutes and timeframe.multiplier <= 240 and timeframe.multiplier >= 1
bool showDly = timeframe.isminutes  //and timeframe.multiplier < 240
bool showRectangle9 = input.bool(group='Daily Open', defval=true, title='Show: line ?', inline='dopenconf') and showDly
bool showLabel9 = input.bool(group='Daily Open', defval=true, title='Label?', inline='dopenconf') and showRectangle9 and showDly
bool showallDly = input.bool(group='Daily Open', defval=false, title='Show historical daily opens?', inline='dopenconf')
color sess9col = input.color(group='Daily Open', title='Daily Open Color', defval=color.rgb(254, 234, 78, 0), inline='dopenconf1')

bool overrideSym = input.bool(group='PVSRA Override', title='Override chart symbol?', defval=false, inline='pvsra')
string pvsraSym = input.string(group='PVSRA Override', title='', defval='INDEX:BTCUSD', tooltip='You can use INDEX:BTCUSD or you can combine multiple feeds, for example BINANCE:BTCUSDT+COINBASE:BTCUSD. Note that adding too many will slow things down.', inline='pvsra')

bool showVCZ = input.bool(true, 'Show VCZ?' , group='Vector Candle Zones', inline="vczOn")
int zonesMax = input.int(500, 'Maximum zones to draw', group='Vector Candle Zones', inline="vczOn")
string zoneType = input.string(group='Vector Candle Zones', defval='Body only', title='Zone top/bottom is defined with: ', options=['Body only', 'Body with wicks'])
string zoneUpdateType = input.string(group='Vector Candle Zones', defval='Body with wicks', title='Zones are cleared using candle: ', options=['Body only', 'Body with wicks'])
int borderWidth = input.int(0, 'Zone border width', group='Vector Candle Zones')
bool colorOverride = input.bool(true, 'Override color?' , group='Vector Candle Zones', inline="vcz1")
color zoneColor = input.color(title='Color', group='Vector Candle Zones', defval=color.rgb(255, 230, 75, 90), inline="vcz1", tooltip='the vector candle zones color to use if you dont not want to use the PVSRA Candle Colors.')
int transperancy = input.int(90, 'Zone Transperancy', minval = 0, maxval = 100, group='Vector Candle Zones', tooltip='If the vector candle zones color is not overriden, then we want to set the transparancy of the vector candle colors as defined by the PBSRA candle colors. This setting only affects the candle zone colors not the candle colors themselves.')

string rectStyle = input.string(group='Market sessions', defval='Dashed', title='Line style of Market Session hi/lo line', options=['Dashed', 'Solid'])
bool showMarkets = input.bool(true, group='Market sessions', title='Show Market Sessions?', tooltip='Turn on or off all market sessions') and show
bool showMarketsWeekends = input.bool(false, group='Market sessions', title='Show Market Session on Weekends?', tooltip='Turn on or off market sessions in the weekends. Note do not turn this on for exchanges that dont have weekend data like OANDA') and show
string weekendSessions = ':1234567'
string noWeekendSessions = ':23456'

bool showRectangle1 = input.bool(group='Market session: London (0800-1630 UTC+0) - DST Aware', defval=true, title='Show: session?', inline='session1conf', tooltip='If this checkbox is off, Label and Open Range have no effect') and showMarkets
bool showLabel1 = input.bool(group='Market session: London (0800-1630 UTC+0) - DST Aware', defval=true, title='Label?', inline='session1conf') and showRectangle1 and showMarkets
bool showOr1 = input.bool(group='Market session: London (0800-1630 UTC+0) - DST Aware', defval=true, title='Opening Range?', inline='session1conf', tooltip='This controls the shaded area for the session') and showRectangle1 and showMarkets
string sess1Label = input.string(group='Market session: London (0800-1630 UTC+0) - DST Aware', defval='London', title='Name:', inline='session1style')
color sess1col = input.color(group='Market session: London (0800-1630 UTC+0) - DST Aware', title='Color: Box', defval=color.rgb(120, 123, 134, 75), inline='session1style')
color sess1colLabel = input.color(group='Market session: London (0800-1630 UTC+0) - DST Aware', title='Label', defval=color.rgb(120, 123, 134, 0), inline='session1style')
string sess1TimeX = '0800-1630'//input.session(group='Market session: London (0800-1630 UTC+0)', defval='0800-1630', title='Time (UTC+0):', inline='session1style', tooltip='Normally you will not want to adjust these times. Defaults are taken as if the session is NOT in DST and times must be in UTC+0. Note due to limitations of pinescript some values sellected here other than the default might not work correctly on all exchanges.')
sess1Time = showMarketsWeekends ? sess1TimeX + weekendSessions : sess1TimeX + noWeekendSessions

bool showRectangle2 = input.bool(group='Market session: New York (1430-2100 UTC+0) - DST Aware', defval=true, title='Show: session?', inline='session2conf', tooltip='If this checkbox is off, Label and Open Range have no effect') and showMarkets
bool showLabel2 = input.bool(group='Market session: New York (1430-2100 UTC+0) - DST Aware', defval=true, title='Label?', inline='session2conf') and showRectangle2 and showMarkets
bool showOr2 = input.bool(group='Market session: New York (1430-2100 UTC+0) - DST Aware', defval=true, title='Opening Range?', inline='session2conf', tooltip='This controls the shaded area for the session') and showRectangle2 and showMarkets
string sess2Label = input.string(group='Market session: New York (1430-2100 UTC+0) - DST Aware', defval='NewYork', title='Name:', inline='session2style')
color sess2col = input.color(group='Market session: New York (1430-2100 UTC+0) - DST Aware', title='Color: Box', defval=color.rgb(251, 86, 91, 75), inline='session2style')
color sess2colLabel = input.color(group='Market session: New York (1430-2100 UTC+0) - DST Aware', title='Label', defval=color.rgb(253, 84, 87, 25), inline='session2style')
string sess2TimeX = '1430-2100'//input.session(group='Market session: New York (1430-2100 UTC+0)', defval='1430-2100', title='Time (UTC+0):', inline='session2style', tooltip='Normally you will not want to adjust these times. Defaults are taken as if the session is NOT in DST times must be in UTC+0. Note due to limitations of pinescript some values sellected here other than the default might not work correctly on all exchanges.')
sess2Time = showMarketsWeekends ? sess2TimeX + weekendSessions : sess2TimeX  + noWeekendSessions

bool showRectangle3 = input.bool(group='Market session: Tokyo (0000-0600 UTC+0) - DST Aware', defval=true, title='Show: session?', inline='session3conf', tooltip='If this checkbox is off, Label and Open Range have no effect') and showMarkets
bool showLabel3 = input.bool(group='Market session: Tokyo (0000-0600 UTC+0) - DST Aware', defval=true, title='Label?', inline='session3conf') and showRectangle3 and showMarkets
bool showOr3 = input.bool(group='Market session: Tokyo (0000-0600 UTC+0) - DST Aware', defval=true, title='Opening Range?', inline='session3conf', tooltip='This controls the shaded area for the session') and showRectangle3 and showMarkets
string sess3Label = input.string(group='Market session: Tokyo (0000-0600 UTC+0) - DST Aware', defval='Tokyo', title='Name:', inline='session3style')
color sess3col = input.color(group='Market session: Tokyo (0000-0600 UTC+0) - DST Aware', title='Color: Box', defval=color.rgb(80, 174, 85, 75), inline='session3style')
color sess3colLabel = input.color(group='Market session: Tokyo (0000-0600 UTC+0) - DST Aware', title='Label', defval=color.rgb(80, 174, 85, 25), inline='session3style')
string sess3TimeX = '0000-0600'//input.session(group='Market session: Tokyo (0000-0600 UTC+0)', defval='0000-0600', title='Time (UTC+0):', inline='session3style', tooltip='Normally you will not want to adjust these times. Defaults are taken as if the session is NOT in DST times must be in UTC+0. Note due to limitations of pinescript some values sellected here other than the default might not work correctly on all exchanges.')
sess3Time = showMarketsWeekends ? sess3TimeX + weekendSessions : sess3TimeX + noWeekendSessions

bool showRectangle4 = input.bool(group='Market session: Hong Kong (0130-0800 UTC+0) - DST Aware', defval=true, title='Show: session?', inline='session4conf', tooltip='If this checkbox is off, Label and Open Range have no effect') and showMarkets
bool showLabel4 = input.bool(group='Market session: Hong Kong (0130-0800 UTC+0) - DST Aware', defval=true, title='Label?', inline='session4conf') and showRectangle4 and showMarkets
bool showOr4 = input.bool(group='Market session: Hong Kong (0130-0800 UTC+0) - DST Aware', defval=true, title='Opening Range?', inline='session4conf', tooltip='This controls the shaded area for the session') and showRectangle4 and showMarkets
string sess4Label = input.string(group='Market session: Hong Kong (0130-0800 UTC+0) - DST Aware', defval='HongKong', title='Name:', inline='session4style')
color sess4col = input.color(group='Market session: Hong Kong (0130-0800 UTC+0) - DST Aware', title='Color: Box', defval=color.rgb(128, 127, 23, 75), inline='session4style')
color sess4colLabel = input.color(group='Market session: Hong Kong (0130-0800 UTC+0) - DST Aware', title='Label', defval=color.rgb(128, 127, 23, 25), inline='session4style')
string sess4TimeX = '0130-0800'//input.session(group='Market session: Hong Kong (0130-0800 UTC+0)', defval='0130-0800', title='Time (UTC+0):', inline='session4style', tooltip='Normally you will not want to adjust these times. Defaults are taken as if the session is NOT in DST times must be in UTC+0. Note due to limitations of pinescript some values sellected here other than the default might not work correctly on all exchanges.')
sess4Time = showMarketsWeekends ? sess4TimeX + weekendSessions : sess4TimeX + noWeekendSessions

bool showRectangle5 = input.bool(group='Market session: Sydney (NZX+ASX 2200-0600 UTC+0) - DST Aware', defval=true, title='Show: session?', inline='session5conf', tooltip='If this checkbox is off, Label and Open Range have no effect') and showMarkets
bool showLabel5 = input.bool(group='Market session: Sydney (NZX+ASX 2200-0600 UTC+0) - DST Aware', defval=true, title='Label?', inline='session5conf') and showRectangle5 and showMarkets
bool showOr5 = input.bool(group='Market session: Sydney (NZX+ASX 2200-0600 UTC+0) - DST Aware', defval=true, title='Opening Range?', inline='session5conf', tooltip='This controls the shaded area for the session') and showRectangle5 and showMarkets
string sess5Label = input.string(group='Market session: Sydney (NZX+ASX 2200-0600 UTC+0) - DST Aware', defval='Sydney', title='Name:', inline='session5style')
color sess5col = input.color(group='Market session: Sydney (NZX+ASX 2200-0600 UTC+0) - DST Aware', title='Color: Box', defval=color.rgb(37, 228, 123, 75), inline='session5style')
color sess5colLabel = input.color(group='Market session: Sydney (NZX+ASX 2200-0600 UTC+0) - DST Aware', title='Label', defval=color.rgb(37, 228, 123, 25), inline='session5style')
string sess5TimeX = '2200-0600'//input.session(group='Market session: Sydney (NZX+ASX 2200-0600 UTC+0)', defval='2200-0600', title='Time (UTC+0):', inline='session5style', tooltip='Normally you will not want to adjust these times. Defaults are taken as if the session is NOT in DST times must be in UTC+0. Note due to limitations of pinescript some values sellected here other than the default might not work correctly on all exchanges.')
sess5Time = showMarketsWeekends ? sess5TimeX + weekendSessions : sess5TimeX + noWeekendSessions

bool showRectangle6 = input.bool(group='Market session: EU Brinks (0800-0900 UTC+0) - DST Aware', defval=true, title='Show: session?', inline='session6conf', tooltip='If this checkbox is off, Label and Open Range have no effect') and showMarkets
bool showLabel6 = input.bool(group='Market session: EU Brinks (0800-0900 UTC+0) - DST Aware', defval=true, title='Label?', inline='session6conf') and showRectangle6 and showMarkets
bool showOr6 = input.bool(group='Market session: EU Brinks (0800-0900 UTC+0) - DST Aware', defval=true, title='Opening Range?', inline='session6conf', tooltip='This controls the shaded area for the session') and showRectangle6 and showMarkets
string sess6Label = input.string(group='Market session: EU Brinks (0800-0900 UTC+0) - DST Aware', defval='EU Brinks', title='Name:', inline='session6style')
color sess6col = input.color(group='Market session: EU Brinks (0800-0900 UTC+0) - DST Aware', title='Color: Box', defval=color.rgb(255, 255, 255, 65), inline='session6style')
color sess6colLabel = input.color(group='Market session: EU Brinks (0800-0900 UTC+0) - DST Aware', title='Label', defval=color.rgb(255, 255, 255, 25), inline='session6style')
string sess6TimeX = '0800-0900'//input.session(group='Market session: EU Brinks (0800-0900 UTC+0)', defval='0800-0900', title='Time (UTC+0):', inline='session6style', tooltip='Normally you will not want to adjust these times. Defaults are taken as if the session is NOT in DST times must be in UTC+0. Note due to limitations of pinescript some values sellected here other than the default might not work correctly on all exchanges.')
sess6Time = showMarketsWeekends ? sess6TimeX + weekendSessions : sess6TimeX + noWeekendSessions

bool showRectangle7 = input.bool(group='Market session: US Brinks (1400-1500 UTC+0) - DST Aware', defval=true, title='Show: session?', inline='session7conf', tooltip='If this checkbox is off, Label and Open Range have no effect') and showMarkets
bool showLabel7 = input.bool(group='Market session: US Brinks (1400-1500 UTC+0) - DST Aware', defval=true, title='Label?', inline='session7conf') and showRectangle7 and showMarkets
bool showOr7 = input.bool(group='Market session: US Brinks (1400-1500 UTC+0) - DST Aware', defval=true, title='Opening Range?', inline='session7conf', tooltip='This controls the shaded area for the session') and showRectangle7 and showMarkets
string sess7Label = input.string(group='Market session: US Brinks (1400-1500 UTC+0) - DST Aware', defval='US Brinks', title='Name:', inline='session7style')
color sess7col = input.color(group='Market session: US Brinks (1400-1500 UTC+0) - DST Aware', title='Color: Box', defval=color.rgb(255, 255, 255, 65), inline='session7style')
color sess7colLabel = input.color(group='Market session: US Brinks (1400-1500 UTC+0) - DST Aware', title='Label', defval=color.rgb(255, 255, 255, 25), inline='session7style')
string sess7TimeX = '1400-1500'//input.session(group='Market session: US Brinks (1400-1500 UTC+0)', defval='1400-1500', title='Time (UTC+0):', inline='session7style', tooltip='Normally you will not want to adjust these times. Defaults are taken as if the session is NOT in DST times must be in UTC+0. Note due to limitations of pinescript some values sellected here other than the default might not work correctly on all exchanges.')
sess7Time = showMarketsWeekends ? sess7TimeX + weekendSessions : sess7TimeX + noWeekendSessions

bool showRectangle8 = input.bool(group='Market session: Frankfurt (0700-1630 UTC+0) - DST Aware', defval=false, title='Show: session?', inline='session8conf', tooltip='If this checkbox is off, Label and Open Range have no effect') and showMarkets
bool showLabel8 = input.bool(group='Market session: Frankfurt (0700-1630 UTC+0) - DST Aware', defval=true, title='Label?', inline='session8conf') and showRectangle8 and showMarkets
bool showOr8 = input.bool(group='Market session: Frankfurt (0700-1630 UTC+0) - DST Aware', defval=true, title='Opening Range?', inline='session8conf', tooltip='This controls the shaded area for the session') and showRectangle8 and showMarkets
string sess8Label = input.string(group='Market session: Frankfurt (0700-1630 UTC+0) - DST Aware', defval='Frankfurt', title='Name:', inline='session8style')
color sess8col = input.color(group='Market session: Frankfurt (0700-1630 UTC+0) - DST Aware', title='Color: Box', defval=color.rgb(253, 152, 39, 75), inline='session8style')
color sess8colLabel = input.color(group='Market session: Frankfurt (0700-1630 UTC+0) - DST Aware', title='Label', defval=color.rgb(253, 152, 39, 25), inline='session8style')
string sess8TimeX = '0700-1630'//input.session(group='Market session: Frankfurt (0700-1630 UTC+0)', defval='0700-1630', title='Time (UTC+0):', inline='session8style', tooltip='Normally you will not want to adjust these times. Defaults are taken as if the session is NOT in DST times must be in UTC+0. Note due to limitations of pinescript some values sellected here other than the default might not work correctly on all exchanges.')
sess8Time = showMarketsWeekends ? sess8TimeX + weekendSessions : sess8TimeX + noWeekendSessions

bool showPsy = timeframe.isminutes and (timeframe.multiplier == 60 or timeframe.multiplier == 30 or timeframe.multiplier == 15 or timeframe.multiplier == 5 or timeframe.multiplier == 3 or timeframe.multiplier == 1)   
bool showPsylevels = input.bool(group='Weekly Psy Levels (valid tf 1h/30min/15min/5min/3min/1min)', defval=true, title='Show: Levels?', inline='psyconf') and showPsy
bool showPsylabel = input.bool(group='Weekly Psy Levels (valid tf 1h/30min/15min/5min/3min/1min)', defval=true, title='Labels?', inline='psyconf', tooltip="The Psy High/Low will only show on these timeframes: 1h/30min/15min/5min/3min/1min. It is disabled on all others. This is because the calculation requires a candle to start at the correct time for Sydney/Tokyo but in other timeframes the data does not have values at the designated time for the Sydney/Tokyo sessions.") and showPsylevels
bool showAllPsy = input.bool(group='Weekly Psy Levels (valid tf 1h/30min/15min/5min/3min/1min)', defval=false, title='Show historical psy levels?', inline='psyconf') and showPsylevels
color psyColH = input.color(group='Weekly Psy Levels (valid tf 1h/30min/15min/5min/3min/1min)', title='Psy Hi Color', defval=color.new(color.orange, 30), inline='psyconf1') 
color psyColL = input.color(group='Weekly Psy Levels (valid tf 1h/30min/15min/5min/3min/1min)', title='Psy Low Color', defval=color.new(color.orange, 30), inline='psyconf1') 
bool overridePsyType = input.bool(group='Weekly Psy Levels (valid tf 1h/30min/15min/5min/3min/1min)', defval=false, title='Override PsyType', inline='psyconf12')
string psyTypeX = input.string(group='Weekly Psy Levels (valid tf 1h/30min/15min/5min/3min/1min)', defval='crypto', title='Psy calc type', options=['crypto', 'forex'], inline='psyconf12', tooltip="Selecting the override psy type lets you manually adjust the psy type otherwise it will be automatic. If the override psy type checkbox is of then this setting has no effect. usAre you looking at Crypto or Forex? Crypto calculations start with the Sydney session on Saturday night. Forex calculations start with the Tokyo session on Monday morning. Note some exchanges like Oanda do not have sessions on the weekends so you might be forced to select Forex for exchanges like Oanda even when looking at symbols like BITCOIN on Oanda.")
string psyType = overridePsyType ? psyTypeX : (syminfo.type == 'forex' ? 'forex' : 'crypto') 


showDstTable = input.bool(group='Daylight Saving Time Info (DST)', title='Show DST Table : ', inline='dstt', defval=false)
choiceDstTable = input.string(group='Daylight Saving Time Info (DST)', title='DST Table postion', inline='dstt', defval='bottom_center', options=['top_right', 'top_left', 'top_center', 'bottom_right', 'bottom_left', 'bottom_center'])
dstTableBgColor = input.color(group='Daylight Saving Time Info (DST)', title='DST Table: Background Color', inline='dsttc', defval=color.rgb(93, 96, 107, 70))
dstTableTxtColor = input.color(group='Daylight Saving Time Info (DST)', title='Text Color', inline='dsttc', defval=color.rgb(31, 188, 211, 0))

//Non repainting security
f_security(_symbol, _res, _src, _repaint) =>
    request.security(_symbol, _res, _src[_repaint ? 0 : barstate.isrealtime ? 1 : 0])[_repaint ? 0 : barstate.isrealtime ? 0 : 1]

// Basic vars (needed in functions)
// Only render intraday
validTimeFrame = timeframe.isintraday == true

// If above the 5 minute, we start drawing yesterday. below, we start today
levelStart = timeframe.isseconds == true or timeframe.isminutes == true and timeframe.multiplier < 5 ? time('D') : time('D') - 86400 * 1000
//levelsstartbar = ta.barssince(levelsstart)

pivotLabelXOffset = time_close + pivotOffsetInput * timeframe.multiplier * 60 * 1000
labelXOffset = time_close + labelOffsetInput * timeframe.multiplier * 60 * 1000
adrLabelXOffset = time_close + adrOffsetInput * timeframe.multiplier * 60 * 1000
adrLabelXOffset50 = time_close + adrOffsetInput50 * timeframe.multiplier * 60 * 1000
rdLabelXOffset = time_close + rdOffsetInput * timeframe.multiplier * 60 * 1000
rdLabelXOffset50 = time_close + rdOffsetInput50 * timeframe.multiplier * 60 * 1000


//Emas
oneEmaLength = 5
twoEmaLength = 13
threeEmaLength = 50
fourEmaLength = 200
fiveEmaLength = 800

oneEma = ta.ema(close, oneEmaLength)
plot(showEmas ? oneEma : na, color=oneEmaColor, title='5 Ema')

twoEma = ta.ema(close, twoEmaLength)
plot(showEmas ? twoEma : na, color=twoEmaColor, title='13 Ema')

threeEma = ta.ema(close, threeEmaLength)
plot(showEmas ? threeEma : na, color=threeEmaColor, title='50 Ema')

fourEma = ta.ema(close, fourEmaLength)
plot(showEmas ? fourEma : na, color=fourEmaColor, title='200 Ema')

fiveEma = ta.ema(close, fiveEmaLength)
plot(showEmas ? fiveEma : na, color=fiveEmaColor, linewidth=2, title='800 Ema')


// Ema 50 cloud placed here for readability on data window
cloudSize = ta.stdev(close, threeEmaLength * 2) / 4
p1 = plot(showEmas ? threeEma + cloudSize : na, 'Upper 50 Ema Cloud', color=emaCloudBorderColor, offset=0)
p2 = plot(showEmas ? threeEma - cloudSize : na, 'Lower 50 Ema Cloud', color=emaCloudBorderColor, offset=0)
fill(p1, p2, title='EMA 50 Cloud', color=emaCloudColor)

//Label emas
trLib.rLabel(oneEma, '5 Ema', label.style_none, oneEmaColor, labelEmas, labelXOffset)  //ry, rtext, rstyle, rcolor,valid
trLib.rLabel(twoEma, '13 Ema', label.style_none, twoEmaColor, labelEmas, labelXOffset)
trLib.rLabel(threeEma, '50 Ema', label.style_none, threeEmaColor, labelEmas, labelXOffset)
trLib.rLabel(fourEma, '200 Ema', label.style_none, fourEmaColor, labelEmas, labelXOffset)
trLib.rLabel(fiveEma, '800 Ema', label.style_none, fiveEmaColor, labelEmas, labelXOffset)


// Get Daily price data
dayHigh = f_security(syminfo.tickerid, 'D', high, false)
dayLow = f_security(syminfo.tickerid, 'D', low, false)
dayOpen = f_security(syminfo.tickerid, 'D', open, false)
dayClose = f_security(syminfo.tickerid, 'D', close, false)


//Compute Values
pivotPoint = (dayHigh + dayLow + dayClose) / 3

// Updated 2021-03-25 by infernix
pivR1 = 2 * pivotPoint - dayLow
pivS1 = 2 * pivotPoint - dayHigh
pivR2 = pivotPoint - pivS1 + pivR1
pivS2 = pivotPoint - pivR1 + pivS1
pivR3 = 2 * pivotPoint + dayHigh - 2 * dayLow
pivS3 = 2 * pivotPoint - (2 * dayHigh - dayLow)

//Plot Values
pivline = trLib.drawPivot(validTimeFrame and (showLevelOnePivotPoints or showLevelTwoPivotPoints or showLevelThreePivotPoints or activeM) ? pivotPoint : na, 'D', 'PP', pivotColor, pivotLabelColor, mStyle, 1, extendPivots ? extend.both : extend.right, showPivotLabels and validTimeFrame, validTimeFrame, levelStart, pivotLabelXOffset)
pivr1line = trLib.drawPivot(validTimeFrame and showLevelOnePivotPoints ? pivR1 : na, 'D', 'R1', color.new(color.green, 50), color.new(color.green, 50), rsStyle, 1, extendPivots ? extend.both : extend.right, showLevelOnePivotPoints and showPivotLabels and validTimeFrame, validTimeFrame, levelStart, pivotLabelXOffset)
pivs1line = trLib.drawPivot(validTimeFrame and showLevelOnePivotPoints ? pivS1 : na, 'D', 'S1', color.new(color.red, 50), color.new(color.red, 50), rsStyle, 1, extendPivots ? extend.both : extend.right, showLevelOnePivotPoints and showPivotLabels and validTimeFrame, validTimeFrame, levelStart, pivotLabelXOffset)
pivr2line = trLib.drawPivot(validTimeFrame and showLevelTwoPivotPoints ? pivR2 : na, 'D', 'R2', color.new(color.green, 50), color.new(color.green, 50), rsStyle, 1, extendPivots ? extend.both : extend.right, showLevelTwoPivotPoints and showPivotLabels and validTimeFrame, validTimeFrame, levelStart, pivotLabelXOffset)
pivs2line = trLib.drawPivot(validTimeFrame and showLevelTwoPivotPoints ? pivS2 : na, 'D', 'S2', color.new(color.red, 50), color.new(color.red, 50), rsStyle, 1, extendPivots ? extend.both : extend.right, showLevelTwoPivotPoints and showPivotLabels and validTimeFrame, validTimeFrame, levelStart, pivotLabelXOffset)
pivr3line = trLib.drawPivot(validTimeFrame and showLevelThreePivotPoints ? pivR3 : na, 'D', 'R3', color.new(color.green, 50), color.new(color.green, 50), rsStyle, 1, extendPivots ? extend.both : extend.right, showLevelThreePivotPoints and showPivotLabels and validTimeFrame, validTimeFrame, levelStart, pivotLabelXOffset)
pivs3line = trLib.drawPivot(validTimeFrame and showLevelThreePivotPoints ? pivS3 : na, 'D', 'S3', color.new(color.red, 50), color.new(color.red, 50), rsStyle, 1, extendPivots ? extend.both : extend.right, showLevelThreePivotPoints and showPivotLabels and validTimeFrame, validTimeFrame, levelStart, pivotLabelXOffset)


// Daily H/L 
weekHigh = f_security(syminfo.tickerid, 'W', high, false)
weekLow = f_security(syminfo.tickerid, 'W', low, false)



validDHLTimeFrame = timeframe.isintraday == true
validWHLTimeFrame = timeframe.isintraday == true or timeframe.isdaily == true
isToday = year(timenow) == year(time) and month(timenow) == month(time) and dayofmonth(timenow) == dayofmonth(time)
isThisWeek = year(timenow) == year(time) and weekofyear(timenow) == weekofyear(time)


plot(validDHLTimeFrame and showDayHighLow and (showDayHighLow ? true : isToday) ? dayHigh : na, linewidth=2, color=dailyHiLoColor, style=plot.style_stepline, title="YDay Hi", editable=true)
plot(validDHLTimeFrame and showDayHighLow and (showDayHighLow ? true : isToday) ? dayLow : na, linewidth=2, color=dailyHiLoColor, style=plot.style_stepline, title="YDay Lo" , editable=true)
trLib.rLabel(dayHigh, 'YDay Hi', label.style_none, color.new(dailyHiLoColor, 0), validDHLTimeFrame and showDayHighLow and showDayHighLowLabels, labelXOffset)  //ry, rtext, rstyle, rcolor, valid
trLib.rLabel(dayLow, 'YDay Lo', label.style_none, color.new(dailyHiLoColor, 0), validDHLTimeFrame and showDayHighLow and showDayHighLowLabels, labelXOffset)

plot(validWHLTimeFrame and showWeekHighLow and (showWeekHighLow ? true : isThisWeek) ? weekHigh : na, linewidth=2, color=weeklyHiLoColor, style=plot.style_stepline, title="LWeek Hi", editable=true)
plot(validWHLTimeFrame and showWeekHighLow and (showWeekHighLow ? true : isThisWeek) ? weekLow : na, linewidth=2, color=weeklyHiLoColor, style=plot.style_stepline, title="LWeek Lo", editable=true)
trLib.rLabel(weekHigh, 'LWeek Hi', label.style_none, color.new(weeklyHiLoColor, 0), validWHLTimeFrame and showWeekHighLow and showDayHighLowLabels, labelXOffset)  //ry, rtext, rstyle, rcolor, valid
trLib.rLabel(weekLow, 'LWeek Lo', label.style_none, color.new(weeklyHiLoColor, 0), validWHLTimeFrame and showWeekHighLow and showDayHighLowLabels, labelXOffset)

trLib.rLabel(high - (high - low) / 2, 'PVSRA Override Active!', label.style_none, color.orange, overrideSym, labelXOffset)  //ry, rtext, rstyle, rcolor, valid

pvsraVolume(overrideSymbolX, pvsraSymbolX, tickerIdX) =>
    request.security(overrideSymbolX ? pvsraSymbolX : tickerIdX, '', [volume,high,low,close,open], barmerge.gaps_off, barmerge.lookahead_off)

[pvsraVolume, pvsraHigh, pvsraLow, pvsraClose, pvsraOpen]  = pvsraVolume(overrideSym, pvsraSym, syminfo.tickerid)

[pvsraColor, alertFlag, averageVolume, volumeSpread, highestVolumeSpread] = trLib.calcPvsra(pvsraVolume, pvsraHigh, pvsraLow, pvsraClose, pvsraOpen, redVectorColor, greenVectorColor, violetVectorColor, blueVectorColor, regularCandleDownColor, regularCandleUpColor)
barcolor(pvsraColor)

alertcondition(alertFlag, title='Alert on Any Vector Candle', message='{{ticker}} Vector Candle on the {{interval}}')

alertcondition(pvsraColor == greenVectorColor, title='Green Vector Candle', message='{{ticker}} Green Vector Candle on the {{interval}} Note: alert triggers in real time before the candle is closed unless you choose "once per bar close" option - ie the alert might trigger at some point and the pa after that could change the vector color completely. Use with caution.')
alertcondition(pvsraColor == redVectorColor, title='Red Vector Candle', message='{{ticker}} Red Vector Candle on the {{interval}} Note: alert triggers in real time before the candle is closed unless you choose "once per bar close" option- ie the alert might trigger at some point and the pa after that could change the vector color completely. Use with caution.')
alertcondition(pvsraColor == blueVectorColor, title='Blue Vector Candle', message='{{ticker}} Blue Vector Candle on the {{interval}} Note: alert triggers in real time before the candle is closed unless you choose "once per bar close" option- ie the alert might trigger at some point and the pa after that could change the vector color completely. Use with caution.')
alertcondition(pvsraColor == violetVectorColor, title='Purple Vector Candle', message='{{ticker}} Purple Vector Candle on the {{interval}} Note: alert triggers in real time before the candle is closed unless you choose "once per bar close" option- ie the alert might trigger at some point and the pa after that could change the vector color completely. Use with caution.')

redGreen = pvsraColor == greenVectorColor and pvsraColor[1] == redVectorColor
greenRed = pvsraColor == redVectorColor and pvsraColor[1] == greenVectorColor
redBlue = pvsraColor == blueVectorColor and pvsraColor[1] == redVectorColor
blueRed = pvsraColor == redVectorColor and pvsraColor[1] == blueVectorColor
greenPurpule = pvsraColor ==  violetVectorColor and pvsraColor[1] == greenVectorColor
purpleGreen = pvsraColor ==  greenVectorColor and pvsraColor[1] == violetVectorColor
bluePurpule = pvsraColor ==  violetVectorColor and pvsraColor[1] == blueVectorColor
purpleBlue = pvsraColor == blueVectorColor and pvsraColor[1] == violetVectorColor
alertcondition(redGreen, title='Red/Green Vector Candle Pattern', message='{{ticker}} Red/Green Vector Candle Pattern on the {{interval}}')
alertcondition(greenRed, title='Green/Red Vector Candle Pattern', message='{{ticker}} Green/Red Vector Candle Pattern on the {{interval}}')
alertcondition(redBlue, title='Red/Blue Vector Candle Pattern', message='{{ticker}} Red/Blue Vector Candle Pattern on the {{interval}}')
alertcondition(blueRed, title='Blue/Red Vector Candle Pattern', message='{{ticker}} Blue/Red Vector Candle Pattern on the {{interval}}')
alertcondition(greenPurpule, title='Green/Purple Vector Candle Pattern', message='{{ticker}} Green/Purple Vector Candle Pattern on the {{interval}}')
alertcondition(purpleGreen, title='Purple/Green Vector Candle Pattern', message='{{ticker}} Purple/Green Vector Candle Pattern on the {{interval}}')
alertcondition(bluePurpule, title='Blue/Purple Vector Candle Pattern', message='{{ticker}} Blue/Purple Vector Candle Pattern on the {{interval}}')
alertcondition(purpleBlue, title='Purple/Blue Vector Candle Pattern', message='{{ticker}} Purple/Blue Vector Candle Pattern on the {{interval}}')


//ADR
// Daily ADR
[dayAdr, dayAdrHigh, dayAdrLow]  = request.security(syminfo.tickerid, 'D', trLib.adrHiLo(aDRRange,1, showADRDO), lookahead=barmerge.lookahead_on)
dayAdrHigh50 = dayAdrHigh - (dayAdr/2)
dayAdrLow50 = dayAdrLow + (dayAdr/2)
    
if showADR
    string hl = 'Hi-ADR'+ (showADRDO?'(DO)':'')
    string ll = 'Lo-ADR'+ (showADRDO?'(DO)':'')
    trLib.drawLine(dayAdrHigh, 'D', hl, adrColor, adrStyle, 2, extend.right, showADRLabels and validTimeFrame, adrLabelXOffset, validTimeFrame)
    trLib.drawLine(dayAdrLow, 'D', ll, adrColor, adrStyle, 2, extend.right, showADRLabels and validTimeFrame, adrLabelXOffset, validTimeFrame)
    trLib.rLabelOffset((dayAdrHigh + dayAdrLow) / 2, 'ADR ' + str.format('{0,number,#.##}', trLib.toPips(dayAdr)) + 'PIPS|' + str.tostring(dayAdr, format.mintick) + syminfo.currency, label.style_none, adrColor, showADRLabels and validTimeFrame and showADRRange, adrLabelXOffset)  //ry, rtext, rstyle, rcolor, valid

if showADR and showADR50
    string hl = '50% Hi-ADR'+ (showADRDO?'(DO)':'')
    string ll = '50% Lo-ADR'+ (showADRDO?'(DO)':'')
    trLib.drawLine(dayAdrHigh50, 'D', hl, adrColor, adrStyle, 2, extend.right, showADRLabels and validTimeFrame, adrLabelXOffset50, validTimeFrame)
    trLib.drawLine(dayAdrLow50, 'D', ll, adrColor, adrStyle, 2, extend.right, showADRLabels and validTimeFrame, adrLabelXOffset50, validTimeFrame)
    trLib.rLabelOffset((dayAdrHigh50 + dayAdrLow50) / 2, '50% ADR ' + str.format('{0,number,#.##}', trLib.toPips(dayAdr/2)) + 'PIPS|' + str.tostring(dayAdr/2, format.mintick) + syminfo.currency, label.style_none, adrColor, showADRLabels and validTimeFrame and showADRRange, adrLabelXOffset50)  //ry, rtext, rstyle, rcolor, valid
    
alertcondition(ta.crossover(close,dayAdrHigh) and dayAdrHigh != 0.0 , "ADR High reached", "PA has reached the calculated ADR High")
alertcondition(ta.crossunder(close,dayAdrLow) and dayAdrLow != 0.0 , "ADR Low reached", "PA has reached the calculated ADR Low")
alertcondition(ta.crossover(close,dayAdrHigh50) and dayAdrHigh50 != 0.0 , "50% of ADR High reached", "PA has reached 50% of the calculated ADR High")
alertcondition(ta.crossunder(close,dayAdrLow50) and dayAdrLow50 != 0.0 , "50% ADR Low reached", "PA has reached 50% the calculated ADR Low")


//Weekly ADR   
[weekAdr, weekAdrHigh,weekAdrLow]  = request.security(syminfo.tickerid, 'W', trLib.adrHiLo(aWRRange, 1, showAWRWO), lookahead=barmerge.lookahead_on)
weekAdrHigh50 = weekAdrHigh - (weekAdr/2)
weekAdrLow50 = weekAdrLow + (weekAdr/2)

if showAWR
    string hl = 'Hi-AWR'+ (showAWRWO?'(WO)':'')
    string ll = 'Lo-AWR'+ (showAWRWO?'(WO)':'')
    trLib.drawLine(weekAdrHigh, 'W', hl, awrColor, awrStyle, 1, extend.right, showAWRLabels and validTimeFrame, adrLabelXOffset, validTimeFrame)
    trLib.drawLine(weekAdrLow, 'W', ll, awrColor, awrStyle, 1, extend.right, showAWRLabels and validTimeFrame, adrLabelXOffset, validTimeFrame)
    trLib.rLabelOffset((weekAdrHigh + weekAdrLow) / 2, 'AWR ' + str.format('{0,number,#.##}', trLib.toPips(weekAdr)) + 'PIPS|' + str.tostring(weekAdr, format.mintick) + syminfo.currency, label.style_none, awrColor, showAWRLabels and validTimeFrame and showAWRRange, adrLabelXOffset)  //ry, rtext, rstyle, rcolor, valid
if showAWR and showAWR50
    string hl = '50% Hi-AWR'+ (showAWRWO?'(WO)':'')
    string ll = '50% Lo-AWR'+ (showAWRWO?'(WO)':'')
    trLib.drawLine(weekAdrHigh50, 'W', hl, awrColor, awrStyle, 1, extend.right, showAWRLabels and validTimeFrame, adrLabelXOffset50, validTimeFrame)
    trLib.drawLine(weekAdrLow50, 'W', ll, awrColor, awrStyle, 1, extend.right, showAWRLabels and validTimeFrame, adrLabelXOffset50, validTimeFrame)
    trLib.rLabelOffset((weekAdrHigh50 + weekAdrLow50) / 2, '50% AWR ' + str.format('{0,number,#.##}', trLib.toPips(weekAdr/2)) + 'PIPS|' + str.tostring(weekAdr/2, format.mintick) + syminfo.currency, label.style_none, awrColor, showAWRLabels and validTimeFrame and showAWRRange, adrLabelXOffset50)  //ry, rtext, rstyle, rcolor, valid

alertcondition(ta.crossover(close,weekAdrHigh) and weekAdrHigh != 0 , "AWR High reached", "PA has reached the calculated AWR High")
alertcondition(ta.crossunder(close,weekAdrLow) and weekAdrLow != 0 , "AWR Low reached", "PA has reached the calculated AWR Low")
alertcondition(ta.crossover(close,weekAdrHigh50) and weekAdrHigh50 != 0 , "50% of AWR High reached", "PA has reached 50% of the calculated AWR High")
alertcondition(ta.crossunder(close,weekAdrLow50) and weekAdrLow50 != 0 , "50% AWR Low reached", "PA has reached 50% of the calculated AWR Low")


//Monthly ADR 
[monthAdr, monthAdrHigh,monthAdrLow]  = request.security(syminfo.tickerid, 'M', trLib.adrHiLo(aMRRange, 1, showAMRMO), lookahead=barmerge.lookahead_on)
monthAdrHigh50 = monthAdrHigh - (monthAdr/2)
monthAdrLow50 = monthAdrLow + (monthAdr/2)

if showAMR and timeframe.isminutes and timeframe.multiplier >= 3
    string hl = 'Hi-AMR'+ (showAMRMO?'(MO)':'')
    string ll = 'Lo-AMR'+ (showAMRMO?'(MO)':'')
    trLib.drawLine(monthAdrHigh, 'M', hl, amrColor, amrStyle, 1, extend.right, showAMRLabels and validTimeFrame, adrLabelXOffset, validTimeFrame)
    trLib.drawLine(monthAdrLow, 'M', ll, amrColor, amrStyle, 1, extend.right, showAMRLabels and validTimeFrame, adrLabelXOffset, validTimeFrame)
    trLib.rLabelOffset((monthAdrHigh + monthAdrLow) / 2, 'AMR ' + str.format('{0,number,#.##}', trLib.toPips(monthAdr)) + 'PIPS|' + str.tostring(monthAdr, format.mintick) + syminfo.currency, label.style_none, amrColor, showAMRLabels and validTimeFrame and showAMRRange,adrLabelXOffset)  //ry, rtext, rstyle, rcolor, valid
if showAMR and showAMR50 and timeframe.isminutes and timeframe.multiplier >= 3
    string hl = '50% Hi-AMR'+ (showAMRMO?'(MO)':'')
    string ll = '50% Lo-AMR'+ (showAMRMO?'(MO)':'')
    trLib.drawLine(monthAdrHigh50, 'M', hl, amrColor, amrStyle, 1, extend.right, showAMRLabels and validTimeFrame, adrLabelXOffset50, validTimeFrame)
    trLib.drawLine(monthAdrLow50, 'M', ll, amrColor, amrStyle, 1, extend.right, showAMRLabels and validTimeFrame, adrLabelXOffset50, validTimeFrame)
    trLib.rLabelOffset((monthAdrHigh50 + monthAdrLow50) / 2, '50% AMR ' + str.format('{0,number,#.##}', trLib.toPips(monthAdr/2)) + 'PIPS|' + str.tostring(monthAdr/2, format.mintick) + syminfo.currency, label.style_none, amrColor, showAMRLabels and validTimeFrame and showAMRRange,adrLabelXOffset50)  //ry, rtext, rstyle, rcolor, valid

alertcondition(ta.crossover(close,monthAdrHigh) and monthAdrHigh != 0 , "AMR High reached", "PA has reached the calculated AMR High")
alertcondition(ta.crossunder(close,monthAdrLow) and monthAdrLow != 0 , "AMR Low reached", "PA has reached the calculated AMR Low")
alertcondition(ta.crossover(close,monthAdrHigh50) and monthAdrHigh50 != 0 , "50% of AMR High reached", "PA has reached 50% of the calculated AMR High")
alertcondition(ta.crossunder(close,monthAdrLow50) and monthAdrLow50 != 0 , "50% of AMR Low reached", "PA has reached 50% of the calculated AMR Low")


//Range Daily Hi/Lo
[dayRd, dayRangeHigh, dayRangeLow] = request.security(syminfo.tickerid, 'D', trLib.adrHiLo(rdRange, 1, showRDDO), lookahead=barmerge.lookahead_on)
dayRangeHigh50 = dayRangeHigh - (dayRd/2)
dayRangeLow50 = dayRangeLow + (dayRd/2)

if showRD
    string hl = 'RD-Hi'+ (showRDDO?'(DO)':'')
    string ll = 'RD-Lo'+ (showRDDO?'(DO)':'')
    trLib.drawLine(dayRangeHigh, 'D', hl, rdColor, rdStyle, 2, extend.right, showRDLabels and validTimeFrame, rdLabelXOffset, validTimeFrame)
    trLib.drawLine(dayRangeLow, 'D', ll, rdColor, rdStyle, 2, extend.right, showRDLabels and validTimeFrame, rdLabelXOffset, validTimeFrame)
    trLib.rLabelOffset((dayRangeHigh + dayRangeLow) / 2, 'RD ' + str.format('{0,number,#.##}', trLib.toPips(dayRd)) + 'PIPS|' + str.tostring(dayRd/2, format.mintick) + syminfo.currency, label.style_none, rdColor, showRDLabels and validTimeFrame and showRDRange, rdLabelXOffset)  //ry, rtext, rstyle, rcolor, valid
if showRD and showRD50
    string hl = '50% RD-Hi'+ (showRDDO?'(DO)':'')
    string ll = '50% RD-Lo'+ (showRDDO?'(DO)':'')
    trLib.drawLine(dayRangeHigh50, 'D', hl, rdColor, rdStyle, 2, extend.right, showRDLabels and validTimeFrame, rdLabelXOffset50, validTimeFrame)
    trLib.drawLine(dayRangeLow50, 'D', ll, rdColor, rdStyle, 2, extend.right, showRDLabels and validTimeFrame, rdLabelXOffset50, validTimeFrame)
    trLib.rLabelOffset((dayRangeHigh50 + dayRangeHigh50) / 2, '50% RD ' + str.format('{0,number,#.##}', trLib.toPips(dayRd/2)) + 'PIPS|' + str.tostring(dayRd/2, format.mintick) + syminfo.currency, label.style_none, rdColor, showRDLabels and validTimeFrame and showRDRange, rdLabelXOffset50)  //ry, rtext, rstyle, rcolor, valid
  
alertcondition(ta.crossover(close, dayRangeHigh) and dayRangeHigh != 0 , "Range Daily High reached", "PA has reached the calculated Range Daily High")
alertcondition(ta.crossunder(close,dayRangeLow) and dayRangeLow != 0 , "Range Daily Low reached", "PA has reached the calculated Range Daily Low")
alertcondition(ta.crossover(close,dayRangeHigh50) and dayRangeHigh50 != 0 , "50% of Range Daily High reached", "PA has reached 50% of the calculated Range Daily High")
alertcondition(ta.crossunder(close,dayRangeLow50) and dayRangeLow50 != 0 , "50% of Range Daily Low reached", "PA has reached 50% of the calculated Range Daily Low")


//Range Weekly Hi/Lo
[weekRd, weekRangeHigh, weekRangeLow]  = request.security(syminfo.tickerid, 'W', trLib.adrHiLo(rwRange, 1, showRWWO), lookahead=barmerge.lookahead_on)
weekRangeHigh50 = weekRangeHigh - (weekRd/2)
weekRangeLow50 = weekRangeLow + (weekRd/2)

if showRW
    string hl = 'RW-Hi'+ (showRWWO?'(WO)':'')
    string ll = 'RW-Lo'+ (showRWWO?'(WO)':'')
    trLib.drawLine(weekRangeHigh, 'D', hl, rwColor, rwStyle, 2, extend.right, showRWLabels and validTimeFrame, rdLabelXOffset, validTimeFrame)
    trLib.drawLine(weekRangeLow, 'D', ll, rwColor, rwStyle, 2, extend.right, showRWLabels and validTimeFrame, rdLabelXOffset, validTimeFrame)
    trLib.rLabelOffset((weekRangeHigh + weekRangeLow) / 2, 'RW ' + str.format('{0,number,#.##}', trLib.toPips(weekRd)) + 'PIPS|' + str.tostring(weekRd/2, format.mintick) + syminfo.currency, label.style_none, rwColor, showRWLabels and validTimeFrame and showRWRange,rdLabelXOffset)  //ry, rtext, rstyle, rcolor, valid
if showRW and showRW50
    string hl = '50% RW-Hi'+ (showRWWO?'(WO)':'')
    string ll = '50% RW-Lo'+ (showRWWO?'(WO)':'')
    trLib.drawLine(weekRangeHigh50, 'D', hl, rwColor, rwStyle, 2, extend.right, showRWLabels and validTimeFrame, rdLabelXOffset50, validTimeFrame)
    trLib.drawLine(weekRangeLow50, 'D', ll, rwColor, rwStyle, 2, extend.right, showRWLabels and validTimeFrame, rdLabelXOffset50, validTimeFrame)
    trLib.rLabelOffset((weekRangeHigh50 + weekRangeLow50) / 2, '50% RW ' + str.format('{0,number,#.##}', trLib.toPips(weekRd/2)) + 'PIPS|' + str.tostring(weekRd/2, format.mintick) + syminfo.currency, label.style_none, rwColor, showRWLabels and validTimeFrame and showRWRange, rdLabelXOffset50)  //ry, rtext, rstyle, rcolor, valid
 
alertcondition(ta.crossover(close,weekRangeHigh) and weekRangeHigh != 0 , "Range Weekly High reached", "PA has reached the calculated Range Weekly High")
alertcondition(ta.crossunder(close,weekRangeLow) and weekRangeLow != 0 , "Range Weekly Low reached", "PA has reached the calculated Range Weekly Low")
alertcondition(ta.crossover(close,weekRangeHigh50) and weekRangeHigh50 != 0 , "50% of Range Weekly High reached", "PA has reached 50% of the calculated Range Weekly High")
alertcondition(ta.crossunder(close,weekRangeLow50) and weekRangeLow50 != 0 , "50% of Range Weekly Low reached", "PA has reached 50% of the calculated Range Weekly Low")



if barstate.islast and showAdrTable and validTimeFrame

    if showAdrPips or showAdrCurrency
        var table panel = table.new(choiceAdrTable, 2, 16, bgcolor=adrTableBgColor)
        // Table header.
        table.cell(panel, 0, 0, '')
        table.cell(panel, 1, 0, '')
        
        if showAdrPips
            table.cell(panel, 0, 2, 'ADR', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 2, str.format('{0,number,#.##}', trLib.toPips(dayAdr)) + "PIPS" , text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 0, 3, 'ADRx3', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 3, str.format('{0,number,#.##}', trLib.toPips(dayAdr) * 3)+ "PIPS", text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 0, 4, 'AWR', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 4, str.format('{0,number,#.##}', trLib.toPips(weekAdr)) + "PIPS", text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 0, 5, 'AMR', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 5, str.format('{0,number,#.##}', trLib.toPips(monthAdr)) + "PIPS", text_color=adrTableTxtColor, text_halign=text.align_left)
        
        if showAdrCurrency  
            table.cell(panel, 0, 6, 'ADR', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 6, str.tostring(dayAdr, format.mintick) + syminfo.currency, text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 0, 7, 'ADRx3', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 7, str.tostring(dayAdr * 3, format.mintick) + syminfo.currency, text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 0, 8, 'AWR', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 8, str.tostring(weekAdr, format.mintick) + syminfo.currency, text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 0, 9, 'AMR', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 9, str.tostring(monthAdr, format.mintick) + syminfo.currency, text_color=adrTableTxtColor, text_halign=text.align_left)
        
        if showRDPips
            table.cell(panel, 0, 10, 'RD', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 10, str.format('{0,number,#.##}', trLib.toPips(dayRd)) + "PIPS" , text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 0, 11, 'RDx3', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 11, str.format('{0,number,#.##}', trLib.toPips(dayRd) * 3)+ "PIPS", text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 0, 12, 'RW', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 12, str.format('{0,number,#.##}', trLib.toPips(weekRd)) + "PIPS", text_color=adrTableTxtColor, text_halign=text.align_left)
            
        if showRDCurrency  
            table.cell(panel, 0, 13, 'RD', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 13, str.tostring(dayRd, format.mintick) + syminfo.currency, text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 0, 14, 'RDx3', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 14, str.tostring(dayRd * 3, format.mintick) + syminfo.currency, text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 0, 15, 'RW', text_color=adrTableTxtColor, text_halign=text.align_left)
            table.cell(panel, 1, 15, str.tostring(weekRd, format.mintick) + syminfo.currency, text_color=adrTableTxtColor, text_halign=text.align_left)
            
   




// M - Levels
//Calculate Pivot Point
// 2021-03025 updated by infernix 
//M calculations
m0C = (pivS2 + pivS3) / 2
m1C = (pivS1 + pivS2) / 2
m2C = (pivotPoint + pivS1) / 2
m3C = (pivotPoint + pivR1) / 2
m4C = (pivR1 + pivR2) / 2
m5C = (pivR2 + pivR3) / 2


trLib.drawPivot(validTimeFrame and activeM and m0C ? m0C : na, 'D', 'M0', mColor, mLabelColor, mStyle, 1, extendPivots ? extend.both : extend.right, showMLabels and validTimeFrame, validTimeFrame, levelStart, pivotLabelXOffset)
trLib.drawPivot(validTimeFrame and activeM and m1C ? m1C : na, 'D', 'M1', mColor, mLabelColor, mStyle, 1, extendPivots ? extend.both : extend.right, showMLabels and validTimeFrame, validTimeFrame, levelStart, pivotLabelXOffset)
trLib.drawPivot(validTimeFrame and activeM and m2C ? m2C : na, 'D', 'M2', mColor, mLabelColor, mStyle, 1, extendPivots ? extend.both : extend.right, showMLabels and validTimeFrame, validTimeFrame, levelStart, pivotLabelXOffset)
trLib.drawPivot(validTimeFrame and activeM and m3C ? m3C : na, 'D', 'M3', mColor, mLabelColor, mStyle, 1, extendPivots ? extend.both : extend.right, showMLabels and validTimeFrame, validTimeFrame, levelStart, pivotLabelXOffset)
trLib.drawPivot(validTimeFrame and activeM and m4C ? m4C : na, 'D', 'M4', mColor, mLabelColor, mStyle, 1, extendPivots ? extend.both : extend.right, showMLabels and validTimeFrame, validTimeFrame, levelStart, pivotLabelXOffset)
trLib.drawPivot(validTimeFrame and activeM and m5C ? m5C : na, 'D', 'M5', mColor, mLabelColor, mStyle, 1, extendPivots ? extend.both : extend.right, showMLabels and validTimeFrame, validTimeFrame, levelStart, pivotLabelXOffset)


//*****************
// Market sessions
//*****************
[nyDST, ukDST, sydDST] = trLib.calcDst()            
if ukDST
    trLib.drawOpenRange(sess1Time,sess1col,showOr1,'GMT+1')
    trLib.drawSessionHiLo(sess1Time, showRectangle1, showLabel1, sess1colLabel, sess1Label, 'GMT+1', rectStyle)
else
    trLib.drawOpenRange(sess1Time,sess1col,showOr1,'GMT+0')
    trLib.drawSessionHiLo(sess1Time, showRectangle1, showLabel1, sess1colLabel, sess1Label, 'GMT+0', rectStyle)

if nyDST
    trLib.drawOpenRange(sess2Time,sess2col,showOr2,'GMT+1')
    trLib.drawSessionHiLo(sess2Time, showRectangle2, showLabel2, sess2colLabel, sess2Label, 'GMT+1', rectStyle)
else
    trLib.drawOpenRange(sess2Time,sess2col,showOr2,'GMT+0')
    trLib.drawSessionHiLo(sess2Time, showRectangle2, showLabel2, sess2colLabel, sess2Label, 'GMT+0', rectStyle)

// Tokyo
trLib.drawOpenRange(sess3Time,sess3col,showOr3,'GMT+0')
trLib.drawSessionHiLo(sess3Time, showRectangle3, showLabel3, sess3colLabel, sess3Label, 'GMT+0', rectStyle)

// Hong Kong
trLib.drawOpenRange(sess4Time,sess4col,showOr4,'GMT+0')
trLib.drawSessionHiLo(sess4Time, showRectangle4, showLabel4, sess4colLabel, sess4Label, 'GMT+0', rectStyle)

if sydDST
    trLib.drawOpenRange(sess5Time,sess5col,showOr5,'GMT+1')
    trLib.drawSessionHiLo(sess5Time, showRectangle5, showLabel5, sess5colLabel, sess5Label, 'GMT+1', rectStyle)
else
    trLib.drawOpenRange(sess5Time,sess5col,showOr5,'GMT+0')
    trLib.drawSessionHiLo(sess5Time, showRectangle5, showLabel5, sess5colLabel, sess5Label, 'GMT+0', rectStyle)

//eu brinks is for london
if ukDST
    trLib.drawOpenRange(sess6Time,sess6col,showOr6,'GMT+1')
    trLib.drawSessionHiLo(sess6Time, showRectangle6, showLabel6, sess6colLabel, sess6Label, 'GMT+1', rectStyle)
else
    trLib.drawOpenRange(sess6Time,sess6col,showOr6,'GMT+0')
    trLib.drawSessionHiLo(sess6Time, showRectangle6, showLabel6, sess6colLabel, sess6Label, 'GMT+0', rectStyle)

//us brinks is ny
if nyDST
    trLib.drawOpenRange(sess7Time,sess7col,showOr7,'GMT+1')
    trLib.drawSessionHiLo(sess7Time, showRectangle7, showLabel7, sess7colLabel, sess7Label, 'GMT+1', rectStyle)
else
    trLib.drawOpenRange(sess7Time,sess7col,showOr7,'GMT+0')
    trLib.drawSessionHiLo(sess7Time, showRectangle7, showLabel7, sess7colLabel, sess7Label, 'GMT+0', rectStyle)

//becuase frankfurt changes with london
if ukDST
    trLib.drawOpenRange(sess8Time,sess8col,showOr8,'GMT+1')
    trLib.drawSessionHiLo(sess8Time, showRectangle8, showLabel8, sess8colLabel, sess8Label, 'GMT+1', rectStyle)
else
    trLib.drawOpenRange(sess8Time,sess8col,showOr8,'GMT+0')
    trLib.drawSessionHiLo(sess8Time, showRectangle8, showLabel8, sess8colLabel, sess8Label, 'GMT+0', rectStyle)


//************//
// Psy Levels //
//************//
var int oneWeekMillis = (7 * 24 * 60 * 60 * 1000)

[psyHi, psyLo, psyHiLabel, psyLoLabel, psySessionStartTime] = trLib.calcPsyLevels(oneWeekMillis, showPsylevels, psyType, sydDST)

// Draw Psy Level Lines
if (barstate.islast) and not showAllPsy and showPsylevels
    
    // Extend line back to the previous start time (after Psy-Hi/Lo have been calculated)
    psyHiLine = line.new(time, psyHi, psySessionStartTime, psyHi, xloc.bar_time, extend.none, psyColH)
    line.delete(psyHiLine[1])
    psyLoLine = line.new(time, psyLo, psySessionStartTime, psyLo, xloc.bar_time, extend.none, psyColL)
    line.delete(psyLoLine[1])

// Write Psy Level Labels - same label regardless if line.new or plot used
trLib.rLabelLastBar(psyHi, psyHiLabel, label.style_none, psyColH, showPsylabel, labelXOffset)
trLib.rLabelLastBar(psyLo, psyLoLabel, label.style_none, psyColL, showPsylabel, labelXOffset) 

// Plot Historical Psy Level
plot(showPsy and showPsylevels and showAllPsy ? psyHi : na, color=psyColH, style=plot.style_stepline, linewidth=2, editable=false, title="Psy-Hi")  //, offset=psy_plot_offset)
plot(showPsy and showPsylevels and showAllPsy ? psyLo : na, color=psyColL, style=plot.style_stepline, linewidth=2, editable=false, title="Psy-Lo")  //, offset=psy_plot_offset)

alertcondition(ta.crossunder(close,psyHi) and not na(psyHi) and psyHi != 0 , "PA crossed under Psy Hi", "PA has crossed under the Psy Hi")
alertcondition(ta.crossover(close,psyHi) and not na(psyHi) and psyHi != 0  , "PA crossed over Psy Hi", "PA has crossed over the Psy Hi")
alertcondition(ta.crossunder(close,psyLo) and not na(psyLo) and psyLo != 0 , "PA crossed under Psy Lo", "PA has crossed under the Psy Lo")
alertcondition(ta.crossover(close,psyLo) and not na(psyLo) and psyLo != 0  , "PA crossed over Psy Lo", "PA has crossed over the Psy Lo")



//***********
// Daily open
//***********
dailyOpen = trLib.getdayOpen()
//this plot is only to show historical values when the option is selected.
plot(showRectangle9 and validTimeFrame and showallDly ? dailyOpen : na, color=sess9col, style=plot.style_stepline, linewidth=2, editable=false, title="Daily Open")
if showallDly
    //if historical values are selected to be shown - then add a label to the plot
    trLib.rLabel(dailyOpen, 'Daily Open', label.style_none, sess9col, validTimeFrame and showLabel9, labelXOffset)
    showallDly
else
    if showRectangle9
        //othewise we draw the line and label together - showing only todays line.
        trLib.drawLineDO(dailyOpen, 'D', 'Daily Open', sess9col, line.style_solid, 1, extend.none, validTimeFrame and showLabel9, labelXOffset, validTimeFrame)

alertcondition(ta.cross(close,dailyOpen) and not na(dailyOpen) and dailyOpen != 0 , "PA crossed Daily open", "PA has crossed the Daily open")


    
//London DST Starts Last Sunday of March DST Edns Last Sunday of October
//New York DST Starts 2nd Sunday of March DST Edns 1st Sunday of November
//Sydney DST Start on 1st Sunday of October DST ends 1st Sunday of Arpil
//Frankfurt DST Starts Last Sunday of March DST Edns Last Sunday of October 
if barstate.islast and showDstTable


    var table dstTable = table.new(choiceDstTable, 2, 8, bgcolor=dstTableBgColor)

    //general
    table.cell(dstTable, 0, 0, 'London DST Starts Last Sunday of March | DST Ends Last Sunday of October', text_color=adrTableTxtColor, text_halign=text.align_left)
    table.cell(dstTable, 0, 1, 'New York DST Starts 2nd Sunday of March | DST Ends 1st Sunday of November', text_color=adrTableTxtColor, text_halign=text.align_left)
    table.cell(dstTable, 0, 2, 'Tokyo does not observe DST', text_color=adrTableTxtColor, text_halign=text.align_left)
    table.cell(dstTable, 0, 3, 'Hong Kong does not observe DST', text_color=adrTableTxtColor, text_halign=text.align_left)
    table.cell(dstTable, 0, 4, 'Sydney DST Start on 1st Sunday of October | DST Ends 1st Sunday of Arpil', text_color=adrTableTxtColor, text_halign=text.align_left)
    table.cell(dstTable, 0, 5, 'EU Brinks DST Starts Last Sunday of March | DST Ends Last Sunday of October', text_color=adrTableTxtColor, text_halign=text.align_left)
    table.cell(dstTable, 0, 6, 'US Brinks DST Starts 2nd Sunday of March | DST Ends 1st Sunday of November', text_color=adrTableTxtColor, text_halign=text.align_left)
    table.cell(dstTable, 0, 7, 'Frankfurt DST Starts Last Sunday of March | DST Ends Last Sunday of October', text_color=adrTableTxtColor, text_halign=text.align_left)

//Vector Candle Zones
var zoneBoxesAbove = array.new_box()
var zoneBoxesBelow = array.new_box()
if showVCZ
    pvsra = trLib.getPvsraFlagByColor(pvsraColor, redVectorColor, greenVectorColor, violetVectorColor, blueVectorColor, regularCandleUpColor)
    trLib.updateZones(pvsra, 0, zoneBoxesBelow, zonesMax, pvsraHigh, pvsraLow, pvsraOpen, pvsraClose, transperancy, zoneUpdateType, zoneColor, zoneType, borderWidth, colorOverride, redVectorColor, greenVectorColor, violetVectorColor, blueVectorColor)
    trLib.updateZones(pvsra, 1, zoneBoxesAbove, zonesMax, pvsraHigh, pvsraLow, pvsraOpen, pvsraClose, transperancy, zoneUpdateType, zoneColor, zoneType, borderWidth, colorOverride, redVectorColor, greenVectorColor, violetVectorColor, blueVectorColor)
    trLib.cleanarr(zoneBoxesAbove)
    trLib.cleanarr(zoneBoxesBelow)