In [204]:
%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 [205]:
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 [206]:
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.percentageMove.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       tlt
timestamp                                                               
2017-09-15 16:46:01.297   1.238402e-07  1.326664e-04  0.000161  0.000394
2017-09-15 16:47:01.228   1.947415e-08 -4.364202e-05 -0.000060 -0.000236
2017-09-15 16:48:01.267   1.950917e-08  1.004959e-04  0.000040  0.000000
2017-09-15 16:49:01.331   1.951729e-08 -2.170493e-05 -0.000120 -0.000079
2017-09-15 16:50:01.393   1.951657e-08 -1.263277e-06 -0.000020  0.000000
2017-09-15 16:51:02.062   1.971385e-08  1.722653e-05  0.000080  0.000079
2017-09-15 16:52:01.027   1.916039e-08 -2.136053e-05 -0.000060  0.000000
2017-09-15 16:53:01.471   1.964075e-08 -5.702005e-05 -0.000040 -0.000039
2017-09-15 16:54:01.824   1.961118e-08 -7.752412e-06 -0.000080  0.000039
2017-09-15 16:55:00.815   1.916856e-08  1.148514e-05 -0.000100 -0.000079
2017-09-15 16:56:01.154   1.960679e-08  4.306880e-05 -0.000040  0.000079
2017-09-15 16:57:01.271   1.953446e-08 -9.474727e-05  0.000120  0.000000
2017-09-15 16:58:01.845   1.968324e-08 -1.722841e-05 -0.000060  0.000236
2017-09-15 16:59:01.292   1.931693e-08 -3.445741e-05 -0.000040 -0.000079
2017-09-15 17:00:01.105   1.943582e-08 -3.158705e-05 -0.000020  0.000000
2017-09-15 17:01:01.630   1.966706e-08  2.584476e-05  0.000040  0.000039
2017-09-15 17:02:01.963   1.960468e-08  3.819183e-05  0.000080  0.000039
2017-09-15 17:03:00.962   1.917145e-08 -1.119868e-05 -0.000040  0.000000
2017-09-15 17:04:01.699   1.973604e-08  5.340969e-05  0.000040  0.000000
2017-09-15 17:05:01.498   1.943116e-08 -1.435668e-06 -0.000020 -0.000079
2017-09-15 17:06:01.935   1.963873e-08  7.178349e-06  0.000020 -0.000079
2017-09-15 17:07:01.905   1.948695e-08  0.000000e+00  0.000000  0.000000
2017-09-15 17:08:01.623   1.940500e-08  5.742638e-08  0.000000 -0.000236
2017-09-15 17:09:01.677   1.951411e-08  2.865576e-05 -0.000040  0.000000
2017-09-15 17:10:01.213   1.934579e-08 -1.722742e-05  0.000000  0.000079
2017-09-15 17:11:01.902   1.972054e-08  5.168315e-05  0.000000  0.000079
2017-09-15 17:12:01.651   1.941510e-08 -1.722683e-05 -0.000040  0.000158
2017-09-15 17:13:01.181   1.934371e-08 -9.762036e-05 -0.000040  0.000000
2017-09-15 17:14:01.243   1.951678e-08 -1.722880e-05 -0.000040 -0.000079
2017-09-15 17:15:02.678   1.996289e-08 -1.183065e-05 -0.000020 -0.000079
...                                ...           ...       ...       ...
2017-09-15 18:59:00.911   1.954494e-08  8.613512e-05  0.000040  0.000000
2017-09-15 19:00:01.155   1.957579e-08  0.000000e+00  0.000040  0.000000
2017-09-15 19:01:01.252   1.952817e-08 -8.727607e-06  0.000040  0.000158
2017-09-15 19:02:00.811   1.935319e-08  4.317906e-05  0.000080 -0.000079
2017-09-15 19:03:01.973   1.987423e-08  4.880401e-05  0.000000 -0.000237
2017-09-15 19:04:01.968   1.949509e-08 -3.157753e-05  0.000040  0.000158
2017-09-15 19:05:01.582   1.937110e-08 -2.296620e-05  0.000060 -0.000039
2017-09-15 19:06:02.344   1.974426e-08 -8.612523e-05 -0.000060  0.000000
2017-09-15 19:07:02.821   1.965147e-08  3.732415e-05 -0.000060  0.000118
2017-09-15 19:08:00.993   1.890273e-08  1.033553e-04 -0.000020  0.000158
2017-09-15 19:09:01.236   1.957569e-08  5.454300e-05 -0.000080  0.000000
2017-09-15 19:10:00.744   1.933679e-08 -3.444633e-05 -0.000040 -0.000079
2017-09-15 19:11:01.233   1.965540e-08 -2.870627e-06  0.000060 -0.000079
2017-09-15 19:12:04.700   2.062310e-08 -1.435317e-05 -0.000020 -0.000158
2017-09-15 19:13:02.129   1.866116e-08  1.550165e-04  0.000020  0.000158
2017-09-15 19:14:01.311   1.923068e-08 -4.879393e-05  0.000020  0.000158
2017-09-15 19:15:01.015   1.940043e-08  0.000000e+00  0.000080  0.000079
2017-09-15 19:16:01.056   1.950992e-08  2.583334e-05  0.000000  0.000079
2017-09-15 19:17:01.241   1.955686e-08 -6.716495e-05  0.000020  0.000039
2017-09-15 19:18:01.559   1.959998e-08  7.003995e-05  0.000060  0.000118
2017-09-15 19:19:03.028   1.997386e-08 -1.578659e-05 -0.000020  0.000000
2017-09-15 19:20:01.108   1.887277e-08  9.902653e-05 -0.000040  0.000000
2017-09-15 19:21:02.025   1.979447e-08 -4.018070e-05 -0.000020  0.000000
2017-09-15 19:22:01.399   1.929327e-08  1.406381e-04 -0.000080  0.000158
2017-09-15 19:23:00.797   1.930091e-08  3.696253e-05  0.000120  0.000079
2017-09-15 19:24:00.992   1.955991e-08 -3.696116e-05  0.000000  0.000000
2017-09-15 19:25:01.427   1.963799e-08  2.008833e-05  0.000060  0.000000
2017-09-15 19:26:01.265   1.944387e-08 -1.176579e-04 -0.000020  0.000000
2017-09-15 19:27:01.909   1.970572e-08  8.610125e-06  0.000000 -0.000039
2017-09-15 19:28:01.299   1.929841e-08 -5.338231e-05  0.000080 -0.000079

[162 rows x 4 columns]
                         90dayTreasury     portfolio           spy       tlt
timestamp                                                                   
2017-09-15 16:46:01.297   0.000000e+00  0.000000e+00  0.000000e+00  0.000000
2017-09-15 16:47:01.228   1.947416e-08 -4.364781e-05 -6.023008e-05 -0.000236
2017-09-15 16:48:01.267   3.898332e-08  5.685701e-05 -2.007669e-05 -0.000236
2017-09-15 16:49:01.331   5.850062e-08  3.514797e-05 -1.405369e-04 -0.000315
2017-09-15 16:50:01.393   7.801719e-08  3.388448e-05 -1.606135e-04 -0.000315
2017-09-15 16:51:02.062   9.773104e-08  5.111388e-05 -8.030677e-05 -0.000236
2017-09-15 16:52:01.027   1.168914e-07  2.974943e-05 -1.405369e-04 -0.000236
2017-09-15 16:53:01.471   1.365322e-07 -2.727988e-05 -1.806902e-04 -0.000276
2017-09-15 16:54:01.824   1.561434e-07 -3.503311e-05 -2.609970e-04 -0.000236
2017-09-15 16:55:00.815   1.753119e-07 -2.354684e-05 -3.613805e-04 -0.000315
2017-09-15 16:56:01.154   1.949187e-07  1.952665e-05 -4.015339e-04 -0.000236
2017-09-15 16:57:01.271   2.144532e-07 -7.523504e-05 -2.810737e-04 -0.000236
2017-09-15 16:58:01.845   2.341365e-07 -9.246444e-05 -3.413038e-04  0.000000
2017-09-15 16:59:01.292   2.534534e-07 -1.269232e-04 -3.814572e-04 -0.000079
2017-09-15 17:00:01.105   2.728892e-07 -1.585105e-04 -4.015339e-04 -0.000079
2017-09-15 17:01:01.630   2.925563e-07 -1.326664e-04 -3.613805e-04 -0.000039
2017-09-15 17:02:01.963   3.121610e-07 -9.447453e-05 -2.810737e-04  0.000000
2017-09-15 17:03:00.962   3.313324e-07 -1.056736e-04 -3.212271e-04  0.000000
2017-09-15 17:04:01.699   3.510685e-07 -5.226251e-05 -2.810737e-04  0.000000
2017-09-15 17:05:01.498   3.704996e-07 -5.369829e-05 -3.011504e-04 -0.000079
2017-09-15 17:06:01.935   3.901384e-07 -4.651937e-05 -2.810737e-04 -0.000158
2017-09-15 17:07:01.905   4.096254e-07 -4.651937e-05 -2.810737e-04 -0.000158
2017-09-15 17:08:01.623   4.290304e-07 -4.646194e-05 -2.810737e-04 -0.000394
2017-09-15 17:09:01.677   4.485445e-07 -1.780371e-05 -3.212271e-04 -0.000394
2017-09-15 17:10:01.213   4.678903e-07 -3.503311e-05 -3.212271e-04 -0.000315
2017-09-15 17:11:01.902   4.876108e-07  1.665508e-05 -3.212271e-04 -0.000236
2017-09-15 17:12:01.651   5.070259e-07 -5.743133e-07 -3.613805e-04 -0.000079
2017-09-15 17:13:01.181   5.263697e-07 -9.820757e-05 -4.015339e-04 -0.000079
2017-09-15 17:14:01.243   5.458865e-07 -1.154370e-04 -4.416872e-04 -0.000158
2017-09-15 17:15:02.678   5.658494e-07 -1.272678e-04 -4.617639e-04 -0.000236
...                                ...           ...           ...       ...
2017-09-15 18:59:00.911   2.592922e-06  9.131581e-05 -3.212271e-04 -0.001104
2017-09-15 19:00:01.155   2.612498e-06  9.131581e-05 -2.810737e-04 -0.001104
2017-09-15 19:01:01.252   2.632026e-06  8.258625e-05 -2.409203e-04 -0.000946
2017-09-15 19:02:00.811   2.651379e-06  1.257746e-04 -1.606135e-04 -0.001025
2017-09-15 19:03:01.973   2.671254e-06  1.745912e-04 -1.606135e-04 -0.001261
2017-09-15 19:04:01.968   2.690749e-06  1.430040e-04 -1.204602e-04 -0.001104
2017-09-15 19:05:01.582   2.710120e-06  1.200315e-04 -6.023008e-05 -0.001143
2017-09-15 19:06:02.344   2.729864e-06  3.388448e-05 -1.204602e-04 -0.001143
2017-09-15 19:07:02.821   2.749516e-06  7.121484e-05 -1.806902e-04 -0.001025
2017-09-15 19:08:00.993   2.768419e-06  1.745912e-04 -2.007669e-04 -0.000867
2017-09-15 19:09:01.236   2.787994e-06  2.291510e-04 -2.810737e-04 -0.000867
2017-09-15 19:10:00.744   2.807331e-06  1.946922e-04 -3.212271e-04 -0.000946
2017-09-15 19:11:01.233   2.826987e-06  1.918206e-04 -2.609970e-04 -0.001025
2017-09-15 19:12:04.700   2.847610e-06  1.774628e-04 -2.810737e-04 -0.001182
2017-09-15 19:13:02.129   2.866271e-06  3.325274e-04 -2.609970e-04 -0.001025
2017-09-15 19:14:01.311   2.885502e-06  2.837108e-04 -2.409203e-04 -0.000867
2017-09-15 19:15:01.015   2.904902e-06  2.837108e-04 -1.606135e-04 -0.000788
2017-09-15 19:16:01.056   2.924412e-06  3.095548e-04 -1.606135e-04 -0.000709
2017-09-15 19:17:01.241   2.943969e-06  2.423602e-04 -1.405369e-04 -0.000670
2017-09-15 19:18:01.559   2.963569e-06  3.124264e-04 -8.030677e-05 -0.000552
2017-09-15 19:19:03.028   2.983543e-06  2.966328e-04 -1.003835e-04 -0.000552
2017-09-15 19:20:01.108   3.002416e-06  3.957018e-04 -1.405369e-04 -0.000552
2017-09-15 19:21:02.025   3.022210e-06  3.554999e-04 -1.606135e-04 -0.000552
2017-09-15 19:22:01.399   3.041504e-06  4.962067e-04 -2.409203e-04 -0.000394
2017-09-15 19:23:00.797   3.060805e-06  5.331924e-04 -1.204602e-04 -0.000315
2017-09-15 19:24:00.992   3.080365e-06  4.962067e-04 -1.204602e-04 -0.000315
2017-09-15 19:25:01.427   3.100003e-06  5.163076e-04 -6.023008e-05 -0.000315
2017-09-15 19:26:01.265   3.119447e-06  3.985734e-04 -8.030677e-05 -0.000315
2017-09-15 19:27:01.909   3.139152e-06  4.071881e-04 -8.030677e-05 -0.000355
2017-09-15 19:28:01.299   3.158451e-06  3.537770e-04 -8.881784e-16 -0.000434

