Automated trading
Bitcoin futures trading bot
A TradingView strategy sends the signals. A small Python service turns them into Coinbase futures orders.
A TradingView strategy I wrote in Pine Script watches 12-hour Bitcoin candles and fires a BUY when the close crosses above the open, or a SELL when it crosses below. Each signal hits a Flask endpoint as a webhook. The Python side checks the shared secret, ignores duplicates, looks at the position that's already open, and places a two-contract BTC futures order through Coinbase's Advanced Trade API: long on BUY, short on SELL.
I kept the two halves apart on purpose. The strategy never sees contract sizes, leverage or credentials, so I can change the trading rules or the order logic without touching the other.
- Python
- Flask
- Pine Script
- TradingView webhooks
- Coinbase Advanced Trade
- REST APIs
- BTC futures
How a signal becomes a trade
- TradingViewPine Script strategy fires on a 12-hour close
- WebhookPosts
BUYorSELLwith a shared secret - FlaskValidates the JSON and the secret, normalizes the signal
- Position checkFlat, same side or opposite side decide what happens next
- CoinbaseTwo-contract BTC futures order, response logged
@app.route("/webhook", methods=["POST"])
def tradingview_webhook():
if not request.is_json:
return jsonify({"success": False, "error": "Request must contain JSON."}), 400
data = request.get_json()
if data.get("secret") != WEBHOOK_SECRET:
logging.warning("Rejected webhook with invalid secret.")
return jsonify({"success": False, "error": "Unauthorized."}), 401
signal = (data.get("signal") or "").upper()
if signal not in ["BUY", "SELL"]:
return jsonify({"success": False, "error": "Signal must be BUY or SELL."}), 400
try:
# BUY -> long, SELL -> short, 2 contracts
result = execute_futures_order(signal)
return jsonify({"success": True, "signal": signal, "result": result}), 200
except Exception as error:
logging.exception("Trading execution failed.")
return jsonify({"success": False, "error": str(error)}), 500
- Example payload
{"secret": "…", "signal": "BUY"}- Safeguards
- Webhook-secret validation, duplicate-signal protection, current-position check before every order, dry-run mode, and every exchange response logged.
- Status
- Signals and the Flask service work end to end in dry-run. Live Coinbase execution is the next step, once the futures product IDs and order fields are verified against the current API.


