本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=4
strategy(title="Stoch RSI Crossover Strat + EMA", shorttitle="Stoch RSI Cross + EMA Strat", overlay = true, pyramiding = 0, calc_on_order_fills = false, commission_type = strategy.commission.percent, commission_value = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 1, initial_capital=10000, currency=currency.USD)
// Time Range
FromMonth=input(defval=1,title="FromMonth",minval=1,maxval=12)
FromDay=input(defval=1,title="FromDay",minval=1,maxval=31)
FromYear=input(defval=2020,title="FromYear",minval=2017)
ToMonth=input(defval=1,title="ToMonth",minval=1,maxval=12)
ToDay=input(defval=1,title="ToDay",minval=1,maxval=31)
ToYear=input(defval=9999,title="ToYear",minval=2017)
start=timestamp(FromYear,FromMonth,FromDay,00,00)
finish=timestamp(ToYear,ToMonth,ToDay,23,59)
window()=>time>=start and time<=finish?true:false
// See if this bar's time happened on/after start date
afterStartDate = time >= start and time<=finish?true:false
//STOCH RSI
smoothK = input(3, minval=1)
smoothD = input(3, minval=1)
lengthRSI = input(14, minval=1)
lengthStoch = input(14, minval=1)
src = input(close, title="RSI Source")
rsi1 = rsi(src, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)
//ATR
lengthATR = input(title="ATR Length", defval=14, minval=1)
atr = atr(lengthATR)
//MULTI EMA
emasrc = close,
len1 = input(8, minval=1, title="EMA 1")
len2 = input(14, minval=1, title="EMA 2")
len3 = input(50, minval=1, title="EMA 3")
ema1 = ema(emasrc, len1)
ema2 = ema(emasrc, len2)
ema3 = ema(emasrc, len3)
col1 = color.lime
col2 = color.blue
col3 = color.orange
//EMA Plots
//plot(ema1, title="EMA 1", linewidth=1, color=col1)
//plot(ema2, title="EMA 2", linewidth=1, color=col2)
//plot(ema3, title="EMA 3", linewidth=1, color=col3)
crossup = k[0] > d[0] and k[1] <= d[1]
emapos = ema1 > ema2 and ema2 > ema3 and close > ema1
barbuy = crossup and emapos
//plotshape(crossup, style=shape.triangleup, location=location.belowbar, color=color.white)
plotshape(barbuy, style=shape.triangleup, location=location.belowbar, color=color.green)
longloss = sma(open, 1)
//plot(longloss, color=color.red)
//Buy and Sell Factors
profitfactor = input(title="Profitfactor", type=input.float, step=0.1, defval=2)
stopfactor = input(title="Stopfactor", type=input.float, step=0.1, defval=3)
bought = strategy.position_size[1] < strategy.position_size
longcondition = barbuy
if (longcondition) and (afterStartDate) and strategy.opentrades < 1
strategy.entry("Long", strategy.long)
if (afterStartDate) and strategy.opentrades > 0
barsbought = barssince(bought)
profit_level = strategy.position_avg_price + (atr*profitfactor)
stop_level = strategy.position_avg_price - (atr*stopfactor)
strategy.exit("Take Profit/ Stop Loss", "Long", stop=stop_level[barsbought], limit=profit_level[barsbought])