Theme
Language
webhookpine scripttradingviewtrading botfreeautomation

How to Connect Pine Script to a Trading Bot via Webhooks

How to Connect Pine Script to a Trading Bot via Webhooks
By fomoed TeamApril 12, 20265 min read

What This Guide Covers

If you have a Pine Script strategy on TradingView that generates good signals, you can automate it. Instead of watching alerts and manually placing trades, a webhook bot receives your Pine Script signals and executes them instantly on your exchange.

The flow: Pine Script fires alert → TradingView sends webhook → fomoed receives it → bot places trade on your exchange. Fully automated, no manual intervention needed.

How Pine Script Alerts Work

TradingView alerts can trigger on any condition in your Pine Script code. When you use alertcondition() or the newer alert() function, TradingView evaluates your conditions on their servers and fires the alert whenever the condition becomes true.

The key feature: TradingView can send a webhook (an HTTP POST request to a URL) whenever an alert fires. This webhook carries a payload — a message you define — that contains the trade information.

Step 1: Prepare Your Pine Script

Your Pine Script needs to output clear, parseable signals. The simplest approach uses the alert() function with a JSON payload:

//@version=5
strategy("My Strategy", overlay=true)

// Your entry/exit logic
longCondition = ta.crossover(ta.sma(close, 20), ta.sma(close, 50))
shortCondition = ta.crossunder(ta.sma(close, 20), ta.sma(close, 50))

if (longCondition)
    strategy.entry("Long", strategy.long)
    alert('{"action": "buy", "symbol": "' + syminfo.ticker + '", "price": ' + str.tostring(close) + '}', alert.freq_once_per_bar_close)

if (shortCondition)
    strategy.close("Long")
    alert('{"action": "sell", "symbol": "' + syminfo.ticker + '", "price": ' + str.tostring(close) + '}', alert.freq_once_per_bar_close)

Key points:

  • Use alert.freq_once_per_bar_close to avoid multiple signals per bar
  • Include the action (buy/sell), symbol, and current price in your payload
  • Use valid JSON format so the receiving bot can parse it cleanly

Step 2: Create a Webhook Bot on fomoed

In your fomoed dashboard, create a new bot and select the Webhook strategy. This generates a unique webhook URL for your bot — something like:

https://api.fomoed.com/webhook/bot/abc123xyz

This URL is your bot's "ears" — any properly formatted POST request sent to it triggers trade execution. Keep it private — anyone with the URL can send signals to your bot.

Webhook Bot Configuration

Configure these settings in the webhook bot setup:

SettingPurposeExample
ExchangeWhere trades executeHyperliquid, Binance, Bybit
PairWhat to trade (or dynamic from payload)BTC/USDC
Position sizeHow much per signal$500 or 2% of balance
LeverageLeverage for perp trades3x
Take profitAuto TP on entries2%
Stop lossAuto SL on entries1%

Step 3: Connect TradingView to Your Webhook URL

  1. On TradingView, add your Pine Script strategy to a chart
  2. Right-click on the strategy → "Add Alert"
  3. In the alert dialog, set the condition to your strategy
  4. Under "Notifications," enable Webhook URL
  5. Paste your fomoed webhook URL
  6. In the "Message" field, define your payload format
  7. Set expiration to "Open-ended" for continuous operation
  8. Click "Create"

The Alert Message (Payload)

The message field is what gets sent to your webhook URL. Use this format for fomoed compatibility:

{
  "action": "{{strategy.order.action}}",
  "symbol": "{{ticker}}",
  "price": {{close}},
  "quantity": "{{strategy.order.contracts}}"
}

TradingView replaces the {{placeholders}} with actual values when the alert fires. The result is a clean JSON object your webhook bot can parse and execute.

Step 4: Test the Connection

Before relying on your webhook bot for real trades:

  1. Set the bot to paper trading mode
  2. Fire a test alert from TradingView (use the "Test" button in alert settings)
  3. Check your fomoed dashboard — you should see the signal received
  4. Verify the paper trade was placed correctly (right pair, right direction, right size)

If the test fails, common issues include:

  • Malformed JSON in the alert message (check brackets and quotes)
  • Incorrect webhook URL (copy-paste error)
  • TradingView plan doesn't support webhooks (requires paid plan)

Step 5: Go Live

Once paper tests confirm the connection works:

  1. Switch your webhook bot from paper to live mode
  2. Keep your TradingView alert active (it's already pointing to the right URL)
  3. The next signal from your Pine Script strategy will execute a real trade

Payload Format Reference

fomoed webhook bots accept these fields:

FieldRequiredValues
actionYes"buy", "sell", "close"
symbolNo (uses bot default)"BTCUSDC", "ETHUSDC"
priceNoExecution price (market if omitted)
quantityNo (uses bot default)Position size
takeProfitNo (uses bot default)TP price or percentage
stopLossNo (uses bot default)SL price or percentage

Advanced: Multiple Strategies, One Bot

You can point multiple Pine Script alerts to the same webhook bot — useful if you have signals on different timeframes or different conditions that all trade the same pair. The bot processes each signal independently.

Alternatively, create separate webhook bots for separate strategies, each with their own risk parameters. This gives you independent PnL tracking and risk management per strategy.

Advanced: Custom Alert Conditions

Beyond basic long/short signals, you can send more nuanced payloads:

  • Partial closes: {"action": "sell", "quantity": "50%"}
  • Trailing activation: {"action": "buy", "trailingStop": "2%"}
  • Multiple TPs: Define in the bot config; webhook just triggers entry

TradingView Plan Requirements

One important note: TradingView webhooks require a paid TradingView plan (Pro, Pro+, or Premium). The free TradingView plan doesn't support webhook notifications — only on-screen and email alerts.

However, once you have a TradingView plan, the fomoed webhook bot itself is completely free. No additional subscription needed on the execution side.

Troubleshooting Common Issues

  • "Webhook failed" in TradingView — check that the URL is correct and the fomoed bot is active
  • Signal received but no trade — verify the payload format matches expected fields; check bot is in live mode
  • Duplicate trades — use alert.freq_once_per_bar_close to prevent multiple signals per bar
  • Delayed execution — normal for 1-3 second delay; ensure your strategy accounts for this on fast timeframes

Next Steps

For more detailed webhook bot configurations, read our TradingView webhook auto-trade guide. For a broader overview of webhook strategies and advanced payload handling, check our webhook bots comprehensive guide.

The combination of Pine Script's analytical power and fomoed's execution infrastructure means you can build sophisticated strategies in TradingView and execute them automatically — for free.

Ready to automate your Pine Script strategy? Create your free fomoed account, set up a webhook bot, and connect your TradingView alerts in minutes.