Forex session - Opening Range- Jayy

jayy · study · 335 行 · 点赞 480 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=2
 
//  Opening Range (OR) for Forex 24 hour regular session.  This is not for regular market day sessions addressed in a separate script.
 
//   This script fixes four issues:
//  syntax error when code compiles 
//  messed up opening range the day after a holiday Monday
//  flaky plotting of the opening range and targets that required page reloading 
//  TradingView problems with starting forex session at 1700 hours EST/EDT when using certain securities eg FX_IDC currently (Jan 2017)
 
//  Additions in his code are more options for trading range
 
//  Time compensation option for  some securities that incorrectly start sessions at 1200 hrs instead of 1700 hrs NY time 
//  - this glitch is likely temporary but present when this script update was created
//  More opening range time period choices
 
//  Opening Range Targets:
//  Opening Range Targets as per Leaf_West
//  Targets are set at 127% , 162%, 200 %, 262 %, 362%, 423%, 685%, 1109% and 1794% and this can be traded intraday 
//  using methods described here charts-by-leaf.com  I also have some Leaf West PDFs that describe how the targets are set 
//  and how they are traded.  There are others that use opening range.
//  You can adjust targets as you like in the dialogue box.  
//  The 362%, 423%, 685%, 1109% and 1794% Targets will not be displayed unless selected in the dialogue box.
//  I have also included fib targets of yesterday's daily range
 
//  The Time Session Glitch and the Fix:
//  The script will correctly default to 1700 hrs to 1700hrs EDT/EST session for FXCM.  
//  Strangely some securities appear to erroneously start their session at 1200 hrs ie. My guess is that they are somehow 
//  tied to GMT+0 instead of New York time (GMT+5).  See this for yourself by selecting EURUSD using the FXCM exchange 
//  (FX:EURUSD) and then EURUSD from the IDC exchange (FX_IDC:EURUSD).  The FX-IDC session opening range starts 5 hours
//  before it actually should at 1700 hrs EDT/EST.  To correct for this  I have implemented an automatic fix (default) and a
//  user selected  "5 hour time shift adjust. ment needed on some securities".
//  There is also a 4 hour time shift button which might be necessary when New York reverts from Eastern Standard Time 
//  to Eastern Daylight Time (1 hour difference) in March (and then back again in November). In the default auto adjust mode you 
//  will need to select the 1 hour time shift.  That is if this glitch still exists at that time.   
//  I have looked at other scripts, other than my own and where the script is available,  that need to use information 
//  about the opening bar and all have the same time shift issue
 
 
//  What are the choices for Opening Range?
//  The dialogue box offers the standard TradingView options.
//  Also where you see Pick Opening Range 1 to 12 hours , SET TO 0 To USE LINE ABOVE TO DETERMINE OR LENGTH
//  As the note says a number other than 0 will override the standard options from the line above
//  The dialogue box below in offers choices by hours 1 to 12.  A number greater than 12 will still only give
//  720 minutes (12 hours) for the length of Opening Range.  
 
//  What sessions within the FOREX time-frame are available?
//  The default is 1700 hours to 1700 hours EST/EDT
//  Check any one (only one) of the time periods to change the opening range period to suit.
//  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)
 
//  There is a build your own session (click the button to select)  
//  The two lines for inputting session times are almost identical except that the second line starts the 
//   be the same as each other. 
//  The default for the build your own session is 2200 hours to 2200 hours. As of the time of publishing
//   this plots EURUSD FX-IDC just right. The GMT+5 and GMT+4 do not apply to this selection. 
//  See my comments above on this strange aberration.
 
//  The opening range targets within the upper and lower boundary of the OR
//  There is an option in the dialogue box to display fib targets within Opening Range itself
//  you may plot from top down or bottom up.  There is little difference in direction 
//  for the five values plotted (.236, .382, .5, .618, .786) however .236 and .786 are not quite symmetrical
//  you can switch them as the direction of price changes - if you so desire.
//  In the default situation only the .5 of target within the opening range is shown.
//  The script originated from work done by Chris Moody.  It has changed significantly but there are remnants
//  of that script lurking within.
 
//  Cheers Jayy
 
 
 
 
 
 
 
 
 
 
study(title="Forex session - Opening Range- Jayy", shorttitle="Forex 87 OR  Jayy", overlay=true)
 
