Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
436f692e4f | ||
|
|
4974b82c25 | ||
|
|
26471a26aa | ||
|
|
489039268f | ||
|
|
443f82e5f7 | ||
|
|
b4fc941a59 | ||
|
|
a0d6f4c361 | ||
|
|
bfb881cd16 |
@@ -2,6 +2,7 @@ from .binance import Binance
|
||||
from .binance_futures import BinanceFutures
|
||||
from .bitfinex import Bitfinex
|
||||
from .coinbase import Coinbase
|
||||
from .coincap import Coincap
|
||||
from .testnet_binance_futures import TestnetBinanceFutures
|
||||
|
||||
drivers = {
|
||||
@@ -10,4 +11,5 @@ drivers = {
|
||||
'Testnet Binance Futures': TestnetBinanceFutures,
|
||||
'Bitfinex': Bitfinex,
|
||||
'Coinbase': Coinbase,
|
||||
'Coincap': Coincap,
|
||||
}
|
||||
|
||||
117
jesse/modes/import_candles_mode/drivers/coincap.py
Normal file
117
jesse/modes/import_candles_mode/drivers/coincap.py
Normal file
@@ -0,0 +1,117 @@
|
||||
import requests
|
||||
|
||||
import jesse.helpers as jh
|
||||
from jesse import exceptions
|
||||
from .interface import CandleExchange
|
||||
|
||||
|
||||
class Coincap(CandleExchange):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__('Coincap', 1440, 0.1)
|
||||
self.endpoint = 'https://api.coincap.io/v2/'
|
||||
|
||||
def init_backup_exchange(self):
|
||||
self.backup_exchange = None
|
||||
|
||||
def get_starting_time(self, symbol: str):
|
||||
|
||||
symbol_splited = symbol.split('-')
|
||||
|
||||
|
||||
baseId = self.find_symbol_id(symbol_splited[0])
|
||||
quoteId = self.find_symbol_id(symbol_splited[1])
|
||||
|
||||
payload = {
|
||||
'interval': 'd1',
|
||||
'exchange': 'binance',
|
||||
'baseId': baseId,
|
||||
'quoteId': quoteId,
|
||||
|
||||
}
|
||||
|
||||
response = requests.get(self.endpoint + 'candles', params=payload)
|
||||
self._handle_errors(response)
|
||||
data = response.json()['data']
|
||||
|
||||
# wrong symbol entered
|
||||
if not len(data):
|
||||
raise ValueError("No candles received to determine start_date.")
|
||||
|
||||
first_timestamp = int(data[0]['period'])
|
||||
second_timestamp = first_timestamp + 60_000 * 1440
|
||||
|
||||
return second_timestamp
|
||||
|
||||
def fetch(self, symbol, start_timestamp):
|
||||
|
||||
end_timestamp = start_timestamp + (self.count - 1) * 60000
|
||||
|
||||
symbol_splited = symbol.split('-')
|
||||
|
||||
|
||||
baseId = self.find_symbol_id(symbol_splited[0])
|
||||
quoteId = self.find_symbol_id(symbol_splited[1])
|
||||
|
||||
payload = {
|
||||
'start': start_timestamp,
|
||||
'end': end_timestamp,
|
||||
'interval': 'm1',
|
||||
'exchange': 'binance',
|
||||
'baseId': baseId,
|
||||
'quoteId': quoteId,
|
||||
|
||||
}
|
||||
|
||||
response = requests.get(self.endpoint + 'candles', params=payload)
|
||||
self._handle_errors(response)
|
||||
|
||||
data = response.json()['data']
|
||||
candles = []
|
||||
|
||||
for d in data:
|
||||
candles.append({
|
||||
'id': jh.generate_unique_id(),
|
||||
'symbol': symbol,
|
||||
'exchange': self.name,
|
||||
'timestamp': int(d['period']),
|
||||
'open': float(d['open']),
|
||||
'close': float(d['close']),
|
||||
'high': float(d['high']),
|
||||
'low': float(d['low']),
|
||||
'volume': float(d['volume'])
|
||||
})
|
||||
|
||||
return candles
|
||||
|
||||
def find_symbol_id(self, symbol):
|
||||
|
||||
payload = {
|
||||
'search': symbol,
|
||||
'limit': 1
|
||||
}
|
||||
|
||||
response = requests.get(self.endpoint + 'assets', params=payload)
|
||||
self._handle_errors(response)
|
||||
data = response.json()['data']
|
||||
|
||||
if not data:
|
||||
raise exceptions.SymbolNotFound("No asset found for {}. You're probably misspelling the symbol name.".format(symbol))
|
||||
|
||||
return data[0]['id']
|
||||
|
||||
@staticmethod
|
||||
def _handle_errors(response):
|
||||
|
||||
first_digit_code = int(str(response.status_code)[0])
|
||||
|
||||
# Exchange In Maintenance
|
||||
if first_digit_code == 5:
|
||||
raise exceptions.ExchangeInMaintenance('Server ERROR. Please try again later')
|
||||
|
||||
# unsupported user params
|
||||
if first_digit_code == 4:
|
||||
raise ValueError(response.json()['error'])
|
||||
|
||||
if response.status_code != 200:
|
||||
raise Exception(response.content)
|
||||
Reference in New Issue
Block a user