Mine Script如何写量化
//@version=5
indicator(title='Market Bias (CEREBR) - 市场偏见 (CEREBR)', shorttitle='市场偏见', overlay=true)
//#region -----> 输入、函数
// 函数
//@function 此函数强制输入时间框架不得低于当前图表时间框架
FORCE_CTF(str_tf) =>
i_tf_sec = timeframe.in_seconds(str_tf)
ctf_sec = timeframe.in_seconds('')
// 如果输入时间框架低于当前图表时间框架,返回当前时间框架
i_tf_sec < ctf_sec ? '' : str_tf
//
// 输入
ha_htf = FORCE_CTF(input.timeframe('', '时间框架', tooltip="此时间框架必须等于或大于图表时间框架", group="Heikin Ashi 市场偏见"))
ha_len = input(100, '周期', group="Heikin Ashi 市场偏见")
ha_len2 = input(100, '平滑', group="Heikin Ashi 市场偏见")
show_ha = input.bool(true, "显示 Heikin Ashi K线", inline='ha/mb display', group='显示设置')
show_mb = input.bool(true, "显示市场偏见", inline='ha/mb display', group='显示设置')
col_bull = input.color(color.lime, '颜色:看涨', inline='bull/bear color', group='显示设置', display=display.none)
col_bear = input.color(color.red, '看跌', inline='bull/bear color', group='显示设置', display=display.none)
// 在导入数据时添加偏移以避免前视偏差
indexHighTF = timeframe.in_seconds(ha_htf) == timeframe.in_seconds('') ? 0 : barstate.isrealtime ? 1 : 0
indexCurrTF = timeframe.in_seconds(ha_htf) == timeframe.in_seconds('') ? 0 : barstate.isrealtime ? 0 : 1
//@function 处理从其他时间框架导入数据,同时防止重绘
//@param _resolution (str) : 要导入数据的目标时间框架
//@param _expression (float | int) : 要导入的数据
f_no_repaint_request(string _resolution, _expression) =>
request.security(syminfo.tickerid, _resolution, _expression)
//#endregion
//#region -----> 计算
// 平滑 OHLC 值
o = ta.ema(open, ha_len)
c = ta.ema(close, ha_len)
h = ta.ema(high, ha_len)
l = ta.ema(low, ha_len)
// 计算 Heikin Ashi OHLC 值
haclose = f_no_repaint_request(ha_htf, (o + h + l + c) / 4)
xhaopen = f_no_repaint_request(ha_htf, (o + c) / 2)
haopen = na(xhaopen) ? (o + c) / 2 : (xhaopen + haclose) / 2
hahigh = math.max(h, math.max(haopen, haclose))
halow = math.min(l, math.min(haopen, haclose))
// 平滑 Heikin Ashi K线
o2 = f_no_repaint_request(ha_htf, ta.ema(haopen, ha_len2))
c2 = f_no_repaint_request(ha_htf, ta.ema(haclose, ha_len2))
h2 = f_no_repaint_request(ha_htf, ta.ema(hahigh, ha_len2))
l2 = f_no_repaint_request(ha_htf, ta.ema(halow, ha_len2))
ha_avg = (h2 + l2) / 2
// 振荡器
osc_len = input.int(7, "振荡器周期", group="Heikin Ashi 市场偏见")
osc_bias = 100 * (c2 - o2)
osc_smooth = ta.ema(osc_bias, osc_len)
sigcolor = switch
(osc_bias > 0) and (osc_bias >= osc_smooth) => color.new(col_bull, 35)
(osc_bias > 0) and (osc_bias < osc_smooth) => color.new(col_bull, 75)
(osc_bias < 0) and (osc_bias <= osc_smooth) => color.new(col_bear, 35)
(osc_bias < 0) and (osc_bias > osc_smooth) => color.new(col_bear, 75)
=> color(na)
//#endregion
//#region -----> 绘图、警报
// 绘图
p_h = plot(h2, "偏见高点", color=color(na), display=display.data_window, editable=false)
p_l = plot(l2, "偏见低点", color=color(na), display=display.data_window, editable=false)
p_avg = plot(ha_avg, "偏见平均", color=color(na), display=display.data_window, editable=false)
fill(p_l, p_h, show_mb ? sigcolor : na)
col = o2 > c2 ? col_bear : col_bull
plotcandle(o2, h2, l2, c2, title='平滑 Heikin Ashi', color=col, display=show_ha ? display.pane : display.data_window, editable=false)
// 警报
// 看涨趋势切换(看跌 -> 看涨)
alertcondition(ta.change(ta.change(math.sign(osc_bias)) > 0),
'看涨趋势切换(看跌 -> 看涨)', '{{exchange}}:{{ticker}}: 趋势现为看涨。')
// 看涨趋势增强
alertcondition(osc_bias > 0 and ta.change(math.sign(osc_bias - osc_smooth)) > 0,
'看涨趋势增强', '{{exchange}}:{{ticker}}: 看涨趋势现更强。')
// 看涨趋势减弱
alertcondition(osc_bias > 0 and ta.change(math.sign(osc_bias - osc_smooth)) < 0,
'看涨趋势减弱', '{{exchange}}:{{ticker}}: 看涨趋势现较弱。')
// 看跌趋势切换(看涨 -> 看跌)
alertcondition(ta.change(ta.change(math.sign(osc_bias)) < 0),
'看跌趋势切换(看涨 -> 看跌)', '{{exchange}}:{{ticker}}: 趋势现为看跌。')
// 看跌趋势增强
alertcondition(osc_bias < 0 and ta.change(math.sign(osc_bias - osc_smooth)) < 0,
'看跌趋势增强', '{{exchange}}:{{ticker}}: 看跌趋势现更强。')
// 看跌趋势减弱
alertcondition(osc_bias < 0 and ta.change(math.sign(osc_bias - osc_smooth)) > 0,
'看跌趋势减弱', '{{exchange}}:{{ticker}}: 看跌趋势现较弱。')
//#endregion
// 自定义警报:要启用此功能,请选择并取消以下代码块的注释(Cmd (或 Ctrl) + /),从下一行开始。
// // {
// use_custom_alerts = input.bool(false, '使用自定义警报消息', group='警报消息', display=display.none)
// i_alert_bull_trend_switch = input.text_area('新看涨趋势', '看涨趋势切换', group='警报消息', display=display.none)
// i_alert_bull_trend_strengthen = input.text_area('新强看涨趋势', '看涨趋势增强', group='警报消息', display=display.none)
// i_alert_bull_trend_weaken = input.text_area('新弱看涨趋势', '看涨趋势减弱', group='警报消息', display=display.none)
// i_alert_bear_trend_switch = input.text_area('新看跌趋势', '看跌趋势切换', group='警报消息', display=display.none)
// i_alert_bear_trend_strengthen = input.text_area('新强看跌趋势', '看跌趋势增强', group='警报消息', display=display.none)
// i_alert_bear_trend_weaken = input.text_area('新弱看跌趋势', '看跌趋势减弱', group='警报消息', display=display.none)
// // 看涨趋势切换(看跌 -> 看涨)
// if (ta.change(ta.change(math.sign(osc_bias)) > 0))
// alert(i_alert_bull_trend_switch, alert.freq_once_per_bar)
// // 看涨趋势增强
// if (osc_bias > 0 and ta.change(math.sign(osc_bias - osc_smooth)) > 0)
// alert(i_alert_bull_trend_strengthen, alert.freq_once_per_bar)
// // 看涨趋势减弱
// if (osc_bias > 0 and ta.change(math.sign(osc_bias - osc_smooth)) < 0)
// alert(i_alert_bull_trend_weaken, alert.freq_once_per_bar)
// // 看跌趋势切换(看涨 -> 看跌)
// if (ta.change(ta.change(math.sign(osc_bias)) < 0))
// alert(i_alert_bear_trend_switch, alert.freq_once_per_bar)
// // 看跌趋势增强
// if (osc_bias < 0 and ta.change(math.sign(osc_bias - osc_smooth)) < 0)
// alert(i_alert_bear_trend_strengthen, alert.freq_once_per_bar)
// // 看跌趋势减弱
// if (osc_bias < 0 and ta.change(math.sign(osc_bias - osc_smooth)) > 0)
// alert(i_alert_bear_trend_weaken, alert.freq_once_per_bar)
// // }
页:
[1]