luguog commited on
Commit
7b54172
Β·
verified Β·
1 Parent(s): 401af46

Upload 7 files

Browse files
Files changed (7) hide show
  1. README.md +20 -6
  2. action.yaml +8 -0
  3. balance_snapshots.json +0 -0
  4. main.py +86 -0
  5. openapi.yaml +62 -0
  6. requirements.txt +3 -0
  7. trading_log.json +0 -0
README.md CHANGED
@@ -1,10 +1,24 @@
1
  ---
2
- title: Gate5
3
- emoji: πŸ“Š
4
- colorFrom: pink
5
- colorTo: blue
6
  sdk: docker
7
- pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
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