标签:
# -*- coding: utf-8 -*- """ 统计一段时期内股票的涨幅情况 """ ‘‘‘导入模块‘‘‘ import os import sqlalchemy import pandas ‘‘‘清屏‘‘‘ cls=os.system(‘cls‘) ‘‘‘连接数据库‘‘‘ engine = sqlalchemy.create_engine(‘mssql+pyodbc://sa:123456@XiTongDSN‘) connection = engine.connect() ‘‘‘股票代码列表‘‘‘ codelist = pandas.read_sql(‘codelist‘, engine) ‘‘‘由股票代码列表计算每只股票一段时期内的涨幅‘‘‘ rise = [] for i in range(0, len(codelist) - 1): dataframe = pandas.read_sql(codelist[‘code‘][i], engine) ‘‘‘注意原数据库日期排序是从大到小的‘‘‘ for j in range(0, len(dataframe) - 1): if dataframe[‘date‘][j] == ‘2016-02-15‘: a = dataframe[‘open‘][j] if dataframe[‘date‘][j] == ‘2016-01-15‘: b = dataframe[‘open‘][j] ‘‘‘还没查不交易的日期时股票数据为 0 还是 null 的情况,后面再说,这只是个粗糙的测试‘‘‘ if not (a == 0) and not (b == 0): c = (a - b) / b break else: c = 0 break ‘‘‘一行数据应包括的内容‘‘‘ rise.append((codelist[‘code‘][i], c, ‘2016-02-15‘, ‘2016-01-15‘)) ‘‘‘ list 转 dataframe ‘‘‘ rise = pandas.DataFrame(rise) ‘‘‘修改列名‘‘‘ rise.columns = [‘code‘, ‘rise‘, ‘date_to‘, ‘date_from‘] ‘‘‘按涨幅排序‘‘‘ rise = rise.sort(columns = ‘rise‘, ascending = False) ‘‘‘写库‘‘‘ rise.to_sql(‘rise‘, engine, if_exists = ‘replace‘, index = False)
标签:
原文地址:http://www.cnblogs.com/blog-3123958139/p/5533268.html