-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgetTickInfo.py
131 lines (114 loc) · 4.48 KB
/
getTickInfo.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
# coding=utf-8
from pyfinance import utils
import jsm
from multiprocessing import Process, Queue
import pymongo
mongo_info= {"host":"localhost", "port":27017, "db":"jp_stock", "collection":"tick_info"}
def initDB(host):
con = pymongo.MongoClient(host=host)
col = con[mongo_info["db"]][mongo_info["collection"]]
col.ensure_index([("tick_id",1)], unique=True, dropDups=True)
class BrandDownloadProcess(Process):
def __init__(self, input_queue, error_queue):
Process.__init__(self)
self.input_queue = input_queue
self.error_queue = error_queue
self.q = jsm.Quotes()
self.con = pymongo.MongoClient(host=mongo_info["host"], port=mongo_info["port"])
def _workf(self, brand_id):
ret = self.q.get_brand(brand_id)
col = self.con[mongo_info["db"]][mongo_info["collection"]]
for r in ret:
key = {"tick_id":r.ccode}
data = {"$set":{"brand_id":brand_id, "market":r.market, "name":r.name, "info":r.info}}
col.update(key, data, upsert=True)
def run(self):
while not self.input_queue.empty():
brand_id = self.input_queue.get(timeout=5)
try:
print "get brand %s" % brand_id
self._workf(brand_id)
# self.output_queue.put((brand_id, ret), False)
print "OK brand %s" % brand_id
except:
print "%s error" %(brand_id,)
self.error_queue.put(brand_id, False)
def getBrands(n_worker=1):
brand_ids = jsm.Brand.IDS.keys()
error_queue = Queue(len(brand_ids))
brand_id_queue = Queue(len(brand_ids))
[brand_id_queue.put(i) for i in brand_ids]
print "total brands = %d" % len(brand_ids)
pool = [BrandDownloadProcess(brand_id_queue, error_queue) for _ in xrange(n_worker)]
for p in pool:
p.start()
for p in pool:
p.join()
print "collecting results"
error_list = utils.dump_queue(error_queue)
print "OK"
return error_list
class FinanceDownloadProcess(Process):
"""財務データ
market_cap: 時価総額
shares_issued: 発行済み株式数
dividend_yield: 配当利回り
dividend_one: 1株配当
per: 株価収益率
pbr: 純資産倍率
eps: 1株利益
bps: 1株純資産
price_min: 最低購入代金
round_lot: 単元株数
years_high: 年初来高値
years_low: 年初来安値
"""
def __init__(self, input_queue, error_queue):
Process.__init__(self)
self.input_queue = input_queue
self.error_queue = error_queue
self.q = jsm.Quotes()
self.con = pymongo.MongoClient(host=mongo_info["host"], port=mongo_info["port"])
def _workf(self, tick_id):
r = self.q.get_finance(tick_id)
col = self.con[mongo_info["db"]][mongo_info["collection"]]
key = {"tick_id":tick_id}
data = {"$set":{"market_cap":r.market_cap, "shares_issued":r.shares_issued,
"dividend_yield":r.dividend_yield, "dividend_one":r.dividend_one,
"per":r.per, "pbr":r.pbr,"eps":r.eps, "bps":r.bps, "price_min":r.price_min,
"round_lot":r.round_lot, "years_high":r.years_high, "years_low":r.years_low}}
col.update(key, data, upsert=True)
def run(self):
while not self.input_queue.empty():
tick_id = self.input_queue.get(timeout=5)
try:
print "get tick %s" % tick_id
self._workf(tick_id)
print "OK brand %s" % tick_id
except Exception, e:
print "%s error" %(tick_id,)
print e
self.error_queue.put(tick_id, False)
def getFinances(n_worker=1):
con = pymongo.MongoClient(host=mongo_info["host"], port=mongo_info["port"])
col = con[mongo_info["db"]][mongo_info["collection"]]
tick_ids = sorted(col.distinct("tick_id"))
error_queue = Queue(len(tick_ids))
tick_id_queue = Queue(len(tick_ids))
[tick_id_queue.put(i) for i in tick_ids]
print "total brands = %d" % len(tick_ids)
pool = [FinanceDownloadProcess(tick_id_queue, error_queue) for _ in xrange(n_worker)]
for p in pool:
p.start()
for p in pool:
p.join()
print "collecting results"
error_list = utils.dump_queue(error_queue)
print "OK"
return error_list
if __name__ == "__main__":
initDB("localhost")
ret = getBrands(20)
print ret
ret = getFinances(20)
print ret