In [1]:
import barchart
from barchart import getHistory, getQuote, CONFIG

API key setup


In [2]:
#barchart.API_KEY = 'YOURAPIKEY'

You can also set an environment variable using Bash

export BARCHART_API_KEY="YOURAPIKEY"

Cache queries

requests_cache is optional

use it to have a cache mechanism

a session parameter can be pass to functions


In [3]:
import datetime
import requests_cache
session = requests_cache.CachedSession(cache_name='cache',
    backend='sqlite', expire_after=datetime.timedelta(days=1))
#session = None # pass a None session to avoid caching queries

getQuote with ONE symbol


In [4]:
symbol = "^EURUSD"
quote = getQuote(symbol, session=session)
quote # quote is a dict


Out[4]:
{'close': '',
 'dayCode': 'S',
 'exchange': 'BATS',
 'flag': '',
 'high': 30.82,
 'lastPrice': 30.66,
 'low': 30.44,
 'mode': 'i',
 'name': 'Eni S.P.A',
 'netChange': 0.36,
 'open': 30.52,
 'percentChange': 1.19,
 'serverTimestamp': datetime.datetime(2015, 9, 29, 14, 40, 10, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400))),
 'symbol': 'E',
 'tradeTimestamp': datetime.datetime(2015, 9, 29, 15, 25, 10, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400))),
 'unitCode': None,
 'volume': 12620}

getQuote with SEVERAL symbols


In [5]:
symbols = ["ZC*1", "IBM", "GOOGL" , "^EURUSD"]
quotes = getQuote(symbols, session=session)
quotes # quotes is a Pandas DataFrame
#print(quotes.dtypes)
#print(type(quotes['serverTimestamp'][0])) # should be a pandas.tslib.Timestamp


Out[5]:
close dayCode exchange flag high lastPrice low mode name netChange open percentChange serverTimestamp symbol tradeTimestamp unitCode volume
0 389 S CBOT s 389.50000 389.00000 385.00000 d Corn 2.25000 387.2500 0.58 2015-09-29 05:00:00 ZCZ15 2015-09-29 05:00:00 -1 117311
1 S BATS 142.54000 141.97000 141.17000 i International Business Machines -0.55000 142.0000 -0.39 2015-09-29 19:41:56 IBM 2015-09-29 20:26:56 None 167005
2 S BATS 634.67000 619.44000 618.49000 i Google Inc -4.81000 625.7600 -0.77 2015-09-29 19:41:49 GOOGL 2015-09-29 20:26:49 None 129034
3 S FOREX 1.12806 1.12604 1.11933 i Euro Fx/U.S. Dollar 0.00172 1.1242 0.15 2015-09-29 19:42:04 ^EURUSD 2015-09-29 19:32:04 5 488052

In [6]:
CONFIG.output_pandas = False
quotes = getQuote(symbols, session=session)
print(quotes) # quotes is a Pandas DataFrame
CONFIG.output_pandas = True


[{'name': 'Corn', 'dayCode': 'S', 'close': 389, 'high': 389.5, 'open': 387.25, 'flag': 's', 'percentChange': 0.58, 'netChange': 2.25, 'unitCode': '-1', 'lastPrice': 389, 'symbol': 'ZCZ15', 'tradeTimestamp': datetime.datetime(2015, 9, 29, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400))), 'volume': 117311, 'low': 385, 'exchange': 'CBOT', 'mode': 'd', 'serverTimestamp': datetime.datetime(2015, 9, 29, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))}, {'name': 'International Business Machines', 'dayCode': 'S', 'close': '', 'high': 142.54, 'open': 142, 'flag': '', 'percentChange': -0.39, 'netChange': -0.55, 'unitCode': None, 'lastPrice': 141.97, 'symbol': 'IBM', 'tradeTimestamp': datetime.datetime(2015, 9, 29, 15, 26, 56, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400))), 'volume': 167005, 'low': 141.17, 'exchange': 'BATS', 'mode': 'i', 'serverTimestamp': datetime.datetime(2015, 9, 29, 14, 41, 56, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))}, {'name': 'Google Inc', 'dayCode': 'S', 'close': '', 'high': 634.67, 'open': 625.76, 'flag': '', 'percentChange': -0.77, 'netChange': -4.81, 'unitCode': None, 'lastPrice': 619.44, 'symbol': 'GOOGL', 'tradeTimestamp': datetime.datetime(2015, 9, 29, 15, 26, 49, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400))), 'volume': 129034, 'low': 618.49, 'exchange': 'BATS', 'mode': 'i', 'serverTimestamp': datetime.datetime(2015, 9, 29, 14, 41, 49, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))}, {'name': 'Euro Fx/U.S. Dollar', 'dayCode': 'S', 'close': '', 'high': 1.12806, 'open': 1.1242, 'flag': '', 'percentChange': 0.15, 'netChange': 0.00172, 'unitCode': '5', 'lastPrice': 1.12604, 'symbol': '^EURUSD', 'tradeTimestamp': datetime.datetime(2015, 9, 29, 14, 32, 4, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400))), 'volume': 488052, 'low': 1.11933, 'exchange': 'FOREX', 'mode': 'i', 'serverTimestamp': datetime.datetime(2015, 9, 29, 14, 42, 4, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))}]

