In [1]:
import numpy as np
import pandas as pd
import Quandl
import os

In [2]:
def load(ticker):
    """Load data from Quandl into a dataframe, modify column names and
    check for non-numeric values."""
    # Grab the Quandl token
    token = os.environ.get('QUANDL_TOKEN')
    if token is None:
        token = input("Enter Quandl token: ")
    # Call Quandl module, trim input by default from 1990 forward
    df = Quandl.get(ticker, authtoken=token, trim_start='1990-01-01')
    # Get the column labels
    old_columns = list(df.columns.values)
    # Use the ticker symbol as our new prefix
    ticker_tag = ticker.split('_')[-1] + '_'
    # Drop spaces and concatenate
    new_labels = [ticker_tag + i.replace(' ', '') for i in old_columns]
    # Create a dictionary of old and new column labels
    new_columns = dict(zip(old_columns, new_labels))
    # Rename the columns using our dictionary
    df = df.rename(columns=new_columns)
    nulls = df[~df.applymap(np.isreal).all(1)]
    # Check for non-numeric values
    if len(nulls) > 0:
        raise ValueError('Dataframe contains non-numeric values')
    row_count = len(df)
    print('%d rows loaded into dataframe.' % row_count)
    return df

In [171]:
df = load('YAHOO/INDEX_GSPC')


6566 rows loaded into dataframe.

In [172]:
def sma(df, column, period, **kwargs):
    """Given a dataframe, a column name and a period the function
    returns a dataframe with new column with a simple moving average
    for the period."""
    if 'label' in kwargs:
        column_label = kwargs['label'] + '_' + str(period) + '-day'
    else:
        column_label = 'SMA_' + column + '_' + str(period) + '-day'
    df[column_label] = pd.stats.moments.rolling_mean(df[column], period)
    return df

df = sma(df, 'GSPC_Close', 20)
df = sma(df, 'GSPC_Close', 10)
df.tail()


Out[172]:
GSPC_Open GSPC_High GSPC_Low GSPC_Close GSPC_Volume GSPC_AdjClose SMA_GSPC_Close_20-day SMA_GSPC_Close_10-day
Date
2016-01-14 1891.680054 1934.469971 1878.930054 1921.839966 5241110000 1921.839966 2005.364520 1960.316003
2016-01-15 1916.680054 1916.680054 1857.829956 1880.329956 5468460000 1880.329956 1995.727515 1943.955005
2016-01-19 1888.660034 1901.439941 1864.599976 1881.329956 4928350000 1881.329956 1987.699512 1930.821997
2016-01-20 1876.180054 1876.180054 1812.290039 1859.329956 6416070000 1859.329956 1980.388507 1915.083997
2016-01-21 1861.459961 1889.849976 1848.979980 1868.989990 5078810000 1868.989990 1972.780505 1902.956995

In [173]:
def diff(df, column_a, column_b, **kwargs):
    """Creates a new column from the differnce of column_a and column_b, 
    as column_a minus column_b."""
    column_a_suffix = column_a.split('_')[-1]
    column_b_suffix = column_b.split('_')[-1]
    column_prefix = "_".join(column_b.split('_')[0:2])
    if 'label' in kwargs:
        column_label = kwargs['label']
    else:
        column_label = 'Delta_' + column_prefix + '_' + column_a_suffix + '_' + column_b_suffix
    df[column_label] = df[column_a] - df[column_b]
    return df

df = diff(df, 'SMA_GSPC_Close_20-day', 'SMA_GSPC_Close_10-day', label='MACD_GSPC_Close')
df.tail()


Out[173]:
GSPC_Open GSPC_High GSPC_Low GSPC_Close GSPC_Volume GSPC_AdjClose SMA_GSPC_Close_20-day SMA_GSPC_Close_10-day MACD_GSPC_Close
Date
2016-01-14 1891.680054 1934.469971 1878.930054 1921.839966 5241110000 1921.839966 2005.364520 1960.316003 45.048517
2016-01-15 1916.680054 1916.680054 1857.829956 1880.329956 5468460000 1880.329956 1995.727515 1943.955005 51.772510
2016-01-19 1888.660034 1901.439941 1864.599976 1881.329956 4928350000 1881.329956 1987.699512 1930.821997 56.877515
2016-01-20 1876.180054 1876.180054 1812.290039 1859.329956 6416070000 1859.329956 1980.388507 1915.083997 65.304510
2016-01-21 1861.459961 1889.849976 1848.979980 1868.989990 5078810000 1868.989990 1972.780505 1902.956995 69.823511

