OPEN-SOURCE SCRIPT

Stochastic Cross Strategy

//version=5
// Stochastic Cross Strategy - Buy on K crossing above D, Sell on D crossing above K
strategy("Stochastic Cross Strategy", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// Inputs for Stochastic
len = input.int(14, minval=1, title="Stochastic Length")
smoothK = input.int(3, minval=1, title="Smooth K")
smoothD = input.int(3, minval=1, title="Smooth D")

// Take-Profit and Stop-Loss Ratios
tp_ratio = input.float(1.5, title="Take Profit / Stop Loss Ratio", step=0.1)
sl_pct = input.float(1, title="Stop Loss %", step=0.1) / 100
tp_pct = sl_pct * tp_ratio

// Stochastic Calculations
k = ta.sma(ta.stoch(close, high, low, len), smoothK)
d = ta.sma(k, smoothD)

// Cross Conditions
longCondition = ta.crossover(k, d) // Buy when %K crosses above %D
shortCondition = ta.crossunder(k, d) // Sell when %D crosses above %K

// Execute Buy or Sell Orders
if (longCondition)
strategy.entry("Long", strategy.long, stop=low * (1 - sl_pct), limit=high * (1 + tp_pct))

if (shortCondition)
strategy.entry("Short", strategy.short, stop=high * (1 + sl_pct), limit=low * (1 - tp_pct))

// Plots for Stochastic
plot(k, title="Stoch %K", style=plot.style_line, linewidth=2, color=color.green)
plot(d, title="Stoch %D", style=plot.style_line, linewidth=2, color=color.red)
Stochastic Oscillator

開源腳本

在真正的TradingView精神中,這個腳本的作者以開源的方式發佈,這樣交易員可以理解和驗證它。請向作者致敬!您可以免費使用它,但在出版物中再次使用這段程式碼將受到網站規則的約束。 您可以收藏它以在圖表上使用。

想在圖表上使用此腳本?

免責聲明