sesstny= input(false, title= "Check for New York 8 am to 5 pm EST (EDT) session FXCM forex")
sesstAUD= input(false, title= "Check for Sydney 5 pm to 2 am EST (EDT) session FXCM forex only")
sesstTokyo= input(false, title= "Check for Tokyo 7 pm to 4 am EST (EDT)  session FXCM forex only")
sesstLondon= input(false, title= "Check for London 3 am to 12 noon EST (EDT) session FXCM forex only")
sesstmyown= input(false, title= "Check for plot your own session,  use boxes below?")
r= input("2200-2200", type=session, title= "Opening Range: Build your own session start and end")
plotr= input("2201-2200", type=session, title= "Plotting: add one minute to the Pick your own session start time shown above and select the identical session end time the same as above")
//sessregular= input(false, title= "Check for NYSE  0930  to 1600  EST (EDT) session US exchange")
GMT5= input(false,title=" 5 hour time shift adjustment needed on some securities? - ckeck one only" )
GMT4= input(false,title=" 4 hour time shift adjustment needed on some securities? - check one only first check has precedence" )
 
resol = input('15', type=resolution, title=" length/time Of Opening Range, NEXT LINE MUST BE SET TO 0 TO USE THIS DIALOGUE BOX")
ORhrs= input (0, title= "Pick Opening Range 1 to 12 hours , SET TO 0 To USE LINE ABOVE TO DETERMINE OR LENGTH")
FXIDC= syminfo.prefix== "FX_IDC"?true:false
 
autoadj=input(true, title="Automatically adjust session if using FX_IDC.  Time shift will be either 5 hours in northern winter (EST) and 4 hours in the northern summer (EDT)" )
ESTEDT=input(true, title="Check if EST time applies to New York, unclick if EDT applies in New York. Times change 1 hour in March and Novemeber. Applies only to FX_IDC"  )
 
Kipp= input(false, title= "Show the fib target of yesterday's daily range")
apt1 = input(127, minval=.01, maxval=2150, title="Target A - 127% of Opening Range")
apt2 = input(161.8, minval=.01, maxval=2600, title="Target 1 -161.8% of Opening Range")
apt3 = input(200, minval=.01, maxval=2600, title="Target 2- 200% of Opening Range")
apt4 = input(261.8, minval=.01, maxval=2600, title="Target 3 -261.8% of Opening Range")
apt5 = input(361.8, minval=.01, maxval=2600, title="Target 4 - 361.8% of Opening Range")
apt6 = input(423, minval=.01, maxval=2600, title="Target 5 - 423% of Opening Range")
apt7 = input(685, minval=.01, maxval=2600, title="Target 6 - 685% of Opening Range")
apt8 = input(1109, minval=.01, maxval=2600, title="Target 7 - 1109% of Opening Range")
apt9 = input(1794.4, minval=.01, maxval=2600, title="Target 8 - 1794% of Opening Range")
sho362= input(true, title="Show 361.8%?")
sho423= input(true, title="Show 423%?")
sho685= input(false, title="Show 685%?")
sho1109= input(false, title="Show 1109? and 1794%?")
pltup = input(false, title= "plot fibs within OR from bottom up?")
pltdwn = input(false, title= "plot fibs within OR from top down?")
highTimeFrame = input("D", type=resolution,title= "Leave as Daily time frame" )
 
pt1= apt1-100
pt2= apt2-100
pt3= apt3-100
pt4= apt4-100
pt5= apt5-100
pt6= apt6-100
pt7= apt7-100
pt8= apt8-100
pt9= apt9-100
res15= resol=='15'?true:false
res30= resol=='30'?true:false
 
 
// ny=GMT5?"1301-2200"  :GMT4?"1201-2100" :  "0801-1700"  
// a= GMT5?"2201-0900"  :GMT4?"2101-0800" :  "1701-0400"
// t= GMT5?"0001-1000"  :GMT4?"2301-0900" :  "1901-0500"
// l= GMT5?"0801-1900"  :GMT4?"0701-1900" :  "0301-1400"
// y= GMT5?"2201-2200"  :GMT4?"2101-2100" :  "1701-1700"
// //reg= GMT5?"1431-2100":GMT4?"1331-2000" :  "0931-1600"
// //plt=sesstny?ny:sesstAUD?a:sesstTokyo?t:sesstLondon?l:sessregular?reg: sesstmyown?plotr:y
// plt=sesstny?ny:sesstAUD?a:sesstTokyo?t:sesstLondon?l: sesstmyown?plotr:y
// nyi=GMT5?"1300-2200"  :GMT4?"1200-2100" :  "0800-1700"  
// ai= GMT5?"2200-0900"  :GMT4?"2100-0800" :  "1700-0400"
// ti= GMT5?"0000-1000"  :GMT4?"2300-0900" :  "1900-0500"
// li= GMT5?"0800-1900"  :GMT4?"0700-1900" :  "0300-1400"
// yi= GMT5?"2200-2200"  :GMT4?"2100-2100" :  "1700-1700"
//regi= GMT5?"1430-2100":GMT4?"1330-2000" :  "0930-1600"
 
 
 
 
 