In [174]:
df = sma(df, 'MACD_GSPC_Close', 3, label='SMA_MACD_GSPC_Close')
df.tail()


Out[174]:
GSPC_Open GSPC_High GSPC_Low GSPC_Close GSPC_Volume GSPC_AdjClose SMA_GSPC_Close_20-day SMA_GSPC_Close_10-day MACD_GSPC_Close SMA_MACD_GSPC_Close_3-day
Date
2016-01-14 1891.680054 1934.469971 1878.930054 1921.839966 5241110000 1921.839966 2005.364520 1960.316003 45.048517 35.591172
2016-01-15 1916.680054 1916.680054 1857.829956 1880.329956 5468460000 1880.329956 1995.727515 1943.955005 51.772510 44.598678
2016-01-19 1888.660034 1901.439941 1864.599976 1881.329956 4928350000 1881.329956 1987.699512 1930.821997 56.877515 51.232847
2016-01-20 1876.180054 1876.180054 1812.290039 1859.329956 6416070000 1859.329956 1980.388507 1915.083997 65.304510 57.984845
2016-01-21 1861.459961 1889.849976 1848.979980 1868.989990 5078810000 1868.989990 1972.780505 1902.956995 69.823511 64.001845

In [176]:
df = diff(df, 'MACD_GSPC_Close', 'SMA_MACD_GSPC_Close_3-day', label='MACD-Delta-3-day')
df.tail()


Out[176]:
GSPC_Open GSPC_High GSPC_Low GSPC_Close GSPC_Volume GSPC_AdjClose SMA_GSPC_Close_20-day SMA_GSPC_Close_10-day MACD_GSPC_Close SMA_MACD_GSPC_Close_3-day MACD-Delta-3-day
Date
2016-01-14 1891.680054 1934.469971 1878.930054 1921.839966 5241110000 1921.839966 2005.364520 1960.316003 45.048517 35.591172 9.457345
2016-01-15 1916.680054 1916.680054 1857.829956 1880.329956 5468460000 1880.329956 1995.727515 1943.955005 51.772510 44.598678 7.173832
2016-01-19 1888.660034 1901.439941 1864.599976 1881.329956 4928350000 1881.329956 1987.699512 1930.821997 56.877515 51.232847 5.644668
2016-01-20 1876.180054 1876.180054 1812.290039 1859.329956 6416070000 1859.329956 1980.388507 1915.083997 65.304510 57.984845 7.319666
2016-01-21 1861.459961 1889.849976 1848.979980 1868.989990 5078810000 1868.989990 1972.780505 1902.956995 69.823511 64.001845 5.821665

In [ ]:
def flag_swings(df, column, period):
    """Given a dataframe and column and a minimum sequence period 
    for the same sign, the function returns: "1" for upward swings,
    "-1" for downward swings, or "0" if niether condition is met."""
    
    # Trim null value artifacts in SMA columns
    df = df.dropna() 
    # Create a temporary dataframe
    tmp = df.copy()
    tmp['sum'] = 0
    # Determine the sign of each day and sum signs from prior days
    tmp['sign-0'] = [1 if x >= 0 else -1 for x in df[column]]
    if period < 2:
        raise ValueError('The minimum swing period is 2.')
    else:
        for i in range(1, period):
            label = 'sign-' + str(i)
            # .shift moves the rows down by i
            tmp[label] = tmp['sign-0'].shift(i)
            tmp['sum'] = tmp['sum'] + tmp[label]
    tmp['sum-shift'] = tmp['sum'].shift(1)

    def flagger(sign_now, sign_prior, sign_run, sign_sum, period):
        if sign_now > sign_prior and abs(sign_run) >= period - 1 and sign_sum != 0:
            return 1  # Upward swing
        else: 
            if sign_now < sign_prior and abs(sign_run) >= period - 1 and sign_sum != 0:
                return -1  # Downward swing
            else:
                return 0

    column_label = str(period) + '-day_SwingFlag_' + column
    df[column_label] = [flagger(n, p, r, s, period) for n, p, r, s in zip(tmp['sign-0'], tmp['sign-1'], tmp['sum-shift'], tmp['sum'])]
    tmp.head(430)
    return df

