Re: [PRO TIP] The Zero-Leverage Silver Buffer: How to Fund Your Forex Scalping Series Risk-Free
Posted: Thu Sep 03, 2026 8:53 am
To route TradingView alerts to your local execution server, we need to dynamically generate standard JSON payloads inside the script and pass them to the alert_message parameter of the strategy functions.
Because TradingView evaluates strategy() orders internally, you use the built-in placeholder {{strategy.order.alert_message}} in the TradingView UI, which tells the platform to extract the specific JSON string we built in the code and send it as the POST request body.
Here is the updated Pine Script with the JSON payload generator and modified Phase 2 execution block:
Because TradingView evaluates strategy() orders internally, you use the built-in placeholder {{strategy.order.alert_message}} in the TradingView UI, which tells the platform to extract the specific JSON string we built in the code and send it as the POST request body.
Here is the updated Pine Script with the JSON payload generator and modified Phase 2 execution block:
Code: Select all
//@version=5
strategy("Two-Phase Buffer Strategy with Webhooks", overlay=true, initial_capital=10000, margin_long=100, margin_short=100)
// --- Input Parameters ---
silverSym = input.symbol("OANDA:XAGUSD", title="Phase 1: Silver Symbol")
bufferTarget = input.float(50.0, title="Phase 1: Buffer Target (USD)")
forexLotSize = input.float(1.0, title="Phase 2: Forex Lot Size")
// --- State Variables ---
var int tradingPhase = 1
var float riskBufferUSD = 0.0
var float phase2StartEquity = 0.0
var float silverEntryPrice = na
var float silverUnits = 0.0
// --- Fetch Silver Data ---
silverClose = request.security(silverSym, timeframe.period, close)
// --- Webhook JSON Builder ---
// Converts the order action into a clean JSON string for your local server
qtyUnits = forexLotSize * 100000
f_build_payload(_action, _comment) =>
'{"action": "' + _action + '", "symbol": "' + syminfo.ticker + '", "volume": ' + str.tostring(qtyUnits) + ', "comment": "' + _comment + '"}'
//+------------------------------------------------------------------+
//| Phase 1: The Zero-Leverage Silver Long (Modeled) |
//+------------------------------------------------------------------+
if tradingPhase == 1
if na(silverEntryPrice)
silverEntryPrice := silverClose
silverUnits := strategy.equity / silverEntryPrice
if not na(silverEntryPrice)
currentSilverPnL = (silverClose - silverEntryPrice) * silverUnits
if currentSilverPnL >= bufferTarget
riskBufferUSD := currentSilverPnL
phase2StartEquity := strategy.equity + riskBufferUSD
tradingPhase := 2
silverEntryPrice := na
silverUnits := 0.0
//+------------------------------------------------------------------+
//| Phase 2: The "House Money" Transfer & Webhook Execution |
//+------------------------------------------------------------------+
if tradingPhase == 2
// Risk Check: Flush all trades if buffer is depleted
if strategy.equity <= (phase2StartEquity - riskBufferUSD)
// Generate emergency flush JSON
flushPayload = f_build_payload("close_all", "Buffer Depleted: Emergency Flush")
strategy.close_all(comment="Buffer Depleted", alert_message=flushPayload)
tradingPhase := 1
riskBufferUSD := 0.0
// --- CORE SCALPING ALGORITHM ---
fastSMA = ta.sma(close, 10)
slowSMA = ta.sma(close, 20)
// Build specific JSON payloads for each action
buyPayload = f_build_payload("buy", "Phase 2 Long Cross")
sellPayload = f_build_payload("sell", "Phase 2 Short Cross")
closePayload = f_build_payload("close", "Phase 2 Exit")
if strategy.position_size == 0
if ta.crossover(fastSMA, slowSMA)
strategy.entry("Phase 2 Long", strategy.long, qty=qtyUnits, alert_message=buyPayload)
else if ta.crossunder(fastSMA, slowSMA)
strategy.entry("Phase 2 Short", strategy.short, qty=qtyUnits, alert_message=sellPayload)
if strategy.position_size > 0 and ta.crossunder(fastSMA, slowSMA)
strategy.close("Phase 2 Long", alert_message=closePayload)
if strategy.position_size < 0 and ta.crossover(fastSMA, slowSMA)
strategy.close("Phase 2 Short", alert_message=closePayload)
//+------------------------------------------------------------------+
//| Visualizing the Phases on the Chart |
//+------------------------------------------------------------------+
bgcolor(tradingPhase == 1 ? color.new(color.silver, 90) : na, title="Phase 1 Background")
plotshape(tradingPhase == 2 and tradingPhase[1] == 1, style=shape.labelup, location=location.belowbar, color=color.green, text="BUFFER LOCKED", textcolor=color.white, size=size.small)
plotshape(tradingPhase == 1 and tradingPhase[1] == 2, style=shape.labeldown, location=location.abovebar, color=color.red, text="BUFFER LOST", textcolor=color.white, size=size.small)