Enhance trading workflow with new order flow management
- Added dingdanliu_nb_mflow for improved order processing - Updated related scripts and configurations to support new functionality
This commit is contained in:
85
2.数据下载与处理/每日更新数据库.py
Normal file
85
2.数据下载与处理/每日更新数据库.py
Normal file
@@ -0,0 +1,85 @@
|
||||
import os
|
||||
|
||||
import requests
|
||||
import time
|
||||
from datetime import datetime
|
||||
from requests.adapters import HTTPAdapter
|
||||
import pandas as pd
|
||||
|
||||
pd.set_option('display.max_rows', 1000)
|
||||
pd.set_option('expand_frame_repr', False) # 当列太多时不换行
|
||||
# 设置命令行输出时的列对齐功能
|
||||
pd.set_option('display.unicode.ambiguous_as_wide', True)
|
||||
pd.set_option('display.unicode.east_asian_width', True)
|
||||
|
||||
|
||||
def requestForNew(url):
|
||||
session = requests.Session()
|
||||
session.mount('http://', HTTPAdapter(max_retries=3))
|
||||
session.mount('https://', HTTPAdapter(max_retries=3))
|
||||
session.keep_alive = False
|
||||
response = session.get(url, headers={'Connection': 'close'}, timeout=30)
|
||||
if response.content:
|
||||
return response
|
||||
else:
|
||||
print("链接失败", response)
|
||||
|
||||
|
||||
def getDate():
|
||||
url = 'http://hq.sinajs.cn/list=sh000001'
|
||||
response = requestForNew(url).text
|
||||
data_date = str(response.split(',')[-4])
|
||||
# 获取上证的指数日期
|
||||
return data_date
|
||||
|
||||
|
||||
# 通过新浪财经获取每日更新的股票代码
|
||||
def getStockCodeForEveryday():
|
||||
df = pd.DataFrame()
|
||||
for page in range(1, 100):
|
||||
# 1~100页,不用担心每天新增
|
||||
url = 'http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=' \
|
||||
+ str(page) + '&num=80&sort=changepercent&asc=0&node=hs_a&symbol=&_s_r_a=page'
|
||||
# print(url)
|
||||
content = requestForNew(url).json()
|
||||
if not content:
|
||||
# if content =[]: 这个写法也可以
|
||||
print("股票信息,获取完毕。")
|
||||
break
|
||||
print("正在读取页面" + str(page))
|
||||
time.sleep(3)
|
||||
df = df.append(pd.DataFrame(content, dtype='float'), ignore_index=True)
|
||||
|
||||
rename_dict = {'symbol': '股票代码', 'code': '交易日期', 'name': '股票名称', 'open': '开盘价',
|
||||
'settlement': '前收盘价', 'trade': '收盘价', 'high': '最高价', 'low': '最低价',
|
||||
'buy': '买一', 'sell': '卖一', 'volume': '成交量', 'amount': '成交额',
|
||||
'changepercent': '涨跌幅', 'pricechange': '涨跌额',
|
||||
'mktcap': '总市值', 'nmc': '流通市值', 'ticktime': '数据更新时间', 'per': 'per', 'pb': '市净率',
|
||||
'turnoverratio': '换手率'}
|
||||
df.rename(columns=rename_dict, inplace=True)
|
||||
tradeDate = getDate()
|
||||
df['交易日期'] = tradeDate
|
||||
df = df[['股票代码', '股票名称', '交易日期', '开盘价', '最高价', '最低价', '收盘价', '前收盘价', '成交量', '成交额', '流通市值', '总市值']]
|
||||
# 把转化成float的code替换成交易日期
|
||||
return df
|
||||
|
||||
|
||||
df = getStockCodeForEveryday()
|
||||
print(df)
|
||||
|
||||
for i in df.index:
|
||||
t = df.iloc[i:i + 1, :]
|
||||
stock_code = t.iloc[0]['股票代码']
|
||||
|
||||
# 构建存储文件路径
|
||||
path = './data/' \
|
||||
+ stock_code + '.csv'
|
||||
# 文件存在,不是新股
|
||||
if os.path.exists(path):
|
||||
t.to_csv(path, header=None, index=False, mode='a', encoding='gbk')
|
||||
# 文件不存在,说明是新股
|
||||
else:
|
||||
# 先将头文件输出
|
||||
pd.DataFrame(columns=['数据由邢不行整理']).to_csv(path, index=False, encoding='gbk')
|
||||
t.to_csv(path, index=False, mode='a', encoding='gbk')
|
||||
print(stock_code)
|
||||
Reference in New Issue
Block a user