In [2]:
# dev system path adjustment - normal usage would not include this cell
import sys
sys.path.insert(0, "/Users/jasonlewris/Desktop/after_hours/afterhours")

In [5]:
from afterhours import AfterHours

In [ ]:
# in packaged version, import follows:
# from afterhours.afterhours import AfterHours

Securing pricing data for pre and after market trading


In [6]:
# getting started is fairly quick. Simply specify the ticker symbol of interest 
# and pre or after hours trading when instantiating AfterHours

ticker = 'aapl'

AH = AfterHours(ticker=ticker, typeof='pre')

In [7]:
# there are several price types we can secure. This include highprice and lowprice
AH.getdata(datatype='highprice')


Out[7]:
160.7

In [8]:
# and low price
AH.getdata(datatype='lowprice')


Out[8]:
158.27

Securing volume data


In [9]:
# securing volume data follows the same format as pricing
AH.getdata(datatype='volume')


Out[9]:
'1,195,487'

Securing date stamps for trade


In [10]:
# once again, very similar usage for time stamps
AH.getdata(datatype='hightime')


Out[10]:
datetime.datetime(2018, 2, 2, 4, 50, 11)

In [11]:
# and for low time
AH.getdata(datatype='lowtime')


Out[11]:
datetime.datetime(2018, 2, 2, 7, 55, 45)

Securing all after or pre market trading data


In [12]:
# we can secure all the trading data for a specific symbol using the following
df = AH.secure_all_pages()

In [23]:
df.head()


Out[23]:
Price Time Volume Ticker Cancelled
0 159.11 2018-02-02 09:30:00 100 aapl False
1 159.10 2018-02-02 09:30:00 200 aapl False
2 159.10 2018-02-02 09:30:00 100 aapl False
3 159.10 2018-02-02 09:30:00 63 aapl False
4 159.10 2018-02-02 09:30:00 37 aapl False

In [24]:
# exmplore the data a bit
df.describe()


Out[24]:
Price Volume
count 50.000000 50.000000
mean 159.101600 131.300000
std 0.027654 166.606986
min 159.000000 1.000000
25% 159.100000 38.000000
50% 159.110000 96.000000
75% 159.110000 186.000000
max 159.150000 1000.000000

In [16]:
# to examine any cancelled trades - no cancelled trades for apple today
df[df['Cancelled'] != False]


Out[16]:
Price Time Volume Ticker Cancelled

In [18]:
# we can get the low and max trading price and volume
df['Price'].min()


Out[18]:
159.0

In [19]:
df['Price'].max()


Out[19]:
159.15000000000001

In [20]:
# and for volume
df['Volume'].min()


Out[20]:
1

In [21]:
df['Volume'].max()


Out[21]:
1000

In [29]:
# lets filter out trades with less than 100 volume
df[df['Volume'] > 100]['Price'].mean()


Out[29]:
159.09312500000001

In [28]:
df[df['Volume'] <= 100]['Price'].mean() # slightly higher average dollar value for low volume trades


Out[28]:
159.10558823529414

In [22]:
df.head()


Out[22]:
Price Time Volume Ticker Cancelled
0 159.11 2018-02-02 09:30:00 100 aapl False
1 159.10 2018-02-02 09:30:00 200 aapl False
2 159.10 2018-02-02 09:30:00 100 aapl False
3 159.10 2018-02-02 09:30:00 63 aapl False
4 159.10 2018-02-02 09:30:00 37 aapl False

Continuous monitoring


In [31]:
# to continuously monitor for updates during a high frequency trading time, we 
# can specify the package to run every n seconds for a maximum of num_iter times
AH.run_every(seconds=5, num_iter=2)


2
1

In [32]:
# and access the data directly 
AH.df.head()


Out[32]:
Cancelled Price Ticker Time Volume
0 False 159.11 aapl 2018-02-02 09:30:00 100
1 False 159.10 aapl 2018-02-02 09:30:00 200
2 False 159.10 aapl 2018-02-02 09:30:00 100
3 False 159.10 aapl 2018-02-02 09:30:00 63
4 False 159.10 aapl 2018-02-02 09:30:00 37