Notebook
In [7]:
#Importing necessary libraries (Numpy, Pandas, and Plot)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [8]:
#Getting open and close prices of QQQ 
QQQ_OPrices = get_pricing("QQQ", end_date = "01-01-2018", fields = "open_price").dropna()
QQQ_CPrices = get_pricing("QQQ", end_date = "01-01-2018", fields = "close_price").dropna()
In [32]:
#shifting close prices
shifted_CP= QQQ_CPrices.shift(periods=1, freq= None , axis=0)
shifted_CP.head(5)
Out[32]:
2013-01-03 00:00:00+00:00       NaN
2013-01-04 00:00:00+00:00    63.169
2013-01-07 00:00:00+00:00    62.943
2013-01-08 00:00:00+00:00    62.961
2013-01-09 00:00:00+00:00    62.857
Freq: C, Name: Equity(19920 [QQQ]), dtype: float64
In [33]:
#Calculating the overnight returns 
overnight_returns = np.log(QQQ_OPrices[0:] / shifted_CP[0:])
overnight_returns.head(5)
Out[33]:
2013-01-03 00:00:00+00:00         NaN
2013-01-04 00:00:00+00:00   -0.000142
2013-01-07 00:00:00+00:00   -0.002720
2013-01-08 00:00:00+00:00   -0.000302
2013-01-09 00:00:00+00:00    0.001065
Freq: C, Name: Equity(19920 [QQQ]), dtype: float64
In [36]:
#Calculating the cumulative sum of overnight returns 
onr_cumsum = np.cumsum(overnight_returns).dropna()
print overnight_returns.head(5)
onr_cumsum.head(10)
2013-01-03 00:00:00+00:00         NaN
2013-01-04 00:00:00+00:00   -0.000142
2013-01-07 00:00:00+00:00   -0.002720
2013-01-08 00:00:00+00:00   -0.000302
2013-01-09 00:00:00+00:00    0.001065
Freq: C, Name: Equity(19920 [QQQ]), dtype: float64
Out[36]:
2013-01-04 00:00:00+00:00   -0.000142
2013-01-07 00:00:00+00:00   -0.002863
2013-01-08 00:00:00+00:00   -0.003165
2013-01-09 00:00:00+00:00   -0.002099
2013-01-10 00:00:00+00:00    0.004475
2013-01-11 00:00:00+00:00    0.004333
2013-01-14 00:00:00+00:00   -0.000891
2013-01-15 00:00:00+00:00   -0.005837
2013-01-16 00:00:00+00:00   -0.004043
2013-01-17 00:00:00+00:00    0.000142
Freq: C, Name: Equity(19920 [QQQ]), dtype: float64
In [38]:
onr_cumsum.plot();
plt.title("QQQ" + " Overnight Cumulative  Return ")
plt.ylabel("Cumulative  Return")
plt.xlabel("Date");