主要功能就是读取csv文件获得一组x,y数据,简单处理后进行画图,然后不断分析,最后输出csv表格。比如,分析某段时间价格波动幅度,可以先用券商或经纪商的接口不断获取数据存储到csv文件,然后利用python读取数据,进行必要的数据分割处理,绘画图像找到需要的特征,图像得到结果后,改代码输出csv数据。我写这个代码主要做金融市场流动性分析,就是某时段某价格区间能够承受的资金量统计分析以及卖价买价差值随时间段动态分析。 参考代码:
import matplotlib.pyplot as plt
import matplotlib.ticker as plk
import csv
# 读入表格
def read_csv(path):
csv_sheet = [] # 若读取失败,则返回空列表,否则返回二维列表
with open(path, 'r', encoding='utf-8') as f:
data = csv.reader(f)
for row in data:
csv_sheet.append(row)
return csv_sheet
# 写出表格
def write_csv(path, csv_sheet):
with open(path, 'w', newline="") as f:
data = csv.writer(f)
for row in csv_sheet:
data.writerow(row)
return True
return False
# 设置尺寸
def set_size(width, height, density):
# width参考值:10,20,30
# height参考值:5,10,15
# density参考值:50,100
plt.figure(figsize=(width, height), dpi=density)
# 设置坐标
def set_axis(x_min, x_max, x_step, y_min, y_max, y_step):
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.gca().xaxis.set_major_locator(plk.MultipleLocator(x_step))
plt.gca().yaxis.set_major_locator(plk.MultipleLocator(y_step))
# 设置数据
def set_data(Xs, Yss):
for i in range(len(Yss)):
if len(Yss[i]) > len(Xs):
Yss[i] = Yss[i][0:len(Xs)]
if len(Yss[i]) |