admin管理员组文章数量:1391968
I have a pine script strategy that I'm working on (not very good at pine script at all). And one thing I cannot figure out for the life of me, is that my take profits and stop loss line bars are not static, and they connect to previous positions's lines. The two take profits and the stop loss also adjust as if they were entering a new trade, but they're still in the current position.
Any help would greatly appreciated!
//@version=5
strategy("Reversal & Breakout Strategy", overlay=true, pyramiding=2, initial_capital=50000)
// --- Inputs ---
ema9Length = input.int(9, "9 EMA Length", minval=1)
ema20Length = input.int(20, "20 EMA Length", minval=1)
sma50Length = input.int(50, "50 SMA Length", minval=1)
sma200Length = input.int(200, "200 SMA Length", minval=1)
rsiLength = input.int(14, "RSI Length", minval=1)
rsiOverbought = input.int(70, "RSI Overbought", minval=0, maxval=100)
rsiOversold = input.int(30, "RSI Oversold", minval=0, maxval=100)
atrLength = input.int(14, "ATR Length", minval=1)
stopMulti = input.float(0.5, "Stop Loss ATR Multiplier", minval=0.1, step=0.1)
stopLookback = input.int(7, "Stop Loss Lookback", minval=1)
rr1 = input.float(1.0, "Risk:Reward Target 1", minval=0.1, step=0.1)
rr2 = input.float(2.0, "Risk:Reward Target 2", minval=0.1, step=0.1)
target1Percent = input.float(25, "Profit % Target 1", minval=0, maxval=100)
// --- Indicators ---
// Moving Averages
ema9 = ta.ema(close, ema9Length)
ema20 = ta.ema(close, ema20Length)
sma50 = ta.sma(close, sma50Length)
sma200 = ta.sma(close, sma200Length)
// VWAP
vwapValue = ta.vwap(close)
// RSI
rsi = ta.rsi(close, rsiLength)
// ATR
atr = ta.atr(atrLength)
// --- Trend Detection ---
trendUp = close > sma200
trendDown = close < sma200
// --- Reversal Conditions ---
reversalLong = ta.crossover(close, sma50) and rsi < rsiOversold and close < vwapValue and trendUp
reversalShort = ta.crossunder(close, sma50) and rsi > rsiOverbought and close > vwapValue and trendDown
// --- Range Breakout Conditions ---
breakoutLong = ta.crossover(ema9, ema20) and close > vwapValue and trendUp
breakoutShort = ta.crossunder(ema9, ema20) and close < vwapValue and trendDown
// Combine conditions
longCondition = reversalLong or breakoutLong
shortCondition = reversalShort or breakoutShort
// --- Calculate Position Size ---
equityPerPosition = 25000.0 // $50,000 / 2 positions
positionSizeLong = math.floor(equityPerPosition / close)
positionSizeShort = math.floor(equityPerPosition / close)
// --- Stop Loss Calculation (from MACD Strategy) ---
longStop = ta.lowest(low, stopLookback) - (atr * stopMulti)
shortStop = ta.highest(high, stopLookback) + (atr * stopMulti)
// --- Variables to Store Trade Levels ---
var float tradeStop = na
var float tradeTarget1 = na
var float tradeTarget2 = na
var float initialPositionSize = na
// --- Strategy Entries ---
if longCondition
strategy.entry("Long1", strategy.long, qty=positionSizeLong)
strategy.entry("Long2", strategy.long, qty=positionSizeLong)
tradeStop := longStop
stopDistance = close - tradeStop
tradeTarget1 := close + (stopDistance * rr1)
tradeTarget2 := close + (stopDistance * rr2)
initialPositionSize := positionSizeLong * 2
if shortCondition
strategy.entry("Short1", strategy.short, qty=positionSizeShort)
strategy.entry("Short2", strategy.short, qty=positionSizeShort)
tradeStop := shortStop
stopDistance = tradeStop - close
tradeTarget1 := close - (stopDistance * rr1)
tradeTarget2 := close - (stopDistance * rr2)
initialPositionSize := positionSizeShort * 2
// --- Trade Exits ---
if strategy.position_size > 0
strategy.exit("Long Exit 1", "Long1", qty_percent=target1Percent, stop=tradeStop, limit=tradeTarget1)
strategy.exit("Long Exit 2", "Long2", stop=tradeStop, limit=tradeTarget2)
if strategy.position_size < 0
strategy.exit("Short Exit 1", "Short1", qty_percent=target1Percent, stop=tradeStop, limit=tradeTarget1)
strategy.exit("Short Exit 2", "Short2", stop=tradeStop, limit=tradeTarget2)
// --- Move Stop to Break-even ---
if strategy.position_size != 0 and initialPositionSize != na
if math.abs(strategy.position_size) < math.abs(initialPositionSize)
tradeStop := strategy.position_avg_price
tradeTarget1 := na // Clear first target after it's hit
// --- Plotting ---
plot(ema9, title="9 EMA", color=color.blue, linewidth=1)
plot(ema20, title="20 EMA", color=color.orange, linewidth=1)
plot(sma50, title="50 SMA", color=color.purple, linewidth=1)
plot(sma200, title="200 SMA", color=color.red, linewidth=1)
plot(vwapValue, title="VWAP", color=color.gray, linewidth=1)
plot(strategy.position_size != 0 ? tradeStop : na, title="Stop Loss", color=color.red, linewidth=1, style=plot.style_linebr)
plot(strategy.position_size != 0 ? tradeTarget1 : na, title="Take Profit 1", color=color.green, linewidth=1, style=plot.style_linebr)
plot(strategy.position_size != 0 ? tradeTarget2 : na, title="Take Profit 2", color=color.blue, linewidth=1, style=plot.style_linebr)
本文标签: Pine Script Strategy Take Profit and Stop Loss Plotting issueStack Overflow
版权声明:本文标题:Pine Script Strategy Take Profit and Stop Loss Plotting issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744730341a2622011.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论