ny= GMT5   or (autoadj and FXIDC and ESTEDT)?"1301-2200"  :GMT4  or (autoadj and FXIDC  and not ESTEDT)?"1201-2100" :  "0801-1700"  
a= GMT5  or (autoadj and FXIDC  and ESTEDT) ?"2201-0900"  :GMT4 or (autoadj  and FXIDC and not ESTEDT)?"2101-0800" :  "1701-0400"
t= GMT5  or (autoadj and FXIDC  and ESTEDT) ?"0001-1000"  :GMT4  or (autoadj and FXIDC  and not ESTEDT)  ?"2301-0900" :  "1901-0500"
l= GMT5  or (autoadj and FXIDC  and ESTEDT)  ?"0801-1900"  :GMT4  or (autoadj and FXIDC  and not ESTEDT)  ?"0701-1900" :  "0301-1400"
y= GMT5  or (autoadj and FXIDC  and ESTEDT) ?"2201-2200"  :GMT4  or (autoadj  and FXIDC and not ESTEDT)  ?"2101-2100" :  "1701-1700"
//reg= GMT5  or (autoadj and ESTEDT) ?"1431-2100":GMT4   or (autoadj and not ESTEDT) ?" 1331-2000" :  "0931-1600"
// plt=sesstny?ny:sesstAUD?a:sesstTokyo?t:sesstLondon?l:sessregular?reg: sesstmyown?plotr:y
plt=sesstny?ny:sesstAUD?a:sesstTokyo?t:sesstLondon?l: sesstmyown?plotr:y
nyi=GMT5 or (autoadj  and FXIDC and ESTEDT)?"1300-2200"  :GMT4   or (autoadj  and FXIDC and not ESTEDT) ?"1200-2100" :  "0800-1700"  
ai= GMT5  or (autoadj and FXIDC  and ESTEDT)  ?"2200-0900"  :GMT4  or (autoadj and FXIDC  and not ESTEDT)  ?"2100-0800" :  "1700-0400"
ti= GMT5   or (autoadj  and FXIDC and ESTEDT) ?"0000-1000"  :GMT4  or (autoadj and FXIDC  and not ESTEDT)  ?"2300-0900" :  "1900-0500"
li= GMT5  or (autoadj  and FXIDC and ESTEDT)  ?"0800-1900"  :GMT4 or (autoadj  and FXIDC and not ESTEDT)   ?"0700-1900" :  "0300-1400"
yi= GMT5 or (autoadj and FXIDC  and ESTEDT)?"2200-2200"  :GMT4  or (autoadj  and FXIDC and not ESTEDT) ?"2100-2100" :  "1700-1700"
// regi= GMT5    or (autoadj and ESTEDT)?"1430-2100":GMT4  or (autoadj and not ESTEDT)  ?"1330-2000" :  "0930-1600"
 
 
// sesstoday=sesstny?nyi:sesstAUD?ai:sesstTokyo?ti:sesstLondon?li :sessregular?regi: sesstmyown?r: yi
// sessSpec=sesstny?nyi:sesstAUD?ai:sesstTokyo?ti:sesstLondon?li :sessregular?regi: sesstmyown?r: yi
sesstoday=sesstny?nyi:sesstAUD?ai:sesstTokyo?ti:sesstLondon?li : sesstmyown?r: yi
sessSpec=sesstny?nyi:sesstAUD?ai:sesstTokyo?ti:sesstLondon?li : sesstmyown?r: yi
 
hiloreg = tickerid(syminfo.prefix, ticker, session.regular)
sessions(sesstodayw) => time('D', sesstodayw)
bartimeSess=sessions(sesstoday)
 
