pymmdrza commited on
Commit
64dfdc8
·
verified ·
1 Parent(s): 55db3a6

Update api/kucoin.py

Browse files
Files changed (1) hide show
  1. api/kucoin.py +19 -39
api/kucoin.py CHANGED
@@ -1,50 +1,30 @@
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
- ]
 
 
1
  import ccxt
 
 
2
  from models.market_data import MarketData, SymbolInfo
3
 
4
 
5
  class KuCoinClient:
6
+ def __init__(self, session):
 
7
  self.client = ccxt.kucoinfutures(
8
  {
9
+ "apiKey": session.kucoin_key,
10
+ "secret": session.kucoin_secret,
11
+ "password": session.kucoin_passphrase,
12
  }
13
  )
14
 
15
+ def get_kline_data(self, symbol, granularity, from_time, to_time):
16
+ klines = self.client.fetch_ohlcv(
17
+ symbol, timeframe=f"{granularity}m", since=from_time, limit=1000
18
+ )
19
+ response = {
20
+ "code": "200000",
21
+ "data": [
22
+ [k[0], str(k[1]), str(k[2]), str(k[3]), str(k[4]), str(k[5])]
23
+ for k in klines
24
+ ],
25
+ }
26
+ return MarketData.from_kucoin_response(response, symbol, granularity)
 
 
 
 
 
 
27
 
28
+ def get_current_price(self, symbol: str) -> float:
29
+ ticker = self.client.fetch_ticker(symbol)
30
+ return float(ticker["last"])