In [1]:
from binance.client import Client
api_key='9loAzaiZJn5VfXqM0RO7k1paib9UTWZnSVe4irHvdg4Woaq7PMNbtYySDrHJOBKW'
api_secret='tXKpZtqWIBcNSN2XtwLS1Vg14nYm4DeMt1zxR3C0jgRnUhGN4QX3Jbntfgl6nem2'
client = Client(api_key, api_secret)

In [2]:
# get market depth
depth = client.get_order_book(symbol='BNBBTC')

In [4]:
# place a test market buy order, to place an actual order use the create_order function
order = client.create_test_order(
    symbol='BNBBTC',
    side=Client.SIDE_BUY,
    type=Client.ORDER_TYPE_MARKET,
    quantity=100)

In [6]:
# get all symbol prices
prices = client.get_all_tickers()

In [3]:
# fetch 1 minute klines for the last day up until now
klines1 = client.get_historical_klines("BNBBTC", Client.KLINE_INTERVAL_1MINUTE, "1 day ago UTC")

# fetch 30 minute klines for the last month of 2017
klines30 = client.get_historical_klines("ETHBTC", Client.KLINE_INTERVAL_30MINUTE, "1 Dec, 2017", "1 Jan, 2018")

# fetch weekly klines since it listed
klines7 = client.get_historical_klines("NEOBTC", Client.KLINE_INTERVAL_1WEEK, "1 Jan, 2017")

In [7]:
btc = client.get_historical_klines("BTCUSDT", Client.KLINE_INTERVAL_1WEEK, "1 Jan, 2013")

In [196]:
import pandas as pd
import matplotlib.image as image

In [193]:
dfs=pd.read_html('https://coinmarketcap.com/currencies/bitcoin/historical-data/?start=20130428&end=20180708')
df=dfs[0]
df=df[['Date','Close**']]
df.columns=['Date','.']
df['Date']=pd.to_datetime(df['Date'])


C:\Anaconda2\lib\site-packages\ipykernel\__main__.py:5: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

In [202]:
fig,ax=plt.subplots(figsize=(13,6))
df[('2016'>df['Date'])&(df['Date']>'2013')].set_index('Date').plot(ax=ax,c='grey')
ax2=ax.twiny()
ax2=ax2.twinx()
dz=df[df['Date']>'2017'].set_index('Date')
dz.columns=[u'Before and after the last bubble\n2013-2016 (bottom + left axis)']
dz.plot(ax=ax2,c='grey')
dz.columns=[u'Bitcoin price in $ since 2017 (top + right axis)']
dz.plot(ax=ax2,c='orange')

#ax.legend(loc=1)
ax.set_xlim('2013','2016')
#ax.set_title('HODL ON',fontsize=40,y=1.2)
ax2.set_xlim('2017-01-15','2020-01-15')
ax.set_xlabel('')
ax2.legend()


Out[202]:
<matplotlib.legend.Legend at 0x38e87cc0>

In [191]:
dfs=pd.read_html('https://coinmarketcap.com/currencies/litecoin/historical-data/?start=20130428&end=20180708')
df=dfs[0]
df=df[['Date','Close**']]
df.columns=['Date','.']
df['Date']=pd.to_datetime(df['Date'])


C:\Anaconda2\lib\site-packages\ipykernel\__main__.py:5: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

In [192]:
fig,ax=plt.subplots(figsize=(13,6))
df[('2016'>df['Date'])&(df['Date']>'2013')].set_index('Date').plot(ax=ax,c='grey')
ax2=ax.twiny()
ax2=ax2.twinx()
dz=df[df['Date']>'2017'].set_index('Date')
dz.columns=[u'Before and after the last bubble\n2013-2016 (bottom + left axis)']
dz.plot(ax=ax2,c='grey')
dz.columns=[u'Litecoin price in $ since 2017 (top + right axis)']
dz.plot(ax=ax2,c='lightgrey')
#ax.legend(loc=1)
ax.set_xlim('2013','2016')
#ax.set_title('HODL ON',fontsize=40,y=1.2)
ax2.set_xlim('2017-01-15','2020-01-15')
ax.set_xlabel('')
ax2.legend()