getHistory with ONE symbol


In [7]:
symbol = 'IBM'
startDate = datetime.date(year=2014, month=9, day=28)
history = getHistory(symbol, typ='daily', startDate=startDate, session=session)
history
#print(history.dtypes)
#print(type(history['timestamp'][0])) # should be a pandas.tslib.Timestamp
#print(type(history.index[0])) # should be a pandas.tslib.Timestamp
#print(type(history['tradingDay'][0])) # should be a pandas.tslib.Timestamp


Out[7]:
close high low open openInterest symbol tradingDay volume
timestamp
2014-09-29 04:00:00 184.1057 184.4163 182.6300 183.0086 None IBM 2014-09-29 2415183
2014-09-30 04:00:00 184.2901 185.2803 183.6299 184.1057 None IBM 2014-09-30 3020653
2014-10-01 04:00:00 181.7077 184.8435 181.3388 184.3678 None IBM 2014-10-01 3835019
2014-10-02 04:00:00 181.4553 182.2999 180.8049 182.1834 None IBM 2014-10-02 2352246
2014-10-03 04:00:00 183.1640 183.8435 182.0863 182.6203 None IBM 2014-10-03 3163831
2014-10-06 04:00:00 183.5231 185.3192 183.2028 184.3387 None IBM 2014-10-06 2163230
2014-10-07 04:00:00 180.2903 182.6300 180.1253 182.2902 None IBM 2014-10-07 3084001
2014-10-08 04:00:00 183.8338 184.0668 180.1933 180.5428 None IBM 2014-10-08 3075040
2014-10-09 04:00:00 180.9796 183.9697 180.6593 183.6008 None IBM 2014-10-09 2704527
2014-10-10 04:00:00 180.5039 182.2611 179.6982 180.4360 None IBM 2014-10-10 5243318
2014-10-13 04:00:00 178.1643 181.2029 178.0672 180.0768 None IBM 2014-10-13 3708115
2014-10-14 04:00:00 178.4361 180.3000 178.2322 179.4943 None IBM 2014-10-14 4042885
2014-10-15 04:00:00 176.4459 178.4264 173.5335 177.3099 None IBM 2014-10-15 7104947
2014-10-16 04:00:00 174.5916 176.1838 173.4752 174.5528 None IBM 2014-10-16 5746296
2014-10-17 04:00:00 176.7372 177.5041 174.9606 175.9508 None IBM 2014-10-17 4485296
2014-10-20 04:00:00 164.1651 165.3592 161.8254 161.9710 None IBM 2014-10-20 24120416
2014-10-21 04:00:00 158.4664 161.8157 156.9616 161.5439 None IBM 2014-10-21 21583480
2014-10-22 04:00:00 157.0684 160.5828 156.3986 157.6606 None IBM 2014-10-22 11424816
2014-10-23 04:00:00 157.4470 158.0781 156.8257 157.3888 None IBM 2014-10-23 7828667
2014-10-24 04:00:00 157.3499 157.6994 156.7383 157.3499 None IBM 2014-10-24 6852273
2014-10-27 04:00:00 157.1461 158.1557 157.0878 157.2723 None IBM 2014-10-27 5139385
2014-10-28 04:00:00 158.8256 158.8256 157.0781 157.2335 None IBM 2014-10-28 8134183
2014-10-29 04:00:00 158.6897 159.8158 158.0101 159.5343 None IBM 2014-10-29 4884136
2014-10-30 04:00:00 159.5537 159.8158 158.2625 158.7285 None IBM 2014-10-30 4013013
2014-10-31 04:00:00 159.6022 160.7575 158.8450 160.5536 None IBM 2014-10-31 5993202
2014-11-03 05:00:00 159.5634 159.7381 158.6120 159.4566 None IBM 2014-11-03 4833457
2014-11-04 05:00:00 157.9033 159.5634 157.5053 159.5440 None IBM 2014-11-04 4374668
2014-11-05 05:00:00 157.0975 158.7673 156.8451 158.3693 None IBM 2014-11-05 4228090
2014-11-06 05:00:00 157.8209 157.8893 156.4426 157.6449 None IBM 2014-11-06 4161393
2014-11-07 05:00:00 158.4171 158.5540 157.2246 157.7818 None IBM 2014-11-07 3575385
... ... ... ... ... ... ... ... ...
2015-08-17 04:00:00 156.3100 156.6900 154.7000 155.2000 None IBM 2015-08-17 2249500
2015-08-18 04:00:00 156.0100 156.5200 155.2500 155.5100 None IBM 2015-08-18 2018300
2015-08-19 04:00:00 153.9400 155.6700 153.4100 155.1500 None IBM 2015-08-19 4206300
2015-08-20 04:00:00 152.6600 153.9100 152.5000 152.7400 None IBM 2015-08-20 4011500
2015-08-21 04:00:00 148.8500 153.1900 148.7000 151.5000 None IBM 2015-08-21 7362100
2015-08-24 04:00:00 143.4700 147.7600 142.3200 143.4700 None IBM 2015-08-24 10189600
2015-08-25 04:00:00 140.9600 147.1100 140.6200 146.9400 None IBM 2015-08-25 7073200
2015-08-26 04:00:00 146.7000 146.9800 142.1400 144.0900 None IBM 2015-08-26 6221700
2015-08-27 04:00:00 148.5400 148.9700 145.6600 148.5000 None IBM 2015-08-27 4976600
2015-08-28 04:00:00 147.9800 148.2000 147.1800 147.7500 None IBM 2015-08-28 4076200
2015-08-31 04:00:00 147.8900 148.4000 146.2600 147.3800 None IBM 2015-08-31 4093000
2015-09-01 04:00:00 142.6800 144.9800 141.8500 144.9100 None IBM 2015-09-01 5272000
2015-09-02 04:00:00 145.0500 145.0800 143.1800 144.7400 None IBM 2015-09-02 4252000
2015-09-03 04:00:00 146.7800 148.0300 145.7700 146.0500 None IBM 2015-09-03 3603500
2015-09-04 04:00:00 143.7000 145.4000 143.3200 144.5700 None IBM 2015-09-04 4201000
2015-09-08 04:00:00 147.2300 147.3400 145.6600 145.8600 None IBM 2015-09-08 3933200
2015-09-09 04:00:00 145.0500 149.0400 144.8500 148.7400 None IBM 2015-09-09 3407700
2015-09-10 04:00:00 146.2000 147.1600 144.5100 145.8500 None IBM 2015-09-10 3461600
2015-09-11 04:00:00 147.3700 147.5000 145.6700 145.7100 None IBM 2015-09-11 3115000
2015-09-14 04:00:00 145.6500 147.3700 145.4100 147.3700 None IBM 2015-09-14 3226700
2015-09-15 04:00:00 147.5300 147.9300 145.7600 146.6000 None IBM 2015-09-15 2717000
2015-09-16 04:00:00 148.4100 148.8900 147.5400 147.8400 None IBM 2015-09-16 2799200
2015-09-17 04:00:00 148.1400 149.6800 147.3000 148.1000 None IBM 2015-09-17 4002900
2015-09-18 04:00:00 144.5100 146.3800 143.9800 146.0500 None IBM 2015-09-18 7975800
2015-09-21 04:00:00 146.4800 146.9800 144.9200 145.3900 None IBM 2015-09-21 3825000
2015-09-22 04:00:00 144.4300 145.0600 143.7700 144.6200 None IBM 2015-09-22 3564000
2015-09-23 04:00:00 143.6600 144.5700 142.7500 144.2100 None IBM 2015-09-23 2674200
2015-09-24 04:00:00 144.4100 145.0700 141.9500 142.6000 None IBM 2015-09-24 3280200
2015-09-25 04:00:00 145.4200 146.2700 144.5300 145.5500 None IBM 2015-09-25 3474400
2015-09-28 04:00:00 142.5200 145.3800 142.4700 144.4200 None IBM 2015-09-28 4317300

252 rows × 8 columns

getHistory with SEVERAL symbols


In [8]:
symbols = ["ZC*1", "IBM", "GOOGL" , "^EURUSD"]
histories = getHistory(symbols, typ='daily', startDate=startDate, session=session)
histories
#print(histories.dtypes)
#print(type(histories.index[0])) # should be a pandas.tslib.Timestamp
#print(type(histories['timestamp'][0])) # should be a pandas.tslib.Timestamp


Out[8]:
<class 'pandas.core.panel.Panel'>
Dimensions: 8 (items) x 511 (major_axis) x 4 (minor_axis)
Items axis: close to volume
Major_axis axis: 2014-09-29 04:00:00 to 2015-09-29 05:00:00
Minor_axis axis: ZC*1 to ^EURUSD

In [9]:
#histories.loc[:, :, "IBM"] #??