Created
June 19, 2018 14:10
-
-
Save cderinbogaz/c132f2d95cf485cb1ba775641ed2c13d to your computer and use it in GitHub Desktop.
Fetch OHLCV Candles from tickers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ticker(exchange, currency): | |
try: | |
ticker = exchange.fetch_ticker(currency) | |
closing = ticker['last'] | |
timestamp = ticker['timestamp'] | |
open = ticker['open'] | |
high = ticker['high'] | |
low = ticker['low'] | |
volume = ticker['baseVolume'] | |
bid = ticker['bid'] | |
ask = ticker['ask'] | |
# Check if there is empty values. Sometimes, some apis doesnt have values | |
# and this causes problems in database records. | |
if not open: | |
open = 0 | |
if not high: | |
high = 0 | |
if not low: | |
low = 0 | |
if not volume: | |
volume = 0 | |
if not bid: | |
bid = 0 | |
if not ask: | |
ask = 0 | |
delta_value = delta(exchange, currency) | |
price_delta_1h = delta_value[0] | |
price_delta_24h = delta_value[1] | |
print(str(exchange.exchangeName) + " " + str(currency) + " Delta 1h: " + str(price_delta_1h)) | |
print(str(exchange.exchangeName) + " " + str(currency) + " Delta 24h: " + str(price_delta_24h)) | |
trading_pairs = { | |
"trading_pair_id": id_convert(exchange, currency), | |
"trading_pair": currency, | |
"price": float(closing), | |
"price_delta_1h": price_delta_1h, | |
"price_delta_24h": price_delta_24h, | |
"price_updated_at": timestamp | |
} | |
database_write(exchange, currency, timestamp, open, high, closing, low, volume, bid, ask) | |
sleep(exchange.rateLimit / 1000) | |
except (ccxt.ExchangeError, ccxt.AuthenticationError, ccxt.ExchangeNotAvailable, ccxt.RequestTimeout) as error: | |
print('Got an error', type(error).__name__, error.args, ', retrying in', hold, 'seconds...') | |
sleep(exchange.rateLimit / 1000) | |
return trading_pairs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment