Position size from TradingView · leverage from this app
These are bridge-side records of named layers per (account, symbol, side). MEXC merges everything into one position per side — we remember the layers so close id=L1 can close exactly that slice. Delete entries here only if you've manually closed on MEXC and the ledger got out of sync.
| Account | Symbol | Side | Layer ID | Volume | Avg entry | SL price | Leverage | Updated |
|---|
| When (UTC) | Account | Symbol | Action | Qty | Lev | Status |
|---|
TradingView can't reach localhost. In a separate terminal run one of:
cloudflared tunnel --url http://localhost:8000
or
ngrok http 8000
In the TradingView alert dialog, set Webhook URL to:
<public-url>/webhook/<account-slug>?secret=<webhook-secret>
Both values are shown on each account card. Putting the secret in the URL means you never have to embed it in your alert message.
Option A — Plain text (simplest, recommended):
{{strategy.order.action}} {{ticker}} {{strategy.order.contracts}}
Resolves at runtime to e.g. buy BTCUSDT 1. Format is ACTION SYMBOL QUANTITY, space or comma separated. You can add market/limit and a price after.
Option B — Key=value:
action={{strategy.order.action}} symbol={{ticker}} quantity={{strategy.order.contracts}}
Option C — JSON:
{
"action": "{{strategy.order.action}}",
"symbol": "{{ticker}}",
"quantity": {{strategy.order.contracts}}
}
buy / long — open or add to longsell / short — open or add to shortclose / exit — flatten everything on the symbol (quantity not required)close_long, close_short — flatten one sidealert_messageIf your Pine strategy does:
strategy.entry("Long", strategy.long, alert_message="buy {{ticker}} {{strategy.position_size}}")
then set the alert's Message field to {{strategy.order.alert_message}} and everything flows through.
MEXC expects BTC_USDT. The app normalizes BTCUSDT, BTC/USDT, BTC-USDT, BINANCE:BTCUSDT automatically.
The number after the symbol is your position size in base coin units, just like TradingView shows.
long BTCUSDT 2.2 id=L1 # open 2.2 BTC long short SOLUSDT 100 id=S1 # open 100 SOL short buy ETHUSDT 0.5 id=L1 # 0.5 ETH long
Bridge fetches MEXC's contractSize per symbol and converts to contracts internally. No need to know that BTC is 0.0001 BTC/contract or DOGE is 1000 DOGE/contract — it's handled.
Other sizing modes (use whichever fits your strategy):
# A. Risk-based — bridge reads live USDT balance, sizes for given risk buy BTCUSDT id=L1 risk_pct=1 sl_price=68500 # B. USD notional — fixed dollar size of position buy BTCUSDT id=L1 qty_usd=200 # C. Raw MEXC contracts (legacy, advanced) buy BTCUSDT id=L1 qty_contracts=42
Pass sl_price=<price> with any entry to register a stop-MARKET on MEXC sized to that layer. The SL lives on the exchange so it survives bridge restarts. To trail, send update_sl alerts as your strategy moves the SL:
buy BTCUSDT 0.5 id=L1 sl_price=68500 # initial SL at 68500 trail BTCUSDT id=L1 sl_price=69000 # later: move SL up to 69000 trail BTCUSDT id=L1 sl_price=69500 # again as price runs close BTCUSDT # exit all (cancels open SLs first)
update_sl / trail / move_sl all do the same thing: cancel the layer's existing SL plan-order and place a new one at the requested price. Backwards moves (worse SL) are silently ignored as a safety net.
Each layer's SL is independent. close always cancels all SLs before flattening, so you don't get an orphan stop firing on an empty position.
//@version=5
// Match MEXC API fees + add some slippage for a realistic backtest:
strategy("Risk Pyramid + Trail", overlay=true, pyramiding=3,
default_qty_type=strategy.fixed, initial_capital=10000,
commission_type=strategy.commission.percent, commission_value=0.05,
slippage=2)
// ===== INPUTS =====
riskPct = input.float(1.0, "Risk % per layer", minval=0.1)
atrLen = input.int(14, "ATR length")
atrMult = input.float(1.5, "Initial SL ATR mult")
trailMult = input.float(1.0, "Trailing SL ATR mult")
// ===== STATE =====
atrVal = ta.atr(atrLen)
// SL price for a long entry on this bar (bridge will register it on MEXC):
slLong = close - atrVal * atrMult
// Backtest sizing — same formula the bridge runs live, off strategy.equity:
slDist = close - slLong
qtyL = (strategy.equity * riskPct / 100) / slDist
// Trailing SL — moves up as price moves up, never down:
var float trailL1 = na, var float trailL2 = na, var float trailL3 = na
trailLong = close - atrVal * trailMult
// ===== SIGNALS =====
longSig = ta.crossover(ta.sma(close, 10), ta.sma(close, 30))
exitSig = ta.crossunder(ta.sma(close, 10), ta.sma(close, 30))
// ===== ENTRIES =====
slStr = str.tostring(slLong, format.mintick)
if longSig and strategy.opentrades == 0
trailL1 := slLong
strategy.entry("L1", strategy.long, qty=qtyL,
alert_message="long {{ticker}} {{strategy.position_size}} id=L1 sl_price=" + slStr)
if longSig and strategy.opentrades == 1
trailL2 := slLong
strategy.entry("L2", strategy.long, qty=qtyL,
alert_message="long {{ticker}} {{strategy.position_size}} id=L2 sl_price=" + slStr)
if longSig and strategy.opentrades == 2
trailL3 := slLong
strategy.entry("L3", strategy.long, qty=qtyL,
alert_message="long {{ticker}} {{strategy.position_size}} id=L3 sl_price=" + slStr)
// ===== TRAILING — fire only when SL moves up =====
if not na(trailL1) and trailLong > trailL1
trailL1 := trailLong
alert("trail {{ticker}} id=L1 sl_price=" + str.tostring(trailLong, format.mintick),
alert.freq_once_per_bar_close)
if not na(trailL2) and trailLong > trailL2
trailL2 := trailLong
alert("trail {{ticker}} id=L2 sl_price=" + str.tostring(trailLong, format.mintick),
alert.freq_once_per_bar_close)
if not na(trailL3) and trailLong > trailL3
trailL3 := trailLong
alert("trail {{ticker}} id=L3 sl_price=" + str.tostring(trailLong, format.mintick),
alert.freq_once_per_bar_close)
// ===== UNIFIED EXIT =====
if exitSig
trailL1 := na
trailL2 := na
trailL3 := na
strategy.close_all(alert_message="close {{ticker}}")
Important: set TradingView alerts to "Once per bar close" for the trail logic to fire cleanly (otherwise you'd spam update_sl on every tick). The strategy's entry alerts naturally fire once per signal.
In the alert Message field, use {{strategy.order.alert_message}} for entries and {{alert_message}} for the standalone alert() calls (trails). For a single alert covering everything, use Pine's alertcondition() or set up multiple alerts on the same chart.
The quantity value is the contract volume sent to MEXC. Leverage only affects margin, not size. MEXC futures volume is in contracts, where 1 contract ≠ 1 base coin for most symbols — calibrate your TradingView strategy's position size against MEXC's contractSize for the pair before going live.