Upload 7 files
Browse files- README.md +20 -6
- action.yaml +8 -0
- balance_snapshots.json +0 -0
- main.py +86 -0
- openapi.yaml +62 -0
- requirements.txt +3 -0
- trading_log.json +0 -0
README.md
CHANGED
|
@@ -1,10 +1,24 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: gate4
|
| 3 |
+
emoji: π°
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: gray
|
| 6 |
sdk: docker
|
| 7 |
+
sdk_version: "1.0"
|
| 8 |
+
app_file: main.py
|
| 9 |
+
pinned: true
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# Gate4: GPT Actions API for Gate.io
|
| 13 |
+
|
| 14 |
+
## π Endpoints
|
| 15 |
+
- /balance
|
| 16 |
+
- /performance
|
| 17 |
+
- /log_trade
|
| 18 |
+
- /openapi.yaml
|
| 19 |
+
|
| 20 |
+
## π Hugging Face Setup
|
| 21 |
+
|
| 22 |
+
Set required secrets:
|
| 23 |
+
- GATE_API_KEY
|
| 24 |
+
- GATE_API_SECRET
|
action.yaml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: GateGPT
|
| 2 |
+
description: GPT Assistant for tracking and logging Gate.io trades
|
| 3 |
+
schema_version: 1.0.0
|
| 4 |
+
api:
|
| 5 |
+
url: https://luguog--gate4.hf.space/openapi.yaml
|
| 6 |
+
has_user_authentication: false
|
| 7 |
+
auth:
|
| 8 |
+
type: none
|
balance_snapshots.json
ADDED
|
File without changes
|
main.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import HTMLResponse, FileResponse
|
| 3 |
+
import os, time, hmac, hashlib, requests, json
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
GATE_API_KEY = os.getenv("GATE_API_KEY")
|
| 8 |
+
GATE_API_SECRET = os.getenv("GATE_API_SECRET")
|
| 9 |
+
GATE_API_BASE = "https://api.gate.io/api/v4"
|
| 10 |
+
|
| 11 |
+
LOG_FILE = "trading_log.json"
|
| 12 |
+
BAL_FILE = "balance_snapshots.json"
|
| 13 |
+
|
| 14 |
+
@app.get("/")
|
| 15 |
+
def home():
|
| 16 |
+
return HTMLResponse("""
|
| 17 |
+
<html><body>
|
| 18 |
+
<h2>π gate4: Live Gate.io GPT API</h2>
|
| 19 |
+
<p>Endpoints:</p>
|
| 20 |
+
<ul>
|
| 21 |
+
<li>/balance</li>
|
| 22 |
+
<li>/performance</li>
|
| 23 |
+
<li>/log_trade</li>
|
| 24 |
+
<li>/openapi.yaml</li>
|
| 25 |
+
</ul>
|
| 26 |
+
</body></html>
|
| 27 |
+
""")
|
| 28 |
+
|
| 29 |
+
@app.get("/openapi.yaml")
|
| 30 |
+
def get_openapi():
|
| 31 |
+
return FileResponse("openapi.yaml", media_type="text/yaml")
|
| 32 |
+
|
| 33 |
+
@app.get("/balance")
|
| 34 |
+
def get_balance():
|
| 35 |
+
path = "/futures/usdt/accounts"
|
| 36 |
+
method = "GET"
|
| 37 |
+
timestamp = str(int(time.time()))
|
| 38 |
+
body = ""
|
| 39 |
+
query = ""
|
| 40 |
+
sign = sign_request(method, path, query, body, timestamp)
|
| 41 |
+
|
| 42 |
+
headers = {
|
| 43 |
+
"KEY": GATE_API_KEY,
|
| 44 |
+
"Timestamp": timestamp,
|
| 45 |
+
"SIGN": sign
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
url = f"{GATE_API_BASE}{path}"
|
| 49 |
+
res = requests.get(url, headers=headers)
|
| 50 |
+
res.raise_for_status()
|
| 51 |
+
|
| 52 |
+
accounts = res.json()
|
| 53 |
+
total = sum(float(acc.get("available", 0)) for acc in accounts)
|
| 54 |
+
|
| 55 |
+
log_balance(total)
|
| 56 |
+
return {"balance": round(total, 2)}
|
| 57 |
+
|
| 58 |
+
@app.get("/performance")
|
| 59 |
+
def get_performance():
|
| 60 |
+
if not os.path.exists(LOG_FILE):
|
| 61 |
+
return {"summary": "No trades logged yet."}
|
| 62 |
+
|
| 63 |
+
with open(LOG_FILE, "r") as f:
|
| 64 |
+
trades = [json.loads(line) for line in f if line.strip()]
|
| 65 |
+
|
| 66 |
+
total_pnl = sum(t.get("pnl_estimate", 0) for t in trades)
|
| 67 |
+
summary = f"π Total PnL: ${total_pnl:.2f} from {len(trades)} trades."
|
| 68 |
+
for t in trades[-5:]:
|
| 69 |
+
summary += f"\nβ’ {t['action']} {t['contract']} β ${t['pnl_estimate']} ({t['reason']})"
|
| 70 |
+
return {"summary": summary}
|
| 71 |
+
|
| 72 |
+
@app.post("/log_trade")
|
| 73 |
+
async def log_trade(request: Request):
|
| 74 |
+
trade = await request.json()
|
| 75 |
+
with open(LOG_FILE, "a") as f:
|
| 76 |
+
f.write(json.dumps(trade) + "\n")
|
| 77 |
+
return {"status": "logged", "trade": trade}
|
| 78 |
+
|
| 79 |
+
def log_balance(balance):
|
| 80 |
+
entry = {"timestamp": int(time.time()), "balance": balance}
|
| 81 |
+
with open(BAL_FILE, "a") as f:
|
| 82 |
+
f.write(json.dumps(entry) + "\n")
|
| 83 |
+
|
| 84 |
+
def sign_request(method, path, query_string, body, timestamp):
|
| 85 |
+
message = f"{method}\n{path}\n{query_string}\n{body}\n{timestamp}"
|
| 86 |
+
return hmac.new(GATE_API_SECRET.encode(), message.encode(), hashlib.sha512).hexdigest()
|
openapi.yaml
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openapi: 3.1.0
|
| 2 |
+
info:
|
| 3 |
+
title: GateGPT Live API
|
| 4 |
+
version: 1.0.0
|
| 5 |
+
servers:
|
| 6 |
+
- url: https://luguog--gate4.hf.space
|
| 7 |
+
paths:
|
| 8 |
+
/balance:
|
| 9 |
+
get:
|
| 10 |
+
operationId: getBalance
|
| 11 |
+
summary: Get live balance from Gate.io
|
| 12 |
+
responses:
|
| 13 |
+
'200':
|
| 14 |
+
description: Balance response
|
| 15 |
+
content:
|
| 16 |
+
application/json:
|
| 17 |
+
schema:
|
| 18 |
+
type: object
|
| 19 |
+
properties:
|
| 20 |
+
balance:
|
| 21 |
+
type: number
|
| 22 |
+
/performance:
|
| 23 |
+
get:
|
| 24 |
+
operationId: getPerformance
|
| 25 |
+
summary: Get trading PnL summary
|
| 26 |
+
responses:
|
| 27 |
+
'200':
|
| 28 |
+
description: PnL summary
|
| 29 |
+
content:
|
| 30 |
+
application/json:
|
| 31 |
+
schema:
|
| 32 |
+
type: object
|
| 33 |
+
properties:
|
| 34 |
+
summary:
|
| 35 |
+
type: string
|
| 36 |
+
/log_trade:
|
| 37 |
+
post:
|
| 38 |
+
operationId: logTrade
|
| 39 |
+
summary: Log a trade
|
| 40 |
+
requestBody:
|
| 41 |
+
required: true
|
| 42 |
+
content:
|
| 43 |
+
application/json:
|
| 44 |
+
schema:
|
| 45 |
+
type: object
|
| 46 |
+
required:
|
| 47 |
+
- action
|
| 48 |
+
- contract
|
| 49 |
+
- size
|
| 50 |
+
- price
|
| 51 |
+
- pnl_estimate
|
| 52 |
+
- reason
|
| 53 |
+
properties:
|
| 54 |
+
action: { type: string }
|
| 55 |
+
contract: { type: string }
|
| 56 |
+
size: { type: number }
|
| 57 |
+
price: { type: number }
|
| 58 |
+
pnl_estimate: { type: number }
|
| 59 |
+
reason: { type: string }
|
| 60 |
+
responses:
|
| 61 |
+
'200':
|
| 62 |
+
description: Trade logged
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
requests
|
trading_log.json
ADDED
|
File without changes
|