Technical Articles

Pine Script Part 8: Full Auto Bot Setup & API Integration

Dec 04, 202526
#Pine Script#API#Bot#Automation#Webhook#Algo Trading

Pine Script Part 8: Full Automatic Bot & API Integration

This chapter covers the complete automation pipeline used by real algo traders.


1. Full Automation Flow

  1. Strategy → Signal
  2. Alert → Trigger
  3. Webhook → JSON message
  4. Bot → Processes JSON
  5. Bot → Sends order via exchange API
  6. Exchange → Executes order

2. Webhook JSON

{
  "signal": "LONG",
  "symbol": "BTCUSDT",
  "price": "{{close}}",
  "time": "{{timenow}}"
}

3. Python Bot (Core)

from flask import Flask, request
import requests, hmac, hashlib, time

app = Flask(__name__)

API_KEY = "YOUR_API_KEY"
SECRET_KEY = "YOUR_SECRET_KEY"

BASE_URL = "https://api.mexc.com"

def sign(q):
    return hmac.new(SECRET_KEY.encode(), q.encode(), hashlib.sha256).hexdigest()

def send_order(side, symbol, amount):
    ts = int(time.time()*1000)
    q = f"symbol={symbol}&side={side}&type=MARKET&quantity={amount}&timestamp={ts}"
    sig = sign(q)
    url = f"{BASE_URL}/api/v3/order?{q}&signature={sig}"
    h = {"X-MEXC-APIKEY": API_KEY}
    return requests.post(url, headers=h).json()

@app.route('/webhook', methods=['POST'])
def webhook():
    d = request.json
    signal = d["signal"]
    symbol = d["symbol"]
    price = float(d["price"])
    amt = 50/price

    if signal == "LONG": send_order("BUY", symbol, amt)
    if signal == "SHORT": send_order("SELL", symbol, amt)

    return {"ok": True}

app.run(port=5000)

4. Server Setup

sudo apt update
sudo apt install python3 python3-pip
pip install flask requests
python bot.py

5. API Security

  • Disable withdrawals
  • Restrict IP
  • Allow trade only
  • Never expose keys

6. Order Types

  • MARKET
  • LIMIT
  • STOP LOSS
  • TAKE PROFIT

7. alert() JSON

msg = '{"signal":"LONG","symbol":"BTCUSDT","price":' + str.tostring(close) + '}'
alert(msg, alert.freq_once_per_bar_close)

8. Logging

import logging
logging.basicConfig(filename="bot.log", level=logging.INFO)

9. Advanced Bot Features

  • Retry
  • Websockets
  • Multi-symbol
  • Position monitoring
  • Portfolio allocation

10. Premium Benefits

https://sancoqhub.com/go/tradingview


11. Conclusion

You now understand the full automation structure.
Next chapter: Part 9 – Multi-Strategy & Portfolio Algo Systems