In [182]:
df = flag_swings(df, 'MACD-Delta-3-day', 3)

In [181]:
df


Out[181]:
GSPC_Open GSPC_High GSPC_Low GSPC_Close GSPC_Volume GSPC_AdjClose SMA_GSPC_Close_20-day SMA_GSPC_Close_10-day MACD_GSPC_Close SMA_MACD_GSPC_Close_3-day MACD-Delta-3-day 3-day_SwingFlag_MACD-Delta-3-day
Date
1990-01-31 322.980011 329.079987 322.980011 329.079987 189660000 329.079987 338.046500 329.872998 8.173502 8.627669 -0.454167 0
1990-02-01 329.079987 329.859985 327.760010 328.790009 154580000 328.790009 336.702500 328.932999 7.769501 8.256168 -0.486667 0
1990-02-02 328.790009 332.100006 328.089996 330.920013 164400000 330.920013 335.638500 328.110001 7.528499 7.823834 -0.295335 0
1990-02-05 330.920013 332.160004 330.450012 331.850006 130950000 331.850006 334.541499 328.257001 6.284499 7.194166 -0.909667 0
1990-02-06 331.850006 331.859985 328.200012 329.660004 134070000 329.660004 333.543500 328.062003 5.481497 6.431498 -0.950001 0
1990-02-07 329.660004 333.760010 326.549988 333.750000 186710000 333.750000 332.865500 328.411002 4.454498 5.406831 -0.952333 0
1990-02-08 333.750000 336.089996 332.000000 332.959991 176240000 332.959991 332.087000 329.099002 2.987997 4.307998 -1.320000 0
1990-02-09 333.019989 334.600006 332.410004 333.619995 146910000 333.619995 331.771500 329.881003 1.890497 3.110998 -1.220501 0
1990-02-12 333.619995 333.619995 329.970001 330.079987 118390000 330.079987 331.425499 330.369000 1.056499 1.978331 -0.921832 0
1990-02-13 330.079987 331.609985 327.920013 331.019989 144490000 331.019989 330.938998 331.172998 -0.234000 0.904332 -1.138332 0
1990-02-14 331.019989 333.200012 330.640015 332.010010 138530000 332.010010 330.669499 331.466000 -0.796501 0.008666 -0.805167 0
1990-02-15 332.010010 335.209991 331.609985 334.890015 174620000 334.890015 330.504500 332.076001 -1.571501 -0.867334 -0.704167 0
1990-02-16 334.890015 335.640015 332.420013 332.720001 166840000 332.720001 330.183000 332.256000 -2.073000 -1.480334 -0.592666 0
1990-02-20 332.720001 332.720001 326.260010 327.989990 147300000 327.989990 330.063499 331.869998 -1.806499 -1.817000 0.010501 1
1990-02-21 327.910004 328.170013 324.470001 327.670013 159240000 327.670013 329.866501 331.670999 -1.804498 -1.894665 0.090167 0
1990-02-22 327.670013 330.980011 325.700012 325.700012 184320000 325.700012 329.638501 330.866000 -1.227499 -1.612832 0.385333 0
1990-02-23 325.700012 326.149994 322.100006 324.149994 148490000 324.149994 329.542001 329.985001 -0.442999 -1.158332 0.715333 0
1990-02-26 324.160004 328.670013 323.980011 328.670013 148900000 328.670013 329.685503 329.490002 0.195500 -0.491666 0.687166 0
1990-02-27 328.679993 331.940002 328.470001 330.260010 152590000 330.260010 329.938502 329.508005 0.430498 0.061000 0.369498 0
1990-02-28 330.260010 333.480011 330.160004 331.890015 184400000 331.890015 330.384003 329.595007 0.788995 0.471664 0.317331 0
1990-03-01 331.890015 334.399994 331.079987 332.739990 157930000 332.739990 330.567003 329.668005 0.898998 0.706164 0.192834 0
1990-03-02 332.739990 335.540009 332.720001 335.540009 164330000 335.540009 330.904503 329.733005 1.171498 0.953164 0.218334 0
1990-03-05 335.540009 336.380005 333.489990 333.739990 140110000 333.739990 331.045502 329.835004 1.210498 1.093665 0.116834 0
1990-03-06 333.739990 337.929993 333.570007 337.929993 143640000 337.929993 331.349501 330.829004 0.520497 0.967498 -0.447001 -1
1990-03-07 337.929993 338.839996 336.329987 336.950012 163580000 336.950012 331.714001 331.757004 -0.043002 0.562664 -0.605667 0
1990-03-08 336.950012 340.660004 336.950012 340.269989 170900000 340.269989 332.040001 333.214002 -1.174001 -0.232169 -0.941832 0
1990-03-09 340.119995 340.269989 336.839996 337.929993 150410000 337.929993 332.288501 334.592001 -2.303500 -1.173501 -1.129999 0
1990-03-12 337.929993 339.079987 336.140015 338.670013 114790000 338.670013 332.541002 335.592001 -3.051000 -2.176167 -0.874833 0
1990-03-13 338.670013 338.670013 335.359985 336.000000 145440000 336.000000 332.837003 336.166000 -3.328998 -2.894499 -0.434499 0
1990-03-14 336.000000 337.630005 334.929993 336.869995 145060000 336.869995 333.129503 336.663998 -3.534496 -3.304831 -0.229665 0
... ... ... ... ... ... ... ... ... ... ... ... ...
2015-12-08 2073.389893 2073.850098 2052.320068 2063.590088 4173570000 2063.590088 2074.129004 2081.264014 -7.135010 -8.203174 1.068164 1
2015-12-09 2061.169922 2080.330078 2036.530029 2047.619995 4385250000 2047.619995 2072.424005 2077.112024 -4.688019 -6.836180 2.148161 0
2015-12-10 2047.930054 2067.649902 2045.670044 2052.229980 3715150000 2052.229980 2071.285504 2073.448010 -2.162506 -4.661845 2.499339 0
2015-12-11 2047.270020 2047.270020 2008.800049 2012.369995 4301060000 2012.369995 2069.605505 2065.673999 3.931506 -0.973006 4.904513 0
2015-12-14 2013.369995 2022.920044 1993.260010 2021.939941 4612440000 2021.939941 2069.550500 2059.827002 9.723499 3.830833 5.892666 0
2015-12-15 2025.550049 2053.870117 2025.550049 2043.410034 4353540000 2043.410034 2069.061505 2053.905017 15.156488 9.603831 5.552657 0
2015-12-16 2046.500000 2076.719971 2042.430054 2073.070068 4635450000 2073.070068 2070.193011 2053.261023 16.931989 13.937325 2.994664 0
2015-12-17 2073.760010 2076.370117 2041.660034 2041.890015 4327390000 2041.890015 2068.108508 2052.488013 15.620496 15.902991 -0.282495 -1
2015-12-18 2040.810059 2040.810059 2005.329956 2005.550049 6683070000 2005.550049 2064.324011 2043.874023 20.449988 17.667491 2.782497 0
2015-12-21 2010.270020 2022.900024 2005.930054 2021.150024 3760280000 2021.150024 2060.923016 2038.282019 22.640997 19.570494 3.070504 0
2015-12-22 2023.150024 2042.739990 2020.489990 2038.969971 3520860000 2038.969971 2058.542010 2035.820007 22.722003 21.937663 0.784340 0
2015-12-23 2042.199951 2064.729980 2042.199951 2064.290039 3484090000 2064.290039 2057.299518 2037.487012 19.812506 21.725169 -1.912663 -1
2015-12-24 2063.520020 2067.360107 2058.729980 2060.989990 1411860000 2060.989990 2055.905511 2038.363013 17.542499 20.025669 -2.483171 0
2015-12-28 2057.770020 2057.770020 2044.199951 2056.500000 2492510000 2056.500000 2054.225006 2042.776013 11.448993 16.267999 -4.819006 0
2015-12-29 2060.540039 2081.560059 2060.540039 2078.360107 2542000000 2078.360107 2054.122516 2048.418030 5.704486 11.565326 -5.860840 0
2015-12-30 2077.340088 2077.340088 2061.969971 2063.360107 2367430000 2063.360107 2052.159027 2050.413037 1.745990 6.299823 -4.553833 0
2015-12-31 2060.590088 2062.540039 2043.619995 2043.939941 2655330000 2043.939941 2050.380523 2047.500024 2.880499 3.443658 -0.563159 0
2016-01-04 2038.199951 2038.199951 1989.680054 2012.660034 4304880000 2012.660034 2048.532519 2044.577026 3.955493 2.860661 1.094832 1
2016-01-05 2013.780029 2021.939941 2004.170044 2016.709961 3706620000 2016.709961 2044.783520 2045.693017 -0.909497 1.975498 -2.884995 0
2016-01-06 2011.709961 2011.709961 1979.050049 1990.260010 4336660000 1990.260010 2040.443017 2042.604016 -2.160999 0.294999 -2.455998 0
2016-01-07 1985.319946 1985.319946 1938.829956 1943.089966 5076590000 1943.089966 2034.418011 2033.016016 1.401996 -0.556167 1.958162 0
2016-01-08 1985.319946 1985.319946 1918.459961 1922.030029 4664940000 1922.030029 2028.138513 2018.790015 9.348499 2.863165 6.485333 0
2016-01-11 1926.119995 1935.650024 1901.099976 1923.670044 4607290000 1923.670044 2021.710516 2005.058020 16.652496 9.134330 7.518166 0
2016-01-12 1927.829956 1947.380005 1914.349976 1938.680054 4887260000 1938.680054 2018.026019 1993.276025 24.749994 16.916996 7.832998 0
2016-01-13 1940.339966 1950.329956 1886.410034 1890.280029 5087030000 1890.280029 2011.443024 1974.468018 36.975006 26.125832 10.849174 0
2016-01-14 1891.680054 1934.469971 1878.930054 1921.839966 5241110000 1921.839966 2005.364520 1960.316003 45.048517 35.591172 9.457345 0
2016-01-15 1916.680054 1916.680054 1857.829956 1880.329956 5468460000 1880.329956 1995.727515 1943.955005 51.772510 44.598678 7.173832 0
2016-01-19 1888.660034 1901.439941 1864.599976 1881.329956 4928350000 1881.329956 1987.699512 1930.821997 56.877515 51.232847 5.644668 0
2016-01-20 1876.180054 1876.180054 1812.290039 1859.329956 6416070000 1859.329956 1980.388507 1915.083997 65.304510 57.984845 7.319666 0
2016-01-21 1861.459961 1889.849976 1848.979980 1868.989990 5078810000 1868.989990 1972.780505 1902.956995 69.823511 64.001845 5.821665 0