newbarSess = bartimeSess != bartimeSess[1]
high_range = valuewhen(newbarSess,high,0)
low_range = valuewhen(newbarSess,low,0)
adopt(r, s) => security(hiloreg, r, s)
 
 
// Opening Range and defining time period
 
 
highRes = Kipp? security(hiloreg, 'D', high[1]) : adopt(ORhrs==0?resol:ORhrs==1?'60':ORhrs==2?'120':ORhrs==3?'180': 
      ORhrs==4?'240': ORhrs==5?'300': ORhrs==6?'360':ORhrs==7?'420': ORhrs==8?'480':ORhrs==9?'540':ORhrs==10?'600': ORhrs==11?'660':
              ORhrs==12?'720': ORhrs>=12?'720':resol, high_range)
lowRes = Kipp? security(hiloreg, 'D', low[1]) :adopt(ORhrs==0?resol:ORhrs==1?'60':ORhrs==2?'120':ORhrs==3?'180': 
           ORhrs==4?'240': ORhrs==5?'300': ORhrs==6?'360':ORhrs==7?'420': ORhrs==8?'480':ORhrs==9?'540':ORhrs==10?'600': ORhrs==11?'660':
               ORhrs==12?'720': ORhrs>12?'720':resol, low_range)
// highRes = adopt(res240?'240':res330?'330':resol, high_range)
// lowRes = adopt(res240?'240':res330?'330':resol, low_range)
 
range = highRes - lowRes
 
 
 
//Highlighting Line Rules For Opening Range
highColor =time ("1", plt)?black:na //black
midColor =time ("1", plt)?black:na //black
lowColor =time ("1", plt)?black:na //black
 
//Plot Statements For Opening Range Lines
openRangeHigh = plot(isintraday and time ("1", sessSpec)? highRes : na, color=highColor, style=linebr, linewidth=2)
openRangeLow = plot(isintraday and time ("1", sessSpec)? lowRes : na, color=lowColor, style=linebr, linewidth=2)
 
fill(openRangeHigh, openRangeLow, color=yellow, transp=90, title="Opening Range Fill")
openRangeMid= lowRes + range/2
 
plot ( isintraday and time ("1", sessSpec)?openRangeMid:na, color=midColor, style=linebr, linewidth=1)//(isintraday? lowRes : na, color=lowColor, style=line, linewidth=2)
 
pltupColor =time ("1", plt)?aqua:na //black
pltdwnColor =time ("1", plt)?fuchsia:na //black
plot(pltup and isintraday and time ("1", sessSpec)?lowRes+ range*.236:na, color=pltupColor, linewidth=1)
plot(pltup and isintraday and time ("1", sessSpec)  ?lowRes+ range*.382:na, color=pltupColor, linewidth=1)
plot(pltup and isintraday and time ("1", sessSpec) ?lowRes+ range*.618:na, color=pltupColor, linewidth=1)
plot(pltup and isintraday and time ("1", sessSpec) ?lowRes+ range*.786:na, color=pltupColor, linewidth=1)
 
plot(pltdwn and isintraday and time ("1", sessSpec)?highRes- range*.236:na, color=pltdwnColor, linewidth=1)
plot(pltdwn and isintraday and time ("1", sessSpec)?highRes- range*.382:na, color=pltdwnColor, linewidth=1)
plot(pltdwn and isintraday and time ("1", sessSpec)?highRes- range*.618:na, color=pltdwnColor, linewidth=1)
plot(pltdwn and isintraday and time ("1", sessSpec)?highRes- range*.786:na, color=pltdwnColor, linewidth=1)
 
//Formula For 127%
pft1 = pt1*.01
highRes2 = highRes+range*pft1
lowRes2 = lowRes-range*pft1
//Highlighting Line rules for 127%
highColor2 =time ("1", plt)?red:na //aqua
lowColor2 =time ("1", plt)?green:na //lime
//Plots For 127%
p1=plot(isintraday and time ("1", sessSpec)? highRes2 : na, color=highColor2, style=linebr, linewidth=1)
p2=plot(isintraday and time ("1", sessSpec)? lowRes2 : na, color=lowColor2, style=linebr, linewidth=1)
 
//Formula For 162%
pft2 = pt2*.01
highRes3 = highRes+range*pft2
lowRes3 = lowRes-range*pft2
//Highlighting Line rules for 162%
highColor3 =time ("1", plt)?red:na //aqua
lowColor3 =time ("1", plt)?teal:na //lime
//Plots For 162%
plot(isintraday and time ("1", sessSpec)? highRes3 : na, color=highColor3, style=linebr, linewidth=1)
plot(isintraday and time ("1", sessSpec)? lowRes3 : na, color=lowColor3, style=linebr, linewidth=1)
 
