refactor: Simplify 3ATR EMA strategy by removing stop loss and adding exit signals
This commit is contained in:
parent
eea3013e0d
commit
e54a30c402
@ -1,11 +1,9 @@
|
|||||||
//@version=5
|
//@version=5
|
||||||
strategy(title="3ATR EMA + Risk Mgmt", shorttitle="3ATR-RM", overlay=true, currency=currency.USD, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)
|
strategy(title="3ATR EMA Basic", shorttitle="3ATR-Basic", overlay=true, currency=currency.USD, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)
|
||||||
|
|
||||||
// Inputs
|
// Inputs
|
||||||
ema_length = input.int(21, "EMA Length")
|
ema_length = input.int(21, "EMA Length")
|
||||||
atr_multiplier = input.float(3.0, "ATR Multiplier")
|
atr_multiplier = input.float(3.0, "ATR Multiplier")
|
||||||
risk_percent = input.float(1.0, "Risk %", step=0.1) // 1% risk per trade
|
|
||||||
max_stop_pct = input.float(7.0, "Max Stop %", step=0.1) // 7% max price stop
|
|
||||||
|
|
||||||
// Calculations
|
// Calculations
|
||||||
ema = ta.ema(close, ema_length)
|
ema = ta.ema(close, ema_length)
|
||||||
@ -13,49 +11,20 @@ atr = ta.atr(ema_length)
|
|||||||
upper_band = ema + (atr * atr_multiplier)
|
upper_band = ema + (atr * atr_multiplier)
|
||||||
lower_band = ema - (atr * atr_multiplier)
|
lower_band = ema - (atr * atr_multiplier)
|
||||||
|
|
||||||
// Entry Conditions
|
// Entry Condition
|
||||||
bullish_entry = (close < ema) and
|
bullish_entry = (close < ema) and
|
||||||
(close[1] <= lower_band[1]) and
|
(close[1] <= lower_band[1]) and
|
||||||
(close > close[1])
|
(close > close[1])
|
||||||
|
|
||||||
// Risk Management Variables
|
// Exit Condition (when price crosses upper band)
|
||||||
var float entry_price = na
|
bullish_exit = close > upper_band
|
||||||
var float initial_stop = na
|
|
||||||
var float max_stop_price = na
|
|
||||||
var float trail_price = na
|
|
||||||
var bool trail_active = false
|
|
||||||
|
|
||||||
// Risk-based Position Sizing
|
|
||||||
position_size() =>
|
|
||||||
equity = strategy.equity // Use direct equity value
|
|
||||||
risk_amount = equity * (risk_percent / 100)
|
|
||||||
price_risk = entry_price * (max_stop_pct / 100)
|
|
||||||
(risk_amount / price_risk) // Final position size calculation
|
|
||||||
|
|
||||||
// Trade Execution
|
// Trade Execution
|
||||||
if bullish_entry
|
if bullish_entry
|
||||||
entry_price := close
|
strategy.entry("Long", strategy.long)
|
||||||
initial_stop := close * (1 - max_stop_pct/100)
|
|
||||||
max_stop_price := initial_stop
|
|
||||||
trail_price := na
|
|
||||||
trail_active := false
|
|
||||||
strategy.entry("Long", strategy.long, qty=position_size())
|
|
||||||
|
|
||||||
// Stop Management
|
if bullish_exit
|
||||||
var float current_stop = na
|
strategy.close("Long")
|
||||||
var float display_max_stop = na
|
|
||||||
|
|
||||||
if strategy.position_size > 0
|
|
||||||
trail_price := math.max(high, nz(trail_price, high))
|
|
||||||
trail_stop = trail_price * 0.98
|
|
||||||
current_stop := math.max(max_stop_price, trail_stop)
|
|
||||||
display_max_stop := max_stop_price
|
|
||||||
|
|
||||||
strategy.exit("Stop Exit", "Long", stop=current_stop)
|
|
||||||
|
|
||||||
// Move plots to global scope with conditional visibility
|
|
||||||
plot(strategy.position_size > 0 ? current_stop : na, "Active Stop", color=color.red, style=plot.style_linebr)
|
|
||||||
plot(strategy.position_size > 0 ? display_max_stop : na, "Max Stop", color=color.gray, style=plot.style_circles)
|
|
||||||
|
|
||||||
// Visualization
|
// Visualization
|
||||||
plot(ema, "EMA", color=color.new(#4A90E2, 0))
|
plot(ema, "EMA", color=color.new(#4A90E2, 0))
|
||||||
@ -67,4 +36,11 @@ plotshape(bullish_entry ? low : na,
|
|||||||
location=location.belowbar,
|
location=location.belowbar,
|
||||||
color=color.new(#00FF00, 0),
|
color=color.new(#00FF00, 0),
|
||||||
size=size.small,
|
size=size.small,
|
||||||
title="Bull Signal")
|
title="Bull Entry")
|
||||||
|
|
||||||
|
plotshape(bullish_exit ? high : na,
|
||||||
|
style=shape.triangledown,
|
||||||
|
location=location.abovebar,
|
||||||
|
color=color.new(#FF0000, 0),
|
||||||
|
size=size.small,
|
||||||
|
title="Bull Exit")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user