奇怪的错误 - 逐月增加 - Python + MySQL

时间:2017-05-14 03:13:00

标签: mysql python-3.x

2017-05-12插入到2017-06-12的MySQL中 - 无法弄清楚调试...粘贴代码的关键部分

Windows平台;使用PyMySQL;如果问题没有解决,我可能会批量更新表格以减少一个月......我想这应该很容易......

exceptions = []
flag = 'N'

def obtain_list_of_db_tickers():
"""
Obtains a list of the ticker symbols in the database.
"""
with con:
    cur = con.cursor()
    cur.execute("SELECT id, ticker FROM symbol")
    data = cur.fetchall()

list1,list2 = map(list,zip(*data))
uniqtickers = list(set(list2))
rangelist = list(range(1,len(uniqtickers)+1))

newtickers = list(zip(rangelist,uniqtickers))

return [(d[0], d[1]) for d in newtickers]


def get_daily_historic_data_yahoo(
    ticker, start_date=(2017,5,12),
    end_date=datetime.date.today().timetuple()[0:3]
) :

""""
Obtains data from Yahoo Finance returns and a list of tuples
ticker: Yahoo Finance ticker symbol eg: GOOG
start_date: Start date in (YYYY,M,D) format
End_date: End date in (YYYY,M,D) format
"""

# Construct the Yahoo URL with the correct integer query parameters
# for start and end dates. Note that some parameters are zero-based!

ticker_tup = (
    ticker, start_date[1]-1, start_date[2],
    start_date[0], end_date[1]-1, end_date[2],
    end_date[0]
)

yahoo_url = "http://ichart.finance.yahoo.com/table.csv"
yahoo_url += "?s=%s&a=%s&b=%s&c=%s&d=%s&e=%s&f=%s"
yahoo_url = yahoo_url % ticker_tup

# Try connecting to Yahoo Finance and obtaining the data
# On failure, print an error message.
global flag

try:
    yf_data = pd.read_csv(yahoo_url)

    yf_data['Date'] = yf_data['Date'].apply(lambda x: 
   datetime.datetime.strptime(x, '%Y-%m-%d'))

    prices = yf_data.set_index(['Date'])[['Open', 'High', 'Low', 'Close', 
'Volume', 'Adj Close']].to_records().tolist()

except Exception as e:
    print("Could not download Yahoo data: %s" %e)
    flag = 'Y'
    exceptions.append(ticker)
    return None

return prices



def insert_daily_data_into_db(
    data_vendor_id, symbol_id, ticker, daily_data
):

"""
Takes a list of tuples of daily data and adds it to the
MySQL database. Appends the vendor ID and symbol ID to the data.

daily_data: List of tuples of the OHLC data(with adj_close and volume)
"""

# Create the time now
now = datetime.datetime.utcnow()

#Amend the data to include the vendor ID and symbol IDprices
daily_data = [
    (data_vendor_id, symbol_id, ticker, d[0], now, now,
     d[1], d[2], d[3], d[4], d[5], d[6])
    for d in daily_data
]

# Create the insert strings
column_str = """data_vendor_id, symbol_id, ticker, price_date, created_date,
last_updated_date, open_price, high_price, low_price,
close_price, volume, adj_close_price"""


insert_str = ("%s, " *12)[:-2]
final_str = "INSERT INTO daily_price (%s) VALUES (%s)" %\
            (column_str, insert_str)

# Using the MySQL connection, carry out an INSERT INTO for every symbol
with con:
    cur = con.cursor()
    cur.executemany(final_str, daily_data)




if __name__ == "__main__":
# This ignores the warnings regarding Data Truncation
# from the Yahoo precision to Decimal(19,4) datatypes
warnings.filterwarnings('ignore')

# Loop over the tickers and insert the daily historical
# data into the database


tickers = obtain_list_of_db_tickers()
lentickers = len(tickers)

for i, t in enumerate(tickers):
    print(
    "Adding data for %s: %s out of %s" %
    (t[1], i+1, lentickers)
     )

    yf_data = get_daily_historic_data_yahoo(t[1])

    if (flag == 'N'):
        insert_daily_data_into_db('1', t[0], t[1], yf_data)
    elif (flag == 'Y'):
        flag = 'N'

0 个答案:

没有答案