6545 rows × 12 columns


In [191]:
def x_days(df):
    """Add a column with an ascending count historically."""
    df['x-day'] = ['x-' + str(i) for i in range(len(df) - 1, -1, -1)]
    return df

df = x_days(df)
df


Out[191]:
GSPC_Open GSPC_High GSPC_Low GSPC_Close GSPC_Volume GSPC_AdjClose SMA_GSPC_Close_20-day SMA_GSPC_Close_10-day MACD_GSPC_Close SMA_MACD_GSPC_Close_3-day MACD-Delta-3-day 3-day_SwingFlag_MACD-Delta-3-day x-day
Date
1990-01-31 322.980011 329.079987 322.980011 329.079987 189660000 329.079987 338.046500 329.872998 8.173502 8.627669 -0.454167 0 x-6544
1990-02-01 329.079987 329.859985 327.760010 328.790009 154580000 328.790009 336.702500 328.932999 7.769501 8.256168 -0.486667 0 x-6543
1990-02-02 328.790009 332.100006 328.089996 330.920013 164400000 330.920013 335.638500 328.110001 7.528499 7.823834 -0.295335 0 x-6542
1990-02-05 330.920013 332.160004 330.450012 331.850006 130950000 331.850006 334.541499 328.257001 6.284499 7.194166 -0.909667 0 x-6541
1990-02-06 331.850006 331.859985 328.200012 329.660004 134070000 329.660004 333.543500 328.062003 5.481497 6.431498 -0.950001 0 x-6540
1990-02-07 329.660004 333.760010 326.549988 333.750000 186710000 333.750000 332.865500 328.411002 4.454498 5.406831 -0.952333 0 x-6539
1990-02-08 333.750000 336.089996 332.000000 332.959991 176240000 332.959991 332.087000 329.099002 2.987997 4.307998 -1.320000 0 x-6538
1990-02-09 333.019989 334.600006 332.410004 333.619995 146910000 333.619995 331.771500 329.881003 1.890497 3.110998 -1.220501 0 x-6537
1990-02-12 333.619995 333.619995 329.970001 330.079987 118390000 330.079987 331.425499 330.369000 1.056499 1.978331 -0.921832 0 x-6536
1990-02-13 330.079987 331.609985 327.920013 331.019989 144490000 331.019989 330.938998 331.172998 -0.234000 0.904332 -1.138332 0 x-6535
1990-02-14 331.019989 333.200012 330.640015 332.010010 138530000 332.010010 330.669499 331.466000 -0.796501 0.008666 -0.805167 0 x-6534
1990-02-15 332.010010 335.209991 331.609985 334.890015 174620000 334.890015 330.504500 332.076001 -1.571501 -0.867334 -0.704167 0 x-6533
1990-02-16 334.890015 335.640015 332.420013 332.720001 166840000 332.720001 330.183000 332.256000 -2.073000 -1.480334 -0.592666 0 x-6532
1990-02-20 332.720001 332.720001 326.260010 327.989990 147300000 327.989990 330.063499 331.869998 -1.806499 -1.817000 0.010501 1 x-6531
1990-02-21 327.910004 328.170013 324.470001 327.670013 159240000 327.670013 329.866501 331.670999 -1.804498 -1.894665 0.090167 0 x-6530
1990-02-22 327.670013 330.980011 325.700012 325.700012 184320000 325.700012 329.638501 330.866000 -1.227499 -1.612832 0.385333 0 x-6529
1990-02-23 325.700012 326.149994 322.100006 324.149994 148490000 324.149994 329.542001 329.985001 -0.442999 -1.158332 0.715333 0 x-6528
1990-02-26 324.160004 328.670013 323.980011 328.670013 148900000 328.670013 329.685503 329.490002 0.195500 -0.491666 0.687166 0 x-6527
1990-02-27 328.679993 331.940002 328.470001 330.260010 152590000 330.260010 329.938502 329.508005 0.430498 0.061000 0.369498 0 x-6526
1990-02-28 330.260010 333.480011 330.160004 331.890015 184400000 331.890015 330.384003 329.595007 0.788995 0.471664 0.317331 0 x-6525
1990-03-01 331.890015 334.399994 331.079987 332.739990 157930000 332.739990 330.567003 329.668005 0.898998 0.706164 0.192834 0 x-6524
1990-03-02 332.739990 335.540009 332.720001 335.540009 164330000 335.540009 330.904503 329.733005 1.171498 0.953164 0.218334 0 x-6523
1990-03-05 335.540009 336.380005 333.489990 333.739990 140110000 333.739990 331.045502 329.835004 1.210498 1.093665 0.116834 0 x-6522
1990-03-06 333.739990 337.929993 333.570007 337.929993 143640000 337.929993 331.349501 330.829004 0.520497 0.967498 -0.447001 -1 x-6521
1990-03-07 337.929993 338.839996 336.329987 336.950012 163580000 336.950012 331.714001 331.757004 -0.043002 0.562664 -0.605667 0 x-6520
1990-03-08 336.950012 340.660004 336.950012 340.269989 170900000 340.269989 332.040001 333.214002 -1.174001 -0.232169 -0.941832 0 x-6519
1990-03-09 340.119995 340.269989 336.839996 337.929993 150410000 337.929993 332.288501 334.592001 -2.303500 -1.173501 -1.129999 0 x-6518
1990-03-12 337.929993 339.079987 336.140015 338.670013 114790000 338.670013 332.541002 335.592001 -3.051000 -2.176167 -0.874833 0 x-6517
1990-03-13 338.670013 338.670013 335.359985 336.000000 145440000 336.000000 332.837003 336.166000 -3.328998 -2.894499 -0.434499 0 x-6516
1990-03-14 336.000000 337.630005 334.929993 336.869995 145060000 336.869995 333.129503 336.663998 -3.534496 -3.304831 -0.229665 0 x-6515
... ... ... ... ... ... ... ... ... ... ... ... ... ...
2015-12-08 2073.389893 2073.850098 2052.320068 2063.590088 4173570000 2063.590088 2074.129004 2081.264014 -7.135010 -8.203174 1.068164 1 x-29
2015-12-09 2061.169922 2080.330078 2036.530029 2047.619995 4385250000 2047.619995 2072.424005 2077.112024 -4.688019 -6.836180 2.148161 0 x-28
2015-12-10 2047.930054 2067.649902 2045.670044 2052.229980 3715150000 2052.229980 2071.285504 2073.448010 -2.162506 -4.661845 2.499339 0 x-27
2015-12-11 2047.270020 2047.270020 2008.800049 2012.369995 4301060000 2012.369995 2069.605505 2065.673999 3.931506 -0.973006 4.904513 0 x-26
2015-12-14 2013.369995 2022.920044 1993.260010 2021.939941 4612440000 2021.939941 2069.550500 2059.827002 9.723499 3.830833 5.892666 0 x-25
2015-12-15 2025.550049 2053.870117 2025.550049 2043.410034 4353540000 2043.410034 2069.061505 2053.905017 15.156488 9.603831 5.552657 0 x-24
2015-12-16 2046.500000 2076.719971 2042.430054 2073.070068 4635450000 2073.070068 2070.193011 2053.261023 16.931989 13.937325 2.994664 0 x-23
2015-12-17 2073.760010 2076.370117 2041.660034 2041.890015 4327390000 2041.890015 2068.108508 2052.488013 15.620496 15.902991 -0.282495 -1 x-22
2015-12-18 2040.810059 2040.810059 2005.329956 2005.550049 6683070000 2005.550049 2064.324011 2043.874023 20.449988 17.667491 2.782497 0 x-21
2015-12-21 2010.270020 2022.900024 2005.930054 2021.150024 3760280000 2021.150024 2060.923016 2038.282019 22.640997 19.570494 3.070504 0 x-20
2015-12-22 2023.150024 2042.739990 2020.489990 2038.969971 3520860000 2038.969971 2058.542010 2035.820007 22.722003 21.937663 0.784340 0 x-19
2015-12-23 2042.199951 2064.729980 2042.199951 2064.290039 3484090000 2064.290039 2057.299518 2037.487012 19.812506 21.725169 -1.912663 -1 x-18
2015-12-24 2063.520020 2067.360107 2058.729980 2060.989990 1411860000 2060.989990 2055.905511 2038.363013 17.542499 20.025669 -2.483171 0 x-17
2015-12-28 2057.770020 2057.770020 2044.199951 2056.500000 2492510000 2056.500000 2054.225006 2042.776013 11.448993 16.267999 -4.819006 0 x-16
2015-12-29 2060.540039 2081.560059 2060.540039 2078.360107 2542000000 2078.360107 2054.122516 2048.418030 5.704486 11.565326 -5.860840 0 x-15
2015-12-30 2077.340088 2077.340088 2061.969971 2063.360107 2367430000 2063.360107 2052.159027 2050.413037 1.745990 6.299823 -4.553833 0 x-14
2015-12-31 2060.590088 2062.540039 2043.619995 2043.939941 2655330000 2043.939941 2050.380523 2047.500024 2.880499 3.443658 -0.563159 0 x-13
2016-01-04 2038.199951 2038.199951 1989.680054 2012.660034 4304880000 2012.660034 2048.532519 2044.577026 3.955493 2.860661 1.094832 1 x-12
2016-01-05 2013.780029 2021.939941 2004.170044 2016.709961 3706620000 2016.709961 2044.783520 2045.693017 -0.909497 1.975498 -2.884995 0 x-11
2016-01-06 2011.709961 2011.709961 1979.050049 1990.260010 4336660000 1990.260010 2040.443017 2042.604016 -2.160999 0.294999 -2.455998 0 x-10
2016-01-07 1985.319946 1985.319946 1938.829956 1943.089966 5076590000 1943.089966 2034.418011 2033.016016 1.401996 -0.556167 1.958162 0 x-9
2016-01-08 1985.319946 1985.319946 1918.459961 1922.030029 4664940000 1922.030029 2028.138513 2018.790015 9.348499 2.863165 6.485333 0 x-8
2016-01-11 1926.119995 1935.650024 1901.099976 1923.670044 4607290000 1923.670044 2021.710516 2005.058020 16.652496 9.134330 7.518166 0 x-7
2016-01-12 1927.829956 1947.380005 1914.349976 1938.680054 4887260000 1938.680054 2018.026019 1993.276025 24.749994 16.916996 7.832998 0 x-6
2016-01-13 1940.339966 1950.329956 1886.410034 1890.280029 5087030000 1890.280029 2011.443024 1974.468018 36.975006 26.125832 10.849174 0 x-5
2016-01-14 1891.680054 1934.469971 1878.930054 1921.839966 5241110000 1921.839966 2005.364520 1960.316003 45.048517 35.591172 9.457345 0 x-4
2016-01-15 1916.680054 1916.680054 1857.829956 1880.329956 5468460000 1880.329956 1995.727515 1943.955005 51.772510 44.598678 7.173832 0 x-3
2016-01-19 1888.660034 1901.439941 1864.599976 1881.329956 4928350000 1881.329956 1987.699512 1930.821997 56.877515 51.232847 5.644668 0 x-2
2016-01-20 1876.180054 1876.180054 1812.290039 1859.329956 6416070000 1859.329956 1980.388507 1915.083997 65.304510 57.984845 7.319666 0 x-1
2016-01-21 1861.459961 1889.849976 1848.979980 1868.989990 5078810000 1868.989990 1972.780505 1902.956995 69.823511 64.001845 5.821665 0 x-0

