tradingview联系代码

indicator("我的脚本", overlay = false )
k_param = input(title="K length", defval = 14)  // 默认时间
d_param= input(title = "D length", defval = 3)  // SMA
rsi_param= input(title = "RSI length", defval = 7)  // rsi是在0-100的区间震动  这里的输入框是震动指标的天数
upper = input(title = "Over Brought", defval = 80)  
lower = input(title = "Over Sold", defval = 20)

// 震荡指标
rsi = ta.rsi(close, rsi_param)  // 这里获取7天的一个震荡指标强度
// 实际震荡指标算法
Stochastic = 100 * (close - ta.lowest(low, k_param)) / (ta.highest(high, k_param) - ta.lowest(low, k_param))  // ta.lowest(low, k_param):最近14天最低点。 low是每天的最低点
SMA = ta.sma(Stochastic, k_param)

plot(upper, color = color.green, linewidth = 2, title = "Over Brought")
plot(lower, color = color.green, linewidth = 2, title = "Over Brought")
plot(rsi, color = rsi > upper ? color.red : rsi < lower ? color.green : color.black, linewidth = 2 )  // 震动指标
plot(Stochastic, color = color.purple, title = "Stochastic")
plot(SMA, color = color.orange, title="SMA")
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © f0711

//@version=5
strategy("SMA", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 99, initial_capital = 10000, currency = currency.USD)

fast = input(defval = 7, title = "fast") 
slow = input(defval = 28, title = "slow")

SMA_fast = ta.sma(close, fast)  // 快线
SMA_slow = ta.sma(close, slow)  // 慢线

// start = timestamp()
p1 = plot(SMA_fast, color = SMA_fast >= SMA_slow ? color.orange : color.black, linewidth = 2)  // 画出快线
p2 = plot(SMA_slow, color = color.blue, linewidth = 2)  // 画出慢线
// fill(p1, p2, SMA_fast >= SMA_slow ? color.orange : color.blue)


longCondition = ta.crossover(SMA_fast, SMA_slow )  // SMA_fast线往SMA_slow线上穿
if (longCondition)
    strategy.entry("Long Id", strategy.long)  // 做多(买入)
    alert("BUY(" + str.tostring(close) + ")", alert.freq_once_per_bar_close)

shortCondition = ta.crossunder(SMA_fast, SMA_slow )  // SMA_fast线往SMA_slow线下穿
if (shortCondition)
    strategy.entry("ShortId", strategy.short)  // 做空(卖出)
    alert("SELL(" + str.tostring(close) + ")", alert.freq_once_per_bar_close) // 警报  参数二是该函数调用仅在实时K线的最后一个脚本迭代期间发生时,在关闭时触发警报


// 固定值的止盈止损
// strategy.exit("Stop Loss", "Long Id", profit = 200, loss = 100)  // 参数一随便写个名字, 参数二就是你上面的买进的名字,参数三就是到了盈利目标就止盈, 参数四到了损失目标就止损

// 按照百分比止盈止损                                   收盘价的百分之5就止盈            收盘价百分之1就止损
strategy.exit("Stop Loss", "Long Id", profit = close * 0.05 / syminfo.mintick, loss = close * 0.01 / syminfo.mintick)