-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_weight_update.py
130 lines (98 loc) · 3.99 KB
/
model_weight_update.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
__author__ = 'lslacker'
# -*- coding: utf-8 -*-
import argparse
from mssqlwrapper import DB, TempTable
import logging
from reader import ExcelReader
import datetime
import decimal
logger = logging.getLogger(__name__)
def get_stock_id(db, stock_code):
return db.get_one_value('''
select stockID
from vewEquities
where stockCode=?
''', stock_code)
def get_latest_investmentlistinvestmentid(db, investment_list_id, stock_id):
query = '''
select investmentlistinvestmentID, weight
from (
select rank() over (order by datefrom desc) as luan_r, *
from tblInvestmentListInvestment
where investmentListID={} and investmentid={} and dateto = '2079-06-06'
) t
where luan_r = 1
'''.format(investment_list_id, stock_id)
data = db.get_data(query)
if len(data) > 1:
raise Exception("> 2")
try:
data = data[0]
except:
return None, None
return data[0], data[1]
def add_weight(db, investmentlistid, investmentid, weight):
datefrom = datetime.date.today()
datefrom = datefrom.strftime('%Y-%m-%d')
logger.info(datefrom)
query = '''
prcInvestmentListInvestmentPut @investmentlistinvestmentid=0
, @investmentListID={investmentlistid}
, @InvestmentID={investmentid}
, @Weight={weight}
, @DateFrom={datefrom!r}, @DateTo='2079-06-06'
, @dateauthorised = {datefrom!r}
, @authorisedby = 'LMai'
'''.format(investmentlistid=investmentlistid, investmentid=investmentid, weight=weight, datefrom=datefrom)
logger.info(query)
count = db.execute(query)
logger.info('{} added'.format(count))
def expire_current_one(db, investmentlistinvestmentid):
dateto = datetime.date.today() - datetime.timedelta(days=1)
dateto = dateto.strftime('%Y-%m-%d')
query = '''
prcInvestmentListInvestmentPut @investmentlistinvestmentid={investmentlistinvestmentid}
, @DateTo={dateto!r}
'''.format(investmentlistinvestmentid=investmentlistinvestmentid, dateto=dateto)
logger.info(query)
count = db.execute(query)
logger.info('{} added'.format(count))
def add(db, investment_list_id, weight):
weights = [x.split(' ') for x in weight]
for new_weight, stock_code in weights:
stock_id = get_stock_id(db, stock_code)
investmentlistinvestmentid, current_weight = get_latest_investmentlistinvestmentid(db, investment_list_id, stock_id)
try:
new_weight = current_weight + decimal.Decimal(new_weight)
except:
pass
add_weight(db, investment_list_id, stock_id, new_weight)
if investmentlistinvestmentid:
expire_current_one(db, investmentlistinvestmentid)
else:
logger.info('Stock Code {} is new'.format(stock_code))
def consoleUI():
parser = argparse.ArgumentParser(description='Merge multiple csv files into excel file, each csv')
parser.add_argument('--server', default=r'MEL-TST-001\WEBSQL', help='Database Server')
parser.add_argument('--database', default=r'Lonsec', help='Database Name')
parser.add_argument('-v', '--verbose', action='count', default=0)
parser.add_argument('--investment-list-id', help='Investment List ID', type=int, required=True)
parser.add_argument('--weight', help='Model Weight ', required=True, nargs='+')
parser.add_argument('--dry-run', help='Run without commit changes', action='store_true')
a = parser.parse_args()
if a.verbose > 1:
logging.basicConfig(level=logging.INFO)
connection_string1 = r'Driver={{SQL Server Native Client 11.0}};Server={server};Database={database};' \
'Trusted_Connection=yes;'.format(server=a.server, database=a.database)
db = DB.from_connection_string(connection_string1)
if a.verbose > 1:
db.debug = True
logger.info(a)
add(db, a.investment_list_id, a.weight)
if not a.dry_run:
logger.info('Commit changes')
db.commit()
else:
logger.info('All changes did not commit')
if __name__ == '__main__':
consoleUI()