0% found this document useful (0 votes)
50 views4 pages

Trend and Range Detection Strategy with Stop Loss and Take Profit

A trading view strategy

Uploaded by

vctata725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views4 pages

Trend and Range Detection Strategy with Stop Loss and Take Profit

A trading view strategy

Uploaded by

vctata725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

//@version=5

strategy("Trend and Range Detection Strategy with Stop Loss and Take Profit",
overlay=true)

// 输入参数

ema_short_period = input.int(50, title="EMA Short Period", minval=1)

ema_long_period = input.int(200, title="EMA Long Period", minval=1)

stop_loss_percentage = input.float(1.0, title="Stop Loss Percentage", minval=0.1)


// 以百分比形式输入

take_profit_percentage = input.float(2.0, title="Take Profit Percentage",


minval=0.1) // 以百分比形式输入

profit_move_to_breakeven_percentage = 61.8 // 当盈利到达止损金额的 61.8%时,将止损价格移动到成本


// 计算均线

ema_short = ta.ema(close, ema_short_period)

ema_long = ta.ema(close, ema_long_period)

// 计算均线差距

ema_diff = ema_short - ema_long

ema_diff_prev = ema_diff[1]

// 确定趋势和震荡状态

bullish_trend = ema_short > ema_long and ema_diff > ema_diff_prev

bearish_trend = ema_short < ema_long and ema_diff < ema_diff_prev

converging = math.abs(ema_diff) < math.abs(ema_diff_prev)

// 入场和出场条件
enter_long = bullish_trend and not converging

enter_short = bearish_trend and not converging

exit_long = close < ema_short

exit_short = close > ema_short

// 计算止损和止盈价格

stop_loss_long = close * (1 - stop_loss_percentage / 100)

take_profit_long = close * (1 + take_profit_percentage / 100)

stop_loss_short = close * (1 + stop_loss_percentage / 100)

take_profit_short = close * (1 - take_profit_percentage / 100)

// 多头交易

if (enter_long)

strategy.entry("Long", strategy.long)

strategy.exit("Take Profit/Stop Loss", "Long", stop=stop_loss_long,


limit=take_profit_long)

if (exit_long)

strategy.close("Long")

// 空头交易

if (enter_short)

strategy.entry("Short", strategy.short)

strategy.exit("Take Profit/Stop Loss", "Short", stop=stop_loss_short,


limit=take_profit_short)

if (exit_short)
strategy.close("Short")

// 追踪止损逻辑

var float entry_price_long = na

var float entry_price_short = na

if (strategy.opentrades > 0 and strategy.opentrades.entry_id(0) == "Long")

entry_price_long := strategy.opentrades.entry_price(0)

profit_target_long = entry_price_long + (entry_price_long - stop_loss_long) *


(profit_move_to_breakeven_percentage / 100)

if (close >= profit_target_long)

strategy.exit("Move SL to Breakeven", "Long", stop=entry_price_long)

if (strategy.opentrades > 0 and strategy.opentrades.entry_id(0) == "Short")

entry_price_short := strategy.opentrades.entry_price(0)

profit_target_short = entry_price_short - (stop_loss_short - entry_price_short) *


(profit_move_to_breakeven_percentage / 100)

if (close <= profit_target_short)

strategy.exit("Move SL to Breakeven", "Short", stop=entry_price_short)

// 绘制均线

plot(ema_short, color=color.blue, title="EMA 50")

plot(ema_long, color=color.red, title="EMA 200")

// 显示状态

bgcolor(bullish_trend and not converging ? color.new(color.green, 90) : na,


title="Bullish Trend")

bgcolor(bearish_trend and not converging ? color.new(color.red, 90) : na,


title="Bearish Trend")

bgcolor(converging ? color.new(color.yellow, 90) : na, title="Converging/Range")

You might also like