Two supported alert approaches for TradingView strategies: alert_message on orders, or explicit alert() calls.
alert_message (strategy.entry / strategy.exit)
Recommended
alert_message.// ===== BEGIN: AlgoWay JSON via alert_message (strategy.entry/strategy.exit) =====
//@version=5
strategy("AlgoWay Strategy Order Alerts (alert_message)", overlay=true, pyramiding=0, process_orders_on_close=true)
// --- AlgoWay toggles ---
aw_enabled = input.bool(true, "AlgoWay Alerts Enabled")
platform = input.string("metatrader5", "Trading Platform",
options=["metatrader5","tradelocker","matchtrader","dxtrade","ctrader","capitalcom","alpaca","tradovate","bybit","binance","okx","bitmex","bitmart","bitget","bingx"])
// --- Fixed SL/TP (pips) ---
sl_pips = input.int(50, "Stop Loss (pips)")
tp_pips = input.int(100, "Take Profit (pips)")
// --- Order size ---
os_group = "ORDER SIZE (for entries)"
os_type = input.string("Percent of equity", "Order size type",
options=["Percent of equity","Cash","Contracts"],
group=os_group)
os_value = input.float(10.0, "Order size value", step=0.01, group=os_group)
f_calc_contracts() =>
float qty = na
if os_type == "Percent of equity"
qty := (strategy.equity * (os_value / 100.0)) / close
else if os_type == "Cash"
qty := os_value / close
else
qty := os_value
qty := na(qty) or close <= 0 ? 0.0 : qty
qty
f_contracts_str(qty) =>
str.format("{0,number,0.000}", qty)
// --- JSON builders (same field names as your alert() variant) ---
aw_entry_json(id, action, qtyStr, slStr, tpStr) =>
'{ "platform_name":"' + platform + '","ticker":"' + syminfo.ticker +
'","order_id":"' + id + '","order_action":"' + action +
'","order_contracts":"' + qtyStr +
'","stop_loss":"' + slStr + '","take_profit":"' + tpStr + '" }'
aw_flat_json(id, qtyStr) =>
'{ "platform_name":"' + platform + '","ticker":"' + syminfo.ticker +
'","order_id":"' + id + '","order_action":"flat","order_contracts":"' + qtyStr + '" }'
// --- Example signals (replace with your own logic) ---
fast = ta.sma(close, 10)
slow = ta.sma(close, 20)
longSignal = ta.crossover(fast, slow)
shortSignal = ta.crossunder(fast, slow)
// --- ENTRY (with alert_message) ---
if aw_enabled and longSignal
float qty = f_calc_contracts()
string qtyStr = f_contracts_str(qty)
// SL/TP based on entry bar price (exit order will later be maintained off avg price below)
float sl0 = close - sl_pips * syminfo.mintick
float tp0 = close + tp_pips * syminfo.mintick
strategy.entry("Long", strategy.long, qty=qty,
alert_message=aw_entry_json("Long", "buy", qtyStr, str.tostring(sl0), str.tostring(tp0)))
if aw_enabled and shortSignal
float qty = f_calc_contracts()
string qtyStr = f_contracts_str(qty)
float sl0 = close + sl_pips * syminfo.mintick
float tp0 = close - tp_pips * syminfo.mintick
strategy.entry("Short", strategy.short, qty=qty,
alert_message=aw_entry_json("Short", "sell", qtyStr, str.tostring(sl0), str.tostring(tp0)))
// --- EXIT (fixed SL/TP from actual average entry price) ---
// Long RM
if aw_enabled and strategy.position_size > 0
float ep = strategy.position_avg_price
float sl = ep - sl_pips * syminfo.mintick
float tp = ep + tp_pips * syminfo.mintick
string qStr = f_contracts_str(math.abs(strategy.position_size))
strategy.exit("Long RM", from_entry="Long", stop=sl, limit=tp,
alert_message=aw_flat_json("Long", qStr))
// Short RM
if aw_enabled and strategy.position_size < 0
float ep = strategy.position_avg_price
float sl = ep + sl_pips * syminfo.mintick
float tp = ep - tp_pips * syminfo.mintick
string qStr = f_contracts_str(math.abs(strategy.position_size))
strategy.exit("Short RM", from_entry="Short", stop=sl, limit=tp,
alert_message=aw_flat_json("Short", qStr))
// ===== END: AlgoWay JSON via alert_message (strategy.entry/strategy.exit) =====
alert() callsf_send_entry(...) / f_send_flat(...) from your own entry/exit logic.// ===== BEGIN: AlgoWay JSON Alerts (alert()) =====
// PROVIDER (AlgoWay only)
provider = input.string("AlgoWay", "Automation Provider",
options=["Off","AlgoWay"],
tooltip="Off = no alerts.\nAlgoWay = send JSON via alert().")
aw_enabled = provider == "AlgoWay"
// PLATFORM (AlgoWay)
platform = input.string("metatrader5", "Trading Platform",
options=["metatrader5","tradelocker","matchtrader","dxtrade","ctrader","capitalcom","alpaca","tradovate","bybit","binance","okx","bitmex","bitmart","bitget","bingx"],
tooltip="Sent as platform_name in AlgoWay JSON.")
// SL/TP/TRAIL MODE (Off / Strategy / Alert)
rm_mode = input.string("Off", "SL/TP/TRAIL Mode",
options=["Off","Strategy","Alert"],
tooltip="Off = no SL/TP/TRAIL in strategy and no SL/TP/TRAIL in JSON.\nStrategy = apply SL/TP/TRAIL inside strategy only (JSON without SL/TP/TRAIL).\nAlert = include SL/TP/TRAIL in AlgoWay JSON only (strategy does not apply SL/TP/TRAIL).")
rm_off = rm_mode == "Off"
rm_strategy = rm_mode == "Strategy"
rm_alert = rm_mode == "Alert"
// SL/TP/TRAIL inputs
sl_pips = input.int(50, "Stop Loss (pips)",
tooltip="Used when SL/TP/TRAIL Mode is Strategy or Alert.")
tp_pips = input.int(100, "Take Profit (pips)",
tooltip="Used when SL/TP/TRAIL Mode is Strategy or Alert.")
trail_pips = input.int(100, "Trailing (pips)",
tooltip="Used when SL/TP/TRAIL Mode is Strategy or Alert.\nIf 0 -> trailing disabled.")
// ORDER SIZE (for alerts & entries)
os_group = "ORDER SIZE (for alerts & entries)"
os_type = input.string("Percent of equity", "Order size type",
options=["Percent of equity","Cash","Contracts"],
group=os_group)
os_value = input.float(10.0, "Order size value", step=0.01, group=os_group,
tooltip="Percent of equity = %; Cash = account currency; Contracts = contracts quantity.")
f_calc_contracts() =>
float qty = na
if os_type == "Percent of equity"
qty := (strategy.equity * (os_value / 100.0)) / close
else if os_type == "Cash"
qty := os_value / close
else
qty := os_value
qty := na(qty) or close <= 0 ? 0.0 : qty
qty
f_contracts_str(qty) =>
str.format("{0,number,0.000}", qty)
// Entry price helper (used for SL/TP prices)
f_entry_price() =>
strategy.position_size != 0 ? strategy.position_avg_price : close
entry_price = f_entry_price()
sl_price_long = entry_price - sl_pips * syminfo.mintick
tp_price_long = entry_price + tp_pips * syminfo.mintick
sl_price_short = entry_price + sl_pips * syminfo.mintick
tp_price_short = entry_price - tp_pips * syminfo.mintick
// JSON BUILDERS (AlgoWay)
algoway_entry_full(id, action, qty, sl, tp, trail) =>
'{ "platform_name":"' + platform + '","ticker":"' + syminfo.ticker + '","order_id":"' + id + '","order_action":"' + action + '","order_contracts":"' + qty + '","stop_loss":"' + sl + '","take_profit":"' + tp + '","trailing_pips":"' + trail + '" }'
algoway_entry_basic(id, action, qty, price) =>
'{ "platform_name":"' + platform + '","ticker":"' + syminfo.ticker + '","order_id":"' + id + '","order_action":"' + action + '","order_contracts":"' + qty + '","price":"' + price + '" }'
algoway_exit_full(id, qty) =>
'{ "platform_name":"' + platform + '","ticker":"' + syminfo.ticker + '","order_id":"' + id + '","order_action":"flat","order_contracts":"' + qty + '" }'
algoway_exit_basic(id, qty) =>
'{ "platform_name":"' + platform + '","ticker":"' + syminfo.ticker + '","order_id":"' + id + '","order_action":"flat","order_contracts":"' + qty + '" }'
// Keep the same simplistic support flag
supports_risk = platform != "ctrader"
// Alert senders (synced contracts)
f_send_entry(id, action, isLong) =>
if aw_enabled
float qtyContracts = f_calc_contracts()
string qtyStr = f_contracts_str(qtyContracts)
if rm_alert and supports_risk
trailStr = (trail_pips > 0) ? str.tostring(trail_pips) : ""
if isLong
alert(algoway_entry_full(id, action, qtyStr,
str.tostring(sl_price_long),
str.tostring(tp_price_long),
trailStr),
freq=alert.freq_once_per_bar_close)
else
alert(algoway_entry_full(id, action, qtyStr,
str.tostring(sl_price_short),
str.tostring(tp_price_short),
trailStr),
freq=alert.freq_once_per_bar_close)
else
alert(algoway_entry_basic(id, action, qtyStr, str.tostring(close)),
freq=alert.freq_once_per_bar_close)
f_send_flat(id, qtyContracts) =>
if aw_enabled
string qtyStr = f_contracts_str(math.abs(qtyContracts))
if supports_risk
alert(algoway_exit_full(id, qtyStr), freq=alert.freq_once_per_bar_close)
else
alert(algoway_exit_basic(id, qtyStr), freq=alert.freq_once_per_bar_close)
// Usage example (call from your entry/exit logic):
// f_send_entry("Long", "buy", true)
// f_send_entry("Short","sell", false)
// f_send_flat("Long", strategy.position_size)
// f_send_flat("Short", strategy.position_size)
// ===== END: AlgoWay JSON Alerts (alert()) =====