用Python分析白银T+D价格走势

###工具与数据

  • numpy scipy matplotlib
  • 新浪历史数据

###效果

  • agtd

###代码

plotAGTD.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

# -*- coding:utf-8 -*-

'''
sudo apt-get install python-dev
sudo apt-get install python-scipy
sudo apt-get install python-numpy
sudo apt-get install python-matplotlib

sudo easy_install -U distribute
sudo pip install matplotlib

'''

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
from matplotlib.ticker import FuncFormatter
import datetime
import numpy as np
import time

import httplib
import StringIO

import subprocess

now_t = time.time()
conn = httplib.HTTPConnection('vip.stock.finance.sina.com.cn',80,timeout=20)

now = time.strftime("%Y-%m-%d", time.localtime())
# print now
conn.request("GET",
"/q/view/download_gold_history.php?breed=AGTD&start=1992-06-05&end="+now)
r1 = conn.getresponse()
# print r1.status, r1.reason
raw_data = r1.read()
conn.close()

print "HTTP time is "+ str(time.time() - now_t)[0:5]

raw_data = StringIO.StringIO(raw_data)


#file = open('AGTD.xls')
#firstline = unicode(file.readline(),'gb2312').encode('utf-8')
firstline = unicode(raw_data.readline(),'gb2312').encode('utf-8')

data = [firstline]

for each_line in raw_data:
data.append(each_line.split('\t'))

close=[]
date =[]

i= 0
for da in data:
if i != 0:
close.append(da[5])
date.append(da[0])
i += 1

close.reverse()
date.reverse()

datelist= [datetime.date(int(ys), int(ms), int(ds)) for ys, ms, ds
in [ da.split('-') for da in date ]]

close = close[2000:]
datelist = datelist[2000:]



def ma(values, window):
values = np.array(values,dtype=float)
weights = np.repeat(1.0, window)/window
ma = np.convolve(values, weights, 'valid')
return ma


zhfont = mpl.font_manager.FontProperties(
fname='/usr/share/fonts/truetype/droid/DroidSansFallback.ttf')
mpl.rcParams['axes.unicode_minus'] = False


left, width = 0.1, 0.8
rect1 = [left, 0.45, width, 0.45]
rect2 = [left, 0.10, width, 0.34]

fig = plt.figure()


axescolor = '#f6f6f6' # the axes background color

ax = fig.add_axes(rect1, facecolor=axescolor) # left, bottom, width, height
ax2 = fig.add_axes(rect2,facecolor=axescolor, sharex=ax)


ax.set_title(u'【白银递延AGTD 日线】 更新日期:%s' %
str((time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))),
fontsize = 14, fontweight='bold',fontproperties=zhfont)
# ax.set_title(u'AGTD 日线 ',fontproperties=zhfont)

for label in ax.xaxis.get_ticklabels():
label.set_visible(False)

for label in ax2.xaxis.get_ticklabels():
label.set_rotation(25)


ax.plot(datelist,close,'.-')

BBI = (ma(close,3)[48-3:]+ma(close,6)[48-6:]
+ma(close,12)[48-12:]+ma(close,24)[48-24:]+ma(close,48))/5

BBIST = (ma(close,3)[6-3:]+ma(close,6))/2
BBIMT = ma(BBI,40)
BBILT = ma(BBI,200)


# ax.plot(datelist[48-1:],BBI)
ax.plot(datelist[6-1:],BBIST)
ax.plot(datelist[48+40-1-1:],BBIMT)
ax.plot(datelist[48+200-1-1:],BBILT)

'''
ax.plot(datelist[5-1:],ma(close,5))
ax.plot(datelist[30-1:],ma(close,30))
ax.plot(datelist[90-1:],ma(close,90))
ax.plot(datelist[180-1:],ma(close,180))
'''


N1 = 5
N2 = 30
N3 = 100
dis = ma(close,N1)[N2-N1:] - ma(close,N2)
disma = ma(dis,N1)
middle_disma = ma(dis,N3)

ax2.plot(datelist[N2-1:],dis)
ax2.plot(datelist[N2+N1-1-1:],disma)
ax2.plot(datelist[N3+N2-1-1:],middle_disma)
ax2.text(0.025, 0.95, u'强弱W_MA (%d, %d, %d)' % (N1, N2, N3), va='top',
transform=ax2.transAxes, fontsize=10,fontproperties=zhfont)

ax.grid(True)
ax2.grid(True)

ax.autoscale_view() # 自动调整坐标轴范围
ax2.autoscale_view()

# position bottom right
fig.text(0.67, 0.001, 'from wuwenjie',
fontsize=60, color='gray',
ha='right', va='bottom', alpha=0.6)

plt.show()

fig.set_size_inches(9.25, 5.25)
plt.savefig('agtd.png', dpi=75)

# sudo apt-get install pngquant
p=subprocess.Popen("pngquant -force 64 agtd.png",
shell=True,stderr=subprocess.PIPE)
if p.wait() != 0:
print u"压缩错误"+p.stderr.read()