//version=5
strategy("Sakahayang trade", overlay=true, default_qty_type=strategy.percent_of_equity)

// Inputs
sma_length = input.int(100, title="SMA Length")
risk_reward_ratios = input.string("2,3,5", title="Risk/Reward Ratios (comma separated)")
risk_percent = input.float(1, title="Risk Percentage", step=0.1) / 100

// Calculate SMA 100
sma100 = ta.sma(close, sma_length)

// Identify Breakout
breakout_up = ta.crossover(close, sma100)
breakout_down = ta.crossunder(close, sma100)

// Parse risk/reward ratios
risk_reward_array = str.split(risk_reward_ratios, ",")
rr1 = str.tonumber(array.get(risk_reward_array, 0))
rr2 = str.tonumber(array.get(risk_reward_array, 1))
rr3 = str.tonumber(array.get(risk_reward_array, 2))

// Initialize variables for stop loss (SL) and take profit (TP)
var float sl_level = na
var float tp1_level = na
var float tp2_level = na
var float tp3_level = na
var float entry_price = na

// Calculate position size based on risk
calculate_position_size(entry_price, sl_level, risk_percent) =>
risk_amount = strategy.equity * risk_percent
sl_distance = math.abs(entry_price - sl_level)
position_size = risk_amount / sl_distance
position_size

// Place Buy Stop Order
if (breakout_up)
sl_level := low - syminfo.mintick
entry_price := high + syminfo.mintick
tp1_level := high + (high - low) * rr1
tp2_level := high + (high - low) * rr2
tp3_level := high + (high - low) * rr3

position_size = calculate_position_size(entry_price, sl_level, risk_percent)
strategy.entry("Buy Stop", strategy.long, qty=position_size, stop=entry_price, comment="Buy Stop")
strategy.exit("TP1", from_entry="Buy Stop", limit=tp1_level, qty=position_size / 2, stop=sl_level)
strategy.exit("TP2", from_entry="Buy Stop", limit=tp2_level, stop=entry_price) // Move SL to BE
strategy.exit("TP3", from_entry="Buy Stop", limit=tp3_level, stop=entry_price) // Move SL to BE

// Place Sell Stop Order
if (breakout_down)
sl_level := high + syminfo.mintick
entry_price := low - syminfo.mintick
tp1_level := low - (high - low) * rr1
tp2_level := low - (high - low) * rr2
tp3_level := low - (high - low) * rr3

position_size = calculate_position_size(entry_price, sl_level, risk_percent)
strategy.entry("Sell Stop", strategy.short, qty=position_size, stop=entry_price, comment="Sell Stop")
strategy.exit("TP1", from_entry="Sell Stop", limit=tp1_level, qty=position_size / 2, stop=sl_level)
strategy.exit("TP2", from_entry="Sell Stop", limit=tp2_level, stop=entry_price) // Move SL to BE
strategy.exit("TP3", from_entry="Sell Stop", limit=tp3_level, stop=entry_price) // Move SL to BE

// Move SL to Break Even when price reaches 2x SL
if (strategy.opentrades.profit(strategy.opentrades - 1) >= 2 * (high - low))
sl_level := entry_price
strategy.exit("Move to BE", from_entry="Buy Stop", stop=entry_price)
strategy.exit("Move to BE", from_entry="Sell Stop", stop=entry_price)

// Close trade fully when price crosses SMA in the opposite direction
if (strategy.position_size > 0)
if (strategy.position_size > 0 and close < sma100)
strategy.close("Buy Stop")
if (strategy.position_size < 0 and close > sma100)
strategy.close("Sell Stop")

// Plot SMA 100
plot(sma100, title="SMA 100", color=color.blue)

// Plot Buy/Sell Stop Levels
plotshape(series=breakout_up, location=location.belowbar, color=color.green, style=shape.labelup, text="Buy")
plotshape(series=breakout_down, location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell")
Trend Analysis

免責事項