资源简介
使用多层LSTM单元的RNN模型 可选的多股票嵌入预测股票市场价格。
代码片段和文件信息
“““
Fetch the daily stock prices from Google Finance for stocks in S & P 500.
@author: lilianweng
“““
import click
import os
import pandas as pd
import random
import time
import urllib2
from BeautifulSoup import BeautifulSoup
from datetime import datetime
DATA_DIR = “data“
RANDOM_SLEEP_TIMES = (1 5)
# This repo “github.com/datasets/s-and-p-500-companies“ has some other information about
# S & P 500 companies.
SP500_LIST_URL = “https://raw.githubusercontent.com/datasets/s-and-p-500-companies/master/data/constituents-financials.csv“
SP500_LIST_PATH = os.path.join(DATA_DIR “constituents-financials.csv“)
def _download_sp500_list():
if os.path.exists(SP500_LIST_PATH):
return
f = urllib2.urlopen(SP500_LIST_URL)
print “Downloading ...“ SP500_LIST_URL
with open(SP500_LIST_PATH ‘w‘) as fin:
print >> fin f.read()
return
def _load_symbols():
_download_sp500_list()
df_sp500 = pd.read_csv(SP500_LIST_PATH)
df_sp500.sort(‘Market Cap‘ ascending=False inplace=True)
stock_symbols = df_sp500[‘Symbol‘].unique().tolist()
print “Loaded %d stock symbols“ % len(stock_symbols)
return stock_symbols
def fetch_prices(symbol out_name):
“““
Fetch daily stock prices for stock ‘symbol‘ since 1980-01-01.
Args:
symbol (str): a stock abbr. symbol like “GOOG“ or “AAPL“.
Returns: a bool whether the fetch is succeeded.
“““
# Format today‘s date to match Google‘s finance history api.
now_datetime = datetime.now().strftime(“%b+%d+%Y“)
base_URL = “https://finance.google.com/finance/historical?output=csv&q={0}&startdate=Jan+1%2C+1980&enddate={1}“
symbol_url = base_URL.format(
urllib2.quote(symbol)
urllib2.quote(now_datetime ‘+‘)
)
print “Fetching {} ...“.format(symbol)
print symbol_url
try:
f = urllib2.urlopen(symbol_url)
with open(out_name ‘w‘) as fin:
print >> fin f.read()
except urllib2.HTTPError:
print “Failed when fetching {}“.format(symbol)
return False
data = pd.read_csv(out_name)
if data.empty:
print “Remove {} because the data set is empty.“.format(out_name)
os.remove(out_name)
else:
dates = data.iloc[:0].tolist()
print “# Fetched rows: %d [%s to %s]“ % (data.shape[0] dates[-1] dates[0])
# Take a rest
sleep_time = random.randint(*RANDOM_SLEEP_TIMES)
print “Sleeping ... %ds“ % sleep_time
time.sleep(sleep_time)
return True
@click.command(help=“Fetch stock prices data“)
@click.option(‘--continued‘ is_flag=True)
def main(continued):
random.seed(time.time())
num_failure = 0
# This is S&P 500 index
#fetch_prices(‘INDEXSP%3A.INX‘)
symbols = _load_symbols()
for idx sym in enumerate(symbols):
out_name = os.path.join(DATA_DIR sym + “.csv“)
if continued and os.path.exists(out_name):
print “Fetched“ sym
continue
succeeded = fetch_pri
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-03-13 07:08 stock-rnn-master\
文件 117 2018-03-13 07:08 stock-rnn-master\.gitignore
文件 2303 2018-03-13 07:08 stock-rnn-master\README.md
目录 0 2018-03-13 07:08 stock-rnn-master\data\
文件 0 2018-03-13 07:08 stock-rnn-master\data\.placeholder
文件 3217 2018-03-13 07:08 stock-rnn-master\data_fetcher.py
文件 2555 2018-03-13 07:08 stock-rnn-master\data_model.py
文件 3849 2018-03-13 07:08 stock-rnn-master\main.py
文件 13033 2018-03-13 07:08 stock-rnn-master\model_rnn.py
目录 0 2018-03-13 07:08 stock-rnn-master\sc
文件 2780 2018-03-13 07:08 stock-rnn-master\sc
文件 659 2018-03-13 07:08 stock-rnn-master\sc
文件 1131 2018-03-13 07:08 stock-rnn-master\sc
文件 4455 2018-03-13 07:08 stock-rnn-master\sc
相关资源
- Python-60DaysRLChallenge中文版强化学习6
- Python-一个非常简单的BiLSTMCRF模型用于
- Python-Tensorflow仿AlphaGo框架实现的AI围棋
- Python-我是小诗姬全唐诗作为训练数据
- Python-用于物体跟踪的全卷积连体网络
- Python-数学建模竞赛中所使用的相关算
- Python-MonoDepthPyTorchPyTorch无监督单目深
- Python-用Tensorflowjs实现的可回收非可回
- Python-利用TensorFlow中的深度学习进行图
- Python-TensorFlow快速入门与实战课件与参
- Python-FCN完全卷积网络中最简单最容易
- Python-匈牙利算法卡尔曼滤波器多目标
- Python-mathAI一个拍照做题程序输入一张
- Python-Tensorflow实现SpatialAsDeepSpatialCNN
- Python-图像分类目标检测姿态估计分割
- Python-用python3opencv3做的中国车牌识别
- Python-各种对抗神经网络GAN大合集
- Python-Intel开源增强学习框架Coach
- Python-CENet用于2D医学图像分割的上下文
- Python-基于深度神经网络和蒙特卡罗树
- Python-SPNLearningAffinityviaSpatialPropagatio
- Python-效果超赞的图片自动增强GANs非成
- Python-VoiceactivitydetectionVAD语音端点检测
- Python-TensorFlow实现的人脸性别年龄识别
- Python-waifu2x利用卷积神经网络放大图片
- Python-TheElementsofStatisticalLearningESL的中
- Python-基于Tensorflow和Keras实现端到端的
- Python-MuseGAN用于乐曲生成的AI
- Python-简单快速实时可定制的机器学习
- Python-PySceneDetect基于PythonOpenCV实现的视
评论
共有 条评论