6545 rows × 13 columns


In [198]:
def transpose(df):
    """Transpose the dataframe and set the x-days as the column labels."""
    df = df.set_index('x-day')
    df = df.transpose()
    pd.options.display.float_format = '{:.3f}'.format
    return df

df = transpose(df)

In [199]:
df


Out[199]:
x-day x-6544 x-6543 x-6542 x-6541 x-6540 x-6539 x-6538 x-6537 x-6536 x-6535 ... x-9 x-8 x-7 x-6 x-5 x-4 x-3 x-2 x-1 x-0
GSPC_Open 322.980 329.080 328.790 330.920 331.850 329.660 333.750 333.020 333.620 330.080 ... 1985.320 1985.320 1926.120 1927.830 1940.340 1891.680 1916.680 1888.660 1876.180 1861.460
GSPC_High 329.080 329.860 332.100 332.160 331.860 333.760 336.090 334.600 333.620 331.610 ... 1985.320 1985.320 1935.650 1947.380 1950.330 1934.470 1916.680 1901.440 1876.180 1889.850
GSPC_Low 322.980 327.760 328.090 330.450 328.200 326.550 332.000 332.410 329.970 327.920 ... 1938.830 1918.460 1901.100 1914.350 1886.410 1878.930 1857.830 1864.600 1812.290 1848.980
GSPC_Close 329.080 328.790 330.920 331.850 329.660 333.750 332.960 333.620 330.080 331.020 ... 1943.090 1922.030 1923.670 1938.680 1890.280 1921.840 1880.330 1881.330 1859.330 1868.990
GSPC_Volume 189660000.000 154580000.000 164400000.000 130950000.000 134070000.000 186710000.000 176240000.000 146910000.000 118390000.000 144490000.000 ... 5076590000.000 4664940000.000 4607290000.000 4887260000.000 5087030000.000 5241110000.000 5468460000.000 4928350000.000 6416070000.000 5078810000.000
GSPC_AdjClose 329.080 328.790 330.920 331.850 329.660 333.750 332.960 333.620 330.080 331.020 ... 1943.090 1922.030 1923.670 1938.680 1890.280 1921.840 1880.330 1881.330 1859.330 1868.990
SMA_GSPC_Close_20-day 338.046 336.702 335.638 334.541 333.543 332.865 332.087 331.771 331.425 330.939 ... 2034.418 2028.139 2021.711 2018.026 2011.443 2005.365 1995.728 1987.700 1980.389 1972.781
SMA_GSPC_Close_10-day 329.873 328.933 328.110 328.257 328.062 328.411 329.099 329.881 330.369 331.173 ... 2033.016 2018.790 2005.058 1993.276 1974.468 1960.316 1943.955 1930.822 1915.084 1902.957
MACD_GSPC_Close 8.174 7.770 7.528 6.284 5.481 4.454 2.988 1.890 1.056 -0.234 ... 1.402 9.348 16.652 24.750 36.975 45.049 51.773 56.878 65.305 69.824
SMA_MACD_GSPC_Close_3-day 8.628 8.256 7.824 7.194 6.431 5.407 4.308 3.111 1.978 0.904 ... -0.556 2.863 9.134 16.917 26.126 35.591 44.599 51.233 57.985 64.002
MACD-Delta-3-day -0.454 -0.487 -0.295 -0.910 -0.950 -0.952 -1.320 -1.221 -0.922 -1.138 ... 1.958 6.485 7.518 7.833 10.849 9.457 7.174 5.645 7.320 5.822
3-day_SwingFlag_MACD-Delta-3-day 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 ... 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000

12 rows × 6545 columns


In [200]:
df.to_csv('data/sma-diff-swingflag-transposed.csv')

In [ ]: