OPEN-SOURCE SCRIPT

Advanced Trading Strategy

//version=5
indicator("Advanced Trading Strategy", overlay=true)

// === Input Parameters ===
input_length = input.int(14, title="Length for Highs/Lows")
input_risk_reward = input.float(2.0, title="Risk-Reward Ratio")
input_stop_offset = input.float(10, title="Stop Loss Offset (ticks)")
input_take_offset = input.float(20, title="Take Profit Offset (ticks)")
input_alerts = input.bool(true, title="Enable Alerts")

// === Functions for Swings ===
swingHigh(len) =>
high > ta.highest(high[1], len) and high > ta.highest(high[len], len)

swingLow(len) =>
low < ta.lowest(low[1], len) and low < ta.lowest(low[len], len)

// === Swing High and Low Detection ===
var float swing_high = na
var float swing_low = na

if bar_index > input_length
if swingHigh(input_length)
swing_high := high
if swingLow(input_length)
swing_low := low

// === Entry Conditions ===
long_condition = not na(swing_high) and ta.crossover(close, swing_high)
short_condition = not na(swing_low) and ta.crossunder(close, swing_low)

// === Risk Management ===
var float long_entry = na
var float short_entry = na
var float stop_loss = na
var float take_profit = na

if long_condition
long_entry := swing_high
stop_loss := long_entry - input_stop_offset * syminfo.mintick
take_profit := long_entry + input_take_offset * input_risk_reward * syminfo.mintick

if short_condition
short_entry := swing_low
stop_loss := short_entry + input_stop_offset * syminfo.mintick
take_profit := short_entry - input_take_offset * input_risk_reward * syminfo.mintick

// === Plotting ===
// Entry Labels
var label long_label = na
var label short_label = na

if long_condition
if na(long_label)
long_label := label.new(bar_index, swing_high, "Buy", style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white)
else
label.set_xy(long_label, bar_index, swing_high)

if short_condition
if na(short_label)
short_label := label.new(bar_index, swing_low, "Sell", style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white)
else
label.set_xy(short_label, bar_index, swing_low)

// Stop Loss and Take Profit Lines
var line stop_loss_line = na
var line take_profit_line = na

if not na(stop_loss)
if na(stop_loss_line)
stop_loss_line := line.new(bar_index, stop_loss, bar_index + 1, stop_loss, color=color.red, width=2, style=line.style_dotted)
else
line.set_xy1(stop_loss_line, bar_index, stop_loss)
line.set_xy2(stop_loss_line, bar_index + 1, stop_loss)

if not na(take_profit)
if na(take_profit_line)
take_profit_line := line.new(bar_index, take_profit, bar_index + 1, take_profit, color=color.green, width=2, style=line.style_dotted)
else
line.set_xy1(take_profit_line, bar_index, take_profit)
line.set_xy2(take_profit_line, bar_index + 1, take_profit)

// Alerts
alertcondition(long_condition, title="Long Alert", message="Buy signal detected at {{close}}")
alertcondition(short_condition, title="Short Alert", message="Sell signal detected at {{close}}")
Candlestick analysisChart patternsCycles

Script de código aberto

No verdadeiro espírito do TradingView, o autor desse script o publicou como código aberto, para que os traders possam compreendê-lo e analisá-lo. Parabéns ao autor! Você pode usá-lo gratuitamente, mas a reutilização desse código em publicações é regida pelas Regras da Casa. Você pode favoritá-lo para usá-lo em um gráfico.

Quer usar esse script no gráfico?

Aviso legal