PPaste!

LoL

Home - All the pastes - Authored by Thooms

Raw version

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import time
import sqlalchemy as saq
import config as cf
import pandas as pd
import urllib.parse
import pymssql, os
import tda

try:
    import pyodbc
except ImportError:
    import pypyodbc as pyodbc

# os.system('apt-get install -y libodbc1')
# os.system('apt-get install -y unixodbc')
# os.system('apt-get install -y curl')
# os.system('su')
# os.system('curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -')
# os.system('curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list')
# os.system('exit')
# os.system('apt-get -y update')
# os.system('ACCEPT_EULA=Y apt-get install -y msodbcsql17')

# print('available drivers: ', pyodbc.drivers())

try:
    client = tda.auth.client_from_token_file('token.json', cf.api_key)
except ValueError:
    pass
except FileNotFoundError:
    pass

# ==================================== #

    
def get_options_data(c, ticker, strkes):
    response = c.get_option_chain(str(ticker), strike_count=strkes)
    # print(json.dumps(response.json(), indent=4))
    response2 = response.json()
    return response2


# STEP 3: parse json output and return a dataframe
def json_data_to_dataframe(json_response):

    # instantiating a dataframe to house all of our data for one ticker
    df = pd.DataFrame()
    # parsing json output for CALL data
    for key in json_response.keys():
        for item in json_response.keys():
            if item == 'callExpDateMap':
                for date in json_response[item]:
                    for strike in json_response[item][date]:
                        if df.empty:
                            df = pd.DataFrame(json_response[item][date][strike],
                                              columns=json_response[item][date][strike][0].keys())
                        else:
                            df2 = pd.DataFrame(json_response[item][date][strike],
                                               columns=json_response[item][date][strike][0].keys())
                            df = df.append(df2)

    # parsing json output for PUT data
    for key in json_response.keys():
        for item in json_response.keys():
            if item == 'putExpDateMap':
                for date in json_response[item]:
                    for strike in json_response[item][date]:
                        if df.empty:
                            df = pd.DataFrame(json_response[item][date][strike],
                                              columns=json_response[item][date][strike][0].keys())
                        else:
                            df2 = pd.DataFrame(json_response[item][date][strike],
                                               columns=json_response[item][date][strike][0].keys())
                            df = df.append(df2)

    # DATA CLEANING STEP TO AVOID DATA ERRORS WHEN LOADING INTO MYSQL
    # this converts all numeric valued columns to numeric columns and will return all NaN's to float Nan that can then be 
    # clearerd with a simple fillna() fxn
    cols = df.columns.values.tolist()
    numeric_cols = cols[4::]
    df[numeric_cols] = df[numeric_cols].apply(pd.to_numeric, errors='coerce')
    df.fillna(0)
    # print(df)
    return df

# ===================================== #


engine = saq.create_engine(cf.ENGINE_URL)

conn = engine.connect()

raw_data = get_options_data(client, 'JPM', 5)

df = json_data_to_dataframe(raw_data)

df.to_sql('anotherTest', con=engine, index=True, if_exists='append')

print('Yay!')