mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-19 14:54:21 +01:00
Adds BfxApi with seed_candles function
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
import asyncio
|
||||
|
||||
from .websockets.BfxWebsocket import BfxWebsocket
|
||||
from .rest.BfxRest import BfxRest
|
||||
|
||||
class Client:
|
||||
def __init__(self, API_KEY=None, API_SECRET=None,
|
||||
host='wss://test.bitfinex.com/ws/2', *args, **kwargs):
|
||||
self.ws = BfxWebsocket(API_KEY=API_KEY, API_SECRET=API_SECRET, host=host, *args, **kwargs)
|
||||
self.rest = None # Eventually will be the rest interface
|
||||
def __init__(self, API_KEY=None, API_SECRET=None, rest_host='https://api.bitfinex.com/v2',
|
||||
ws_host='wss://api.bitfinex.com/ws/2', loop=None, logLevel='INFO', *args, **kwargs):
|
||||
self.loop = loop or asyncio.get_event_loop()
|
||||
self.ws = BfxWebsocket(API_KEY=API_KEY, API_SECRET=API_SECRET, host=ws_host,
|
||||
loop=self.loop, *args, **kwargs)
|
||||
self.rest = BfxRest(API_KEY=API_KEY, API_SECRET=API_SECRET, host=rest_host,
|
||||
loop=self.loop, *args, **kwargs)
|
||||
|
||||
16
bfxapi/examples/get_seed_trades.py
Normal file
16
bfxapi/examples/get_seed_trades.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import os
|
||||
import sys
|
||||
import asyncio
|
||||
sys.path.append('../')
|
||||
|
||||
from bfxapi import Client
|
||||
|
||||
bfx = Client(
|
||||
logLevel='INFO'
|
||||
)
|
||||
|
||||
async def get_seeds():
|
||||
candles = await bfx.rest.get_seed_candles('tBTCUSD')
|
||||
print (candles)
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(get_seeds())
|
||||
44
bfxapi/rest/BfxRest.py
Normal file
44
bfxapi/rest/BfxRest.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import asyncio
|
||||
import aiohttp
|
||||
import time
|
||||
import json
|
||||
|
||||
from ..utils.CustomLogger import CustomLogger
|
||||
|
||||
class BfxRest:
|
||||
|
||||
def __init__(self, API_KEY, API_SECRET, host='https://api.bitfinex.com/v2', loop=None, logLevel='INFO'):
|
||||
self.loop = loop or asyncio.get_event_loop()
|
||||
self.host = host
|
||||
self.logger = CustomLogger('BfxRest', logLevel=logLevel)
|
||||
|
||||
async def fetch(self, endpoint):
|
||||
url = '{}/{}'.format(self.host, endpoint)
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url) as resp:
|
||||
text = await resp.text()
|
||||
if resp.status is not 200:
|
||||
raise Exception('Unable to seed trades. Received status {} - {}'
|
||||
.format(resp.status, text))
|
||||
return json.loads(text)
|
||||
|
||||
async def get_seed_candles(self, symbol):
|
||||
endpoint = 'candles/trade:1m:{}/hist?limit=5000&_bfx=1'.format(symbol)
|
||||
time_difference = (1000 * 60) * 5000
|
||||
# get now to the nearest min
|
||||
now = int(round((time.time() // 60 * 60) * 1000))
|
||||
task_batch = []
|
||||
for x in range(0, 10):
|
||||
start = x * time_difference
|
||||
end = now - (x * time_difference) - time_difference
|
||||
e2 = endpoint + '&start={}&end={}'.format(start, end)
|
||||
task_batch += [asyncio.ensure_future(self.fetch(e2))]
|
||||
self.logger.info("Downloading seed candles from Bitfinex...")
|
||||
# call all fetch requests async
|
||||
done, _ = await asyncio.wait(*[ task_batch ])
|
||||
candles = []
|
||||
for task in done:
|
||||
candles += task.result()
|
||||
candles.sort(key=lambda x: x[0], reverse=True)
|
||||
self.logger.info("Downloaded {} candles.".format(len(candles)))
|
||||
return candles
|
||||
@@ -16,10 +16,10 @@ def is_json(myjson):
|
||||
|
||||
class GenericWebsocket(object):
|
||||
|
||||
def __init__(self, host, logLevel='ERROR'):
|
||||
def __init__(self, host, logLevel='INFO', loop=None):
|
||||
self.host = host
|
||||
self.logger = CustomLogger('HFWebSocket', logLevel=logLevel)
|
||||
self.loop = asyncio.get_event_loop()
|
||||
self.logger = CustomLogger('BfxWebsocket', logLevel=logLevel)
|
||||
self.loop = loop or asyncio.get_event_loop()
|
||||
self.events = EventEmitter(scheduler=asyncio.ensure_future, loop=self.loop)
|
||||
|
||||
def run(self):
|
||||
@@ -28,6 +28,7 @@ class GenericWebsocket(object):
|
||||
async def _main(self, host):
|
||||
async with websockets.connect(host) as websocket:
|
||||
self.ws = websocket
|
||||
self.logger.info("Wesocket connectedt to {}".format(self.host))
|
||||
while True:
|
||||
await asyncio.sleep(0)
|
||||
message = await websocket.recv()
|
||||
|
||||
Reference in New Issue
Block a user