本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("smile Multi frame", overlay=true, max_boxes_count=500)
// ============================================================================
// 可见性辅助:按当前图表周期决定每组框是否绘制
// 例:"1" = 仅1分钟;"3,5" = 仅3/5分钟;"ALL" = 所有周期
// ============================================================================
frameVisible(string spec) =>
string clean = str.replace_all(str.upper(spec), " ", "")
string current = str.upper(timeframe.period)
clean == "ALL" or str.contains("," + clean + ",", "," + current + ",")
// ============================================================================
// 1) 周线框 —— 原 v5 逻辑保留,仅做 v6 兼容与命名隔离
// ============================================================================
W_HLR = "High/Low Range"
W_TR = "True Range"
W_HAR = "Heikin Ashi Range"
W_DEFAULT_RESOLUTION = "1D"
W_DEFAULT_COLOR_GROWTH = color.rgb(0, 150, 136, 80)
W_DEFAULT_COLOR_FALL = color.rgb(244, 67, 54, 80)
W_DEFAULT_CALCULATION_METHOD = W_HLR
var int w_prevBarIndex = bar_index
w_drawBox(left, right, top, bottom, diff, growthColorInput, fallColorInput, borderGrowth, borderFall) =>
box drawnBox = box.new(left, top, right, bottom)
if diff < 0
box.set_border_color(drawnBox, borderFall)
box.set_bgcolor(drawnBox, fallColorInput)
else
box.set_border_color(drawnBox, borderGrowth)
box.set_bgcolor(drawnBox, growthColorInput)
drawnBox
w_visibleTFs = input.string("ALL", "显示图表周期", tooltip="填写当前图表周期,多个用英文逗号分隔。例如:1,3,5,15,60,1D;填写 ALL 表示所有周期显示。", group="周线框")
bool w_visible = frameVisible(w_visibleTFs)
w_resolutionInput = input.string("1W", "Box Resolution", options=["60","120","240","360","720","1D","1W","1M"], group="周线框")
w_timeframe = w_resolutionInput
w_calculationMethodInput = input.string(W_DEFAULT_CALCULATION_METHOD, "Calculation", options=[W_HLR, W_TR, W_HAR], group="周线框")
w_groupName = "周线框 - Border and fill colors"
w_growthBorderColorInput = input.color(color.new(W_DEFAULT_COLOR_GROWTH, 0), title="Growth Body", inline="w_growth", group=w_groupName)
w_growthColorInput = input.color(W_DEFAULT_COLOR_GROWTH, title="", inline="w_growth", group=w_groupName)
w_fallBorderColorInput = input.color(color.new(W_DEFAULT_COLOR_FALL, 0), title="Fall Body", inline="w_fall", group=w_groupName)
w_fallColorInput = input.color(W_DEFAULT_COLOR_FALL, title="", inline="w_fall", group=w_groupName)
type W_OHLC
float o
float h
float l
float c
float prevC
w_makeOHLC(int bb = 0) =>
W_OHLC.new(open[bb], high[bb], low[bb], close[bb], close[bb+1])
[w_bar0, w_bar1] = request.security(syminfo.tickerid, w_timeframe, [w_makeOHLC(0), w_makeOHLC(1)], lookahead=barmerge.lookahead_on)
bool w_isNewPeriod = ta.change(time(w_timeframe)) != 0
bool w_drawCurrent = barstate.islast and not w_isNewPeriod
W_OHLC w_sec = w_drawCurrent ? w_bar0 : w_bar1
w_sec := na(w_sec) ? W_OHLC.new() : w_sec
float w_diff = w_sec.c - w_sec.o
int w_left = w_prevBarIndex
int w_right = w_drawCurrent ? bar_index : bar_index - 1
[w_ashiHigh, w_ashiLow] = request.security(ticker.heikinashi(syminfo.tickerid), w_timeframe, [high, low])
float w_top = na
float w_bottom = na
if w_calculationMethodInput == W_HLR
w_top := w_sec.h
w_bottom := w_sec.l
else if w_calculationMethodInput == W_TR
w_top := math.max(w_sec.h, w_sec.prevC)
w_bottom := math.min(w_sec.l, w_sec.prevC)
else if w_calculationMethodInput == W_HAR
w_top := w_ashiHigh
w_bottom := w_ashiLow
if w_isNewPeriod
w_prevBarIndex := bar_index
if w_visible
w_drawBox(w_left, w_right, w_top, w_bottom, w_diff, w_growthColorInput, w_fallColorInput, w_growthBorderColorInput, w_fallBorderColorInput)
var box w_prevBox = na
if barstate.islast
box.delete(w_prevBox)
w_prevBox := na
if w_visible and not w_isNewPeriod
w_prevBox := w_drawBox(w_left, w_right, w_top, w_bottom, w_diff, w_growthColorInput, w_fallColorInput, w_growthBorderColorInput, w_fallBorderColorInput)
// ============================================================================
// 2) 日线框(原 Multi-Time Period Charts / Pine v6)—— 原逻辑保留并命名隔离
// ============================================================================
string D_TT_AT = "If selected, the indicator automatically chooses the timeframe of the displayed bars. The chosen higher" +
" timeframe is:\n\n" +
" • '2 hours' if the chart timeframe is a seconds-based timeframe.\n" +
" • '1 day' if the chart timeframe is lower than one day and not a seconds-based timeframe.\n" +
" • '1 week' if the chart timeframe is higher than one day and lower than one week.\n" +
" • '1 month' if the chart timeframe is higher than one week and lower than one month.\n" +
" • '3 months' if the chart timeframe is higher than one month and lower than three months.\n" +
" • '12 months' if the chart timeframe is higher than three months." +
" \n\nIf not selected, it uses the timeframe specified in the 'Timeframe' input."
string D_TT_UC = "When this option is unchecked, MTPC will use intraday data while calculating on intraday charts." +
" If Extended Hours are displayed on the chart, they will be taken into account during the calculation." +
" If intraday OHLC values are different from daily-based values (normal for stocks), the MTPC will also differ."
color D_UP_COLOR = #009688
color D_DN_COLOR = #F44336
color D_UPBD_COLOR = color.new(D_UP_COLOR, 70)
color D_DNBD_COLOR = color.new(D_DN_COLOR, 70)
enum D_CalcType
hl = "High/Low Range"
oc = "Open/Close Range"
ohlc = "OHLC"
tr = "True Range"
string d_visibleTFs = input.string("ALL", "显示图表周期", tooltip="填写当前图表周期,多个用英文逗号分隔。例如:1,3,5,15,60,1D;填写 ALL 表示所有周期显示。", group="日线框")
bool d_visible = frameVisible(d_visibleTFs)
bool d_autoTFInput = input.bool(true, "Auto-timeframe", tooltip=D_TT_AT, group="日线框")
string d_tfInput = input.timeframe("1M", "Timeframe", active=not d_autoTFInput, group="日线框")
D_CalcType d_calcTypeInput = input.enum(D_CalcType.hl, "Calculation", group="日线框")
bool d_useHaInput = input.bool(false, "Display Heikin Ashi values", group="日线框")
bool d_useDailyInput = input.bool(false, "Use daily-based values", tooltip=D_TT_UC, group="日线框")
string D_GRP01 = "日线框 - Border and fill colors"
color d_upBorderColorInput = input.color(D_UP_COLOR, "Up bars ", inline="d10", group=D_GRP01)
color d_upBodyColorInput = input.color(D_UPBD_COLOR, "", inline="d10", group=D_GRP01)
color d_dnBorderColorInput = input.color(D_DN_COLOR, "Down bars", inline="d11", group=D_GRP01)
color d_dnBodyColorInput = input.color(D_DNBD_COLOR, "", inline="d11", group=D_GRP01)
type D_OHLC
float o
float h
float l
float c
float prevC
method d_setBox(box bx, bool cond, int left, int right, float top, float bottom, float diff, color upBodyColor, color dnBodyColor, color upBorderColor, color dnBorderColor) =>
color bodyColor = diff < 0 ? dnBodyColor : upBodyColor
color borderColor = diff < 0 ? dnBorderColor : upBorderColor
switch
cond =>
bx.set_bgcolor(na)
bx.set_border_color(na)
=>
bx.set_left(left)
bx.set_right(right)
bx.set_top(top)
bx.set_bottom(bottom)
bx.set_bgcolor(bodyColor)
bx.set_border_color(borderColor)
d_selectAutoTimeframe() =>
int secondsInTF = timeframe.in_seconds()
string result = switch
timeframe.isseconds => "120"
secondsInTF < 86400 => "1440"
secondsInTF < 604800 => "1W"
secondsInTF < 2628003 => "1M"
secondsInTF < 7884009 => "3M"
=> "12M"
result
d_selectTimeframeFromInput(string tf) =>
syminfo.session == session.extended and tf == "1D" ? "1440" : tf
d_makeOHLC(int offset = 0) =>
D_OHLC.new(open[offset], high[offset], low[offset], close[offset], close[offset + 1])
d_haFrom(float o, float h, float l, float c, float prevHO, float prevHC) =>
float haC = (o + h + l + c) / 4
float haO = na(prevHO) or na(prevHC) ? (o + c) / 2 : (prevHO + prevHC) / 2
float haH = math.max(h, haO, haC)
float haL = math.min(l, haO, haC)
[haO, haH, haL, haC]
d_chartOHLC(string tf, bool useHA) =>
var float htfOpen = na
var float htfHigh = na
var float htfLow = na
var float prevClose = na
var float haClose = na
var float haOpen = na
var float prevHaC = na
var float prevHaO = na
if timeframe.change(tf)
htfOpen := open
htfHigh := high
htfLow := low
prevClose := close[1]
prevHaO := haOpen
prevHaC := haClose
htfHigh := math.max(high, htfHigh)
htfLow := math.min(low, htfLow)
switch
useHA =>
[haO, haH, haL, haC] = d_haFrom(htfOpen, htfHigh, htfLow, close, prevHaO, prevHaC)
haOpen := haO
haClose := haC
D_OHLC.new(haO, haH, haL, haC, prevHaC)
=> D_OHLC.new(htfOpen, htfHigh, htfLow, close, prevClose)
var int d_prevBarIndex = bar_index
var string d_timeframe = d_autoTFInput ? d_selectAutoTimeframe() : d_selectTimeframeFromInput(d_tfInput)
var string d_ticker = d_useHaInput ? ticker.heikinashi(syminfo.tickerid) : syminfo.tickerid
[d_bar0, d_bar1] = switch
not d_useDailyInput => [d_chartOHLC(d_timeframe, d_useHaInput), d_chartOHLC(d_timeframe, d_useHaInput)[1]]
=> request.security(d_ticker, d_timeframe, [d_makeOHLC(0), d_makeOHLC(1)], lookahead=barmerge.lookahead_on)
bool d_isNewPeriod = timeframe.change(d_timeframe)
bool d_drawCurrent = barstate.islast and not d_isNewPeriod
D_OHLC d_bar = d_drawCurrent ? d_bar0 : d_bar1
d_bar := na(d_bar) ? D_OHLC.new() : d_bar
float d_diff = d_bar.c - d_bar.o
int d_left = d_prevBarIndex
int d_right = d_drawCurrent ? bar_index : bar_index - 1
float d_top = na
float d_bottom = na
switch d_calcTypeInput
D_CalcType.oc =>
d_top := d_bar.c
d_bottom := d_bar.o
D_CalcType.tr =>
d_top := math.max(d_bar.h, d_bar.prevC)
d_bottom := math.min(d_bar.l, d_bar.prevC)
=>
d_top := d_bar.h
d_bottom := d_bar.l
if d_isNewPeriod
d_prevBarIndex := bar_index
if d_visible
box d_htBar = box.new(na, na, na, na)
d_htBar.d_setBox(false, d_left, d_right, d_top, d_bottom, d_diff, d_upBodyColorInput, d_dnBodyColorInput, d_upBorderColorInput, d_dnBorderColorInput)
if d_calcTypeInput == D_CalcType.ohlc
box d_htBody = box.new(na, na, na, na)
d_htBody.d_setBox(false, d_left, d_right, d_bar.o, d_bar.c, d_diff, d_upBodyColorInput, d_dnBodyColorInput, d_upBorderColorInput, d_dnBorderColorInput)
var box d_rtBox = box.new(na, na, na, na)
var box d_rtBody = box.new(na, na, na, na)
if barstate.islast
d_rtBox.d_setBox(not d_visible or d_isNewPeriod, d_left, d_right, d_top, d_bottom, d_diff, d_upBodyColorInput, d_dnBodyColorInput, d_upBorderColorInput, d_dnBorderColorInput)
if d_calcTypeInput == D_CalcType.ohlc
d_rtBody.d_setBox(not d_visible or d_isNewPeriod, d_left, d_right, d_bar.o, d_bar.c, d_diff, d_upBodyColorInput, d_dnBodyColorInput, d_upBorderColorInput, d_dnBorderColorInput)
else
d_rtBody.d_setBox(true, d_left, d_right, d_bar.o, d_bar.c, d_diff, d_upBodyColorInput, d_dnBodyColorInput, d_upBorderColorInput, d_dnBorderColorInput)
// ============================================================================
// 3) 1小时框 —— 原 v5 逻辑保留,仅做 v6 兼容与命名隔离
// ============================================================================
H1_HLR = "High/Low Range"
H1_TR = "True Range"
H1_HAR = "Heikin Ashi Range"
H1_DEFAULT_RESOLUTION = "1H"
H1_DEFAULT_COLOR_GROWTH = color.rgb(0, 150, 136, 80)
H1_DEFAULT_COLOR_FALL = color.rgb(244, 67, 54, 80)
H1_DEFAULT_CALCULATION_METHOD = H1_HLR
var int h1_prevBarIndex = bar_index
h1_drawBox(left, right, top, bottom, diff, growthColorInput, fallColorInput, borderGrowth, borderFall) =>
box drawnBox = box.new(left, top, right, bottom)
if diff < 0
box.set_border_color(drawnBox, borderFall)
box.set_bgcolor(drawnBox, fallColorInput)
else
box.set_border_color(drawnBox, borderGrowth)
box.set_bgcolor(drawnBox, growthColorInput)
drawnBox
h1_visibleTFs = input.string("3,5", "显示图表周期", tooltip="默认只在3分钟和5分钟图显示。多个周期用英文逗号分隔,例如:1,3,5,15,60;填写 ALL 表示所有周期显示。", group="1小时框")
bool h1_visible = frameVisible(h1_visibleTFs)
h1_resolutionInput = input.string("60", "Box Resolution", options=["60","120","240","360","720","1D","1W","1M"], group="1小时框")
h1_timeframe = h1_resolutionInput
h1_calculationMethodInput = input.string(H1_DEFAULT_CALCULATION_METHOD, "Calculation", options=[H1_HLR, H1_TR, H1_HAR], group="1小时框")
h1_groupName = "1小时框 - Border and fill colors"
h1_growthBorderColorInput = input.color(color.new(H1_DEFAULT_COLOR_GROWTH, 0), title="Growth Body", inline="h1_growth", group=h1_groupName)
h1_growthColorInput = input.color(H1_DEFAULT_COLOR_GROWTH, title="", inline="h1_growth", group=h1_groupName)
h1_fallBorderColorInput = input.color(color.new(H1_DEFAULT_COLOR_FALL, 0), title="Fall Body", inline="h1_fall", group=h1_groupName)
h1_fallColorInput = input.color(H1_DEFAULT_COLOR_FALL, title="", inline="h1_fall", group=h1_groupName)
type H1_OHLC
float o
float h
float l
float c
float prevC
h1_makeOHLC(int bb = 0) =>
H1_OHLC.new(open[bb], high[bb], low[bb], close[bb], close[bb+1])
H1_OHLC h1_bar0 = na
H1_OHLC h1_bar1 = na
float h1_ashiHigh = na
float h1_ashiLow = na
if h1_visible
[h1_reqBar0, h1_reqBar1] = request.security(
syminfo.tickerid,
h1_timeframe,
[h1_makeOHLC(0), h1_makeOHLC(1)],
lookahead=barmerge.lookahead_on
)
h1_bar0 := h1_reqBar0
h1_bar1 := h1_reqBar1
[h1_reqAshiHigh, h1_reqAshiLow] = request.security(
ticker.heikinashi(syminfo.tickerid),
h1_timeframe,
[high, low]
)
h1_ashiHigh := h1_reqAshiHigh
h1_ashiLow := h1_reqAshiLow
bool h1_isNewPeriod = h1_visible and ta.change(time(h1_timeframe)) != 0
bool h1_drawCurrent = barstate.islast and h1_visible and not h1_isNewPeriod
H1_OHLC h1_sec = h1_drawCurrent ? h1_bar0 : h1_bar1
h1_sec := na(h1_sec) ? H1_OHLC.new() : h1_sec
float h1_diff = h1_sec.c - h1_sec.o
int h1_left = h1_prevBarIndex
int h1_right = h1_drawCurrent ? bar_index : bar_index - 1
float h1_top = na
float h1_bottom = na
if h1_calculationMethodInput == H1_HLR
h1_top := h1_sec.h
h1_bottom := h1_sec.l
else if h1_calculationMethodInput == H1_TR
h1_top := math.max(h1_sec.h, h1_sec.prevC)
h1_bottom := math.min(h1_sec.l, h1_sec.prevC)
else if h1_calculationMethodInput == H1_HAR
h1_top := h1_ashiHigh
h1_bottom := h1_ashiLow
if h1_isNewPeriod
h1_prevBarIndex := bar_index
if h1_visible
h1_drawBox(h1_left, h1_right, h1_top, h1_bottom, h1_diff, h1_growthColorInput, h1_fallColorInput, h1_growthBorderColorInput, h1_fallBorderColorInput)
var box h1_prevBox = na
if barstate.islast
box.delete(h1_prevBox)
h1_prevBox := na
if h1_visible and not h1_isNewPeriod
h1_prevBox := h1_drawBox(h1_left, h1_right, h1_top, h1_bottom, h1_diff, h1_growthColorInput, h1_fallColorInput, h1_growthBorderColorInput, h1_fallBorderColorInput)
// ============================================================================
// 4) 15分钟框 —— 原 v5 逻辑保留,仅做 v6 兼容与命名隔离
// ============================================================================
M15_HLR = "High/Low Range"
M15_TR = "True Range"
M15_HAR = "Heikin Ashi Range"
M15_DEFAULT_RESOLUTION = "15min"
M15_DEFAULT_COLOR_GROWTH = color.rgb(0, 150, 136, 80)
M15_DEFAULT_COLOR_FALL = color.rgb(244, 67, 54, 80)
M15_DEFAULT_CALCULATION_METHOD = M15_HLR
var int m15_prevBarIndex = bar_index
m15_drawBox(left, right, top, bottom, diff, growthColorInput, fallColorInput, borderGrowth, borderFall) =>
box drawnBox = box.new(left, top, right, bottom)
if diff < 0
box.set_border_color(drawnBox, borderFall)
box.set_bgcolor(drawnBox, fallColorInput)
else
box.set_border_color(drawnBox, borderGrowth)
box.set_bgcolor(drawnBox, growthColorInput)
drawnBox
m15_visibleTFs = input.string("1", "显示图表周期", tooltip="默认只在1分钟图显示。多个周期用英文逗号分隔,例如:1,3,5;填写 ALL 表示所有周期显示。", group="15分钟框")
bool m15_visible = frameVisible(m15_visibleTFs)
m15_resolutionInput = input.string("15", "Box Resolution", options=["15","60","120","240","360","720","1D","1W","1M"], group="15分钟框")
m15_timeframe = m15_resolutionInput
m15_calculationMethodInput = input.string(M15_DEFAULT_CALCULATION_METHOD, "Calculation", options=[M15_HLR, M15_TR, M15_HAR], group="15分钟框")
m15_groupName = "15分钟框 - Border and fill colors"
m15_growthBorderColorInput = input.color(color.new(M15_DEFAULT_COLOR_GROWTH, 0), title="Growth Body", inline="m15_growth", group=m15_groupName)
m15_growthColorInput = input.color(M15_DEFAULT_COLOR_GROWTH, title="", inline="m15_growth", group=m15_groupName)
m15_fallBorderColorInput = input.color(color.new(M15_DEFAULT_COLOR_FALL, 0), title="Fall Body", inline="m15_fall", group=m15_groupName)
m15_fallColorInput = input.color(M15_DEFAULT_COLOR_FALL, title="", inline="m15_fall", group=m15_groupName)
type M15_OHLC
float o
float h
float l
float c
float prevC
m15_makeOHLC(int bb = 0) =>
M15_OHLC.new(open[bb], high[bb], low[bb], close[bb], close[bb+1])
M15_OHLC m15_bar0 = na
M15_OHLC m15_bar1 = na
float m15_ashiHigh = na
float m15_ashiLow = na
if m15_visible
[m15_reqBar0, m15_reqBar1] = request.security(
syminfo.tickerid,
m15_timeframe,
[m15_makeOHLC(0), m15_makeOHLC(1)],
lookahead=barmerge.lookahead_on
)
m15_bar0 := m15_reqBar0
m15_bar1 := m15_reqBar1
[m15_reqAshiHigh, m15_reqAshiLow] = request.security(
ticker.heikinashi(syminfo.tickerid),
m15_timeframe,
[high, low]
)
m15_ashiHigh := m15_reqAshiHigh
m15_ashiLow := m15_reqAshiLow
bool m15_isNewPeriod = m15_visible and ta.change(time(m15_timeframe)) != 0
bool m15_drawCurrent = barstate.islast and m15_visible and not m15_isNewPeriod
M15_OHLC m15_sec = m15_drawCurrent ? m15_bar0 : m15_bar1
m15_sec := na(m15_sec) ? M15_OHLC.new() : m15_sec
float m15_diff = m15_sec.c - m15_sec.o
int m15_left = m15_prevBarIndex
int m15_right = m15_drawCurrent ? bar_index : bar_index - 1
float m15_top = na
float m15_bottom = na
if m15_calculationMethodInput == M15_HLR
m15_top := m15_sec.h
m15_bottom := m15_sec.l
else if m15_calculationMethodInput == M15_TR
m15_top := math.max(m15_sec.h, m15_sec.prevC)
m15_bottom := math.min(m15_sec.l, m15_sec.prevC)
else if m15_calculationMethodInput == M15_HAR
m15_top := m15_ashiHigh
m15_bottom := m15_ashiLow
if m15_isNewPeriod
m15_prevBarIndex := bar_index
if m15_visible
m15_drawBox(m15_left, m15_right, m15_top, m15_bottom, m15_diff, m15_growthColorInput, m15_fallColorInput, m15_growthBorderColorInput, m15_fallBorderColorInput)
var box m15_prevBox = na
if barstate.islast
box.delete(m15_prevBox)
m15_prevBox := na
if m15_visible and not m15_isNewPeriod
m15_prevBox := m15_drawBox(m15_left, m15_right, m15_top, m15_bottom, m15_diff, m15_growthColorInput, m15_fallColorInput, m15_growthBorderColorInput, m15_fallBorderColorInput)
// ============================================================================
// 5) 4小时框 —— 复制1小时框逻辑,独立设置与命名隔离
// ============================================================================
H4_HLR = "High/Low Range"
H4_TR = "True Range"
H4_HAR = "Heikin Ashi Range"
H4_DEFAULT_RESOLUTION = "240"
H4_DEFAULT_COLOR_GROWTH = color.rgb(0, 150, 136, 80)
H4_DEFAULT_COLOR_FALL = color.rgb(244, 67, 54, 80)
H4_DEFAULT_CALCULATION_METHOD = H4_HLR
var int h4_prevBarIndex = bar_index
h4_drawBox(left, right, top, bottom, diff, growthColorInput, fallColorInput, borderGrowth, borderFall) =>
box drawnBox = box.new(left, top, right, bottom)
if diff < 0
box.set_border_color(drawnBox, borderFall)
box.set_bgcolor(drawnBox, fallColorInput)
else
box.set_border_color(drawnBox, borderGrowth)
box.set_bgcolor(drawnBox, growthColorInput)
drawnBox
h4_visibleTFs = input.string("ALL", "显示图表周期", tooltip="填写当前图表周期,多个用英文逗号分隔。例如:15,60 表示只在15分钟和1小时图显示;填写 ALL 表示所有周期显示。", group="4小时框")
bool h4_visible = frameVisible(h4_visibleTFs)
h4_resolutionInput = input.string("240", "Box Resolution", options=["60","120","240","360","720","1D","1W","1M"], group="4小时框")
h4_timeframe = h4_resolutionInput
h4_calculationMethodInput = input.string(H4_DEFAULT_CALCULATION_METHOD, "Calculation", options=[H4_HLR, H4_TR, H4_HAR], group="4小时框")
h4_groupName = "4小时框 - Border and fill colors"
h4_growthBorderColorInput = input.color(color.new(H4_DEFAULT_COLOR_GROWTH, 0), title="Growth Body", inline="h4_growth", group=h4_groupName)
h4_growthColorInput = input.color(H4_DEFAULT_COLOR_GROWTH, title="", inline="h4_growth", group=h4_groupName)
h4_fallBorderColorInput = input.color(color.new(H4_DEFAULT_COLOR_FALL, 0), title="Fall Body", inline="h4_fall", group=h4_groupName)
h4_fallColorInput = input.color(H4_DEFAULT_COLOR_FALL, title="", inline="h4_fall", group=h4_groupName)
type H4_OHLC
float o
float h
float l
float c
float prevC
h4_makeOHLC(int bb = 0) =>
H4_OHLC.new(open[bb], high[bb], low[bb], close[bb], close[bb+1])
H4_OHLC h4_bar0 = na
H4_OHLC h4_bar1 = na
float h4_ashiHigh = na
float h4_ashiLow = na
if h4_visible
[h4_reqBar0, h4_reqBar1] = request.security(
syminfo.tickerid,
h4_timeframe,
[h4_makeOHLC(0), h4_makeOHLC(1)],
lookahead=barmerge.lookahead_on
)
h4_bar0 := h4_reqBar0
h4_bar1 := h4_reqBar1
[h4_reqAshiHigh, h4_reqAshiLow] = request.security(
ticker.heikinashi(syminfo.tickerid),
h4_timeframe,
[high, low]
)
h4_ashiHigh := h4_reqAshiHigh
h4_ashiLow := h4_reqAshiLow
bool h4_isNewPeriod = h4_visible and ta.change(time(h4_timeframe)) != 0
bool h4_drawCurrent = barstate.islast and h4_visible and not h4_isNewPeriod
H4_OHLC h4_sec = h4_drawCurrent ? h4_bar0 : h4_bar1
h4_sec := na(h4_sec) ? H4_OHLC.new() : h4_sec
float h4_diff = h4_sec.c - h4_sec.o
int h4_left = h4_prevBarIndex
int h4_right = h4_drawCurrent ? bar_index : bar_index - 1
float h4_top = na
float h4_bottom = na
if h4_calculationMethodInput == H4_HLR
h4_top := h4_sec.h
h4_bottom := h4_sec.l
else if h4_calculationMethodInput == H4_TR
h4_top := math.max(h4_sec.h, h4_sec.prevC)
h4_bottom := math.min(h4_sec.l, h4_sec.prevC)
else if h4_calculationMethodInput == H4_HAR
h4_top := h4_ashiHigh
h4_bottom := h4_ashiLow
if h4_isNewPeriod
h4_prevBarIndex := bar_index
if h4_visible
h4_drawBox(h4_left, h4_right, h4_top, h4_bottom, h4_diff, h4_growthColorInput, h4_fallColorInput, h4_growthBorderColorInput, h4_fallBorderColorInput)
var box h4_prevBox = na
if barstate.islast
box.delete(h4_prevBox)
h4_prevBox := na
if h4_visible and not h4_isNewPeriod
h4_prevBox := h4_drawBox(h4_left, h4_right, h4_top, h4_bottom, h4_diff, h4_growthColorInput, h4_fallColorInput, h4_growthBorderColorInput, h4_fallBorderColorInput)