Skip to content

Instantly share code, notes, and snippets.

@oscarvs
Last active April 4, 2023 14:59
Show Gist options
  • Save oscarvs/d67ad428353e3de19331 to your computer and use it in GitHub Desktop.
Save oscarvs/d67ad428353e3de19331 to your computer and use it in GitHub Desktop.
Forex Kill Zones
// Created by https://www.tradingview.com/u/oscarvs @ 19 October 2014 | @theoscarvs
// Inspired by @ICT_MHuddleston concepts, code based on @ChrisMoody scripts
// https://github.com/oscarvs/forex-killzones
// Time sessions are directly sync from exchange/tv data (Change manually if you need to adjust DST Daylight saving time)
study(title="Forex Kill Zones v1 [oscarvs]",shorttitle="Forex Kill Zones v1", overlay=true)
timeinrange(res, sess) => time(res, sess) != 0
// **** Checkboxes and custom Session
iKZNY = input(true, title="New York Kill Zone")
KZNY = input('0700-0900', type=session, title="New York Kill Zone")
NYOpenAlertBar = input('0800-0815', type=session, title="New York Open Session")
NYSession = input('0900-1700', type=session, title="New York Session")
iOpenKZLondon = input(true, title="London Open Kill Zone")
LondonOpenKZ = input('0200-0400', type=session, title="London Open Kill Zone")
LondonOpenAlertBar = input('0300-0315', type=session, title="London Open Session")
LondonSession = input('0300-1200', type=session, title="London Session")
iOpenKZSydney = input(true, title="Sydney Open Kill Zone")
SydneyOpenKZ = input('1600-1800', type=session, title="Sydney Open Kill Zone")
SydneyOpenAlertBar = input('1700-1715', type=session, title="Sydney Open Session")
SydneySession = input('1700-0200', type=session, title="Sydney Session")
iTokyoKZ = input(true, title="Tokyo Kill Zone")
TokyoKZ = input('1800-2200', type=session, title="Tokyo Kill Zone")
TokyoOpenAlertBar = input('1900-1915', type=session, title="Tokyo Open Session")
TokyoSession = input('1900-0400', type=session, title="Tokyo Session")
iRealAllSessions = input(false, title="Show entire session")
iRealOpenSessionAlertBar = input(true, title="Real Open Session Alert Bar")
// **** Logic
sessToUse = iKZNY == 1 ? KZNY : '0000-0000'
sessToUse2 = iRealOpenSessionAlertBar == 1 ? NYOpenAlertBar : '0000-0000'
sessToUse22 = iRealAllSessions == 1 ? NYSession : '0000-0000'
sessToUse3 = iOpenKZLondon == 1 ? LondonOpenKZ : '0000-0000'
sessToUse4 = iRealOpenSessionAlertBar == 1 ? LondonOpenAlertBar : '0000-0000'
sessToUse44 = iRealAllSessions == 1 ? LondonSession : '0000-0000'
sessToUse5 = iOpenKZSydney == 1 ? SydneyOpenKZ : '0000-0000'
sessToUse6 = iRealOpenSessionAlertBar == 1 ? SydneyOpenAlertBar : '0000-0000'
sessToUse66 = iRealAllSessions == 1 ? SydneySession : '0000-0000'
sessToUse7 = iTokyoKZ == 1 ? TokyoKZ : '0000-0000'
sessToUse8 = iRealOpenSessionAlertBar == 1 ? TokyoOpenAlertBar : '0000-0000'
sessToUse88 = iRealAllSessions == 1 ? TokyoSession : '0000-0000'
// *** Apply custom rules
bgPlot = (iKZNY == 0 ? time(period) : time(period, sessToUse))
bgPlot2 = (iRealOpenSessionAlertBar == 0 ? time(period) : time(period, sessToUse2))
bgPlot22 = (iRealAllSessions == 0 ? time(period) : time(period, sessToUse22))
bgPlot3 = (iOpenKZLondon == 0 ? time(period) : time(period, sessToUse3))
bgPlot4 = (iRealOpenSessionAlertBar == 0 ? time(period) : time(period, sessToUse4))
bgPlot44 = (iRealOpenSessionAlertBar == 0 ? time(period) : time(period, sessToUse44))
bgPlot5 = (iOpenKZSydney == 0 ? time(period) : time(period, sessToUse5))
bgPlot6 = (iRealOpenSessionAlertBar == 0 ? time(period) : time(period, sessToUse6))
bgPlot66 = (iRealOpenSessionAlertBar == 0 ? time(period) : time(period, sessToUse66))
bgPlot7 = (iTokyoKZ == 0 ? time(period) : time(period, sessToUse7))
bgPlot8 = (iRealOpenSessionAlertBar == 0 ? time(period) : time(period, sessToUse8))
bgPlot88 = (iRealOpenSessionAlertBar == 0 ? time(period) : time(period, sessToUse88))
//**** Plot as background
bgcolor(iKZNY and bgPlot > 0 ? red : na, transp=90)
bgcolor(iKZNY and iRealOpenSessionAlertBar and bgPlot2 > 0 ? red : na, transp=70)
bgcolor(iKZNY and iRealAllSessions and bgPlot22 > 0 ? red : na, transp=95)
bgcolor(iOpenKZLondon and bgPlot3 > 0 ? green : na, transp=90)
bgcolor(iOpenKZLondon and iRealOpenSessionAlertBar and bgPlot4 > 0 ? green : na, transp=70)
bgcolor(iOpenKZLondon and iRealAllSessions and bgPlot44 > 0 ? green : na, transp=95)
bgcolor(iOpenKZSydney and bgPlot5 > 0 ? olive : na, transp=90)
bgcolor(iOpenKZSydney and iRealOpenSessionAlertBar and bgPlot6 > 0 ? navy : na, transp=70)
bgcolor(iOpenKZSydney and iRealAllSessions and bgPlot66 > 0 ? navy : na, transp=95)
bgcolor(iTokyoKZ and bgPlot7 > 0 ? orange : na, transp=90)
bgcolor(iTokyoKZ and iRealOpenSessionAlertBar and bgPlot8 > 0 ? orange : na, transp=70)
bgcolor(iTokyoKZ and iRealAllSessions and bgPlot88 > 0 ? orange : na, transp=95)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment