Last active
July 14, 2018 13:40
-
-
Save anthonytxie/67e9e25315ed262eb77bcb0a4a637a64 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from binance.client import Client\n", | |
"import time\n", | |
"import pandas as pd\n", | |
"import datetime\n", | |
"\n", | |
"#instantiate with API Keys\n", | |
"client = Client(\"\", \"\")\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"# Get historical candlesticks data\n", | |
"def get_historical_klines(symbol, interval, startTime):\n", | |
" klines = client.get_historical_klines(symbol, Client.KLINE_INTERVAL_1DAY, startTime)\n", | |
" candlesticks = []\n", | |
" for tick in klines:\n", | |
" dict = {}\n", | |
" dict['Open Time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(tick[0]/1000))\n", | |
" dict['Open'] = tick[1]\n", | |
" dict['High'] = tick[2]\n", | |
" dict['Low'] = tick[3]\n", | |
" dict['Close'] = tick[4]\n", | |
" dict['Volume'] = tick[5]\n", | |
" dict['Close time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(tick[6]/1000))\n", | |
" dict['Quote asset volume'] = tick[7]\n", | |
" dict['Number of trades'] = tick[8]\n", | |
" dict['Taker buy base asset volume'] = tick[9]\n", | |
" dict['Taker buy quote asset volume'] = tick[10]\n", | |
" dict['Ignore'] = tick[11]\n", | |
" candlesticks.append(dict)\n", | |
" klines_df = pd.DataFrame(candlesticks)\n", | |
" klines_df['Open Time'] = pd.to_datetime(klines_df['Open Time'])\n", | |
" klines_df['Close time'] = pd.to_datetime(klines_df['Close time'])\n", | |
" klines_df.index = klines_df['Open Time']\n", | |
" klines_df = klines_df[['Close', 'Open', 'Low', 'High', 'Volume', 'Number of trades']]\n", | |
" klines_df['Close'] = pd.to_numeric(klines_df['Close'])\n", | |
" klines_df['Open'] = pd.to_numeric(klines_df['Open'])\n", | |
" klines_df['High'] = pd.to_numeric(klines_df['High'])\n", | |
" klines_df['Volume'] = pd.to_numeric(klines_df['Volume'])\n", | |
" klines_df['Number of trades'] = pd.to_numeric(klines_df['Number of trades'])\n", | |
" return klines_df\n", | |
"\n", | |
"\n", | |
"sys_klines = get_historical_klines(\"SYSBTC\", Client.KLINE_INTERVAL_1DAY, '60 days ago UTC')\n", | |
"via_klines = get_historical_klines(\"VIABTC\", Client.KLINE_INTERVAL_1DAY, '180 days ago UTC')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_historical_aggregate_trades(startTime, symbol):\n", | |
" trades = []\n", | |
" for num in range(1,50):\n", | |
" agg_trades = client.get_aggregate_trades(symbol=symbol, startTime=str(startTime), endTime=str(startTime+3600000))\n", | |
" for trade in agg_trades:\n", | |
" trades.append(trade)\n", | |
" startTime = int(agg_trades[-1]['T'])\n", | |
" agg_trades_df = pd.DataFrame(trades)\n", | |
" agg_trades_df['p'] = pd.to_numeric(agg_trades_df['p'])\n", | |
" agg_trades_df['q'] = pd.to_numeric(agg_trades_df['q'])\n", | |
" agg_trades_df['T'] = pd.to_datetime(agg_trades_df['T'], unit='ms')\n", | |
" pd.options.display.float_format = '{:.4f}'.format\n", | |
" agg_trades_df['volume'] = agg_trades_df['p'] * agg_trades_df['q'] * 6500 \n", | |
" agg_trades_df.columns = [['Was the trade the best price match?', 'Timestamp', 'Aggregate TradeID', 'First TradeID', 'Last Trade ID', 'Was the buyer the maker?', 'Price', 'Quantity', 'Volume']]\n", | |
" return agg_trades_df\n", | |
" \n", | |
"\n", | |
"\n", | |
"via_historical_agg_trades = get_historical_aggregate_trades(1520294400000, 'VIABTC')\n", | |
"sys_historical_agg_trades = get_historical_aggregate_trades(1530650644504, 'SYSBTC')\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_historical_individual_trades(startingId, symbol):\n", | |
" all_trades = []\n", | |
" for num in range(1,20):\n", | |
" new_trades = client.get_historical_trades(symbol=symbol, fromId=startingId)\n", | |
" for trade in new_trades:\n", | |
" all_trades.append(trade)\n", | |
" startingId = new_trades[-1]['id']\n", | |
" historical_trades_df = pd.DataFrame(all_trades)\n", | |
" historical_trades_df['time'] = pd.to_datetime(historical_trades_df['time'], unit='ms')\n", | |
" return historical_trades_df\n", | |
"\n", | |
"\n", | |
"historical_individual_trades_for_sys = get_historical_individual_trades('901620', 'SYSBTC')\n", | |
"historical_individual_trades_for_via = get_historical_individual_trades('165944', 'VIABTC')\n", | |
"\n", | |
"historical_individual_trades_for_via.to_csv('/users/anthony/desktop/via-btc/individual-trades.csv')" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"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.6.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment