Pine Script + OpenClaw via Webhook: Tutorial Completo

Pine Script + OpenClaw via webhook: TradingView alerts disparando bot. Tutorial completo.

TradingView tem os melhores charts. Pine Script é linguagem pra criar indicadores e alertas neles. Combinar TradingView (sinal) + OpenClaw (decisão+execução) é setup popular. Este guia cobre.

Arquitetura

TradingView (Pine Script alerta) 
    ↓ webhook HTTPS
Servidor HTTP do OpenClaw 
    ↓ skill call
Exchange/Broker (executa ordem)

Pré-requisitos

  • TradingView assinatura Essential ou superior (webhooks)
  • OpenClaw rodando em VPS com IP público
  • HTTPS configurado (Caddy ou nginx + Let's Encrypt)
  • Skill de exchange já configurada

Passo 1 — Webhook endpoint no OpenClaw

openclaw skill install @openclaw/skill-webhook

Config:

# ~/.openclaw/config.yml
webhook:
  port: 8443
  path: "/tv-alert"
  secret: "${TV_WEBHOOK_SECRET}"  # validado em headers
  rate_limit: "20/min"

Passo 2 — Reverse proxy HTTPS

Caddy (mais simples):

# /etc/caddy/Caddyfile
tv-webhook.seudominio.com {
    reverse_proxy localhost:8443
}

Caddy auto-renova certificate Let's Encrypt.

Passo 3 — Pine Script alerta

//@version=5
strategy("EMA Cross", overlay=true)
ema_fast = ta.ema(close, 20)
ema_slow = ta.ema(close, 50)

if ta.crossover(ema_fast, ema_slow)
    strategy.entry("Long", strategy.long, alert_message='{"action":"buy","symbol":"BTCUSDT","secret":"YOUR_SECRET"}')
    
if ta.crossunder(ema_fast, ema_slow)
    strategy.close("Long", alert_message='{"action":"close","symbol":"BTCUSDT","secret":"YOUR_SECRET"}')

Passo 4 — Configurar alert no TradingView

  1. Adicione script ao chart
  2. Create Alert (sino no topo)
  3. Condition: "EMA Cross" → "Any alert() function call"
  4. Webhook URL: https://tv-webhook.seudominio.com/tv-alert
  5. Message: {{strategy.order.alert_message}}
  6. Save

Passo 5 — Validação do payload no OpenClaw

Skill recebe webhook e valida:

  1. Secret matches?
  2. Symbol whitelisted?
  3. Action válida (buy/sell/close)?
  4. Rate limit OK?
  5. Risk checks (saldo, max ordens abertas)?

Só depois executa.

Segurança crítica

  • Secret obrigatório no payload — TradingView IPs não são white-listáveis confiavelmente
  • HTTPS sempre — HTTP expose secret
  • Rate limit — webhook bug pode disparar tempestade
  • Whitelist de symbols/actions — não execute payload arbitrário
  • Risk checks antes de qualquer ordem

Testing

  1. Test webhook manualmente: curl -X POST https://... -d '{"action":"buy","secret":"..."}'
  2. Verifique logs no OpenClaw — alert recebido?
  3. Trigger alert manual no TradingView (Alert → Test alert)
  4. Confirme execução em conta demo
  5. Operar em conta demo por 1-2 semanas antes de live

Veja também: TradingView setup completo. 🦞