Spaces:
Sleeping
Sleeping
Create api/kucoin.py
Browse files- api/kucoin.py +50 -0
api/kucoin.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import ccxt
|
3 |
+
from typing import Optional, Dict, List, Any
|
4 |
+
from config import SessionManager
|
5 |
+
from models.market_data import MarketData, SymbolInfo
|
6 |
+
|
7 |
+
|
8 |
+
class KuCoinClient:
|
9 |
+
def __init__(self):
|
10 |
+
s = SessionManager()
|
11 |
+
self.client = ccxt.kucoinfutures(
|
12 |
+
{
|
13 |
+
"apiKey": s.kucoin_key,
|
14 |
+
"secret": s.kucoin_secret,
|
15 |
+
"password": s.kucoin_passphrase,
|
16 |
+
}
|
17 |
+
)
|
18 |
+
|
19 |
+
def get_kline_data(
|
20 |
+
self,
|
21 |
+
symbol: str,
|
22 |
+
granularity: int,
|
23 |
+
from_time: int,
|
24 |
+
to_time: Optional[int] = None,
|
25 |
+
) -> MarketData:
|
26 |
+
if to_time is None:
|
27 |
+
to_time = int(time.time() * 1000)
|
28 |
+
data = self.client.fetch_ohlcv(symbol, f"{granularity}m", since=from_time)
|
29 |
+
formatted = [
|
30 |
+
[d[0], str(d[1]), str(d[2]), str(d[3]), str(d[4]), str(d[5])] for d in data
|
31 |
+
]
|
32 |
+
return MarketData.from_kucoin_response({"data": formatted}, symbol, granularity)
|
33 |
+
|
34 |
+
def get_ticker(self, symbol: str) -> Dict[str, Any]:
|
35 |
+
t = self.client.fetch_ticker(symbol)
|
36 |
+
return {"price": str(t["last"]), "time": t["timestamp"]}
|
37 |
+
|
38 |
+
def get_symbols_list(self) -> List[SymbolInfo]:
|
39 |
+
return [
|
40 |
+
SymbolInfo.from_kucoin_data(
|
41 |
+
{
|
42 |
+
"symbol": m["id"],
|
43 |
+
"name": m["symbol"],
|
44 |
+
"baseCurrency": m["base"],
|
45 |
+
"quoteCurrency": m["quote"],
|
46 |
+
"maxLeverage": 20,
|
47 |
+
}
|
48 |
+
)
|
49 |
+
for m in self.client.fetch_markets()
|
50 |
+
]
|