How to write indicators in TradingView with examples and trade settings
In this article, you will learn how to write an indicator in TradingView and implement it step by step. It will also cover important points regarding security and connecting to exchanges.

How to write indicators in TradingView with examples and trade settings

This article will teach you how to write indicators in TradingView and connect them to exchanges using Pine Script. Implementation details, security, and technical tips for safe and effective trading are also discussed.
0 Shares
0
0
0
0

 

How to write indicators and strategies in TradingView and connect it to an exchange?

This comprehensive and practical guide explains step-by-step how to write indicators and strategies with Pine Script version 5, create webhook alarms in TradingView, set up a secure webhook server with Flask and ccxt, and technical/security tips and location selection to reduce latency. This article is suitable for traders, developers, and operations teams.

 

Part 1 — Getting Started with Pine Script (Version 5)

Pine Script version 5 has new features and advanced functions. To get started:

  • Choose version: From //@version=5 Use.
  • Two main types: indicator (for display on the chart and signal generation) and strategy (For backtesting and executing hypothetical orders).
  • Input variables: From the function input Use for user-modifiable parameters.
  • Alarms: From alertcondition Use it to define usable alarm conditions in TradingView.

 

Simple indicator example (EMA Crossover + RSI filter)

//@version=5
indicator("EMA Crossover + RSI Filter", overlay=true)

// Inputs
shortLen = input.int(9, "EMA Short Length")
longLen  = input.int(21, "EMA Long Length")
rsiLen   = input.int(14, "RSI Length")
rsiThresh = input.int(50, "RSI Threshold")

// Price
price = close

// Indicators
emaShort = ta.ema(price, shortLen)
emaLong  = ta.ema(price, longLen)
rsi = ta.rsi(price, rsiLen)

// Signals
bull = ta.crossover(emaShort, emaLong) and rsi > rsiThresh
bear = ta.crossunder(emaShort, emaLong) and rsi < rsiThresh

plot(emaShort, color=color.blue)
plot(emaLong, color=color.orange)
plotshape(bull, title="Long", location=location.belowbar, color=color.green, style=shape.labelup, text="LONG")
plotshape(bear, title="Short", location=location.abovebar, color=color.red, style=shape.labeldown, text="SHORT")

// Alerts
alertcondition(bull, title="Long Alert", message="LONG_SIGNAL")
alertcondition(bear, title="Short Alert", message="SHORT_SIGNAL")

Practical tips: The alarm message should be in JSON format or a specific structure so that the web server can easily parse it; for example: {"signal":"LONG","symbol":"BTCUSDT","timeframe":"1m"}.

 

Simple strategy example with risk management

