0%

实时监控股票交易数据

实时监控股票交易数据的买入卖出比,监控全盘可能存在的黑马股票。

监控了10只票的买卖比

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
# -*-coding:utf-8-*-

import os, sys, time
import urllib
import urllib2
import xlwt

update_data = []
down_data = []

t = time.time()
now_time = int(round(t * 1000))
now_time = str(now_time)

codes = ['6008591','6002361','0004002','0022012','6002971','0025792','6000281','0000012','3001662']
print '##################################################################'
# 新建excel表空间
xls = xlwt.Workbook()
sheet = xls.add_sheet("Sheet1")
row = 0
#写表头
sheet.write(row, 0, 'time')
r = 1
for i in codes:
sheet.write(row, r, i)
r += 1
sheet.write(row, r, 'price'+i)
r += 1

file_name = 'piao'+now_time + '.xls'

while True:
time.sleep(5)
use = []
t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
#0
use.append(t)
column = 0
for code in codes:
url = 'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=' + code + '&sty=DPTTFD&st=z&sr=&p=&ps=&cb=&js=var%20fenshiEntity=(x)&token=7bc05d0d4c3c22ef9fca8c2a912d779c&_=' + now_time
req = urllib2.Request(url)
try:
response = urllib2.urlopen(req)
except:
print 'connect error try agian...'
try:
response = urllib2.urlopen(req)
except:
print 'connect error try agian and agian...'
response = urllib2.urlopen(req)
responsedata = response.read()
if responsedata == 'var fenshiEntity="-"' :
print 'check the url:'+url
print 'have no data begian....'
print 'wait 1 mins....'
time.sleep(60)
continue

new_data = responsedata.split('|')
for data in new_data:
data_item = data.split('~')
if data_item[3] == '1':
update_data.append(data_item)
else:
down_data.append(data_item)

update_data_priece = []
for update_data_item in update_data:
priece = float(update_data_item[1]) * int(update_data_item[2])
priece = float('%.2f' % priece)
update_data_priece.append(priece)

down_data_priece = []
for down_data_item in down_data:
priece = float(down_data_item[1]) * int(down_data_item[2])
priece = float('%.2f' % priece)
down_data_priece.append(priece)

bi = sum(update_data_priece) / sum(down_data_priece) * 100
bi = float('%.2f' % bi)
#1
use.append(bi)

#盘口信息
pan_url = 'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd='+ code +'&sty=MPNASDP&st=a&sr=1&p=1&ps=1000&cb=&js=var%20pankou=(x)&token=7bc05d0d4c3c22ef9fca8c2a912d779c&_=' + now_time
pan_req = urllib2.Request(pan_url)
try:
pan_response = urllib2.urlopen(pan_req)
except:
print 'connect error try agian...'
try:
pan_response = urllib2.urlopen(pan_req)
except:
print 'connect error try agian and agian...'
pan_response = urllib2.urlopen(pan_req)
pan_responsedata = pan_response.read()
new_pan_data = pan_responsedata.split(',')
pan_p = float(new_pan_data[3])
#2
use.append(pan_p)

print use
row += 1
for i in range(len(use)):
sheet.write(row, i, use[i])
xls.save(file_name)

也玩了把以太经典的数据监控:

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
# -*-coding:utf-8-*-

import os, sys, time
import urllib
import urllib2
import xlwt,json


t = time.time()
now_time = int(round(t * 1000))
now_time = str(now_time)

print '##################################################################'
# 新建excel表空间
xls = xlwt.Workbook()
sheet = xls.add_sheet("Sheet1")
row = 0

url = 'https://appapi.btctrade.com/coin/rmb/etc/app.trusts.js'

while True:
time.sleep(5)
trace_data = []
t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
trace_data.append(t)

req = urllib2.Request(url)
try:
response = urllib2.urlopen(req)
except:
print 'connect error try agian...'
try:
response = urllib2.urlopen(req)
except:
print 'connect error try agian and agian...'
response = urllib2.urlopen(req)
responsedata = response.read()
data = json.loads(responsedata)
last_p = float(data["etc"]["last"])
buy_datas = data["etc"]["trusts"]["buy"]
buy_p_total = []
for buy_data in buy_datas:
p = float('%.2f' % float(buy_data["p"]))
n = float('%.2f' % float(buy_data["n"]))
buy_p = p * n
buy_p_total.append(buy_p)

sale_datas = data["etc"]["trusts"]["sale"]
sale_p_total = []
for sale_data in sale_datas:
p = float('%.2f' % float(sale_data["p"]))
n = float('%.2f' % float(sale_data["n"]))
sale_p = p * n
sale_p_total.append(sale_p)

buy_total = sum(buy_p_total)
sale_total = sum(sale_p_total)
bi = buy_total / sale_total * 100
bi = float('%.2f' % bi)

trace_data.append(bi)
trace_data.append(last_p)
trace_data.append(buy_total)
trace_data.append(sale_total)
print trace_data
sheet.write(row, 0, trace_data[0])
sheet.write(row, 1, trace_data[1])
sheet.write(row, 2, trace_data[2])
sheet.write(row, 3, trace_data[3])
sheet.write(row, 4, trace_data[4])
row += 1
file_name = now_time + '.xls'
xls.save(file_name)