python+聚宽 统计A股市场个股在某时间段的最高价、最低价及其时间

使用工具pycharm + 聚宽数据源,统计A股市场个股在某时间段的最高价、最低价及其时间,并打印excel表格输出

from jqdatasdk import *
import pandas as pd
import logging
import sys
logger = logging.getLogger("logger")
logger.setLevel(logging.INFO)

# 聚宽数据账户名和密码设置
auth('username','password')

#获取A股列表,包括代号,名称,上市退市时间等。
security = get_all_securities(types=[], date=None)
pd2 = get_all_securities(['stock'])


# 获取股票代号
stocks = list(get_all_securities(['stock']).index)

# 获取股票名称
stocknames = pd2['display_name']

start_date = '2015-01-01'
end_date = '2018-12-31'
def get_stocks_high_low(start_date,end_date):
    # 新建表,表头列 
    # 为:"idx","stockcode","stockname","maxvalue","maxtime","lowvalue","lowtime"
    result = pd.DataFrame(columns=["idx", "stockcode", "stockname", "maxvalue", "maxtime", "lowvalue", "lowtime"])
    for i in range(0,stocks.__len__()-1):
         pd01 = get_price(stocks[i], start_date, end_date, frequency='daily',                 
                fields=None, skip_paused=False,fq='pre', count=None)
        result=result.append(pd.DataFrame({'idx':[i],'stockcode':[stocks[i]],'stockname':    
               [stocknames[i]],'maxvalue':[pd01['high'].max()],'maxtime': 
               [pd01['high'].idxmax()],'lowvalue': [pd01['low'].min()], 'lowtime': 
               [pd01['low'].idxmin()]}),ignore_index=True)

    result.to_csv("stock_max_min.csv",encoding = 'utf-8', index = True)
    logger.warning("执行完毕!")

你可能感兴趣的:(量化投资)