//@version=5
strategy("EMA Crossover Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=1)

// inputs ...
// (same indicators as above)
shortLen = input.int(9, "EMA Short Length")
longLen  = input.int(21, "EMA Long Length")
rsiLen   = input.int(14, "RSI Length")
rsiThresh = input.int(50, "RSI Threshold")

price = close
emaShort = ta.ema(price, shortLen)
emaLong  = ta.ema(price, longLen)
rsi = ta.rsi(price, rsiLen)

bull = ta.crossover(emaShort, emaLong) and rsi > rsiThresh
bear = ta.crossunder(emaShort, emaLong) and rsi < rsiThresh

if bull
    strategy.entry("Long", strategy.long)
if bear
    strategy.entry("Short", strategy.short)

// Example Stop Loss / Take Profit
strategy.exit("Exit Long", from_entry="Long", loss=100, profit=200)
strategy.exit("Exit Short", from_entry="Short", loss=100, profit=200)

Explanation: For more accurate backtesting than strategy Use and parameters stop loss and take-profit And adjust the position size.

 

Part 2 — Structure of Alarms and Webhooks in TradingView

After adding an indicator or strategy to the chart, create a new alert from the Alerts menu. In the settings:

  • Alert type: Option Webhook URL Select and enter your server address (e.g. https://trade.example.com/webhook).
  • Message: Use standard JSON; include fields symbol, action, price, timeframe, uuid and timestamp.

Alarm message example:

{"symbol":"BTCUSDT","action":"LONG","price":"${close}","timeframe":"1m","strategy":"EMA_CROSS_V1"}

 

Part 3 — Setting up a web server to receive alarms and send orders to the exchange

Proposed architecture:

  • TradingView (Alert) → HTTPS Webhook (NGINX + Flask/Node) → Bot (Business Logic) → Exchange API (ccxt or SDK)

Installing prerequisites (example for Ubuntu)

sudo apt update
sudo apt install -y python3 python3-venv python3-pip
python3 -m venv venv
source venv/bin/activate
pip install flask ccxt

Sample app.py with Flask and ccxt

from flask import Flask, request, jsonify
import ccxt, os, hmac, hashlib, time

app = Flask(__name__)

API_KEY = os.getenv("EXCHANGE_API_KEY")
API_SECRET = os.getenv("EXCHANGE_API_SECRET")

exchange = ccxt.binance({
    'apiKey': API_KEY,
    'secret': API_SECRET,
    'enableRateLimit': True,
})

@app.route('/webhook', methods=['POST'])
def webhook():
    data = request.json
    # simple validation: check data presence (use HMAC in production)
    if not data:
        return jsonify({"error":"no data"}),400

    symbol = data.get("symbol")
    action = data.get("action")
    qty = data.get("qty", 0.001)

    try:
        if action == "LONG":
            order = exchange.create_market_buy_order(symbol, qty)
        elif action == "SHORT":
            order = exchange.create_market_sell_order(symbol, qty)
        else:
            return jsonify({"error":"unknown action"}),400
        return jsonify(order)
    except Exception as e:
        return jsonify({"error":str(e)}),500

 

Part 4 — Secure Server Deployment (Linux/Docker/systemd)

Recommendation: Use Docker Compose with nginx as a reverse proxy and certbot for TLS.

docker-compose.yml example

version: '3.8'
services:
  app:
    build: .
    restart: always
    environment:
      - EXCHANGE_API_KEY
      - EXCHANGE_API_SECRET
  nginx:
    image: nginx:stable
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./certs:/etc/letsencrypt
    ports:
      - "80:80"
      - "443:443"

Example systemd unit to run without Docker

[Unit]
Description=Trading Webhook Bot
After=network.target

[Service]
User=trade
WorkingDirectory=/home/trade/app
Environment=EXCHANGE_API_KEY=your_key_here
Environment=EXCHANGE_API_SECRET=your_secret_here
ExecStart=/home/trade/venv/bin/gunicorn -w 4 -b 127.0.0.1:5000 app:app
Restart=always

[Install]
WantedBy=multi-user.target

Sample administrative commands:

sudo systemctl daemon-reload
sudo systemctl enable --now tradebot.service
sudo ufw allow 443/tcp
sudo ufw allow 80/tcp

 

Section 5 — Security and Stability Tips

  • Secure storage of API Key: From the environment variable, file with chmod 600 Or use Secret Manager.
  • TLS: Use a valid certificate (certbot + nginx) for HTTPS.
  • Message confirmation: Use HMAC-SHA256 to verify incoming TradingView messages.
  • Limitations and Retry: Implement rate limiting and retry/backoff algorithms.
  • Monitoring and logging: Use Prometheus + Grafana or ELK to monitor health and response times.
  • Protection against attacks: Use Fail2Ban, iptables, and Anti-DDoS or CDN services for public endpoints.

 

Section 6 — Backtesting, Optimization, and Forward Testing

  • Backtest: From functions strategy.* Use Pine for historical analysis, but be aware of Pine's parametric limitations.
  • Optimization: Test parameters manually or with external scripts (e.g. generating multiple charts with different parameters).
  • Forward-test: Always test on a demo account or with a small volume on a low-latency VPS after backtesting.
  • Slippage and Latency Measurement: Record and analyze the time of receipt of the webhook and the time of order execution on the exchange.

 

Section 7 — Comparing Datacenter Locations for Trading

Data center location can make every millisecond count in the success of high-frequency strategies. Location selection should be based on exchanges and BGP routes.

  • Europe (Frankfurt, Amsterdam): Suitable for European exchanges and some liquidity funds.
  • Eastern US (New York, NJ): Suitable for Coinbase and related markets in New York.
  • Asia (Singapore, Tokyo, Hong Kong): Suitable for Binance Asia, OKX and Asian markets.

If you trade mostly on Binance or Asian exchanges, a VPS in Singapore or Tokyo is more suitable; for US exchanges, a New York/New Jersey location will work better.

 

Section 8 — Recommended Server Configurations for Trading

VPS for Trading (Low-Latency)

  • CPU: 2-4 high-frequency vCPUs
  • RAM: 4-8 GB
  • Storage: NVMe SSD 50-100GB
  • Network: low-latency/1Gbps, BGP/Anycast if needed

Heavy computing/backtesting server

  • CPU: 8+ vCPUs
  • RAM: 32+ GB
  • Storage: NVMe 1TB
  • GPU: If ML/AI is needed to process models

Additionally, use security features such as Anti-DDoS, Private Networking, and Firewall Management to increase service stability and security.

 

Section 9 — Practical tips for reducing risk in trading automation

  • Stop Loss and Maximum Exposure: Set a stop loss and maximum exposure amount for each order.
  • API Account Status: Check restrictions and KYC status.
  • Circuit breaker: Stop the bot if errors increase or latency increases significantly.
  • Health monitoring: Monitor uptime, latency, and fills status and set alerts for the support team.

 

Conclusion

This guide covered the main steps from writing indicators and strategies in Pine Script (version 5), creating webhook alarms in TradingView, setting up a secure webhook server with Flask/ccxt, to important security considerations, choosing a data center location, and recommended server configurations.

If you need infrastructure solutions including low-latency VPS, anti-DDoS servers, or graphics server services for heavy processing, our company provides these facilities with over 85 global locations and can set up and optimize your infrastructure.

 

Frequently Asked Questions

You May Also Like