本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Session Ranges EST + TDO + Custom Reversible Fib",
overlay=true,
behind_chart=true,
max_boxes_count=500,
max_lines_count=100,
max_labels_count=500)
// ==========================================
// GENERAL SETTINGS
// ==========================================
groupGeneral = "General Settings"
sessionTimezone = input.string(
"America/New_York", "Session Time Zone",
options=["America/New_York", "GMT-5", "GMT-4", "Etc/UTC"],
group=groupGeneral)
showHistorical = input.bool(
true, "Keep Historical Session Boxes",
group=groupGeneral)
maxHistory = input.int(
100, "Maximum Historical Sessions",
minval=1, maxval=120,
group=groupGeneral)
// ==========================================
// SESSION LABEL SETTINGS
// ==========================================
groupLabels = "Session Labels"
showSessionLabels = input.bool(
true, "Show Session Names",
group=groupLabels)
sessionLabelSize = input.string(
"Small", "Label Text Size",
options=["Tiny", "Small", "Normal", "Large"],
group=groupLabels)
labelOffsetTicks = input.int(
4, "Label Distance Above Box (Ticks)",
minval=0, maxval=100,
group=groupLabels)
// ==========================================
// ASIA SESSION
// ==========================================
groupAsia = "Asia Session"
showAsia = input.bool(true, "Show Asia Session", group=groupAsia)
asiaSession = input.session("2000-0000", "Session Time", group=groupAsia)
asiaColor = input.color(color.lime, "Box Color", group=groupAsia)
asiaTransparency = input.int(
85, "Transparency", minval=0, maxval=100, group=groupAsia)
// ==========================================
// LONDON SESSION
// ==========================================
groupLondon = "London Session"
showLondon = input.bool(true, "Show London Session", group=groupLondon)
londonSession = input.session("0200-0500", "Session Time", group=groupLondon)
londonColor = input.color(
color.rgb(64, 224, 208), "Box Color", group=groupLondon)
londonTransparency = input.int(
85, "Transparency", minval=0, maxval=100, group=groupLondon)
// ==========================================
// NEW YORK AM SESSION
// ==========================================
groupNYAM = "New York AM Session"
showNYAM = input.bool(true, "Show NY AM Session", group=groupNYAM)
nyAMSession = input.session("0830-1200", "Session Time", group=groupNYAM)
nyAMColor = input.color(color.orange, "Box Color", group=groupNYAM)
nyAMTransparency = input.int(
85, "Transparency", minval=0, maxval=100, group=groupNYAM)
// ==========================================
// NEW YORK PM SESSION
// ==========================================
groupNYPM = "New York PM Session"
showNYPM = input.bool(true, "Show NY PM Session", group=groupNYPM)
nyPMSession = input.session("1300-1600", "Session Time", group=groupNYPM)
nyPMColor = input.color(color.orange, "Box Color", group=groupNYPM)
nyPMTransparency = input.int(
85, "Transparency", minval=0, maxval=100, group=groupNYPM)
// ==========================================
// TRUE DAY OPEN SETTINGS
// ==========================================
groupTDO = "True Day Open (TDO)"
showTDO = input.bool(true, "Show TDO", group=groupTDO)
tdoWidth = input.int(
1, "Line Width", minval=1, maxval=5, group=groupTDO)
tdoColor = input.color(color.white, "Line Color", group=groupTDO)
tdoTextColor = input.color(color.white, "Text Color", group=groupTDO)
tdoTextSize = input.string(
"Tiny", "Label Text Size",
options=["Tiny", "Small", "Normal", "Large"],
group=groupTDO)
// ==========================================
// PRE-MARKET FIB GENERAL SETTINGS
// ==========================================
groupFib = "Pre-Market Fib - General"
showFib = input.bool(
true, "Show Pre-Market Fib", group=groupFib)
reverseFib = input.bool(
false, "Reverse Fibonacci Direction", group=groupFib)
showFibLabels = input.bool(
true, "Show Fib Level Labels", group=groupFib)
// ==========================================
// CUSTOM FIB LEVEL SETTINGS
// ==========================================
// LEVEL 1: 0
g1 = "Fib Level 1"
f1Show = input.bool(true, "Show", group=g1)
f1Value = input.float(0.0, "Level", step=0.001, group=g1)
f1Color = input.color(color.white, "Color", group=g1)
f1Style = input.string(
"Dotted", "Line Style",
options=["Dotted", "Dashed", "Solid"], group=g1)
f1Width = input.int(1, "Line Width", minval=1, maxval=5, group=g1)
// LEVEL 2: 0.382
g2 = "Fib Level 2"
f2Show = input.bool(true, "Show", group=g2)
f2Value = input.float(0.382, "Level", step=0.001, group=g2)
f2Color = input.color(color.rgb(64, 224, 208), "Color", group=g2)
f2Style = input.string(
"Dotted", "Line Style",
options=["Dotted", "Dashed", "Solid"], group=g2)
f2Width = input.int(1, "Line Width", minval=1, maxval=5, group=g2)
// LEVEL 3: 0.5
g3 = "Fib Level 3"
f3Show = input.bool(true, "Show", group=g3)
f3Value = input.float(0.5, "Level", step=0.001, group=g3)
f3Color = input.color(color.blue, "Color", group=g3)
f3Style = input.string(
"Dotted", "Line Style",
options=["Dotted", "Dashed", "Solid"], group=g3)
f3Width = input.int(1, "Line Width", minval=1, maxval=5, group=g3)
// LEVEL 4: 0.786
g4 = "Fib Level 4"
f4Show = input.bool(true, "Show", group=g4)
f4Value = input.float(0.786, "Level", step=0.001, group=g4)
f4Color = input.color(color.orange, "Color", group=g4)
f4Style = input.string(
"Dotted", "Line Style",
options=["Dotted", "Dashed", "Solid"], group=g4)
f4Width = input.int(1, "Line Width", minval=1, maxval=5, group=g4)
// LEVEL 5: 1
g5 = "Fib Level 5"
f5Show = input.bool(true, "Show", group=g5)
f5Value = input.float(1.0, "Level", step=0.001, group=g5)
f5Color = input.color(color.white, "Color", group=g5)
f5Style = input.string(
"Dotted", "Line Style",
options=["Dotted", "Dashed", "Solid"], group=g5)
f5Width = input.int(1, "Line Width", minval=1, maxval=5, group=g5)
// LEVEL 6: -0.382
g6 = "Fib Level 6"
f6Show = input.bool(true, "Show", group=g6)
f6Value = input.float(-0.382, "Level", step=0.001, group=g6)
f6Color = input.color(color.rgb(64, 224, 208), "Color", group=g6)
f6Style = input.string(
"Dotted", "Line Style",
options=["Dotted", "Dashed", "Solid"], group=g6)
f6Width = input.int(1, "Line Width", minval=1, maxval=5, group=g6)
// LEVEL 7: -0.786
g7 = "Fib Level 7"
f7Show = input.bool(true, "Show", group=g7)
f7Value = input.float(-0.786, "Level", step=0.001, group=g7)
f7Color = input.color(color.orange, "Color", group=g7)
f7Style = input.string(
"Dotted", "Line Style",
options=["Dotted", "Dashed", "Solid"], group=g7)
f7Width = input.int(1, "Line Width", minval=1, maxval=5, group=g7)
// LEVEL 8: -1
g8 = "Fib Level 8"
f8Show = input.bool(true, "Show", group=g8)
f8Value = input.float(-1.0, "Level", step=0.001, group=g8)
f8Color = input.color(color.white, "Color", group=g8)
f8Style = input.string(
"Dotted", "Line Style",
options=["Dotted", "Dashed", "Solid"], group=g8)
f8Width = input.int(1, "Line Width", minval=1, maxval=5, group=g8)
// ==========================================
// HELPER FUNCTIONS
// ==========================================
getTextSize(string s) =>
switch s
"Tiny" => size.tiny
"Small" => size.small
"Normal" => size.normal
=> size.large
getFibStyle(string s) =>
switch s
"Solid" => line.style_solid
"Dashed" => line.style_dashed
=> line.style_dotted
// ==========================================
// SESSION DETECTION
// ==========================================
asiaActive = not na(time(
timeframe.period, asiaSession, sessionTimezone))
londonActive = not na(time(
timeframe.period, londonSession, sessionTimezone))
nyAMActive = not na(time(
timeframe.period, nyAMSession, sessionTimezone))
nyPMActive = not na(time(
timeframe.period, nyPMSession, sessionTimezone))
asiaStart = asiaActive and not asiaActive[1]
londonEnd = not londonActive and londonActive[1]
// ==========================================
// HISTORICAL SESSION STORAGE
// ==========================================
var array<box> historyBoxes = array.new<box>()
var array<label> historyLabels = array.new<label>()
saveSession(box b, label l) =>
if not na(b)
if not showHistorical
box.delete(b)
if not na(l)
label.delete(l)
else
array.push(historyBoxes, b)
array.push(historyLabels, l)
while array.size(historyBoxes) > maxHistory
box oldBox = array.shift(historyBoxes)
label oldLabel = array.shift(historyLabels)
box.delete(oldBox)
if not na(oldLabel)
label.delete(oldLabel)
true
// ==========================================
// SESSION BOX FUNCTION
// ==========================================
updateSession(
bool active,
bool wasActive,
box currentBox,
label currentLabel,
float sessionHigh,
float sessionLow,
string sessionName,
color sessionColor,
int transparency) =>
box b = currentBox
label l = currentLabel
float hi = sessionHigh
float lo = sessionLow
if active and not wasActive
hi := high
lo := low
b := box.new(
left=bar_index,
top=hi,
right=bar_index + 1,
bottom=lo,
border_color=na,
border_width=0,
bgcolor=color.new(sessionColor, transparency))
if showSessionLabels
l := label.new(
x=bar_index,
y=hi + labelOffsetTicks * syminfo.mintick,
text=sessionName,
style=label.style_none,
textcolor=sessionColor,
size=getTextSize(sessionLabelSize))
else
l := na
else if active
hi := math.max(hi, high)
lo := math.min(lo, low)
if not na(b)
box.set_right(b, bar_index + 1)
box.set_top(b, hi)
box.set_bottom(b, lo)
if not na(l)
int centerX = int(math.floor(
(box.get_left(b) + box.get_right(b)) / 2.0))
label.set_xy(
l, centerX,
hi + labelOffsetTicks * syminfo.mintick)
if not active and wasActive
saveSession(b, l)
b := na
l := na
hi := na
lo := na
[b, l, hi, lo]
// ==========================================
// CURRENT SESSION STORAGE
// ==========================================
var box asiaBox = na
var label asiaLabel = na
var float asiaHigh = na
var float asiaLow = na
var box londonBox = na
var label londonLabel = na
var float londonHigh = na
var float londonLow = na
var box nyAMBox = na
var label nyAMLabel = na
var float nyAMHigh = na
var float nyAMLow = na
var box nyPMBox = na
var label nyPMLabel = na
var float nyPMHigh = na
var float nyPMLow = na
// ==========================================
// DRAW SESSION BOXES
// ==========================================
if showAsia
[b, l, hi, lo] = updateSession(
asiaActive, asiaActive[1],
asiaBox, asiaLabel, asiaHigh, asiaLow,
"ASIA", asiaColor, asiaTransparency)
asiaBox := b
asiaLabel := l
asiaHigh := hi
asiaLow := lo
if showLondon
[b, l, hi, lo] = updateSession(
londonActive, londonActive[1],
londonBox, londonLabel, londonHigh, londonLow,
"LONDON", londonColor, londonTransparency)
londonBox := b
londonLabel := l
londonHigh := hi
londonLow := lo
if showNYAM
[b, l, hi, lo] = updateSession(
nyAMActive, nyAMActive[1],
nyAMBox, nyAMLabel, nyAMHigh, nyAMLow,
"NY AM", nyAMColor, nyAMTransparency)
nyAMBox := b
nyAMLabel := l
nyAMHigh := hi
nyAMLow := lo
if showNYPM
[b, l, hi, lo] = updateSession(
nyPMActive, nyPMActive[1],
nyPMBox, nyPMLabel, nyPMHigh, nyPMLow,
"NY PM", nyPMColor, nyPMTransparency)
nyPMBox := b
nyPMLabel := l
nyPMHigh := hi
nyPMLow := lo
// ==========================================
// DATE AND TIME HELPERS
// ==========================================
nyYear = year(time, sessionTimezone)
nyMonth = month(time, sessionTimezone)
nyDay = dayofmonth(time, sessionTimezone)
midnightTime = timestamp(
sessionTimezone, nyYear, nyMonth, nyDay, 0, 0)
fourPMTime = timestamp(
sessionTimezone, nyYear, nyMonth, nyDay, 16, 0)
// ==========================================
// TRUE DAY OPEN
// ==========================================
var line tdoLine = na
var label tdoLabel = na
if showTDO and time == midnightTime
if not na(tdoLine)
line.delete(tdoLine)
if not na(tdoLabel)
label.delete(tdoLabel)
tdoLine := line.new(
x1=midnightTime,
y1=open,
x2=fourPMTime,
y2=open,
xloc=xloc.bar_time,
extend=extend.none,
color=tdoColor,
style=line.style_dotted,
width=tdoWidth)
tdoLabel := label.new(
x=fourPMTime,
y=open,
text="TDO",
xloc=xloc.bar_time,
style=label.style_label_left,
color=color.new(color.black, 100),
textcolor=tdoTextColor,
size=getTextSize(tdoTextSize))
// ==========================================
// PRE-MARKET RANGE TRACKING
// ==========================================
var float preHigh = na
var float preLow = na
var int preHighTime = na
var int preLowTime = na
var bool preHasAsia = false
var bool preHasLondon = false
// ==========================================
// FIB DRAWING STORAGE
// ==========================================
var array<line> fibLines = array.new<line>()
var array<label> fibLabels = array.new<label>()
clearFib() =>
while array.size(fibLines) > 0
line oldLine = array.pop(fibLines)
line.delete(oldLine)
while array.size(fibLabels) > 0
label oldLabel = array.pop(fibLabels)
label.delete(oldLabel)
true
// ==========================================
// DRAW INDIVIDUAL FIB LEVEL
// ==========================================
drawFibLevel(
float price,
string levelText,
color levelColor,
string levelStyle,
int levelWidth,
int startTime,
int endTime) =>
line ln = line.new(
x1=startTime,
y1=price,
x2=endTime,
y2=price,
xloc=xloc.bar_time,
extend=extend.none,
color=levelColor,
style=getFibStyle(levelStyle),
width=levelWidth)
array.push(fibLines, ln)
if showFibLabels
label lb = label.new(
x=endTime,
y=price,
text=levelText,
xloc=xloc.bar_time,
style=label.style_label_left,
color=color.new(color.black, 100),
textcolor=levelColor,
size=size.small)
array.push(fibLabels, lb)
true
// ==========================================
// START NEW PRE-MARKET SESSION
// ==========================================
if asiaStart
clearFib()
preHigh := high
preLow := low
preHighTime := time
preLowTime := time
preHasAsia := true
preHasLondon := false
// ==========================================
// TRACK ASIA
// ==========================================
if asiaActive and preHasAsia
if high > preHigh
preHigh := high
preHighTime := time
if low < preLow
preLow := low
preLowTime := time
// ==========================================
// TRACK LONDON
// ==========================================
if londonActive and preHasAsia
preHasLondon := true
if high > preHigh
preHigh := high
preHighTime := time
if low < preLow
preLow := low
preLowTime := time
// ==========================================
// FINALIZE FIB AFTER LONDON CLOSE
// ==========================================
if londonEnd and preHasAsia and preHasLondon
clearFib()
if showFib and not na(preHigh) and not na(preLow)
// Original direction from the order of extremes.
bool isUptrend = preLowTime <= preHighTime
// NEW: Reverse the Fib anchors if enabled.
bool fibUptrend = reverseFib ? not isUptrend : isUptrend
// Default:
// Uptrend: 0 = High, 1 = Low
// Downtrend: 0 = Low, 1 = High
//
// Reverse ON swaps these anchors.
float fibZero = fibUptrend ? preHigh : preLow
float fibOne = fibUptrend ? preLow : preHigh
float fibRange = fibOne - fibZero
// Lines begin at 5 AM and end at 4 PM.
int fibStart = time
int fibEnd = timestamp(
sessionTimezone,
year(time, sessionTimezone),
month(time, sessionTimezone),
dayofmonth(time, sessionTimezone),
16, 0)
if f1Show
drawFibLevel(
fibZero + fibRange * f1Value,
str.tostring(f1Value),
f1Color, f1Style, f1Width,
fibStart, fibEnd)
if f2Show
drawFibLevel(
fibZero + fibRange * f2Value,
str.tostring(f2Value),
f2Color, f2Style, f2Width,
fibStart, fibEnd)
if f3Show
drawFibLevel(
fibZero + fibRange * f3Value,
str.tostring(f3Value),
f3Color, f3Style, f3Width,
fibStart, fibEnd)
if f4Show
drawFibLevel(
fibZero + fibRange * f4Value,
str.tostring(f4Value),
f4Color, f4Style, f4Width,
fibStart, fibEnd)
if f5Show
drawFibLevel(
fibZero + fibRange * f5Value,
str.tostring(f5Value),
f5Color, f5Style, f5Width,
fibStart, fibEnd)
if f6Show
drawFibLevel(
fibZero + fibRange * f6Value,
str.tostring(f6Value),
f6Color, f6Style, f6Width,
fibStart, fibEnd)
if f7Show
drawFibLevel(
fibZero + fibRange * f7Value,
str.tostring(f7Value),
f7Color, f7Style, f7Width,
fibStart, fibEnd)
if f8Show
drawFibLevel(
fibZero + fibRange * f8Value,
str.tostring(f8Value),
f8Color, f8Style, f8Width,
fibStart, fibEnd)
// ==========================================
// END OF INDICATOR
// ==========================================