请求合并EMA多时间帧表格与EMA Band指标并新增表格行
合并多时间帧EMA表格与EMA Band指标,新增Band状态行
我有两段Pine Script代码:
- 一段是多时间帧200EMA数值表格指标:在表格中展示不同时间帧下的200EMA数值,单元格颜色随价格与EMA的关系变化
- 一段是可自定义参数的EMA Band指标:生成由两条EMA组成的通道,通道颜色根据价格趋势切换红绿
需求:在200EMA表格中新增一行,显示对应时间帧下EMA Band的颜色状态(绿色代表向上趋势,红色代表向下趋势)
实现步骤
要完成需求,需将两个指标的逻辑合并,并扩展表格功能:
- 整合EMA Band的核心逻辑:将原Band指标的输入参数、EMA计算、趋势判断逻辑移入表格指标中
- 获取多时间帧Band状态:使用
request.security函数,针对每个选定时间帧,获取该时间帧下EMA Band的趋势状态(价格同时在两条EMA上方为绿色趋势,同时在下方为红色趋势) - 扩展表格结构:将原表格的行数从2改为3,新增一行用于显示Band趋势状态
- 编写状态填充函数:新增函数处理Band状态单元格的颜色和文本显示,用直观的图标对应趋势状态
完整合并代码
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ //@version=5 indicator(title="EMA Table with Band Trend", shorttitle="EMA+Band", overlay=true) // -------------------------- // 表格位置与尺寸设置 // -------------------------- PosTable = input.string(title="Position", defval="Bottom Right", options=["Top Right", "Middle Right", "Bottom Right", "Top Center", "Middle Center", "Bottom Center", "Top Left", "Middle Left", "Bottom Left"], group="Position & Size", inline="01") SizTable = input.string(title="Size", defval="Small", options=["Auto", "Huge", "Large", "Normal", "Small", "Tiny"], group="Position & Size", inline="01") Pos1Table = PosTable == "Top Right" ? position.top_right : PosTable == "Middle Right" ? position.middle_right : PosTable == "Bottom Right" ? position.bottom_right : PosTable == "Top Center" ? position.top_center : PosTable == "Middle Center" ? position.middle_center : PosTable == "Bottom Center" ? position.bottom_center : PosTable == "Top Left" ? position.top_left : PosTable == "Middle Left" ? position.middle_left : position.bottom_left Siz1Table = SizTable == "Auto" ? size.auto : SizTable == "Huge" ? size.huge : SizTable == "Large" ? size.large : SizTable == "Normal" ? size.normal : SizTable == "Small" ? size.small : size.tiny // 扩展表格为7列3行(新增Band趋势行) EMATable = table.new(Pos1Table, 7, 3, bgcolor=#000000, frame_color=#4F4F4F, frame_width=2, border_color=#696969, border_width=1) // -------------------------- // 200 EMA 参数设置 // -------------------------- len_ema200 = input.int(200, minval=1, title="Length", inline="001", group="EMA200 inputs") src_ema200 = input.source(close, "Source", inline="002", group="EMA200 inputs") // -------------------------- // EMA Band 参数设置(来自原Band指标) // -------------------------- switchColor = input(true, "Color according to trend?", group="EMA Band Settings") visualSwitch = input(true, title="Show as a Band?", group="EMA Band Settings") thicknesSwitch = input.int(1, title="Line Thickness", minval=0, group="EMA Band Settings") transpSwitch = input.int(25, title="Band Transparency", step=5, group="EMA Band Settings") ema200Color = input.color(color.red, "EMA 200 Color", group="EMA Band Settings") // EMA Band 核心参数 len1 = input.int(50, minval=1, title="EMA 1 Length", group="EMA Band Settings") src1 = input.source(close, title="EMA 1 Source", group="EMA Band Settings") len2 = input.int(50, minval=1, title="EMA 2 Length", group="EMA Band Settings") src2 = input.source(close, title="EMA 2 Source", group="EMA Band Settings") // -------------------------- // 时间帧选择 // -------------------------- tf1 = input.timeframe("1", "TF 1", inline="1", group="Select Timeframes") tf2 = input.timeframe("5", "TF 2", inline="3", group="Select Timeframes") tf3 = input.timeframe("15", "TF 3", inline="5", group="Select Timeframes") tf4 = input.timeframe("60", "TF 4", inline="1", group="Select Timeframes") tf5 = input.timeframe("240","TF 5", inline="3", group="Select Timeframes") tf6 = input.timeframe("1D", "TF 6", inline="5", group="Select Timeframes") // -------------------------- // 获取多时间帧200EMA数值 // -------------------------- get_ema200(tf) => request.security(syminfo.tickerid, tf, ta.ema(close, len_ema200)) TF1_ema200 = get_ema200(tf1) TF5_ema200 = get_ema200(tf2) TF15_ema200 = get_ema200(tf3) TF60_ema200 = get_ema200(tf4) TF4H_ema200 = get_ema200(tf5) TF1D_ema200 = get_ema200(tf6) // -------------------------- // 获取多时间帧EMA Band趋势状态 // -------------------------- get_band_trend(tf) => ema1_tf = ta.ema(src1, len1) ema2_tf = ta.ema(src2, len2) price_above = close > ema1_tf and close > ema2_tf price_below = close < ema1_tf and close < ema2_tf [price_above, price_below] // 各时间帧的Band趋势状态 TF1_band = request.security(syminfo.tickerid, tf1, get_band_trend(tf1)) TF5_band = request.security(syminfo.tickerid, tf2, get_band_trend(tf2)) TF15_band = request.security(syminfo.tickerid, tf3, get_band_trend(tf3)) TF60_band = request.security(syminfo.tickerid, tf4, get_band_trend(tf4)) TF4H_band = request.security(syminfo.tickerid, tf5, get_band_trend(tf5)) TF1D_band = request.security(syminfo.tickerid, tf6, get_band_trend(tf6)) // -------------------------- // 工具函数:格式化时间帧文本 // -------------------------- tfTxt(x) => out = x if not str.contains(x, "S") and not str.contains(x, "M") and not str.contains(x, "W") and not str.contains(x, "D") if str.tonumber(x)%60 == 0 out := str.tostring(str.tonumber(x)/60)+"H" else out := x + "m" out // -------------------------- // 填充200EMA单元格函数 // -------------------------- f_fillEMACell(_table, _column, _row, _value) => cell_color = #000000 cell_txt = #00000000 if _value >= close cell_color := #8B0000 cell_txt := #FF8C00 else if _value <= close cell_color := #006400 cell_txt := #00FF00 else cell_color := #1C1C1C cell_txt := #DCDCDC _cellText = str.tostring(_value, '#') table.cell(_table, _column, _row, _cellText, bgcolor=color.new(cell_color, 40), text_color=cell_txt, text_size=Siz1Table) // -------------------------- // 填充Band趋势单元格函数 // -------------------------- f_fillBandCell(_table, _column, _row, _band_status) => price_above = _band_status[0] price_below = _band_status[1] cell_color = #000000 cell_txt = #FFFFFF cell_text = "—" if price_above cell_color := #006400 cell_txt := #00FF00 cell_text = "🟢" else if price_below cell_color := #8B0000 cell_txt := #FF8C00 cell_text = "🔴" table.cell(_table, _column, _row, cell_text, bgcolor=color.new(cell_color, 40), text_color=cell_txt, text_size=Siz1Table) // -------------------------- // 绘制表头 // -------------------------- table.cell(EMATable, 0, 0, "TF", text_color=#FFD700, bgcolor=#000000, text_size=size.small) table.cell(EMATable, 0, 1, "EMA ("+str.tostring(len_ema200)+")", text_color=#FFD700, bgcolor=#000000, text_size=Siz1Table) table.cell(EMATable, 0, 2, "Band Trend", text_color=#FFD700, bgcolor=#000000, text_size=Siz1Table) // 填充时间帧表头 table.cell(EMATable, 1, 0, tfTxt(tf1), text_color=#FFFFFF, text_size=Siz1Table) table.cell(EMATable, 2, 0, tfTxt(tf2), text_color=#FFFFFF, text_size=Siz1Table) table.cell(EMATable, 3, 0, tfTxt(tf3), text_color=#FFFFFF, text_size=Siz1Table) table.cell(EMATable, 4, 0, tfTxt(tf4), text_color=#FFFFFF, text_size=Siz1Table) table.cell(EMATable, 5, 0, tfTxt(tf5), text_color=#FFFFFF, text_size=Siz1Table) table.cell(EMATable, 6, 0, tfTxt(tf6), text_color=#FFFFFF, text_size=Siz1Table) // -------------------------- // 填充表格内容(仅最后一根K线更新) // -------------------------- if barstate.islast // 填充200EMA行 f_fillEMACell(EMATable, 1, 1, TF1_ema200) f_fillEMACell(EMATable, 2, 1, TF5_ema200) f_fillEMACell(EMATable, 3, 1, TF15_ema200) f_fillEMACell(EMATable, 4, 1, TF60_ema200) f_fillEMACell(EMATable, 5, 1, TF4H_ema200) f_fillEMACell(EMATable, 6, 1, TF1D_ema200) // 填充Band趋势行 f_fillBandCell(EMATable, 1, 2, TF1_band) f_fillBandCell(EMATable, 2, 2, TF5_band) f_fillBandCell(EMATable, 3, 2, TF15_band) f_fillBandCell(EMATable, 4, 2, TF60_band) f_fillBandCell(EMATable, 5, 2, TF4H_band) f_fillBandCell(EMATable, 6, 2, TF1D_band) // -------------------------- // 绘制EMA Band(保留原Band指标的绘图逻辑) // -------------------------- ema1 = ta.ema(src1, len1) ema2 = ta.ema(src2, len2) ema200 = ta.ema(close, len_ema200) price_above_ema = close > ema1 and close > ema2 price_below_ema = close < ema1 and close < ema2 fill_color = price_above_ema ? color.rgb(80, 175, 80, transpSwitch) : (price_below_ema ? color.rgb(250, 100, 100, transpSwitch) : color.new(color.gray, transpSwitch)) ema1_color = close >= ema1 ? color.green : color.red ema2_color = close >= ema2 ? color.green : color.red plot(ema2, title='EMA 2', style=plot.style_line, color=ema1_color, linewidth=1) plot(ema1, title='EMA 1', style=plot.style_line, color=ema2_color, linewidth=1) plot(ema200, title='EMA 200', style=plot.style_line, color=ema200Color, linewidth=2) BandColor = switchColor ? fill_color : color.new(#ff9800, 50) Fi1 = plot(ema1, title="ema1", color=BandColor, linewidth=thicknesSwitch) Fi2 = plot(visualSwitch ? ema2 : na, title="ema2", color=BandColor, linewidth=thicknesSwitch) alertcondition(ta.crossover(ema1,ema2), title='Trending Up', message='Trending Up') alertcondition(ta.crossover(ema2, ema1), title='Trending Down', message='Trending Down') fill(Fi1, Fi2, title="Band Filler", color=fill_color)
内容的提问来源于stack exchange,提问作者AYUSH singh
相关产品推荐
相关产品推荐

