This repository was archived by the owner on Jun 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (57 loc) · 2.12 KB
/
Copy pathmain.py
File metadata and controls
69 lines (57 loc) · 2.12 KB
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
from api.protos.database_pb2 import Symbol
from quote.fugle import Fugle
import asyncio
from datetime import date, timedelta, datetime
import grpc
from quote.utils import get_grpc_hostname
from api.protos import database_pb2_grpc
from api.protos.protobuf_datatype_utils import datetime_to_timestamp
if __name__ == '__main__':
channel = grpc.insecure_channel(f'{get_grpc_hostname()}:6565')
stub = database_pb2_grpc.DatabaseStub(channel)
over_boughts = []
over_solds = []
_datetime = datetime.now()
retry = 0
max_retry = 7
while len(over_boughts) == 0 or len(over_solds) == 0:
print(f'try to get date for datetime = {_datetime}')
res = stub.query_twse_over_bought_by_date(datetime_to_timestamp(_datetime))
over_boughts = list(res)
res = stub.query_twse_over_sold_by_date(datetime_to_timestamp(_datetime))
over_solds = list(res)
_datetime -= timedelta(days=1)
retry += 1
if retry > max_retry:
break
if len(over_boughts) == 0 or len(over_solds) == 0:
raise Exception('Cannot get TWSE oversold/overbought records')
else:
print(f'over_boughts: {[item.symbol for item in over_boughts]}')
print(f'over_solds: {[item.symbol for item in over_solds]}')
symbols = []
tasks = []
for ele in over_boughts:
symbols.append(Fugle('overbought', ele.symbol))
tasks.append(symbols[-1].exec())
for ele in over_solds:
symbols.append(Fugle('oversold', ele.symbol))
tasks.append(symbols[-1].exec())
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
try:
print(f'==> test read before write')
_test_res = stub.get_stock(Symbol(symbol='AAPL'))
print(f'==> get symbol {_test_res}')
except Exception as e:
print(e)
try:
print(f'==> try again test read before write')
_test_res = stub.get_stock(Symbol(symbol='AAPL'))
print(f'==> get symbol {_test_res}')
except Exception as e:
print(e)
for symbol in symbols:
symbol.dump_to_file()
symbol.save_to_db()