Out[192]:
<matplotlib.legend.Legend at 0x2d23ddd8>

In [186]:
dfs=pd.read_html('https://coinmarketcap.com/currencies/ripple/historical-data/?start=20130428&end=20180708')
df=dfs[0]
df=df[['Date','Close**']]
df.columns=['Date','.']
df['Date']=pd.to_datetime(df['Date'])


C:\Anaconda2\lib\site-packages\ipykernel\__main__.py:5: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

In [190]:
fig,ax=plt.subplots(figsize=(13,6))
df[('2016'>df['Date'])&(df['Date']>'2013')].set_index('Date').plot(ax=ax,c='grey')
ax2=ax.twiny()
ax2=ax2.twinx()
dz=df[df['Date']>'2017'].set_index('Date')
dz.columns=[u'Before and after the last bubble\n2013-2016 (bottom + left axis)']
dz.plot(ax=ax2,c='grey')
dz.columns=[u'Ripple price in $ since 2017 (top + right axis)']
dz.plot(ax=ax2,c='dodgerblue')
#ax.legend(loc=1)
ax.set_xlim('2013','2016')
#ax.set_title('HODL ON',fontsize=40,y=1.2)
ax2.set_xlim('2017-02','2020-02')
ax.set_xlabel('')
ax2.legend()


Out[190]:
<matplotlib.legend.Legend at 0x2d259f28>

In [19]:
df.index=pd.to_datetime(df.index)

In [22]:
import matplotlib.pyplot as plt
%matplotlib inline

In [43]:
plt.xkcd()
fig,ax=plt.subplots(figsize=(10,5))
df.loc['2014-01-01']


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-43-17e0f20084d2> in <module>()
      1 plt.xkcd()
      2 fig,ax=plt.subplots(figsize=(10,5))
----> 3 df.loc[['2014-01-01']].loc[:'2015-01-01']

C:\Anaconda2\lib\site-packages\pandas\core\indexing.pyc in __getitem__(self, key)
   1371 
   1372             maybe_callable = com._apply_if_callable(key, self.obj)
-> 1373             return self._getitem_axis(maybe_callable, axis=axis)
   1374 
   1375     def _is_scalar_access(self, key):

C:\Anaconda2\lib\site-packages\pandas\core\indexing.pyc in _getitem_axis(self, key, axis)
   1614                     raise ValueError('Cannot index with multidimensional key')
   1615 
-> 1616                 return self._getitem_iterable(key, axis=axis)
   1617 
   1618             # nested tuple slicing

C:\Anaconda2\lib\site-packages\pandas\core\indexing.pyc in _getitem_iterable(self, key, axis)
   1113 
   1114         if self._should_validate_iterable(axis):
-> 1115             self._has_valid_type(key, axis)
   1116 
   1117         labels = self.obj._get_axis(axis)

C:\Anaconda2\lib\site-packages\pandas\core\indexing.pyc in _has_valid_type(self, key, axis)
   1470                         raise KeyError(
   1471                             u"None of [{key}] are in the [{axis}]".format(
-> 1472                                 key=key, axis=self.obj._get_axis_name(axis)))
   1473                     else:
   1474 

KeyError: u"None of [['2014-01-01']] are in the [index]"

In [ ]:
[
    [
        1499040000000,      # Open time
        "0.01634790",       # Open
        "0.80000000",       # High
        "0.01575800",       # Low
        "0.01577100",       # Close
        "148976.11427815",  # Volume
        1499644799999,      # Close time
        "2434.19055334",    # Quote asset volume
        308,                # Number of trades
        "1756.87402397",    # Taker buy base asset volume
        "28.46694368",      # Taker buy quote asset volume
        "17928899.62484339" # Can be ignored
    ]
]