[162 rows x 4 columns]
                         spyActual  spyTarget  tltActual  tltTarget
timestamp                                                          
2017-09-15 18:42:01.193   0.857746   0.904555   0.072777   0.095445
2017-09-15 18:43:01.094   0.857772   0.904555   0.072778   0.095445
2017-09-15 18:44:01.246   0.857734   0.904555   0.072774   0.095445
2017-09-15 18:45:02.455   0.857763   0.903924   0.072775   0.096076
2017-09-15 18:46:01.089   0.857802   0.903924   0.072771   0.096076
2017-09-15 18:47:01.337   0.857827   0.903924   0.072773   0.096076
2017-09-15 18:48:02.668   0.857805   0.903924   0.072777   0.096076
2017-09-15 18:49:01.885   0.857721   0.903924   0.072774   0.096076
2017-09-15 18:50:01.042   0.857706   0.903924   0.072775   0.096076
2017-09-15 18:51:01.483   0.857863   0.903402   0.072782   0.096598
2017-09-15 18:52:01.473   0.857864   0.903402   0.072785   0.096598
2017-09-15 18:53:01.091   0.857879   0.903402   0.072788   0.096598
2017-09-15 18:54:01.226   0.857871   0.903402   0.072787   0.096598
2017-09-15 18:55:00.935   0.857808   0.903402   0.072788   0.096598
2017-09-15 18:56:01.324   0.857842   0.902875   0.072795   0.097125
2017-09-15 18:57:01.773   0.857852   0.902875   0.072793   0.097125
2017-09-15 18:58:00.762   0.857889   0.902875   0.072793   0.097125
2017-09-15 18:59:00.911   0.857849   0.902875   0.072787   0.097125
2017-09-15 19:00:01.155   0.857884   0.902875   0.072787   0.097125
2017-09-15 19:01:01.252   0.857925   0.902336   0.072799   0.097664
2017-09-15 19:02:00.811   0.857957   0.902336   0.072790   0.097664
2017-09-15 19:03:01.973   0.857915   0.902336   0.072769   0.097664
2017-09-15 19:04:01.968   0.857977   0.902336   0.072783   0.097664
2017-09-15 19:05:01.582   0.858048   0.901817   0.072782   0.098183
2017-09-15 19:06:02.344   0.858071   0.901817   0.072788   0.098183
2017-09-15 19:07:02.821   0.857987   0.901817   0.072794   0.098183
2017-09-15 19:08:00.993   0.857881   0.901817   0.072798   0.098183
2017-09-15 19:09:01.236   0.857765   0.901817   0.072794   0.098183
2017-09-15 19:10:00.744   0.857760   0.901817   0.072790   0.098183
2017-09-15 19:11:01.233   0.857815   0.901301   0.072785   0.098699
2017-09-15 19:12:04.700   0.857810   0.901301   0.072775   0.098699
2017-09-15 19:13:02.129   0.857694   0.901301   0.072775   0.098699
2017-09-15 19:14:01.311   0.857753   0.901301   0.072790   0.098699
2017-09-15 19:15:01.015   0.857822   0.901301   0.072795   0.098699
2017-09-15 19:16:01.056   0.857800   0.900850   0.072799   0.099150
2017-09-15 19:17:01.241   0.857875   0.900850   0.072807   0.099150
2017-09-15 19:18:01.559   0.857866   0.900850   0.072811   0.099150
2017-09-15 19:19:03.028   0.857862   0.900850   0.072812   0.099150
2017-09-15 19:20:01.108   0.857743   0.900448   0.072805   0.099552
2017-09-15 19:21:02.025   0.857760   0.900448   0.072807   0.099552
2017-09-15 19:22:01.399   0.857571   0.900448   0.072809   0.099552
2017-09-15 19:23:00.797   0.857642   0.900448   0.072812   0.099552
2017-09-15 19:24:00.992   0.857674   0.900448   0.072814   0.099552
2017-09-15 19:25:01.427   0.857709   0.900116   0.072813   0.099884
2017-09-15 19:26:01.265   0.857792   0.900116   0.072822   0.099884
2017-09-15 19:27:01.909   0.857785   0.900116   0.072818   0.099884
2017-09-15 19:28:01.299   0.857900   0.900116   0.072816   0.099884

In [207]:
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

In [208]:
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.0182202202918432
spySharpe =  13.209344207135768

rawTltSharpe =  -0.0028525187455550175
tltSharpe =  -2.0680266958249587

rawPortfolioSharpe =  0.05522687700119815
portfolioSharpe =  40.038529507821735

rawSpyInfo =  0.027280493866364428
spyInfo =  19.777885659416974

rawTltInfo =  0.03256864104673505
tltInfo =  23.611700794724733