OPEN-SOURCE SCRIPT

Enhanced Swing Trading Strategy

//version=5
strategy("Enhanced Swing Trading Strategy", overlay=true)

// Input Parameters
smaShortLength = input.int(50, title="Short SMA Length")
smaLongLength = input.int(200, title="Long SMA Length")
rsiLength = input.int(14, title="RSI Length")
rsiOverbought = input.int(70, title="RSI Overbought Level")
rsiOversold = input.int(30, title="RSI Oversold Level")
takeProfitPerc = input.float(2.0, title="Take Profit (%)")
stopLossPerc = input.float(1.0, title="Stop Loss (%)")
riskPerc = input.float(1.0, title="Risk per Trade (%)")
alertOnSignal = input.bool(true, title="Enable Alerts")

// Indicators
smaShort = ta.sma(close, smaShortLength)
smaLong = ta.sma(close, smaLongLength)
rsi = ta.rsi(close, rsiLength)
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)

// Conditions
longCondition = ta.crossover(smaShort, smaLong) and rsi > rsiOversold and macdLine > signalLine
shortCondition = ta.crossunder(smaShort, smaLong) and rsi < rsiOverbought and macdLine < signalLine

// Position Sizing based on Risk
capital = strategy.equity
riskAmount = capital * (riskPerc / 100)
stopLossValue = riskAmount / (stopLossPerc / 100)
positionSize = stopLossValue / close

// Entry Signals
if (longCondition)
strategy.entry("Long", strategy.long, qty=positionSize)
if alertOnSignal
alert("Swing Trading: Long Position Opened", alert.freq_once)

if (shortCondition)
strategy.entry("Short", strategy.short, qty=positionSize)
if alertOnSignal
alert("Swing Trading: Short Position Opened", alert.freq_once)

// Exit Conditions
strategy.exit("Take Profit/Stop Loss",
from_entry="Long",
profit=takeProfitPerc,
loss=stopLossPerc)

strategy.exit("Take Profit/Stop Loss",
from_entry="Short",
profit=takeProfitPerc,
loss=stopLossPerc)

// Plotting
plot(smaShort, color=color.blue, title="SMA 50")
plot(smaLong, color=color.red, title="SMA 200")
hline(rsiOverbought, "RSI Overbought", color=color.red)
hline(rsiOversold, "RSI Oversold", color=color.green)
plot(macdLine, color=color.green, title="MACD Line")
plot(signalLine, color=color.orange, title="Signal Line")

// Draw Entry and Exit Labels
if (strategy.position_size > 0)
label.new(bar_index, high, "Long Open", style=label.style_label_up, color=color.green)
if (strategy.position_size < 0)
label.new(bar_index, low, "Short Open", style=label.style_label_down, color=color.red)
Bands and Channels

开源脚本

本着真正的TradingView精神,此脚本的作者已将其开源,以便交易者可以理解和验证它。向作者致敬!您可以免费使用它,但在出版物中重复使用此代码受网站规则约束。 您可以收藏它以在图表上使用。

想在图表上使用此脚本?

免责声明