Skip to content

Instantly share code, notes, and snippets.

@TechNinjaWeb
Last active December 10, 2018 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TechNinjaWeb/e980d73bd91f02ef3cd5fceff484c164 to your computer and use it in GitHub Desktop.
Save TechNinjaWeb/e980d73bd91f02ef3cd5fceff484c164 to your computer and use it in GitHub Desktop.
Time Session Study
//@version=3
study(title="Support Resistance Fibonacci Levels",shorttitle="SupResFib",overlay=true)
//Created by UCSgears
//Plots Standard deviation on pricechart based on Historical Volatility.
//This Code will be revised when Implied Volatility is available in trading view.
// strategy(title="UCS_Standard Deviation-Historical Volatility_V2", shorttitle="UCS_StdDev(HV)", overlay = true, pyramiding=1, initial_capital=100.00, commission_type=strategy.commission.percent, commission_value=1)
stdev_hv_length = input(21, minval=1) // 10
DaystoExpire = input (26, defval=30, minval=1) // 7
show_stdev_bands=input(true,title="Show Standard Deviation Bands")
show_avg_stdev=input(true,title="Show Average Standard Deviation Bands")
stddev1=input(true,title="Standard Deviation 1")
stddev2=input(true,title="Standard Deviation 2")
stddev3=input(true,title="Standard Deviation 3")
stdev_middle=input(true,title="Standard Deviation Average Bands")
Smooth = input(true)
sm = input (21, title = "Smooth Length")
stdev_displacement = input(5, defval=26, minval=0, maxval=60, title="Upper Band Displacement") // 17
stdev_mid_displacement = input(0, defval=26, minval=0, maxval=60, title="Mid Band Displacement")
annual = 365
per = isintraday or isdaily and interval == 1 ? 1 : 7
hv = stdev(log(close / close[1]), stdev_hv_length) * sqrt(annual / per)
stdhv = close*hv*sqrt(DaystoExpire/365)
stdhv1 = Smooth ? ema(stdhv,sm) : stdhv
stdhv2 = stdhv1*2
stdhv3 = stdhv1*3
//EMA Definitions
emaFast = sma(close, 8)
emaSlow = sma(close, 26)
emaSmooth = sma(close, 55)
stdev_average = avg(emaSlow, emaFast, emaSmooth)
ahv = stdev(log(stdev_average / stdev_average[1]), stdev_hv_length) * sqrt(annual / per)
astdhv = stdev_average*ahv*sqrt(DaystoExpire/365)
stdev1 = astdhv
stdev2 = stdev1*2
stdev3 = stdev1*3
Stdhv1u = plot(show_stdev and stddev1 ? (close+stdhv1):na, color = teal, title = "1st Standard Deviation Upperband", offset=stdev_displacement)
Stdhv1d = plot(show_stdev and stddev1 ? (close-stdhv1):na, color = teal, title = "1st Standard Deviation Lowerband", offset=stdev_displacement)
Stdhv2u = plot(show_stdev and stddev2 ? (close+stdhv2):na, color = red, title = "2nd Standard Deviation Upperband", offset=stdev_displacement)
Stdhv2d = plot(show_stdev and stddev2 ? (close-stdhv2):na, color = red, title = "2nd Standard Deviation Lowerband", offset=stdev_displacement)
Stdhv3u = plot(show_stdev and stddev3 ? (close+stdhv3):na, color = white, title = "3rd Standard Deviation Upperband", offset=stdev_displacement)
Stdhv3d = plot(show_stdev and stddev3 ? (close-stdhv3):na, color = white, title = "3rd Standard Deviation Lowerband", offset=stdev_displacement)
// aStdhv1u = plot(show_avg_stdev and stddev1 ? (stdev_average+stdev1):na, color = teal, title = "1st Average Deviation Upperband", offset=stdev_displacement)
// aStdhv1d = plot(show_avg_stdev and stddev1 ? (stdev_average-stdev1):na, color = teal, title = "1st Average Deviation Lowerband", offset=stdev_displacement)
// aStdhv2u = plot(show_avg_stdev and stddev2 ? (stdev_average+stdev2):na, color = red, title = "2nd Average Deviation Upperband", offset=stdev_displacement)
// aStdhv2d = plot(show_avg_stdev and stddev2 ? (stdev_average-stdev2):na, color = red, title = "2nd Average Deviation Lowerband", offset=stdev_displacement)
average_plot = plot(show_stdev ? stdev_average : na, color = blue, title = "Average Deviation", offset=stdev_mid_displacement, linewidth=4)
astdev_up_plot = plot(show_avg_stdev and stdev_middle ? (stdev_average+stdev3):na, color = white, title = "3rd Average Deviation Upperband", offset=stdev_mid_displacement)
astdev_down_plot = plot(show_avg_stdev and stdev_middle ? (stdev_average-stdev3):na, color = white, title = "3rd Average Deviation Lowerband", offset=stdev_mid_displacement)
fill(astdev_up_plot, astdev_down_plot, color=yellow, transp=90, title="Average STDev Channel")
use_monthly = input(true,"Use Monthly Basis?")
months_in = input(1,"# Months for Monthly Basis (this is limited by max lookback at your resolution!)",minval=1)
use_weekly = input(true,"Use Weekly Basis?")
weeks_in = input(2,"# Weeks for Weekly Basis",minval=1)
use_daily = input(true,"Use Daily Basis?")
days_in = input(7,"# Days for Daily Basis",minval=1)
use_hourly = input(false,"Use Hourly Basis? (only works on intraday charts)")
hours_in = input(84,"# Hours for Hourly Basis",minval=1)
use_minutes = input(false,"Use per-Minute Basis? (only works on intraday charts)")
minutes_in = input(2520,"# Minutes for per-Minute Basis",minval=1)
hold_intraday = input(true,"Keep Lines Straight All Day? (holds morning value until next day)")
connect_lines = input(false,"Connect Sup/Res Crosses with a Line?")
zigzag_depth = input(50,"Zig Zag Depth",minval=4)
zigzag_filter = input(0.0,"Zig Zag Reversal Sensitivity, %",minval=-99,maxval=99)
zigzag(_price,_high,_low,_depth,_filter) =>
_offset = max(floor(_depth/2),2)
_deviation = highest(stdev(_price[_offset],50)/nz(_price[_offset],1),100)*(1-_filter/100)
_window = na
_window := nz(_window[1])+1
_long_enough = _window>=_depth
_top = _high[_offset-1]<nz(_high[_offset])
_bottom = _low[_offset-1]>nz(_low[_offset])
_zz_candidate = (_top or _bottom) and _long_enough
_last_extreme = _high[_offset]
_last_extreme := nz(_last_extreme[1])
if _top and _bottom
if abs(_last_extreme-_high[_offset])>abs(_last_extreme-_low[_offset])
_bottom := false
else
_top := false
_this_extreme = _top?_high[_offset]:(_bottom?_low[_offset]:na)
_zz_found = (_zz_candidate and _long_enough and (abs(_this_extreme-_last_extreme)/nz(_price[_offset],1))>_deviation)
or _high[_offset] >= highest(_high,_depth)
or _low[_offset] <= lowest(_low,_depth)
if _zz_found
_last_extreme := _this_extreme
_window := 0
_zigzag = _zz_found?_this_extreme:na
zz = zigzag(hl2,high,low,zigzag_depth,zigzag_filter)
plot(zz,color=blue,linewidth=2,offset=-max(floor(zigzag_depth/2),2))
fib_source = input(title="Draw Fibonacci Levels For Which Basis Pair?",defval="Hourly",options=["None","Monthly","Weekly","Daily","Hourly","Minutes"])
fib_invert = input(false,"Invert Fibonacci Levels? (i.e. draw from max to min)")
daysinweek = input(5,"# Days in a Week of Trading (change to 7 for crypto)")
months = months_in%12
year_months = floor(months_in/12)*12 //*12 added after commenting out below
//it really bugs me that highest() and lowest() cannot accept series inputs...
// monthly_bar = (month>(months+1))?(month-months_in):(month+12-months_in)
// yearly_bar = year - year_months
// monthly_high = highest(high,barssince(month==monthly_bar and year==yearly_bar))
// monthly_low = lowest(low,barssince(month==monthly_bar and year==yearly_bar))
monthly_bars = (ismonthly?months_in:((isintraday?1440:1)*(year_months*52*daysinweek+months*4*daysinweek)/(isweekly?daysinweek:1)))/interval
monthly_high = highest(high,min(5000,monthly_bars))
monthly_low = lowest(low,min(5000,monthly_bars))
//plot(n)
weeks = weeks_in%52
year_weeks = floor(weeks_in/52)*daysinweek
weekly_bars = (isweekly?weeks_in:((isintraday?1440:1)*(year_weeks*52*daysinweek+weeks*daysinweek)/(ismonthly?4*daysinweek:1)))/interval
weekly_high = highest(high,min(5000,weekly_bars))
weekly_low = lowest(low,min(5000,weekly_bars))
days = ismonthly?max(days_in,4*daysinweek):isweekly?max(days_in,daysinweek):days_in
daily_bars = (isdaily?days:((isintraday?1440:1)*days/((ismonthly?4*daysinweek:1)*(isweekly?daysinweek:1))))/interval
daily_high = highest(high,min(5000,daily_bars))
daily_low = lowest(low,min(5000,daily_bars))
//# hours per day varies widely...restrict these next two to intraday.
hours = isdwm?na:hours_in
hourly_bars = max(hours*60/interval,1)
hourly_high = highest(high,min(5000,isdwm?24:hourly_bars))
hourly_low = lowest(low,min(5000,isdwm?24:hourly_bars))
minutes = isdwm?na:minutes_in
minute_bars = max(minutes/interval,1)
minute_high = highest(high,min(5000,isdwm?1440:minute_bars))
minute_low = lowest(low,min(5000,isdwm?1440:minute_bars))
flat_monthly_high = high
flat_monthly_low = low
flat_weekly_high = high
flat_weekly_low = low
flat_daily_high = high
flat_daily_low = low
flat_monthly_high := change(dayofmonth)?monthly_high:na(monthly_high)?na:nz(flat_monthly_high[1],na(monthly_high[1])?monthly_high:high)
flat_monthly_low := change(dayofmonth)?monthly_low:na(monthly_low)?na:nz(flat_monthly_low[1],na(monthly_low[1])?monthly_low:low)
flat_weekly_high := change(dayofmonth)?weekly_high:na(weekly_high)?na:nz(flat_weekly_high[1],na(weekly_high[1])?weekly_high:high)
flat_weekly_low := change(dayofmonth)?weekly_low:na(weekly_low)?na:nz(flat_weekly_low[1],na(weekly_low[1])?weekly_low:low)
flat_daily_high := change(dayofmonth)?daily_high:na(daily_high)?na:nz(flat_daily_high[1],na(daily_high[1])?daily_high:high)
flat_daily_low := change(dayofmonth)?daily_low:na(daily_low)?na:nz(flat_daily_low[1],na(daily_low[1])?daily_low:low)
plot_monthly_high = hold_intraday?flat_monthly_high:monthly_high
plot_monthly_low = hold_intraday?flat_monthly_low:monthly_low
plot_weekly_high = hold_intraday?flat_weekly_high:weekly_high
plot_weekly_low = hold_intraday?flat_weekly_low:weekly_low
plot_daily_high = hold_intraday?flat_daily_high:daily_high
plot_daily_low = hold_intraday?flat_daily_low:daily_low
fhigh = na
flow = na
if fib_source!= "None"
fhigh := fib_source=="Monthly"?plot_monthly_high:
fib_source=="Weekly"?plot_weekly_high:
fib_source=="Daily"?plot_daily_high:
fib_source=="Hourly"?hourly_high:
fib_source=="Minutes"?minute_high:
na
flow := fib_source=="Monthly"?plot_monthly_low:
fib_source=="Weekly"?plot_weekly_low:
fib_source=="Daily"?plot_daily_low:
fib_source=="Hourly"?hourly_low:
fib_source=="Minutes"?minute_low:
na
calc_fib(_max,_min,_dir) =>
_diff = _max-_min
_236 = _diff*0.236*(_dir?-1:1)+(_dir?_max:_min)
_382 = _diff*0.382*(_dir?-1:1)+(_dir?_max:_min)
_500 = _diff*0.5*(_dir?-1:1)+(_dir?_max:_min)
_618 = _diff*0.618*(_dir?-1:1)+(_dir?_max:_min)
_786 = _diff*0.786*(_dir?-1:1)+(_dir?_max:_min)
_1618 = _diff*1.618*(_dir?-1:1)+(_dir?_max:_min)
[_236,_382,_500,_618,_786,_1618]
[F236,F382,F500,F618,F786,F1618] = calc_fib(fhigh,flow,fib_invert)
p236 = plot(F236,color=red)
p382 = plot(F382,color=yellow)
p500 = plot(F500,color=lime)
p618 = plot(F618,color=aqua)
p786 = plot(F786,color=teal)
p1618 = plot(F1618,color=blue)
color_monthly_high = use_monthly?gray:na
color_monthly_low = use_monthly?gray:na
pMh = plot(use_monthly?plot_monthly_high:high,style=cross,color=color_monthly_high,linewidth=4,transp=65,join=connect_lines) //change plotted values for better autoscaling
pMl = plot(use_monthly?plot_monthly_low:low,style=cross,color=color_monthly_low,linewidth=4,transp=80,join=connect_lines)
color_weekly_high = use_weekly?red:na
color_weekly_low = use_weekly?red:na
pwh = plot(use_weekly?plot_weekly_high:high,style=circles,color=color_weekly_high,linewidth=3,transp=65,join=connect_lines)
pwl = plot(use_weekly?plot_weekly_low:low,style=circles,color=color_weekly_low,linewidth=3,transp=80,join=connect_lines)
color_daily_high = use_daily?blue:na
color_daily_low = use_daily?blue:na
pdh = plot(use_daily?plot_daily_high:high,style=cross,color=color_daily_high,linewidth=3,transp=65,join=connect_lines)
pdl = plot(use_daily?plot_daily_low:low,style=cross,color=color_daily_low,linewidth=3,transp=80,join=connect_lines)
color_hourly_high = (isintraday and use_hourly)?green:na
color_hourly_low = (isintraday and use_hourly)?green:na
phh = plot((isintraday and use_hourly)?hourly_high:high,style=circles,color=color_hourly_high,linewidth=2,transp=65,join=connect_lines)
phl = plot((isintraday and use_hourly)?hourly_low:low,style=circles,color=color_hourly_low,linewidth=2,transp=80,join=connect_lines)
color_minute_high = (isintraday and use_minutes)?black:na
color_minute_low = (isintraday and use_minutes)?black:na
pmh = plot((isintraday and use_minutes)?minute_high:high,style=cross,color=color_minute_high,linewidth=2,transp=65,join=connect_lines)
pml = plot((isintraday and use_minutes)?minute_low:low,style=cross,color=color_minute_low,linewidth=2,transp=80,join=connect_lines)
// pzh=plot(high,color=na)
// pzl=plot(low,color=na)
fphigh=fib_invert?flow:fhigh
fplow=fib_invert?fhigh:flow
phigh = plot(fphigh,color=na)
plow = plot(fplow,color=na)
fill(plow,p236,red)
fill(p236,p382,yellow)
fill(p382,p500,lime)
fill(p500,p618,aqua)
fill(p618,p786,teal)
fill(p786,phigh,gray)
fill(phigh,p1618,blue)
study(title="Trading hours", shorttitle="Trading hours", overlay=false)
// Standard Forex Trade Sessions
// New York opens at 8:00 am to 5:00 pm EST (EDT)
// Tokyo opens at 7:00 pm to 4:00 am EST (EDT)
// Sydney opens at 5:00 pm to 2:00 am EST (EDT)
// London opens at 3:00 am to 12:00 noon EST (EDT)
// Overlapping Sessions
// New York and London: between 8:00 am — 12:00 noon EST (EDT)
// Sydney and Tokyo: between 7:00 pm — 2:00 am EST (EDT)
// London and Tokyo: between 3:00 am — 4:00am EST (EDT)
//--- Europe
EUR = "0300-1200"
//--- America
USA = "0800-1700"
//--- Asia
Asia = "1900-0400"
//--- Pacific
Ausi = "1700-0200"
//--- Overlap
EUR_USA = "0800-1200"
AUS_ASIA = "1900-0200"
EUR_ASIA = "0300-0400"
//-- Time In Range
timeinrange(res, sess) => time(res, sess) != 0
// InSession() determines if a price bar falls inside the specified session
InSession(sess) => na(time(period, sess)) == false
london = timeinrange(period, EUR)
newyork = timeinrange(period, USA)
tokyo = timeinrange(period, Asia)
sydney = timeinrange(period, Ausi)
//--- Plots
shift = period == "1" ? 61 : period == "3" ? 21 : period == "5" ? 13 : period == "15" ? 5 : period == "30" ? 3 : period == "60" ? 2 : 0
// plot(8, color=london?#3fa9f5:white, linewidth=5, offset=shift, title="London")
// plot(7, color=frankfurt?#3fa9f5:white, linewidth=5, offset=shift, title="Frankfurt")
// plot(6, color=newyork?#ed1e79:white, linewidth=5, offset=shift, title="New York")
// plot(5, color=chicago?#ed1e79:white, linewidth=5, offset=shift, title="Chicago")
// plot(4, color=tokyo?#79c942:white, linewidth=5, offset=shift, title="Tokyo")
// plot(3, color=hongkong?#79c942:white, linewidth=5, offset=shift, title="Hong Kong")
// plot(2, color=sydney?#ff931e:white, linewidth=5, offset=shift, title="Sydney")
// plot(1, color=wellington?#ff931e:white, linewidth=5, offset=shift, title="Wellington")
showUS = input(defval=true, type = bool, title="NY Session On")
tradeHoursUS = input("0800-1700", title="Trade hours US", type=session)
sessionUS = white
showAsia = input(defval=true, type = bool, title="Asia Session On")
tradeHoursAsia= input("1900-0400", title="Trade hours Asia", type=session)
sessionAsia = yellow
showAustralia = input(defval=true, type = bool, title="Sydney Session On")
tradeHoursAustralia = input("1700-0200", title="Trade hours Sydney", type=session)
sessionAustralia = red
showEU = input(defval=true, type = bool, title="Euro Session On")
tradeHoursEU = input("0300-1200", title="Trade hours Europe", type=session)
sessionEU = blue
USA_plot = plot(0, title="US Timezone", color = showUS and timeinrange(period, tradeHoursUS) ? sessionUS : na, transp=0, linewidth=4)
Asia_plot = plot(0.2, title="Asia Timezone", color = showAsia and timeinrange(period, tradeHoursAsia) ? sessionAsia : na, transp=0, linewidth=4)
Ausi_plot = plot(0.4, title="Australia Timezone", color = showAustralia and timeinrange(period, tradeHoursAustralia) ? sessionAustralia : na, transp=0, linewidth=4)
EUR_plot = plot(0.6, title="EU Timezone", color = showEU and timeinrange(period, tradeHoursEU) ? sessionEU : na, transp=0, linewidth=4)
// Constants for colours
timezoneBgColour = #FF0000
timezoneColour = #FFFFFF
// Colour the background of each session
// bgcolor(USA_plot >= EUR_plot, color=InSession(EUR_USA)[1] ? timezoneBgColour : na, transp=100)
// bgcolor(Ausi_plot >= Asia_plot, color=InSession(AUS_ASIA)[1] ? timezoneColour : na, transp=100)
// bgcolor(EUR_plot >= Asia_plot, color=InSession(EUR_ASIA)[1] ? timezoneColour : na, transp=100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment