I am running python 3.5 on windows 10, I am currently stuck on this part of my code that involves an if statement
I have an if statement that runs the criteria for true even though the condition being tested is actually false. ticker is a list that contains stock ticker symbols. When this program starts to run I open tickerlist_data.csv which is an excel csv, and I want the list "ticker" to be checked with the tickerlist_data.csv first to see if one of the current tickers in the list "tickers" is already on file in the tickerlist_data.csv, If it is in the file I want it to perform the else: criteria
This code works, It will download the data from yahoo finance just fine, it appends the symbol in the list 'ticker' into the tickerlist_data.csv(even if it is already there which is wrong, I don't want it to do that), and it saves it to the csv file. It works well except for the fact that it runs when it is not suppose to and the csv file is being appended even though the symbol is already there. I have tried to put a criteria in the else: statement but it will not work. I don't necessary need the code for the else: unless you have a suggestion, I am mainly concerned with why the else: won't work.
I feel that the rest it is too hard for me to explain in this description so I commented my code by either what it actually does or what I want it to do.
This is tickerlist_data.csv
Tickers
aapl
tvix
ugaz
nugt
This is my code
from pandas_datareader import data as dreader
import pandas as pd
from pandas_datareader import data as dreader
from pandas_datareader._utils import RemoteDataError
from datetime import datetime
ticker = ['ugaz','dust']
# The with open is opening the file tickerlist.csv, this is a csv file
# that opens in excel, that has a list of all the current symbols.
with open('tickerlist_data.csv') as file_object:
# If the ticker is not in the above file tickerlist_data.csv than
# I want it to perform the following
if ticker not in file_object:
# First it will go to yahoo finance and retrieve the data for
# the stock from 1985-01-01 to the current date, it will do this
# until all the symbols in the ticker list been processed this
# should only happen if the ticker is not in tickerlist_data.csv
pnls = {i:dreader.DataReader(i,'yahoo','1985-01-01',datetime.today()) for i in ticker}
# Next I want the ticker to append to the file so the next time
# that this code runs this process will not repeat. This part
with open('tickerlist_data.csv','a') as file_object:
for symbol in ticker:
file_object.write(symbol)
# This is the final step that I want to take place if the symbol
# in ticker is not listed in tickerlist_data.csv. It will save
# the loaded data from yahoo finance to a csv that opens in excel
for df_name in pnls:
pnls.get(df_name).to_csv("{}_data.csv".format(df_name), index=True, header=True)
else:
# I would like to have the code here to only to be ran if the
# symbol in ticker is in tickerlist. If the symbol is in the
# tickerlist_data.csv than this means that I already have a
# a csv file on my computer with that current stock ticker.
# at this point I just want to up date the current file.
Aucun commentaire:
Enregistrer un commentaire