1 Star 0 Fork 0

李贞/stock_picker

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
getSandP.py 6.82 KB
一键复制 编辑 原始数据 按行查看 历史
#!/usr/bin/python
import warnings
warnings.filterwarnings("ignore")
import datetime
from concurrent import futures
import pandas as pd
pd.core.common.is_list_like=pd.api.types.is_list_like
from pandas import DataFrame
import pandas_datareader.data as web
import os
IEX=False
def download_stock(stock):
""" try to query the iex for a stock, if failed note with print """
""" set the download window """
now_time = datetime.datetime.now()
past_five_pm=now_time.replace(hour=18, minute=0, second=0, microsecond=0)
start_time = datetime.datetime(now_time.year - 5, now_time.month , now_time.day)
weekno=datetime.datetime.today().weekday()
try:
print(stock)
stock_df=''
if(IEX==True):
stock_df = web.DataReader(stock,'iex', start_time, now_time)
stock_df['Name'] = stock
else:
stock_df = web.DataReader(stock,'robinhood', start_time, now_time)
output_name = os.path.join('.','stocks',stock+'_data.csv')
stock_df.to_csv(output_name)
if(now_time>=past_five_pm and weekno<5):
#get the last value padding ohlcv
if(IEX==True):
current=web.get_last_iex(stock)[0][0]
temphigh=stock_df['close'][len(stock_df)-1]
templow=stock_df['close'][len(stock_df)-1]
previous_close=stock_df['close'][len(stock_df)-1]
else:
current = float(web.get_quotes_robinhood(stock)[stock][7])
temphigh=float(stock_df['close_price'][len(stock_df)-1])
templow=float(stock_df['close_price'][len(stock_df)-1])
previous_close=float(stock_df['close_price'][len(stock_df)-1])
tempvol=int(stock_df['volume'][len(stock_df)-1])
current_date=datetime.datetime.today().strftime('%Y-%m-%d')
if(current>temphigh):
temphigh=current
if(templow>current):
templow=current
#df.append({ 'date':current_date, 'open':previous_close, 'high':temphigh, 'low':templow, 'close':current, 'volume':tempvol, 'Name':stock })
with open(os.path.join('.','stocks',stock+'_data.csv'), 'a') as f:
if(IEX==True):
f.write( '%s,%0.2f,%0.2f,%0.2f,%0.2f,%d,%s\n'%(current_date, previous_close, temphigh, templow, current, tempvol, stock))
else:
f.write( '%s,%s,%0.6f,%0.6f,False,%0.6f,%0.6f,reg,%d\n'%(stock, current_date, current, temphigh, templow, previous_close, tempvol))
except Exception as e:
bad_names.append(stock)
print('bad: %s %s' % (e,stock))
if __name__ == '__main__':
begin = datetime.datetime(datetime.datetime.now().year - 5, datetime.datetime.now().month , datetime.datetime.now().day)
""" list of s_anp_p companies """
s_and_p = ['A', 'AAL', 'AAP', 'AAPL', 'ABBV', 'ABC', 'ABMD', 'ABT', 'ACN', 'ADBE', 'ADI', 'ADM', 'ADP', 'ADS', 'ADSK', 'AEE', 'AEP', 'AES', 'AET', 'AFL', 'AGN', 'AIG', 'AIV', 'AIZ', 'AJG', 'AKAM', 'ALB', 'ALGN', 'ALK', 'ALL', 'ALLE', 'ALXN', 'AMAT', 'AMD', 'AME', 'AMG', 'AMGN', 'AMP', 'AMT', 'AMZN', 'ANDV', 'ANSS', 'ANTM', 'AON', 'AOS', 'APA', 'APC', 'APD', 'APH', 'APTV', 'ARE', 'ARNC', 'ATVI', 'AVB', 'AVGO', 'AVY', 'AWK', 'AXP', 'AZO', 'BA', 'BAC', 'BAX', 'BBT', 'BBY', 'BDX', 'BEN', 'BF.B', 'BHF', 'BHGE', 'BIIB', 'BK', 'BKNG', 'BLK', 'BLL', 'BMY', 'BR', 'BRK.B', 'BSX', 'BWA', 'BXP', 'C', 'CA', 'CAG', 'CAH', 'CAT', 'CB', 'CBOE', 'CBRE', 'CBS', 'CCI', 'CCL', 'CDNS', 'CELG', 'CERN', 'CF', 'CFG', 'CHD', 'CHRW', 'CHTR', 'CI', 'CINF', 'CL', 'CLX', 'CMA', 'CMCSA', 'CME', 'CMG', 'CMI', 'CMS', 'CNC', 'CNP', 'COF', 'COG', 'COL', 'COO', 'COP', 'COST', 'COTY', 'CPB', 'CRM', 'CSCO', 'CSX', 'CTAS', 'CTL', 'CTSH', 'CTXS', 'CVS', 'CVX', 'CXO', 'D', 'DAL', 'DE', 'DFS', 'DG', 'DGX', 'DHI', 'DHR', 'DIS', 'DISCA', 'DISCK', 'DISH', 'DLR', 'DLTR', 'DOV', 'DPS', 'DRE', 'DRI', 'DTE', 'DUK', 'DVA', 'DVN', 'DWDP', 'DXC', 'EA', 'EBAY', 'ECL', 'ED', 'EFX', 'EIX', 'EL', 'EMN', 'EMR', 'EOG', 'EQIX', 'EQR', 'EQT', 'ES', 'ESRX', 'ESS', 'ETFC', 'ETN', 'ETR', 'EVHC', 'EVRG', 'EW', 'EXC', 'EXPD', 'EXPE', 'EXR', 'F', 'FAST', 'FB', 'FBHS', 'FCX', 'FDX', 'FE', 'FFIV', 'FIS', 'FISV', 'FITB', 'FL', 'FLIR', 'FLR', 'FLS', 'FLT', 'FMC', 'FOX', 'FOXA', 'FRT', 'FTI', 'FTV', 'GD', 'GE', 'GGP', 'GILD', 'GIS', 'GLW', 'GM', 'GOOG', 'GOOGL', 'GPC', 'GPN', 'GPS', 'GRMN', 'GS', 'GT', 'GWW', 'HAL', 'HAS', 'HBAN', 'HBI', 'HCA', 'HCP', 'HD', 'HES', 'HFC', 'HIG', 'HII', 'HLT', 'HOG', 'HOLX', 'HON', 'HP', 'HPE', 'HPQ', 'HRB', 'HRL', 'HRS', 'HSIC', 'HST', 'HSY', 'HUM', 'IBM', 'ICE', 'IDXX', 'IFF', 'ILMN', 'INCY', 'INFO', 'INTC', 'INTU', 'IP', 'IPG', 'IPGP', 'IQV', 'IR', 'IRM', 'ISRG', 'IT', 'ITW', 'IVZ', 'JBHT', 'JCI', 'JEC', 'JEF', 'JNJ', 'JNPR', 'JPM', 'JWN', 'K', 'KEY', 'KHC', 'KIM', 'KLAC', 'KMB', 'KMI', 'KMX', 'KO', 'KORS', 'KR', 'KSS', 'KSU', 'L', 'LB', 'LEG', 'LEN', 'LH', 'LKQ', 'LLL', 'LLY', 'LMT', 'LNC', 'LNT', 'LOW', 'LRCX', 'LUV', 'LYB', 'M', 'MA', 'MAA', 'MAC', 'MAR', 'MAS', 'MAT', 'MCD', 'MCHP', 'MCK', 'MCO', 'MDLZ', 'MDT', 'MET', 'MGM', 'MHK', 'MKC', 'MLM', 'MMC', 'MMM', 'MNST', 'MO', 'MOS', 'MPC', 'MRK', 'MRO', 'MS', 'MSCI', 'MSFT', 'MSI', 'MTB', 'MTD', 'MU', 'MYL', 'NBL', 'NCLH', 'NDAQ', 'NEE', 'NEM', 'NFLX', 'NFX', 'NI', 'NKE', 'NKTR', 'NLSN', 'NOC', 'NOV', 'NRG', 'NSC', 'NTAP', 'NTRS', 'NUE', 'NVDA', 'NWL', 'NWS', 'NWSA', 'O', 'OKE', 'OMC', 'ORCL', 'ORLY', 'OXY', 'PAYX', 'PBCT', 'PCAR', 'PCG', 'PEG', 'PEP', 'PFE', 'PFG', 'PG', 'PGR', 'PH', 'PHM', 'PKG', 'PKI', 'PLD', 'PM', 'PNC', 'PNR', 'PNW', 'PPG', 'PPL', 'PRGO', 'PRU', 'PSA', 'PSX', 'PVH', 'PWR', 'PX', 'PXD', 'PYPL', 'QCOM', 'QRVO', 'RCL', 'RE', 'REG', 'REGN', 'RF', 'RHI', 'RHT', 'RJF', 'RL', 'RMD', 'ROK', 'ROP', 'ROST', 'RSG', 'RTN', 'SBAC', 'SBUX', 'SCG', 'SCHW', 'SEE', 'SHW', 'SIVB', 'SJM', 'SLB', 'SLG', 'SNA', 'SNPS', 'SO', 'SPG', 'SPGI', 'SRCL', 'SRE', 'STI', 'STT', 'STX', 'STZ', 'SWK', 'SWKS', 'SYF', 'SYK', 'SYMC', 'SYY', 'T', 'TAP', 'TDG', 'TEL', 'TGT', 'TIF', 'TJX', 'TMK', 'TMO', 'TPR', 'TRIP', 'TROW', 'TRV', 'TSCO', 'TSN', 'TSS', 'TTWO', 'TWTR', 'TXN', 'TXT', 'UA', 'UAA', 'UAL', 'UDR', 'UHS', 'ULTA', 'UNH', 'UNM', 'UNP', 'UPS', 'URI', 'USB', 'UTX', 'V', 'VAR', 'VFC', 'VIAB', 'VLO', 'VMC', 'VNO', 'VRSK', 'VRSN', 'VRTX', 'VTR', 'VZ', 'WAT', 'WBA', 'WDC', 'WEC', 'WELL', 'WFC', 'WHR', 'WLTW', 'WM', 'WMB', 'WMT', 'WRK', 'WU', 'WY', 'WYNN', 'XEC', 'XEL', 'XL', 'XLNX', 'XOM', 'XRAY', 'XRX', 'XYL', 'YUM', 'ZBH', 'ZION', 'ZTS']
bad_names =[] #to keep track of failed queries
"""here we use the concurrent.futures module's ThreadPoolExecutor
to speed up the downloads buy doing them in parallel
as opposed to sequentially """
#set the maximum thread number
max_workers = 50
workers = min(max_workers, len(s_and_p)) #in case a smaller number of stocks than threads was passed in
with futures.ThreadPoolExecutor(workers) as executor:
res = executor.map(download_stock, s_and_p)
""" Save failed queries to a text file to retry """
if len(bad_names) > 0:
with open('failed_queries.txt','w') as outfile:
for name in bad_names:
outfile.write(name+'\n')
#timing:
finish_time = datetime.datetime.now()
duration = finish_time - begin
minutes, seconds = divmod(duration.seconds, 60)
print('The script took %d minutes and %d seconds to run.'%(minutes, seconds))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lizhen_hbu/stock_picker.git
git@gitee.com:lizhen_hbu/stock_picker.git
lizhen_hbu
stock_picker
stock_picker
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385