In [4]:
%matplotlib inline

from pymongo import MongoClient
import math
import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)

In [5]:
def divtd(td1, td2):
    us1 = td1.microseconds + 1000000 * (td1.seconds + 86400 * td1.days)
    us2 = td2.microseconds + 1000000 * (td2.seconds + 86400 * td2.days)
    return float(us1) / us2

try:
    import config
    print('using local config file')
    mongodb_uri = config.mongodb_uri
except:
    print('using environment variable')
    mongodb_uri = os.getenv('MONGODB_URI')


using local config file

In [6]:
print("Analyzing Data")
success = True

if success:
    try:
        client = MongoClient(mongodb_uri)
        db = client.get_database()
    except Exception as e:
        print('mongo login error ', str(e))
        success = False
if success:
    try:
        dataCursor = db.percentageMoveTest.find(projection={'_id': False},sort=[("timestamp", 1)])
    except Exception as e:
        print('mongo data cursor error ', str(e))
        success = False
        
if success:
    try:
        df =  pd.DataFrame(list(dataCursor)).set_index(['timestamp'])
        print(df)
        increase = df+1
        cumulative = increase.cumprod(axis=0)
        cumulative = cumulative - cumulative.iloc[0]
        print(cumulative)
    except Exception as e:
        print('mongo data cursor error ', str(e))
        success = False  
        
"""       
if success:
    try:
        dataCursor = db.tracking.find(projection={'_id': False},sort=[("timestamp", 1)])
    except Exception as e:
        print('mongo data cursor error ', str(e))
        success = False
        
if success:
    try:
        tracking =  pd.DataFrame(list(dataCursor)).set_index(['timestamp'])
        print(tracking)
    except Exception as e:
        print('mongo data cursor error ', str(e))
        success = False 
"""


Analyzing Data
                         90dayTreasury     portfolio           spy  \
timestamp                                                            
2016-09-15 13:30:08.905   1.042600e-07  9.502560e-06  1.732656e-05   
2016-09-15 13:31:08.905   1.042600e-07 -1.068206e-05  9.669740e-06   
2016-09-15 13:32:08.905   1.042600e-07 -2.054629e-05 -1.907024e-05   
2016-09-15 13:33:08.905   1.042600e-07  2.051233e-05 -1.202655e-06   
2016-09-15 13:34:08.905   1.042600e-07  3.165038e-05  3.346574e-05   
2016-09-15 13:35:08.905   1.042600e-07 -4.241881e-07  4.377680e-05   
2016-09-15 13:36:08.905   1.042600e-07  1.150198e-05  1.056903e-05   
2016-09-15 13:37:08.905   1.042600e-07 -2.756307e-06 -2.638356e-06   
2016-09-15 13:38:08.905   1.042600e-07 -1.248820e-05  2.048732e-05   
2016-09-15 13:39:08.905   1.042600e-07  1.104375e-05  1.261777e-05   
2016-09-15 13:40:08.905   1.042600e-07  2.284670e-05  5.897027e-06   
2016-09-15 13:41:08.905   1.042600e-07 -5.786695e-06  1.419843e-05   
2016-09-15 13:42:08.905   1.042600e-07 -1.345121e-06 -6.389505e-06   
2016-09-15 13:43:08.905   1.042600e-07 -2.787521e-05 -4.172200e-05   
2016-09-15 13:44:08.905   1.042600e-07  4.596871e-06 -1.341571e-05   
2016-09-15 13:45:08.905   1.042600e-07  1.615275e-05  2.859447e-05   
2016-09-15 13:46:08.905   1.042600e-07  9.957465e-07 -1.193861e-05   
2016-09-15 13:47:08.905   1.042600e-07 -7.031693e-07  1.678258e-05   
2016-09-15 13:48:08.905   1.042600e-07 -2.799324e-05 -3.043182e-05   
2016-09-15 13:49:08.905   1.042600e-07 -3.695282e-06 -1.491205e-05   
2016-09-15 13:50:08.905   1.042600e-07 -4.976693e-06  3.175914e-05   
2016-09-15 13:51:08.905   1.042600e-07  1.936439e-05  3.061050e-05   
2016-09-15 13:52:08.905   1.042600e-07  6.595964e-07  1.172348e-05   
2016-09-15 13:53:08.905   1.042600e-07  3.486452e-05  5.537832e-05   
2016-09-15 13:54:08.905   1.042600e-07 -2.896507e-06  1.566402e-05   
2016-09-15 13:55:08.905   1.042600e-07  3.167075e-05  2.794278e-05   
2016-09-15 13:56:08.905   1.042600e-07 -6.751124e-06 -6.904177e-07   
2016-09-15 13:57:08.905   1.042600e-07  1.264379e-05 -4.278551e-06   
2016-09-15 13:58:08.905   1.042600e-07  2.403249e-05  1.323484e-05   
2016-09-15 13:59:08.905   1.042600e-07  1.445548e-05  3.282187e-06   
...                                ...           ...           ...   
2017-09-14 19:30:08.905   1.042600e-07  1.931961e-05  1.947669e-05   
2017-09-14 19:31:08.905   1.042600e-07  2.132474e-05  2.336068e-05   
2017-09-14 19:32:08.905   1.042600e-07  2.178105e-05  2.220791e-05   
2017-09-14 19:33:08.905   1.042600e-07 -1.139265e-05  3.892450e-06   
2017-09-14 19:34:08.905   1.042600e-07  1.211062e-05  1.359886e-05   
2017-09-14 19:35:08.905   1.042600e-07  8.854727e-06  2.566905e-05   
2017-09-14 19:36:08.905   1.042600e-07 -2.967100e-05 -5.001316e-05   
2017-09-14 19:37:08.905   1.042600e-07  4.140727e-05  4.262602e-05   
2017-09-14 19:38:08.905   1.042600e-07  1.725389e-05  5.396904e-06   
2017-09-14 19:39:08.905   1.042600e-07 -3.082603e-05 -3.509411e-05   
2017-09-14 19:40:08.905   1.042600e-07 -5.511431e-06 -5.417480e-06   
2017-09-14 19:41:08.905   1.042600e-07 -2.923344e-05  3.101948e-05   
2017-09-14 19:42:08.905   1.042600e-07 -2.979902e-05 -3.287521e-05   
2017-09-14 19:43:08.905   1.042600e-07  5.247618e-06 -1.062989e-05   
2017-09-14 19:44:08.905   1.042600e-07  6.906723e-06  1.217298e-05   
2017-09-14 19:45:08.905   1.042600e-07  1.299221e-05  1.921505e-05   
2017-09-14 19:46:08.905   1.042600e-07 -5.955946e-06 -3.036878e-06   
2017-09-14 19:47:08.905   1.042600e-07  1.443935e-06 -4.496851e-05   
2017-09-14 19:48:08.905   1.042600e-07  2.639770e-05  3.641148e-05   
2017-09-14 19:49:08.905   1.042600e-07  2.012513e-06 -1.933888e-06   
2017-09-14 19:50:08.905   1.042600e-07 -1.321174e-05 -8.126649e-06   
2017-09-14 19:51:08.905   1.042600e-07 -1.122756e-05 -1.247180e-05   
2017-09-14 19:52:08.905   1.042600e-07  4.278435e-06  5.786775e-06   
2017-09-14 19:53:08.905   1.042600e-07  2.491826e-05  2.443111e-05   
2017-09-14 19:54:08.905   1.042600e-07  2.072085e-05  3.381196e-05   
2017-09-14 19:55:08.905   1.042600e-07 -7.420221e-06 -6.834554e-06   
2017-09-14 19:56:08.905   1.042600e-07  2.027617e-05 -3.039314e-05   
2017-09-14 19:57:08.905   1.042600e-07 -1.711009e-05 -3.122751e-05   
2017-09-14 19:58:08.905   1.042600e-07 -2.404214e-05  1.154739e-05   
2017-09-14 19:59:08.905   1.042600e-07 -2.957851e-05 -3.618723e-05   

                                  tlt  
timestamp                              
2016-09-15 13:30:08.905 -2.796710e-05  
2016-09-15 13:31:08.905 -1.931863e-05  
2016-09-15 13:32:08.905 -2.222772e-05  
2016-09-15 13:33:08.905  2.168834e-05  
2016-09-15 13:34:08.905  1.363946e-05  
2016-09-15 13:35:08.905 -9.288270e-06  
2016-09-15 13:36:08.905  1.304531e-05  
2016-09-15 13:37:08.905 -2.822077e-06  
2016-09-15 13:38:08.905 -1.847448e-05  
2016-09-15 13:39:08.905 -1.545896e-05  
2016-09-15 13:40:08.905  2.497085e-05  
2016-09-15 13:41:08.905 -3.306520e-05  
2016-09-15 13:42:08.905  3.336095e-07  
2016-09-15 13:43:08.905 -5.025378e-06  
2016-09-15 13:44:08.905  2.980608e-05  
2016-09-15 13:45:08.905 -1.589134e-05  
2016-09-15 13:46:08.905  1.122720e-05  
2016-09-15 13:47:08.905 -6.880386e-06  
2016-09-15 13:48:08.905 -1.815870e-05  
2016-09-15 13:49:08.905  1.186126e-05  
2016-09-15 13:50:08.905 -1.364502e-05  
2016-09-15 13:51:08.905 -1.432597e-05  
2016-09-15 13:52:08.905 -1.969025e-05  
2016-09-15 13:53:08.905  2.160674e-05  
2016-09-15 13:54:08.905 -5.408087e-06  
2016-09-15 13:55:08.905  3.559376e-05  
2016-09-15 13:56:08.905 -1.213115e-05  
2016-09-15 13:57:08.905  1.350705e-05  
2016-09-15 13:58:08.905  2.942075e-05  
2016-09-15 13:59:08.905  1.721048e-05  
...                               ...  
2017-09-14 19:30:08.905 -2.513459e-05  
2017-09-14 19:31:08.905  1.017176e-05  
2017-09-14 19:32:08.905  1.695404e-05  
2017-09-14 19:33:08.905 -1.601398e-05  
2017-09-14 19:34:08.905  9.403792e-06  
2017-09-14 19:35:08.905 -4.159162e-05  
2017-09-14 19:36:08.905 -2.596819e-05  
2017-09-14 19:37:08.905  4.018505e-05  
2017-09-14 19:38:08.905  2.142425e-05  
2017-09-14 19:39:08.905  1.369246e-05  
2017-09-14 19:40:08.905 -8.781106e-06  
2017-09-14 19:41:08.905 -2.986020e-05  
2017-09-14 19:42:08.905 -2.847323e-05  
2017-09-14 19:43:08.905  1.038342e-05  
2017-09-14 19:44:08.905  3.473821e-06  
2017-09-14 19:45:08.905 -1.091255e-05  
2017-09-14 19:46:08.905 -1.818585e-05  
2017-09-14 19:47:08.905  1.765203e-06  
2017-09-14 19:48:08.905 -9.528152e-06  
2017-09-14 19:49:08.905  4.142693e-06  
2017-09-14 19:50:08.905 -3.407593e-05  
2017-09-14 19:51:08.905  8.790382e-06  
2017-09-14 19:52:08.905  2.760328e-06  
2017-09-14 19:53:08.905  2.498226e-05  
2017-09-14 19:54:08.905  2.679045e-06  
2017-09-14 19:55:08.905 -1.292683e-05  
2017-09-14 19:56:08.905  3.392285e-05  
2017-09-14 19:57:08.905 -1.166599e-05  
2017-09-14 19:58:08.905 -2.602549e-05  
2017-09-14 19:59:08.905  1.952647e-05  

[101790 rows x 4 columns]
                         90dayTreasury     portfolio       spy       tlt
timestamp                                                               
2016-09-15 13:30:08.905   0.000000e+00  0.000000e+00  0.000000  0.000000
2016-09-15 13:31:08.905   1.042600e-07 -1.068216e-05  0.000010 -0.000019
2016-09-15 13:32:08.905   2.085200e-07 -3.122843e-05 -0.000009 -0.000042
2016-09-15 13:33:08.905   3.127801e-07 -1.071654e-05 -0.000011 -0.000020
2016-09-15 13:34:08.905   4.170401e-07  2.093380e-05  0.000023 -0.000006
2016-09-15 13:35:08.905   5.213002e-07  2.050960e-05  0.000067 -0.000016
2016-09-15 13:36:08.905   6.255602e-07  3.201192e-05  0.000077 -0.000002
2016-09-15 13:37:08.905   7.298203e-07  2.925550e-05  0.000075 -0.000005
2016-09-15 13:38:08.905   8.340804e-07  1.676681e-05  0.000095 -0.000024
2016-09-15 13:39:08.905   9.383405e-07  2.781085e-05  0.000108 -0.000039
2016-09-15 13:40:08.905   1.042601e-06  5.065840e-05  0.000114 -0.000014
2016-09-15 13:41:08.905   1.146861e-06  4.487136e-05  0.000128 -0.000047
2016-09-15 13:42:08.905   1.251121e-06  4.352616e-05  0.000121 -0.000047
2016-09-15 13:43:08.905   1.355381e-06  1.564948e-05  0.000080 -0.000052
2016-09-15 13:44:08.905   1.459641e-06  2.024646e-05  0.000066 -0.000022
2016-09-15 13:45:08.905   1.563901e-06  3.639969e-05  0.000095 -0.000038
2016-09-15 13:46:08.905   1.668161e-06  3.739548e-05  0.000083 -0.000027
2016-09-15 13:47:08.905   1.772422e-06  3.669228e-05  0.000100 -0.000034
2016-09-15 13:48:08.905   1.876682e-06  8.697746e-06  0.000069 -0.000052
2016-09-15 13:49:08.905   1.980942e-06  5.002397e-06  0.000054 -0.000040
2016-09-15 13:50:08.905   2.085202e-06  2.563122e-08  0.000086 -0.000054
2016-09-15 13:51:08.905   2.189463e-06  1.939020e-05  0.000117 -0.000068
2016-09-15 13:52:08.905   2.293723e-06  2.004982e-05  0.000128 -0.000088
2016-09-15 13:53:08.905   2.397983e-06  5.491537e-05  0.000184 -0.000066
2016-09-15 13:54:08.905   2.502243e-06  5.201868e-05  0.000199 -0.000072
2016-09-15 13:55:08.905   2.606504e-06  8.369137e-05  0.000227 -0.000036
2016-09-15 13:56:08.905   2.710764e-06  7.693962e-05  0.000227 -0.000048
2016-09-15 13:57:08.905   2.815024e-06  8.958450e-05  0.000222 -0.000035
2016-09-15 13:58:08.905   2.919284e-06  1.136194e-04  0.000236 -0.000005
2016-09-15 13:59:08.905   3.023545e-06  1.280766e-04  0.000239  0.000012
...                                ...           ...       ...       ...
2017-09-14 19:30:08.905   1.066598e-02  6.790914e-02  0.073095  0.058803
2017-09-14 19:31:08.905   1.066608e-02  6.793192e-02  0.073120  0.058814
2017-09-14 19:32:08.905   1.066619e-02  6.795518e-02  0.073143  0.058832
2017-09-14 19:33:08.905   1.066629e-02  6.794301e-02  0.073148  0.058815
2017-09-14 19:34:08.905   1.066640e-02  6.795594e-02  0.073162  0.058825
2017-09-14 19:35:08.905   1.066651e-02  6.796540e-02  0.073190  0.058781
2017-09-14 19:36:08.905   1.066661e-02  6.793371e-02  0.073136  0.058753
2017-09-14 19:37:08.905   1.066672e-02  6.797793e-02  0.073182  0.058796
2017-09-14 19:38:08.905   1.066682e-02  6.799636e-02  0.073188  0.058818
2017-09-14 19:39:08.905   1.066693e-02  6.796344e-02  0.073150  0.058833
2017-09-14 19:40:08.905   1.066703e-02  6.795755e-02  0.073144  0.058824
2017-09-14 19:41:08.905   1.066714e-02  6.792633e-02  0.073177  0.058792
2017-09-14 19:42:08.905   1.066724e-02  6.789451e-02  0.073142  0.058762
2017-09-14 19:43:08.905   1.066735e-02  6.790011e-02  0.073131  0.058773
2017-09-14 19:44:08.905   1.066745e-02  6.790749e-02  0.073144  0.058777
2017-09-14 19:45:08.905   1.066756e-02  6.792136e-02  0.073164  0.058765
2017-09-14 19:46:08.905   1.066766e-02  6.791500e-02  0.073161  0.058746
2017-09-14 19:47:08.905   1.066777e-02  6.791654e-02  0.073113  0.058748
2017-09-14 19:48:08.905   1.066788e-02  6.794473e-02  0.073152  0.058738
2017-09-14 19:49:08.905   1.066798e-02  6.794688e-02  0.073150  0.058742
2017-09-14 19:50:08.905   1.066809e-02  6.793277e-02  0.073141  0.058706
2017-09-14 19:51:08.905   1.066819e-02  6.792078e-02  0.073128  0.058715
2017-09-14 19:52:08.905   1.066830e-02  6.792535e-02  0.073134  0.058718
2017-09-14 19:53:08.905   1.066840e-02  6.795196e-02  0.073160  0.058745
2017-09-14 19:54:08.905   1.066851e-02  6.797409e-02  0.073197  0.058747
2017-09-14 19:55:08.905   1.066861e-02  6.796617e-02  0.073189  0.058734
2017-09-14 19:56:08.905   1.066872e-02  6.798782e-02  0.073157  0.058770
2017-09-14 19:57:08.905   1.066882e-02  6.796955e-02  0.073123  0.058757
2017-09-14 19:58:08.905   1.066893e-02  6.794387e-02  0.073135  0.058730
2017-09-14 19:59:08.905   1.066903e-02  6.791228e-02  0.073097  0.058750

[101790 rows x 4 columns]
Out[6]:
'       \nif success:\n    try:\n        dataCursor = db.tracking.find(projection={\'_id\': False},sort=[("timestamp", 1)])\n    except Exception as e:\n        print(\'mongo data cursor error \', str(e))\n        success = False\n        \nif success:\n    try:\n        tracking =  pd.DataFrame(list(dataCursor)).set_index([\'timestamp\'])\n        print(tracking)\n    except Exception as e:\n        print(\'mongo data cursor error \', str(e))\n        success = False \n'

In [7]:
if success:
    plt.figure()
    plt.plot(df['portfolio'])
    plt.plot(df['spy'])
    plt.plot(df['tlt'])
    plt.plot(df['90dayTreasury'])
    plt.legend()
    plt.figure()
    sns.distplot(df['portfolio'],label="portfolio")
    sns.distplot(df['spy'],label="spy")
    sns.distplot(df['tlt'],label="tlt")
    plt.legend()
    
    plt.figure()
    plt.plot(cumulative['portfolio'])
    plt.plot(cumulative['spy'])
    plt.plot(cumulative['tlt'])
    plt.plot(cumulative['90dayTreasury'])
    plt.legend()
"""
    plt.figure()
    plt.plot(tracking['spyTarget'])
    plt.plot(tracking['tltTarget'])
    plt.plot(tracking['spyActual'])
    plt.plot(tracking['tltActual'])
    plt.legend()
"""


/Users/stephan/anaconda/lib/python3.6/site-packages/statsmodels/nonparametric/kdetools.py:20: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j
Out[7]:
"\n    plt.figure()\n    plt.plot(tracking['spyTarget'])\n    plt.plot(tracking['tltTarget'])\n    plt.plot(tracking['spyActual'])\n    plt.plot(tracking['tltActual'])\n    plt.legend()\n"

In [8]:
if success:
    df['elapsed'] = df.index-df.index[0]
    year = datetime.timedelta(days=365)
    percentageOfYear = divtd((df['elapsed'])[-1], year)
    
    df['spyExcess'] = df['spy']-df['90dayTreasury']
    df['tltExcess'] = df['tlt']-df['90dayTreasury']
    df['portfolioExcess'] = df['portfolio']-df['90dayTreasury']
    df['spyInfoExcess'] = df['portfolio']-df['spy']
    df['tltInfoExcess'] = df['portfolio']-df['tlt']
    
    
    meanSpyExcess = df['spyExcess'].mean()
    meanTltExcess = df['tltExcess'].mean()
    meanPortfolioExcess = df['portfolioExcess'].mean()
    meanSpyInfoExcess = df['spyInfoExcess'].mean()
    meanTltInfoExcess = df['tltInfoExcess'].mean()
    
    stdSpyExcess = df['spyExcess'].std()
    stdTltExcess = df['tltExcess'].std()
    stdPortfolioExcess = df['portfolioExcess'].std()
    stdSpyInfoExcess = df['spyInfoExcess'].std()
    stdTltInfoExcess = df['tltInfoExcess'].std()
    
    rawSpySharpe = meanSpyExcess/stdSpyExcess
    spySharpe = math.sqrt(len(df.index)/percentageOfYear)*rawSpySharpe
    print('rawSpySharpe = ',rawSpySharpe)
    print('spySharpe = ',spySharpe)
    
    rawTltSharpe = meanTltExcess/stdTltExcess
    tltSharpe = math.sqrt(len(df.index)/percentageOfYear)*rawTltSharpe
    print('\nrawTltSharpe = ',rawTltSharpe)
    print('tltSharpe = ',tltSharpe)
    
    rawPortfolioSharpe = meanPortfolioExcess/stdPortfolioExcess
    portfolioSharpe = math.sqrt(len(df.index)/percentageOfYear)*rawPortfolioSharpe
    print('\nrawPortfolioSharpe = ',rawPortfolioSharpe)
    print('portfolioSharpe = ',portfolioSharpe)
    
    rawSpyInfo = meanSpyInfoExcess/stdSpyInfoExcess
    spyInfo = math.sqrt(len(df.index)/percentageOfYear)*rawSpyInfo
    print('\nrawSpyInfo = ',rawSpyInfo)
    print('spyInfo = ',spyInfo)
    
    rawTltInfo = meanTltInfoExcess/stdTltInfoExcess
    tltInfo = math.sqrt(len(df.index)/percentageOfYear)*rawTltInfo
    print('\nrawTltInfo = ',rawTltInfo)
    print('tltInfo = ',tltInfo)


rawSpySharpe =  0.023714994301015098
spySharpe =  7.573737144914237

rawTltSharpe =  0.018223688564120502
tltSharpe =  5.8200067536815885

rawPortfolioSharpe =  0.026639517942380415
portfolioSharpe =  8.507727389762842

rawSpyInfo =  -0.0023447851919255955
spyInfo =  -0.7488421240806115

rawTltInfo =  0.00418716963886792
tltInfo =  1.337235076821995