Last active
July 24, 2019 08:17
-
-
Save inlinechan/9c8c77c58321e65804574b80a4d24b0e to your computer and use it in GitHub Desktop.
This file contains 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
# Python | |
import asyncio | |
import ccxt.async_support as ccxt | |
from datetime import datetime, timedelta | |
async def print_bitfinex_ethbtc_ticker(): | |
bitfinex = ccxt.bitfinex() | |
# print(await bitfinex.fetch_ticker('ETH/BTC')) | |
timestamp = datetime.timestamp(datetime.now() - timedelta(days=50)) | |
ohlcs = await bitfinex.fetch_ohlcv('BTC/USD', since=timestamp * 1000, | |
timeframe='1d') | |
o = ohlcs[-1] | |
dt_object = datetime.fromtimestamp(o[0]/1000) | |
print(dt_object, o[1:]) | |
await bitfinex.close() | |
# asyncio.get_event_loop().run_until_complete(print_bitfinex_ethbtc_ticker()) | |
asyncio.run(print_poloniex_ethbtc_ticker()) |
This file contains 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
import asyncio | |
from datetime import datetime, timedelta | |
from pytz import timezone, utc | |
# def next_15min(dt): | |
# extra_minutes = dt.timetuple()[4] % 15 | |
# dt_next = dt + timedelta(minutes=15 - extra_minutes) | |
# return dt_next.replace(second=0, microsecond=0) | |
def next_minutes(dt, minutes=15): | |
extra_minutes = dt.timetuple()[4] % minutes | |
dt_next = dt + timedelta(minutes=minutes - extra_minutes) | |
return dt_next.replace(second=0, microsecond=0) | |
async def main(): | |
while True: | |
now = utc.localize(datetime.utcnow()) | |
kst = timezone('Asia/Seoul') | |
# fmt = '%Y-%m-%d %H:%M:%S %Z%z' | |
# next_dt = next_15min(now) | |
next_dt = next_minutes(now, minutes=15) | |
remain = (next_dt - now).total_seconds() | |
# print(f"{now} - {next_dt} - {next_dt.astimezone(kst)}") | |
print(f"waiting for {remain} seconds until {next_dt.astimezone(kst)}") | |
await asyncio.sleep(remain) | |
if __name__ == '__main__': | |
asyncio.run(main()) |
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import aiohttp\n", | |
"import asyncio\n", | |
"import json\n", | |
"import pandas as pd\n", | |
"\n", | |
"async def fetch(session, url):\n", | |
" async with session.get(url) as response:\n", | |
" return await response.text()\n", | |
" \n", | |
"async def fetch_ohlc_json(fsym, tsym):\n", | |
" async with aiohttp.ClientSession() as session:\n", | |
" url = 'https://min-api.cryptocompare.com/data/histoday?fsym={}&tsym={}&limit=10&allData=true'\n", | |
" response = await fetch(session, url.format(fsym, tsym))\n", | |
" resp_json = json.loads(response)\n", | |
" return resp_json\n", | |
"\n", | |
"async def fetch_ohlc(fsym='ETH', tsym='BTC'):\n", | |
" resp_json = await fetch_ohlc_json(fsym, tsym)\n", | |
" df = pd.read_json(json.dumps(resp_json['Data']))\n", | |
" del resp_json['Data']\n", | |
" return resp_json, df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 79, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"resp = await fetch_ohlc_json(fsym='ETH', tsym='BTC')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 80, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"begin, end = resp['TimeFrom']/1000, resp['TimeTo']\n", | |
"data = resp['Data']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 81, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1438905600.0, 1563926400)" | |
] | |
}, | |
"execution_count": 81, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"begin, end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from datetime import datetime\n", | |
"beginDate, endDate = datetime.fromtimestamp(begin), datetime.fromtimestamp(end)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 83, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(datetime.datetime(2015, 8, 7, 9, 0), datetime.datetime(2019, 7, 24, 9, 0))" | |
] | |
}, | |
"execution_count": 83, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"beginDate, endDate" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 84, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"datetime.datetime(2015, 8, 7, 9, 0)" | |
] | |
}, | |
"execution_count": 84, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"datetime.fromtimestamp(data[0]['time'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 85, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"datetime.datetime(2019, 7, 24, 9, 0)" | |
] | |
}, | |
"execution_count": 85, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"datetime.fromtimestamp(data[-1]['time'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 86, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df = pd.read_json(json.dumps(data))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 87, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>close</th>\n", | |
" <th>high</th>\n", | |
" <th>low</th>\n", | |
" <th>open</th>\n", | |
" <th>time</th>\n", | |
" <th>volumefrom</th>\n", | |
" <th>volumeto</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>0.009980</td>\n", | |
" <td>0.100000</td>\n", | |
" <td>0.002810</td>\n", | |
" <td>0.002810</td>\n", | |
" <td>1438905600</td>\n", | |
" <td>53584.56</td>\n", | |
" <td>577.47</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>0.003123</td>\n", | |
" <td>0.009980</td>\n", | |
" <td>0.002304</td>\n", | |
" <td>0.009980</td>\n", | |
" <td>1438992000</td>\n", | |
" <td>722558.00</td>\n", | |
" <td>2958.54</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>0.002815</td>\n", | |
" <td>0.003631</td>\n", | |
" <td>0.002290</td>\n", | |
" <td>0.003123</td>\n", | |
" <td>1439078400</td>\n", | |
" <td>737119.57</td>\n", | |
" <td>2012.14</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>0.002600</td>\n", | |
" <td>0.002897</td>\n", | |
" <td>0.002275</td>\n", | |
" <td>0.002815</td>\n", | |
" <td>1439164800</td>\n", | |
" <td>585917.04</td>\n", | |
" <td>1486.71</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>0.003938</td>\n", | |
" <td>0.004331</td>\n", | |
" <td>0.002434</td>\n", | |
" <td>0.002600</td>\n", | |
" <td>1439251200</td>\n", | |
" <td>1479695.62</td>\n", | |
" <td>4812.95</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" close high low open time volumefrom volumeto\n", | |
"0 0.009980 0.100000 0.002810 0.002810 1438905600 53584.56 577.47\n", | |
"1 0.003123 0.009980 0.002304 0.009980 1438992000 722558.00 2958.54\n", | |
"2 0.002815 0.003631 0.002290 0.003123 1439078400 737119.57 2012.14\n", | |
"3 0.002600 0.002897 0.002275 0.002815 1439164800 585917.04 1486.71\n", | |
"4 0.003938 0.004331 0.002434 0.002600 1439251200 1479695.62 4812.95" | |
] | |
}, | |
"execution_count": 87, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 88, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"close float64\n", | |
"high float64\n", | |
"low float64\n", | |
"open float64\n", | |
"time int64\n", | |
"volumefrom float64\n", | |
"volumeto float64\n", | |
"dtype: object" | |
] | |
}, | |
"execution_count": 88, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.dtypes" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 89, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dict_keys(['Response', 'Type', 'Aggregated', 'Data', 'TimeTo', 'TimeFrom', 'FirstValueInArray', 'ConversionType', 'RateLimit', 'HasWarning'])" | |
] | |
}, | |
"execution_count": 89, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"resp.keys()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 90, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# async def fetch_ohlc(fsym='ETH', tsym='BTC'):\n", | |
"# resp_json = await fetch_ohlc_json(fsym, tsym)\n", | |
"# df = pd.read_json(json.dumps(resp_json['Data']))\n", | |
"# del resp_json['Data']\n", | |
"# return resp_json, df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 91, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"resp, df = await fetch_ohlc()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 92, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'Response': 'Success',\n", | |
" 'Type': 100,\n", | |
" 'Aggregated': False,\n", | |
" 'TimeTo': 1563926400,\n", | |
" 'TimeFrom': 1438905600000,\n", | |
" 'FirstValueInArray': True,\n", | |
" 'ConversionType': {'type': 'direct', 'conversionSymbol': ''},\n", | |
" 'RateLimit': {},\n", | |
" 'HasWarning': False}" | |
] | |
}, | |
"execution_count": 92, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"resp" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 93, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>close</th>\n", | |
" <th>high</th>\n", | |
" <th>low</th>\n", | |
" <th>open</th>\n", | |
" <th>time</th>\n", | |
" <th>volumefrom</th>\n", | |
" <th>volumeto</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>0.009980</td>\n", | |
" <td>0.100000</td>\n", | |
" <td>0.002810</td>\n", | |
" <td>0.002810</td>\n", | |
" <td>1438905600</td>\n", | |
" <td>53584.56</td>\n", | |
" <td>577.47</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>0.003123</td>\n", | |
" <td>0.009980</td>\n", | |
" <td>0.002304</td>\n", | |
" <td>0.009980</td>\n", | |
" <td>1438992000</td>\n", | |
" <td>722558.00</td>\n", | |
" <td>2958.54</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>0.002815</td>\n", | |
" <td>0.003631</td>\n", | |
" <td>0.002290</td>\n", | |
" <td>0.003123</td>\n", | |
" <td>1439078400</td>\n", | |
" <td>737119.57</td>\n", | |
" <td>2012.14</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>0.002600</td>\n", | |
" <td>0.002897</td>\n", | |
" <td>0.002275</td>\n", | |
" <td>0.002815</td>\n", | |
" <td>1439164800</td>\n", | |
" <td>585917.04</td>\n", | |
" <td>1486.71</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>0.003938</td>\n", | |
" <td>0.004331</td>\n", | |
" <td>0.002434</td>\n", | |
" <td>0.002600</td>\n", | |
" <td>1439251200</td>\n", | |
" <td>1479695.62</td>\n", | |
" <td>4812.95</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" close high low open time volumefrom volumeto\n", | |
"0 0.009980 0.100000 0.002810 0.002810 1438905600 53584.56 577.47\n", | |
"1 0.003123 0.009980 0.002304 0.009980 1438992000 722558.00 2958.54\n", | |
"2 0.002815 0.003631 0.002290 0.003123 1439078400 737119.57 2012.14\n", | |
"3 0.002600 0.002897 0.002275 0.002815 1439164800 585917.04 1486.71\n", | |
"4 0.003938 0.004331 0.002434 0.002600 1439251200 1479695.62 4812.95" | |
] | |
}, | |
"execution_count": 93, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment