Multi-timeframe Moving Averages + Stochastic RSI V2

tommyf1001 · study · 163 行 · 点赞 2,077 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tommyf1001
// This script is free to use and modify, however if you do publish a modified version of this script, please make sure you credit me for the original script.
 
//@version=4
study(title="Multi-timeframe Moving Averages + Stochastic RSI", shorttitle="MAs + Stoch RSI", overlay=true)
 
 
 
// ------------------- Indicator User Inputs
 
smoothK = input(3, "K", minval=1, group="Stoch RSI")
lengthRSI = input(14, "RSI Length", minval=1, group="Stoch RSI")
lengthStoch = input(14, "Stochastic Length", minval=1, group="Stoch RSI")
stoch_low = input(10, title="Stoch Oversold Level", group="Stoch RSI")
stoch_high = input(90, title="Stoch Overbought Level", group="Stoch RSI")
 
ma_1_period = input(21, "Fast MA", group="MAs")
ma_1_type = input(title="Fast MA Type", defval="EMA", options=["SMA", "EMA"], group="MAs")
ma_2_period = input(50, "Slow MA", group="MAs")
ma_2_type = input(title="Slow MA Type", defval="SMA", options=["SMA", "EMA"], group="MAs")
 
currentonly = input(title="Plot/Alert Current Timeframe?", defval=true, group="Timeframes")
tf1only = input(title="Plot/Alert TF1?", defval=true, group="Timeframes")
tf1 = input(defval="60", title="Timeframe 1", type=input.resolution, group="Timeframes")
tf2only = input(title="Plot/Alert TF2?", defval=true, group="Timeframes")
tf2 = input(defval="240", title="Timeframe 2", type=input.resolution, group="Timeframes")
tf3only = input(title="Plot/Alert TF3?", defval=true, group="Timeframes")
tf3 = input(defval="D", title="Timeframe 3", type=input.resolution, group="Timeframes")
 
repaint = input(defval=true, title="Repaint?")
 
 
// ------------------ Calculations
 
// --------- Stoch RSI
rsi1 = rsi(close, lengthRSI)
stoch_k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
 
rsi_tf1 = security(syminfo.tickerid, tf1, repaint ? rsi(close,lengthRSI) : rsi(close, lengthRSI)[1], barmerge.gaps_on, barmerge.lookahead_on)
stoch_k_tf1 = security(syminfo.tickerid, tf1, sma(stoch(rsi_tf1, rsi_tf1, rsi_tf1, lengthStoch), smoothK), barmerge.gaps_on, barmerge.lookahead_on)
rsi_tf2 = security(syminfo.tickerid, tf2, repaint ? rsi(close,lengthRSI) : rsi(close, lengthRSI)[1], barmerge.gaps_on, barmerge.lookahead_on)
stoch_k_tf2 = security(syminfo.tickerid, tf2, sma(stoch(rsi_tf2, rsi_tf2, rsi_tf2, lengthStoch), smoothK), barmerge.gaps_on, barmerge.lookahead_on)
rsi_tf3 = security(syminfo.tickerid, tf3, repaint ? rsi(close,lengthRSI) : rsi(close, lengthRSI)[1], barmerge.gaps_on, barmerge.lookahead_on)
stoch_k_tf3 = security(syminfo.tickerid, tf3, sma(stoch(rsi_tf3, rsi_tf3, rsi_tf3, lengthStoch), smoothK), barmerge.gaps_on, barmerge.lookahead_on)
 
stoch_rsi_bull = crossover(stoch_k, stoch_low)
stoch_rsi_bear = crossunder(stoch_k, stoch_high)
 
stoch_rsi_bull_tf1 = crossover(stoch_k_tf1, stoch_low)
stoch_rsi_bear_tf1 = crossunder(stoch_k_tf1, stoch_high)
stoch_rsi_bull_tf2 = crossover(stoch_k_tf2, stoch_low)
stoch_rsi_bear_tf2 = crossunder(stoch_k_tf2, stoch_high)
stoch_rsi_bull_tf3 = crossover(stoch_k_tf3, stoch_low)
stoch_rsi_bear_tf3 = crossunder(stoch_k_tf3, stoch_high)
 
 
// --------- MAs
 
ma_1 = if ma_1_type == "SMA"
    sma(close, ma_1_period)
else
    if ma_1_type == "EMA"
        ema(close, ma_1_period)
 
ma_2 = if ma_2_type == "EMA"
    ema(close, ma_2_period)
else
    if ma_2_type == "SMA"
        sma(close, ma_2_period)
 
 
ma_1_tf1 = security(syminfo.tickerid, tf1, ma_1[1], barmerge.gaps_on, barmerge.lookahead_on)
ma_2_tf1 = security(syminfo.tickerid, tf1, ma_2[1], barmerge.gaps_on, barmerge.lookahead_on)
 
ma_1_tf2 = security(syminfo.tickerid, tf2, ma_1[1], barmerge.gaps_on, barmerge.lookahead_on)
ma_2_tf2 = security(syminfo.tickerid, tf2, ma_2[1], barmerge.gaps_on, barmerge.lookahead_on)
 
ma_1_tf3 = security(syminfo.tickerid, tf3, ma_1[1], barmerge.gaps_on, barmerge.lookahead_on)
ma_2_tf3 = security(syminfo.tickerid, tf3, ma_2[1], barmerge.gaps_on, barmerge.lookahead_on)
 
 
ma_bull = ma_1 > ma_2
ma_bear = ma_1 < ma_2
ma_bull_tf1 = ma_1_tf1 > ma_2_tf1
ma_bear_tf1 = ma_1_tf1 < ma_2_tf1
ma_bull_tf2 = ma_1_tf2 > ma_2_tf2
ma_bear_tf2 = ma_1_tf2 < ma_2_tf2
ma_bull_tf3 = ma_1_tf3 > ma_2_tf3
ma_bear_tf3 = ma_1_tf3 < ma_2_tf3
 
 
// --------- Final conditions
bull = (ma_bull and stoch_rsi_bull)
bear = (ma_bear and stoch_rsi_bear)
bull_tf1 = (ma_bull_tf1 and stoch_rsi_bull_tf1)
bear_tf1 = (ma_bear_tf1 and stoch_rsi_bear_tf1)
bull_tf2 = (ma_bull_tf2 and stoch_rsi_bull_tf2)
bear_tf2 = (ma_bear_tf2 and stoch_rsi_bear_tf2)
bull_tf3 = (ma_bull_tf3 and stoch_rsi_bull_tf3)
bear_tf3 = (ma_bear_tf3 and stoch_rsi_bear_tf3)
 
 
 
// --------- This code ensures that no plots from lower time frames will be plotted on higher time frames
// ————— Converts current chart resolution into a float minutes value.
f_resInMinutes() => 
    _resInMinutes = timeframe.multiplier * (
      timeframe.isseconds ? 1. / 60             :
      timeframe.isminutes ? 1.                  :
      timeframe.isdaily   ? 60. * 24            :
      timeframe.isweekly  ? 60. * 24 * 7        :
      timeframe.ismonthly ? 60. * 24 * 30.4375  : na)
// ————— Returns the float minutes value of the string _res.
f_tfResInMinutes(_res) =>
    // _res: resolution of any TF (in "timeframe.period" string format).
    // Dependency: f_resInMinutes().
    security(syminfo.tickerid, _res, f_resInMinutes())
 
// —————————— Determine if current timeframe is smaller that higher timeframe selected in Inputs.
// Get higher timeframe in minutes.
tf1InMinutes = f_tfResInMinutes(tf1)
tf2InMinutes = f_tfResInMinutes(tf2)
tf3InMinutes = f_tfResInMinutes(tf3)
// Get current timeframe in minutes.
currentTfInMinutes = f_resInMinutes()
// Compare current TF to higher TF to make sure it is smaller, otherwise our plots don't make sense.
chartOnLowerTf1 = currentTfInMinutes < tf1InMinutes
chartOnLowerTf2 = currentTfInMinutes < tf2InMinutes
chartOnLowerTf3 = currentTfInMinutes < tf3InMinutes
 
 
 
// -----------------------------------------------------------
// Plots
 
plotshape(repaint ? currentonly and bull : currentonly and bull[1], title='Buy', style=shape.arrowup, location=location.belowbar, color=color.green, text='Buy', size=size.small)
plotshape(repaint ? currentonly and bear : currentonly and bear[1], title='Sell', style=shape.arrowdown, location=location.abovebar, color=color.red, text='Sell', size=size.small)
plotshape(tf1only and chartOnLowerTf1 and bull_tf1, title='Buy TF1', style=shape.arrowup, location=location.belowbar, color=color.green, text='Buy TF1', size=size.small)
plotshape(tf1only and chartOnLowerTf1 and bear_tf1, title='Sell TF1', style=shape.arrowdown, location=location.abovebar, color=color.red, text='Sell TF1', size=size.small)
plotshape(tf2only and chartOnLowerTf2 and bull_tf2, title='Buy TF2', style=shape.arrowup, location=location.belowbar, color=color.green, text='Buy TF2', size=size.small)
plotshape(tf2only and chartOnLowerTf2 and bear_tf2, title='Sell TF2', style=shape.arrowdown, location=location.abovebar, color=color.red, text='Sell TF2', size=size.small)
plotshape(tf3only and chartOnLowerTf3 and bull_tf3, title='Buy TF3', style=shape.arrowup, location=location.belowbar, color=color.green, text='Buy TF3', size=size.small)
plotshape(tf3only and chartOnLowerTf3 and bear_tf3, title='Sell TF3', style=shape.arrowdown, location=location.abovebar, color=color.red, text='Sell TF3', size=size.small)
 
// -----------------------------------------------------------
// Alerts
if (currentonly and bull)
    alert(syminfo.ticker + " Buy on " + "Timeframe: " + timeframe.period, repaint ? alert.freq_once_per_bar : alert.freq_once_per_bar_close)
if (currentonly and bear)
    alert(syminfo.ticker + " Sell on " + "Timeframe: " + timeframe.period, repaint ? alert.freq_once_per_bar : alert.freq_once_per_bar_close)
if (tf1only and bull_tf1 and not bull)
    alert(syminfo.ticker + " Buy on " + "Timeframe: " + tf1, alert.freq_once_per_bar)
if (tf1only and bear_tf1 and not bear)
    alert(syminfo.ticker + " Sell on " + "Timeframe: " + tf1, alert.freq_once_per_bar)
if (tf2only and bull_tf2 and not bull)
    alert(syminfo.ticker + " Buy on " + "Timeframe: " + tf2, alert.freq_once_per_bar)
if (tf2only and bear_tf2 and not bear)
    alert(syminfo.ticker + " Sell on " + "Timeframe: " + tf2, alert.freq_once_per_bar)
if (tf3only and bull_tf3 and not bull)
    alert(syminfo.ticker + " Buy on " + "Timeframe: " + tf3, alert.freq_once_per_bar)
if (tf3only and bear_tf3 and not bear)
    alert(syminfo.ticker + " Sell on " + "Timeframe: " + tf3, alert.freq_once_per_bar)

← 返回列表