> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hoox.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# TradingView Webhook Integration

> How to configure TradingView alerts, write copy-paste Pine Script v5 indicators, and link webhook signals to your edge gateway.

Connecting **TradingView Alerts** directly to your Hoox edge gateway is the most common way to automate your trading strategies. By combining TradingView's advanced charting engines with Hoox's low-latency edge execution, you can trigger orders instantly based on mathematical indicators, chart patterns, or custom Pine Script v5 logic.

This tutorial walks you through writing a Pine Script v5 script, setting up a TradingView webhook alert, and testing executions.

***

## 💻 Step 1: Writing a Pine Script v5 Signal Indicator

To trigger clean trade alerts, use TradingView's **Pine Script v5**. Below is a copy-paste indicator script that evaluates a Moving Average Cross strategy and generates standardized trade alert signals:

```pinescript theme={null}
//@version=5
indicator("Hoox EMA Cross Signals", overlay=true)

// 1. Inputs & Parameters
fastLength = input.int(9, title="Fast EMA Length")
slowLength = input.int(21, title="Slow EMA Length")

// 2. Indicators Calculation
fastEMA = ta.ema(close, fastLength)
slowEMA = ta.ema(close, slowLength)

plot(fastEMA, color=color.blue, title="Fast EMA")
plot(slowEMA, color=color.orange, title="Slow EMA")

// 3. Trade Conditions
longCondition = ta.crossover(fastEMA, slowEMA)
shortCondition = ta.crossunder(fastEMA, slowEMA)

plotshape(longCondition, title="Long Cross", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(shortCondition, title="Short Cross", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)

// 4. Alert Payloads Construction (JSON compliant)
longAlertJson = '{"apiKey": "your-hoox-webhook-passkey", "exchange": "bybit", "action": "LONG", "symbol": "BTCUSDT", "quantity": 0.001, "leverage": 10}'
shortAlertJson = '{"apiKey": "your-hoox-webhook-passkey", "exchange": "bybit", "action": "SHORT", "symbol": "BTCUSDT", "quantity": 0.001, "leverage": 10}'

// 5. Trigger Alerts
if (longCondition)
    alert(longAlertJson, alert.freq_once_per_bar_close)

if (shortCondition)
    alert(shortAlertJson, alert.freq_once_per_bar_close)
```

<Tip>
  Replace `"your-hoox-webhook-passkey"` in your Pine Script with the actual
  passkey configured in your `CONFIG_KV` store (`webhooks:api_key`). This is a
  critical security step—the gateway will reject any alerts with mismatched keys
  with a `401 Unauthorized` error.
</Tip>

***

## 🔔 Step 2: Creating the TradingView Webhook Alert

Once your script is saved and added to your chart:

1. Click the **Alarm Clock (Alerts)** icon on the top right panel in TradingView.
2. Select **Condition**: Choose your indicator `"Hoox EMA Cross Signals"` and select `"Any Alert Function Call"` (this directs TradingView to parse the custom JSON payloads from the `alert()` functions inside your script).
3. Under **Expiration**: Select your alert lifespan (Premium accounts support Open-Ended alerts).
4. Navigate to the **Notifications** Tab:
   * Check the **Webhook URL** box.
   * Paste your public Hoox Gateway endpoint URL:
     `https://hoox.alpha-trading.workers.dev/webhook`
5. Navigate to the **Settings** Tab:
   * In the **Message** box, type: `{{strategy.order.alert_message}}` (if using a Backtesting Strategy) or leave it blank (if using an Indicator, as the JSON payload is already defined inside our `alert()` function).
6. Click **Create**.

***

## 🔍 Step 3: Verifying Execution in Production

When the Moving Average crossover occurs on your chart:

1. TradingView compiles the JSON payload and POSTs it to your edge gateway.
2. The `hoox` gateway validates the signature and routes the order to Bybit.
3. Check the transaction directly in your terminal:
   ```bash theme={null}
   hoox monitor trades
   ```
4. Stream live gateway telemetry logs to verify execution latency:
   ```bash theme={null}
   hoox logs tail hoox
   ```

***

## 🛠️ Webhook Troubleshooting Runbook

| Symptom / Error                       | Primary Cause                           | Resolution                                                                                                                                    |
| :------------------------------------ | :-------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------- |
| **Alert fires but no trade executes** | Mismatched API keys or WAF block.       | Run `hoox logs tail hoox` to stream traffic. Check if the client IP was dropped by Cloudflare WAF.                                            |
| **`401 Unauthorized`**                | Incorrect `apiKey` in Pine Script.      | Run `hoox config kv get webhooks:api_key` to check your key. Paste the exact string into your Pine Script alert payload.                      |
| **`409 Conflict`**                    | Double-alert fired within the same bar. | Adjust the alert frequency in Pine Script: use `alert.freq_once_per_bar_close` instead of `alert.freq_all` to prevent rapid-fire retries.     |
| **`Invalid Symbol` API reject**       | Symbol format mismatch.                 | Hoox automatically converts variations (like `BTC-USDT` or `btc/usdt`) to `BTCUSDT`, but ensure your script sends standard uppercase symbols. |

### 🔗 Next Steps

* **[Setting Up Telegram Alerts](../tutorials/telegram-bot.mdx)** — Integrate Telegram notifications to get fills pushed directly to your phone.
* **[API Endpoint Reference](../reference/api-endpoints.mdx)** — Review full request schemas and status codes.