In [12]:
klines7


Out[12]:
[[1499644800000L,
  u'0.00375000',
  u'0.00375000',
  u'0.00235700',
  u'0.00263800',
  u'473736.36000000',
  1500249599999L,
  u'1196.77166665',
  8973,
  u'286442.84000000',
  u'723.69695083',
  u'218593.00000000'],
 [1500249600000L,
  u'0.00263800',
  u'0.00345800',
  u'0.00255700',
  u'0.00326100',
  u'1594660.22000000',
  1500854399999L,
  u'4589.96992084',
  31760,
  u'1000499.53000000',
  u'2879.09366021',
  u'359801.00000000'],
 [1500854400000L,
  u'0.00326600',
  u'0.00326900',
  u'0.00240100',
  u'0.00261200',
  u'3971754.21000000',
  1501459199999L,
  u'11114.83093982',
  51384,
  u'2219779.02000000',
  u'6233.71564475',
  u'533884.00000000'],
 [1501459200000L,
  u'0.00261100',
  u'0.00498400',
  u'0.00249700',
  u'0.00482100',
  u'4986580.16000000',
  1502063999999L,
  u'17488.29338244',
  69886,
  u'2569264.33000000',
  u'9026.54218521',
  u'630085.00000000'],
 [1502064000000L,
  u'0.00481900',
  u'0.01381600',
  u'0.00440800',
  u'0.01170400',
  u'8971365.12000000',
  1502668799999L,
  u'70289.94990929',
  149558,
  u'4583357.71000000',
  u'35975.96593847',
  u'975334.00000000'],
 [1502668800000L,
  u'0.01170400',
  u'0.01330000',
  u'0.00756800',
  u'0.00929800',
  u'7354935.19000000',
  1503273599999L,
  u'74278.97967737',
  166864,
  u'3807134.04000000',
  u'38512.47450285',
  u'1494621.00000000'],
 [1503273600000L,
  u'0.00931000',
  u'0.01074600',
  u'0.00842300',
  u'0.00920500',
  u'2592193.79000000',
  1503878399999L,
  u'23966.19393344',
  118851,
  u'1330793.01000000',
  u'12284.55099467',
  u'1805071.00000000'],
 [1503878400000L,
  u'0.00916200',
  u'0.00933900',
  u'0.00570100',
  u'0.00669900',
  u'2328378.96000000',
  1504483199999L,
  u'16915.17702905',
  111948,
  u'1140653.23000000',
  u'8260.92633938',
  u'1878716.13000000'],
 [1504483200000L,
  u'0.00669800',
  u'0.00735000',
  u'0.00331900',
  u'0.00557800',
  u'2419624.27000000',
  1505087999999L,
  u'13383.81703613',
  94747,
  u'1278794.99000000',
  u'7076.75322818',
  u'1901402.13000000'],
 [1505088000000L,
  u'0.00557800',
  u'0.00595000',
  u'0.00440200',
  u'0.00536100',
  u'2170095.15000000',
  1505692799999L,
  u'11096.19770457',
  120201,
  u'1095920.49000000',
  u'5610.30638869',
  u'2032269.57887000'],
 [1505692800000L,
  u'0.00536300',
  u'0.00545800',
  u'0.00461700',
  u'0.00540300',
  u'1723681.01000000',
  1506297599999L,
  u'8824.87522473',
  119074,
  u'874922.55000000',
  u'4484.32987530',
  u'2077050.58000000'],
 [1506297600000L,
  u'0.00540300',
  u'0.00833900',
  u'0.00536700',
  u'0.00810500',
  u'3985736.96000000',
  1506902399999L,
  u'28182.56414476',
  167348,
  u'1976511.00000000',
  u'13951.41164814',
  u'2239602.58000000'],
 [1506902400000L,
  u'0.00811400',
  u'0.00882900',
  u'0.00665000',
  u'0.00693700',
  u'3846444.22000000',
  1507507199999L,
  u'29864.76536690',
  179033,
  u'1807998.25000000',
  u'14055.55879919',
  u'2362143.58000000'],
 [1507507200000L,
  u'0.00693600',
  u'0.00712900',
  u'0.00455500',
  u'0.00486800',
  u'3450411.34000000',
  1508111999999L,
  u'19453.23854126',
  144094,
  u'1630255.57000000',
  u'9154.97942502',
  u'2501821.58000000'],
 [1508112000000L,
  u'0.00486800',
  u'0.00588800',
  u'0.00425100',
  u'0.00479400',
  u'2988037.52000000',
  1508716799999L,
  u'15048.15414045',
  102239,
  u'1539351.40000000',
  u'7749.85520494',
  u'2617631.58000000'],
 [1508716800000L,
  u'0.00479400',
  u'0.00580000',
  u'0.00428500',
  u'0.00449400',
  u'3112199.77000000',
  1509321599999L,
  u'15538.30944565',
  109351,
  u'1614888.73000000',
  u'8062.38686734',
  u'2631299.58000000'],
 [1509321600000L,
  u'0.00450000',
  u'0.00513200',
  u'0.00309900',
  u'0.00357500',
  u'3358632.44000000',
  1509926399999L,
  u'13194.61821863',
  132832,
  u'1737244.27000000',
  u'6843.71176713',
  u'2870262.58000000'],
 [1509926400000L,
  u'0.00357400',
  u'0.00487000',
  u'0.00351200',
  u'0.00460900',
  u'3451587.69000000',
  1510531199999L,
  u'14282.96872073',
  136202,
  u'1637577.13000000',
  u'6764.48271934',
  u'2706810.45955000'],
 [1510531200000L,
  u'0.00460900',
  u'0.00630000',
  u'0.00349400',
  u'0.00495800',
  u'5571813.50000000',
  1511135999999L,
  u'26828.85000446',
  231908,
  u'2837395.60000000',
  u'13679.41285759',
  u'3047928.46000000'],
 [1511136000000L,
  u'0.00495800',
  u'0.00546600',
  u'0.00400000',
  u'0.00411900',
  u'3910182.09000000',
  1511740799999L,
  u'17501.45090555',
  190617,
  u'1780108.47000000',
  u'7989.09761841',
  u'2870842.46000000'],
 [1511740800000L,
  u'0.00411900',
  u'0.00438100',
  u'0.00313200',
  u'0.00327500',
  u'3168996.20000000',
  1512345599999L,
  u'11269.53438873',
  164863,
  u'1360118.57000000',
  u'4804.75577662',
  u'3110653.46000000'],
 [1512345600000L,
  u'0.00327500',
  u'0.00382000',
  u'0.00183800',
  u'0.00220500',
  u'5110467.20000000',
  1512950399999L,
  u'13719.39164606',
  236175,
  u'2518893.00000000',
  u'6908.36260226',
  u'3264237.46000000'],
 [1512950400000L,
  u'0.00220200',
  u'0.00397800',
  u'0.00198000',
  u'0.00356100',
  u'9645212.58000000',
  1513555199999L,
  u'26817.82904376',
  432353,
  u'4086405.40000000',
  u'11403.84969005',
  u'3938166.46000000'],
 [1513555200000L,
  u'0.00356800',
  u'0.00476000',
  u'0.00347000',
  u'0.00432500',
  u'7416389.09000000',
  1514159999999L,
  u'31119.33610944',
  462358,
  u'3783027.40000000',
  u'15891.92602080',
  u'0'],
 [1514160000000L,
  u'0.00432300',
  u'0.00598000',
  u'0.00382500',
  u'0.00546100',
  u'6239360.66000000',
  1514764799999L,
  u'28737.35727401',
  448217,
  u'3224236.69000000',
  u'14829.67045937',
  u'0'],
 [1514764800000L,
  u'0.00546100',
  u'0.00735600',
  u'0.00520000',
  u'0.00629500',
  u'5258537.91000000',
  1515369599999L,
  u'32840.35233575',
  611840,
  u'2765034.31000000',
  u'17318.27625889',
  u'0'],
 [1515369600000L,
  u'0.00629800',
  u'0.01370000',
  u'0.00621300',
  u'0.01260000',
  u'5454442.10000000',
  1515974399999L,
  u'48522.34732727',
  772607,
  u'3050908.61000000',
  u'27144.62648409',
  u'0'],
 [1515974400000L,
  u'0.01260000',
  u'0.01441000',
  u'0.00902000',
  u'0.01152000',
  u'6872694.23000000',
  1516579199999L,
  u'84894.18100385',
  1196384,
  u'3446402.44000000',
  u'42765.75411644',
  u'0'],
 [1516579200000L,
  u'0.01152000',
  u'0.01300000',
  u'0.01096000',
  u'0.01297800',
  u'1683689.68000000',
  1517183999999L,
  u'20111.99773007',
  411054,
  u'818339.60000000',
  u'9782.93506087',
  u'0'],
 [1517184000000L,
  u'0.01297900',
  u'0.01520000',
  u'0.01180000',
  u'0.01307500',
  u'2377528.31000000',
  1517788799999L,
  u'32803.20195546',
  685218,
  u'1118122.14000000',
  u'15482.67246283',
  u'0'],
 [1517788800000L,
  u'0.01307500',
  u'0.01449000',
  u'0.01059200',
  u'0.01263000',
  u'2191491.21000000',
  1518393599999L,
  u'27954.12253793',
  661829,
  u'1039231.63000000',
  u'13328.44207766',
  u'0'],
 [1518393600000L,
  u'0.01263000',
  u'0.01343100',
  u'0.01182100',
  u'0.01206300',
  u'1445582.93000000',
  1518998399999L,
  u'18200.14653659',
  520099,
  u'673136.86000000',
  u'8483.81753367',
  u'0'],
 [1518998400000L,
  u'0.01206400',
  u'0.01243400',
  u'0.01088000',
  u'0.01218700',
  u'1149941.66000000',
  1519603199999L,
  u'13418.35400793',
  403560,
  u'566342.76000000',
  u'6605.96008605',
  u'0'],
 [1519603200000L,
  u'0.01218600',
  u'0.01398800',
  u'0.01025400',
  u'0.01035700',
  u'2202764.43000000',
  1520207999999L,
  u'27132.57485068',
  542024,
  u'1100133.17000000',
  u'13497.93592042',
  u'0'],
 [1520208000000L,
  u'0.01035700',
  u'0.01044700',
  u'0.00939300',
  u'0.00949100',
  u'1843443.25000000',
  1520812799999L,
  u'18196.29373453',
  433688,
  u'931443.62000000',
  u'9204.53341141',
  u'0'],
 [1520812800000L,
  u'0.00949100',
  u'0.00960000',
  u'0.00610000',
  u'0.00796000',
  u'2577762.23000000',
  1521417599999L,
  u'21382.64121103',
  431788,
  u'1295545.37000000',
  u'10740.34101363',
  u'0'],
 [1521417600000L,
  u'0.00796000',
  u'0.00882600',
  u'0.00751200',
  u'0.00773400',
  u'2859484.23000000',
  1522022399999L,
  u'22945.89864408',
  449252,
  u'1429521.17000000',
  u'11473.10575426',
  u'0'],
 [1522022400000L,
  u'0.00773400',
  u'0.00779600',
  u'0.00670000',
  u'0.00694000',
  u'2921631.28000000',
  1522627199999L,
  u'21084.84511908',
  387297,
  u'1526747.74000000',
  u'11019.99038074',
  u'0'],
 [1522627200000L,
  u'0.00693400',
  u'0.00722500',
  u'0.00666800',
  u'0.00703400',
  u'2306330.67000000',
  1523231999999L,
  u'15927.93818350',
  297339,
  u'1182855.55000000',
  u'8170.75379790',
  u'0'],
 [1523232000000L,
  u'0.00703400',
  u'0.00873200',
  u'0.00699700',
  u'0.00845500',
  u'4510081.38000000',
  1523836799999L,
  u'35872.39126509',
  597567,
  u'2267785.15000000',
  u'18045.68195878',
  u'0'],
 [1523836800000L,
  u'0.00845300',
  u'0.00915000',
  u'0.00810200',
  u'0.00837200',
  u'3159168.31000000',
  1524441599999L,
  u'27212.07248458',
  484102,
  u'1572584.57000000',
  u'13555.28103639',
  u'0'],
 [1524441600000L,
  u'0.00837200',
  u'0.00986000',
  u'0.00777700',
  u'0.00961700',
  u'3827332.81000000',
  1525046399999L,
  u'32733.75899798',
  584717,
  u'1922092.45000000',
  u'16476.52023584',
  u'0'],
 [1525046400000L,
  u'0.00961700',
  u'0.01000000',
  u'0.00845300',
  u'0.00866000',
  u'3116945.65000000',
  1525651199999L,
  u'28276.52750444',
  503500,
  u'1526482.54000000',
  u'13852.72863869',
  u'0'],
 [1525651200000L,
  u'0.00866300',
  u'0.00866900',
  u'0.00700000',
  u'0.00775800',
  u'2408073.60000000',
  1526255999999L,
  u'19169.09985469',
  402613,
  u'1158950.63000000',
  u'9221.00120364',
  u'0'],
 [1526256000000L,
  u'0.00775700',
  u'0.00795500',
  u'0.00706100',
  u'0.00747300',
  u'2084001.35000000',
  1526860799999L,
  u'15432.70305754',
  338164,
  u'1102466.81000000',
  u'8170.62526437',
  u'0'],
 [1526860800000L,
  u'0.00747200',
  u'0.00774300',
  u'0.00684900',
  u'0.00719900',
  u'1905191.74000000',
  1527465599999L,
  u'13693.33176341',
  297924,
  u'989970.14000000',
  u'7114.19615214',
  u'0'],
 [1527465600000L,
  u'0.00719900',
  u'0.00750000',
  u'0.00670100',
  u'0.00731400',
  u'905144.68000000',
  1528070399999L,
  u'6459.03192964',
  189002,
  u'414640.41000000',
  u'2966.78871898',
  u'0'],
 [1528070400000L,
  u'0.00731500',
  u'0.00739000',
  u'0.00657300',
  u'0.00670100',
  u'739120.52000000',
  1528675199999L,
  u'5149.39910413',
  149143,
  u'367398.67000000',
  u'2558.71953365',
  u'0'],
 [1528675200000L,
  u'0.00669500',
  u'0.00680400',
  u'0.00569000',
  u'0.00598100',
  u'948957.74000000',
  1529279999999L,
  u'5814.42899684',
  200671,
  u'430647.42000000',
  u'2632.62844596',
  u'0'],
 [1529280000000L,
  u'0.00598200',
  u'0.00605700',
  u'0.00500000',
  u'0.00506200',
  u'2384134.84000000',
  1529884799999L,
  u'13340.56040371',
  283596,
  u'1158193.15000000',
  u'6496.42070412',
  u'0'],
 [1529884800000L,
  u'0.00506200',
  u'0.00522900',
  u'0.00464800',
  u'0.00491800',
  u'2346510.20000000',
  1530489599999L,
  u'11484.85956673',
  286112,
  u'1093622.54000000',
  u'5352.01770025',
  u'0'],
 [1530489600000L,
  u'0.00491700',
  u'0.00652100',
  u'0.00489200',
  u'0.00558300',
  u'3425514.06000000',
  1531094399999L,
  u'19770.96781618',
  399434,
  u'1749068.89000000',
  u'10104.84462548',
  u'0']]