//Formula For 200%
pft3 = pt3*.01
highRes4 = highRes+range*pft3
lowRes4 = lowRes-range*pft3
//Highlighting Line rules for 200%
highColor4 =time ("1", plt)?red:na
lowColor4=time ("1", plt)?teal:na
//Plots For 200%
plot(isintraday and time ("1", sessSpec)? highRes4 : na, color=highColor4, style=linebr, linewidth=1)
plot(isintraday and time ("1", sessSpec)? lowRes4 : na, color=lowColor4, style=linebr, linewidth=1)
 
 
//Formula For 262%
pft4 = pt4*.01
highRes5 = highRes+range*pft4
lowRes5 = lowRes-range*pft4
//Highlighting Line rules for 262%
highColor5 =time ("1", plt)?red:na
lowColor5 =time ("1", plt)?green :na
//Plots For 262%
plot(isintraday and time ("1", sessSpec)? highRes5 : na, color=highColor5, style=linebr, linewidth=1)
plot(isintraday and time ("1", sessSpec)? lowRes5 : na, color=lowColor5, style=linebr, linewidth=1)
 
//Formula For 362%
pft5 = pt5*.01
highRes6 = highRes+range*pft5
lowRes6 = lowRes-range*pft5
//Highlighting Line rules for 362%
highColor6 =time ("1", plt)?red:na //aqua
lowColor6=time ("1", plt)?green:na //lime
//Plots For 362%
plot(isintraday and sho362 and time ("1", sessSpec)? highRes6 : na, color=highColor6, style=linebr, linewidth=1)
plot(isintraday and sho362 and time ("1", sessSpec)? lowRes6 : na, color=lowColor6, style=linebr, linewidth=1)
 
 
//Formula For 423%
pft6 = pt6*.01
highRes7 = highRes+range*pft6
lowRes7 = lowRes-range*pft6
//Highlighting Line rules for 423%
highColor7 =time ("1", plt)?red:na //aqua
lowColor7=time ("1", plt)?teal:na //lime 
//Plots For 423%
plot(isintraday and sho423 and time ("1", sessSpec)? highRes7: na, color=highColor7, style=linebr, linewidth=1)
plot(isintraday and sho423 and time ("1", sessSpec)? lowRes7 : na, color=lowColor7, style=linebr, linewidth=1)
 
//Formula For 685%
pft7 = pt7*.01
highRes8 = highRes+range*pft7
lowRes8 = lowRes-range*pft7
//Highlighting Line rules for 685%
//a=time ("1", "931-1600")?aqua:na
highColor8 =time ("1", plt)?red:na //aqua
lowColor8 = time ("1", plt)? green:na //lime
//Plots For 685% 
plot(isintraday and sho685 and time ("1", sessSpec)? highRes8: na, color=highColor8, style=linebr, linewidth=1)
plot(isintraday and sho685 and time ("1", sessSpec)? lowRes8 : na, color=lowColor8, style=linebr, linewidth=1)
 
 
//Formula For 1109%
pft8 = pt8*.01
highRes9 = highRes+range*pft8
lowRes9 = lowRes-range*pft8
//Highlighting Line rules for1109%
//a=time ("1", "931-1600")?aqua:na
highColor9 =time ("1", plt)?red:na //aqua
lowColor9 = time ("1", plt)? green:na //lime
//Plots For1109%
plot(isintraday and sho1109 and time ("1", sessSpec)? highRes9: na, color=highColor9, style=linebr, linewidth=1)
plot(isintraday and sho1109 and time ("1", sessSpec)? lowRes9 : na, color=lowColor9, style=linebr, linewidth=1)
 
//Formula For Profit Target 1794%
pft9 = pt9*.01
highRes10 = highRes+range*pft8
lowRes10 = lowRes-range*pft8
//Highlighting Line rules for 1794%
//a=time ("1", "931-1600")?aqua:na
highColor10 =time ("1", plt)?red:na //aqua
lowColor10 = time ("1", plt)? green:na //lime
//Plots For 1794%
plot(isintraday and sho1109 and time ("1", sessSpec)? highRes10: na, color=highColor10, style=linebr, linewidth=1)
plot(isintraday and sho1109 and time ("1", sessSpec)? lowRes10 : na, color=lowColor10, style=linebr, linewidth=1)
 
//plot(sma(close,390),color= red, style=line, linewidth=2 )
//plot(sma(close,195), color= green, style=line, linewidth=2)
 
 
 
//// last line

← 返回列表