##记录Internship第一个小project
任务描述:1.从Bloomberg上download A50现货期货的成交价格和交易量数据,并作图。
2.从Bloomberg上download 沪深300的现货期货成交价格和交易量数据,并作图。
download 数据:利用excel 中的Bloomberg插件 注意结束时间要选当下时间,否则数据易报错,无法下载。 注意对交易间隔的数据的处理,成交价格应当选用沿用之前价格(接近实际情况,当商品交易量为0,价格可以近似认为不变。)交易量自动填充0。注意Bloomberg上分钟数据,最早在excel只能到140天,在平台上可以找到230天,在那之前的历史数据无法取得。
处理excel数据多使用python openpyxl这个包。R语言多用来处理SAS数据库内的表。
运行环境:OS X EI Capitan Version 10.11.5 Python 2.7 编辑软件:Sublime Text 2 命令行 调用
openpyxl学习文档:https://openpyxl.readthedocs.io/en/default/charts/line.html#id1 官方profile 优点:例子众多,事例详实。缺点:英语(多用英文excel易理解)解释说明少
http://liyangliang.me/posts/2013/02/using-openpyxl-to-read-and-write-xlsx-files/ 民间说明。但这个例子非常典型,很好用
编写程序:(说明在注释中)
# coding=utf-8
import types
from openpyxl import load_workbook
from datetime import date
from openpyxl import Workbook
from openpyxl.chart import (
LineChart,
Reference,
)
from openpyxl.chart.axis import DateAxis
wbb = Workbook()
wss = wbb.active
##上述为固定需要import进来的一些包,wss是新建的一个具体写的excel对象。
'''
rows = [
['Date', 'Batch 1', 'Batch 2', 'Batch 3'],
[date(2015,9, 1), 40, 30, 25],
[date(2015,9, 2), 40, 25, 30],
[date(2015,9, 3), 50, 30, 45],
[date(2015,9, 4), 30, 25, 40],
[date(2015,9, 5), 25, 35, 30],
[date(2015,9, 6), 20, 40, 35],
]
for row in rows:
ws.append(row)
'''
wb = load_workbook(filename=r'sh300year.xlsx') # 打开22.xlsx从里面读数据
sheets = wb.get_sheet_names()
sheet0 = sheets[0] # 第一个表格的名称 #其实感觉没什么用,可以直接写worksheet的名字
#sheet1 = sheets[1]
#sheet2 = sheets[2]
wo = wb.get_sheet_by_name('sum') # 获取特定的 worksheet 填写excel表左下角表的名字
#wp = wb.get_sheet_by_name('IFBA')
#wq = wb.get_sheet_by_name('XUN1')
# 获取表格所有行和列,两者都是可迭代的
#rows = wo.rows
columns1 = wo.columns
#columns2 = wp.columns
#columns3 = wq.columns
# 列迭代
content1 = []
#content2 = []
#content3 = []
rank = [0,0,0,0,0] # 开一个数组,用于标记交易量最大的期货组合,方便后续拼接
num1 = 0
num2 = 0
num3 = 0
#三张表的列迭代
#colunms写入列表,改变格式,方便调用
for aol in columns1:
a = [x.value for x in aol]
content1.append(a)
'''
for bol in columns2:
b = [y.value for y in bol]
content2.append(b)
for dol in columns3:
c = [z.value for z in dol]
content3.append(c)
#从Bloomberg中取数据时,注意严格规定5min一个点格式,确保三张表,有相同rows,从而只需要一次for循环,降低复杂度
#分五段,分别算每段何种期货品种交易量最大,认为最活跃,将编号记录进rank列表
#五段的分界点,通过均分确定
for i in range(105455):
num1 = num1 + content1[2][i]
num2 = num2 + content2[2][i]
num3 = num3 + content3[2][i]
if i == 21091:
if num1 >= num2 and num1 >= num3:
rank[0] = 1
if num2 >= num1 and num2 >= num3:
rank[0] = 2
if num3 >= num1 and num3 >= num1:
rank[0] = 3
num1 = 0
num2 = 0
num3 = 0
if i == 42182:
if num1 >= num2 and num1 >= num3:
rank[1] = 1
if num2 >= num1 and num2 >= num3:
rank[1] = 2
if num3 >= num1 and num3 >= num1:
rank[1] = 3
num1 = 0
num2 = 0
num3 = 0
if i == 63273:
if num1 >= num2 and num1 >= num3:
rank[2] = 1
if num2 >= num1 and num2 >= num3:
rank[2] = 2
if num3 >= num1 and num3 >= num1:
rank[2] = 3
num1 = 0
num2 = 0
num3 = 0
if i == 84364:
if num1 >= num2 and num1 >= num3:
rank[3] = 1
if num2 >= num1 and num2 >= num3:
rank[3] = 2
if num3 >= num1 and num3 >= num1:
rank[3] = 3
num1 = 0
num2 = 0
num3 = 0
if i == 105454:
if num1 >= num2 and num1 >= num3:
rank[4] = 1
if num2 >= num1 and num2 >= num3:
rank[4] = 2
if num3 >= num1 and num3 >= num1:
rank[4] = 3
num1 = 0
num2 = 0
num3 = 0
'''
#注意由官方文档的例子中,画表只能从list中读数据,不可以直接从文档中读,所以兴建一个中间ll[]来过渡一下,拼好的值写入其中
ll = []
'''
#拼接
for i in range(5):
count = 0
if rank[i] == 1:
rows = wo.rows
if rank[i] == 2:
rows = wp.rows
if rank[i] == 3:
rows = wq.rows
if i == 0:
for row in rows[0:21092]:
line = [col.value for col in row]
ll.append(line)
if i == 1:
for row in rows[21092:42183]:
line = [col.value for col in row]
ll.append(line)
if i == 2:
for row in rows[42183:63274]:
line = [col.value for col in row]
ll.append(line)
if i == 3:
for row in rows[63274:84365]:
line = [col.value for col in row]
ll.append(line)
if i == 4:
for row in rows[84365:105455]:
line = [col.value for col in row]
ll.append(line)
'''
#if num3 >= num1 and num3 >= num2:
rows = wo.rows
for row in rows:
line = [col.value for col in row]
ll.append(line)
for roww in ll:
wss.append(roww)
#写数据到表中
#for roww in ll:
# wss.append(roww)
c1 = LineChart() #新建一张图
c1.title = "Line Chart" #图的标题
c1.style = 8 #线条的style
c1.y_axis.title = 'price' #y坐标的标题
#c1.x_axis = DateAxis(crossAx=100)
c1.x_axis.number_format = 'mm-e' #规定日期格式 这是月,年格式
c1.x_axis.majorTimeUnit = "Months" #规定日期间隔 注意days;Months大写
c1.x_axis.title = "Date" #x坐标的标题
c1.y_axis.scaling.min = 2.5 #y坐标的区间
c1.y_axis.scaling.max = 3.4
data = Reference(wss, min_col=4, min_row=64981, max_col=4, max_row=105455) #图像的数据 一列是一条线 若此处取两列,会直接画一个两线的图像
dataa = Reference(wss, min_col=7, min_row=64981, max_col=7, max_row=105455) #这里要画期货和现货,选择写两列数据,分别画两条线
c1.add_data(data, titles_from_data=True)
c1.add_data(dataa, titles_from_data=True)
#s2 = c1.series[0]
#s1 = c1.series[1]
dates = Reference(wss, min_col=1, min_row=64981, max_row=105455)
c1.set_categories(dates)
#s2.smooth = True # Make the line smooth
wss.add_chart(c1, "H1") # 添加c1这个chart 图的左上角位置在H1处
wbb.save("ratio.xlsx") # 写的excel文件为line.xlsx
#for row in rows:
# line = [col.value for col in row]
# content.append(line)
# 通过坐标读取值
#print ws.cell('B12').value # B 表示列,12 表示行
#print ws.cell(row=12, column=2).value