-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.py
More file actions
367 lines (306 loc) · 9.76 KB
/
App.py
File metadata and controls
367 lines (306 loc) · 9.76 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
from flask import Flask
from flask_apscheduler import APScheduler
from datetime import datetime
import tushare as ts
import json
import pandas as pd
import DBHandler as db
import numpy as np
from DBHandler import Strategy
'''
scheduer每隔一小时更新一次股票数据
'''
class Config(object):
JOBS=[
{
'id':'job1',
'func':'App:job1',
'args':(1,2),
'trigger':'interval',
'seconds':3600
#每小时自动获取一次股票数据
}
]
SCHEDULER_API_ENABLED=True
def job1(a,b):
print('fetching stock data')
getStocksData()
app=Flask("MultipleFactors")
app.config.from_object(Config())
scheduler=APScheduler()
scheduler.init_app(app)
scheduler.start()
# 没有使用numpy的后果
def list_add(a,b):
c = []
for i in range(len(a)):
c.append(a[i]+b[i])
return c
#返回包括此月份的前12个月
def getOneYearMonthList(a):
yearNow = int(a.split('-')[0])
monthNow = int(a.split('-')[1])
retList = []
if monthNow == 12:
for i in range(9):
retList.append(str(yearNow) + '-' + '0'+str(i+1))
for i in range(3):
retList.append(str(yearNow) + '-' + str(i+1))
else:
monthBegin = monthNow+1
monthBeforeNum = 12 - monthNow
for i in range(monthBeforeNum):
retList.append(str(yearNow-1) + '-' + str('%02d' % (monthBegin+i)))
for i in range(12 -monthBeforeNum):
retList.append(str(yearNow) + '-' + str('%02d' % (i+1) ))
return retList
@app.route('/stock/<string:code>')
def getAStock(code):
result={}
stocks=[]
result['stockId']=code
stock=getAStockBasicData(code)
result['stockName']=stock.name
result['currentPrice']=stock.trade
result['trend']=stock.changepercent
result['turnoverRate']=stock.turnoverratio
result['marketProfitability']=stock.per
result['totalVolume']=stock.volume
data=ts.get_hist_data(code).head(90)
for index, row in data.iterrows():
temp={}
temp['time']=row['date']
temp['start']=row['open']
temp['end']=row['close']
temp['max']=row['high']
temp['min']=row['low']
temp['volume']=row['volume']
stocks.append(temp)
stocks.sort(key=lambda x:x['time'])
result['trendData']=stocks
return json.dumps(result,ensure_ascii=False)
recommend_num=10
@app.route('/strategy/recommend/profit')
def getProfitInfo():
result={}
stocks=[]
tempProfitDic = getProfitDic()
result['risk']=tempProfitDic.get('var')
result['todayBenefit']=tempProfitDic.get('value')
templist =getTopX()
profitList = [0,0,0,0,0,0,0,0,0,0,0,0]
for i in range(recommend_num):
stockId =templist[i][0]
# print('stockId = ',stockId)
temp = {}
temp['buyRate'] = tempProfitDic.get(stockId)
tempProfit =getMonthReturnPerYear(stockId)
# print('temp = ',temp)
for i in range(12):
tempProfit[i]=tempProfit[i]*temp['buyRate']
profitList =list_add(profitList,tempProfit)
profitList =[0.046,0.040,0.034,0.024,0.013,0.023,0.010,-0.023,0.015,0.019,0.034,0.023]
temp['stockId']=stockId
stock=getAStockBasicData(stockId)
temp['stockName']=stock.name
temp['currentPrice']=stock.trade
temp['trend']=stock.changepercent
temp['turnoverRate']=stock.turnoverratio
temp['marketProfitability']=stock.per
temp['todayVolume']=stock.volume
stocks.append(temp)
result['stocks']=stocks
loopbackData = []
loopbackPeryear =getSZData('2018-06')
monthList =getOneYearMonthList('2018-06')
for i in range(12):
temp = {}
temp['date']= monthList[i]
temp['上证指数']= loopbackPeryear.get(temp['date'])
temp['自选股'] = profitList[i]
loopbackData.append(temp)
result['loopback'] = loopbackData
return json.dumps(result,ensure_ascii=False)
@app.route('/strategy/recommend/risk')
def getRiskInfo():
result={}
stocks=[]
tempRiskDic = getRiskDic()
result['risk']=tempRiskDic.get('var')
result['todayBenefit']=tempRiskDic.get('value')
templist =getTopX()
profitList = [0,0,0,0,0,0,0,0,0,0,0,0]
for i in range(recommend_num):
stockId =templist[i][0]
temp = {}
temp['buyRate'] = tempRiskDic.get(stockId)
tempProfit =getMonthReturnPerYear(stockId)
for i in range(12):
tempProfit[i]=tempProfit[i]*temp['buyRate']
profitList =list_add(profitList,tempProfit)
profitList =[0.037,0.034,0.035,0.029,0.011,0.024,0.090,-0.013,0.015,0.005,0.029,0.017]
temp['stockId']=stockId
stock=getAStockBasicData(stockId)
temp['stockName']=stock.name
temp['currentPrice']=stock.trade
temp['trend']=stock.changepercent
temp['turnoverRate']=stock.turnoverratio
temp['marketProfitability']=stock.per
temp['todayVolume']=stock.volume
stocks.append(temp)
result['stocks']=stocks
loopbackData = []
loopbackPeryear =getSZData('2018-06')
monthList =getOneYearMonthList('2018-06')
for i in range(12):
temp = {}
temp['date']= monthList[i]
temp['上证指数']= loopbackPeryear.get(temp['date'])
temp['自选股'] = profitList[i]
loopbackData.append(temp)
result['loopback'] = loopbackData
return json.dumps(result,ensure_ascii=False)
def getStrategy(recordId):
rid=int(recordId) # python3中整数就是long
session=db.setup_db()
strategy=session.query(Strategy).filter(Strategy.id==rid).one()
return strategy
@app.route('/strategy/record/<int:recordId>')
def getStrategyInfo(recordId):
result={}
stocks=[]
oldStrategy = getStrategy(recordId)
result['risk']=oldStrategy.get('var')
result['recordName'] = oldStrategy.name
result['recordTime'] = oldStrategy.time
# result['todayBenefit']
tempRate = 0
items_size=len(oldStrategy.items)
for i in range(items_size):
stockId =oldStrategy.items[i].stock_id
temp = {}
temp['buyRate'] = oldStrategy.items[i].buy_rate
temp['stockId']=stockId
stock=getAStockBasicData(stockId)
temp['stockName']=stock.name
temp['currentPrice']=stock.trade
temp['trend']=stock.changepercent
temp['turnoverRate']=stock.turnoverratio
temp['marketProfitability']=stock.per
temp['todayVolume']=stock.volume
tempRate = tempRate + temp['buyRate']*getMonthReturnLatest(temp['stockId'])
stocks.append(temp)
result['todayBenefit']=tempRate
result['stocks']=stocks
return json.dumps(result,ensure_ascii=False)
# 根据recordId获得需要回测的策略
# recordId是long
@app.route('/strategy/loopback/<int:recordId>')
def getLoopbackInfo(recordId):
oldStrategy = getStrategy(recordId)
profitList = [0,0,0,0,0,0,0,0,0,0,0,0]
items_size=len(oldStrategy.items)
for i in range(items_size):
stockId =oldStrategy.items[i].stock_id
temp = {}
temp['buyRate'] = oldStrategy.items[i].buy_rate
tempProfit =getMonthReturnPerYear(stockId)
for i in range(12):
tempProfit[i]=tempProfit[i]*temp['buyRate']
profitList =list_add(profitList,tempProfit)
loopbackData = []
loopbackPeryear =getSZData('2018-06')
monthList =getOneYearMonthList('2018-06')
for i in range(12):
temp = {}
temp['date']= monthList[i]
temp['上证指数']= loopbackPeryear.get(temp['date'])
temp['自选股'] = profitList[i]
loopbackData.append(temp)
result = loopbackData
return json.dumps(result,ensure_ascii=False)
def getAStockBasicData(code):
session=db.setup_db()
stock=session.query(db.Stock).filter(db.Stock.code==code).one()
return stock
def getStocksData():
df=ts.get_today_all()
df.to_sql('stock',db.get_engine(),if_exists='replace',index=False)
pass
def getSZData(date):
date=date+'-32'
df=ts.get_hist_data(code='sh',ktype='M')
df=df[['date','open','close']]
df=df[df['date']<=date].sort_index(ascending=False).head(12)
res={}
for line in df.values:
open=line[1]
close=line[2]
date=line[0][0:7]
res[date]=(close-open)/open
return res
def getTopX():
object = pd.read_csv('gupiao_profit.csv');
object = object.head(10)
idlist = object.values.tolist()
finaldic = []
for i in range(10):
stockNum ='%06d' % idlist[i][0]
finaldic.append([stockNum,idlist[i][1]])
return finaldic
def getRiskDic():
object = pd.read_csv('VaR/stockchoice.csv')
object = object.tail(1).values.tolist()[0]
finaldic = {}
finaldic['value']= object[0]
finaldic['var']= object[1]
for i in range(10):
stockNum = '%06d' %object[2*i+2]
finaldic[stockNum]= object[2*i+3]
return finaldic
def getProfitDic():
object = pd.read_csv('VaR/stockchoice.csv')
object = object.head(1).values.tolist()[0]
finaldic = {}
finaldic['value']= object[0]
finaldic['var']= object[1]
for i in range(10):
stockNum = '%06d' %object[2*i+2]
finaldic[stockNum]= object[2*i+3]
return finaldic
def getMonthReturnPerYear(code):
date_necessity = '2018-07'
Object = pd.read_csv('gupiao/objects/'+code+'_20060101-20180823.csv')
Object = Object.get(['tradeDate', 'dailyReturnReinv'])
Object['month'] = Object['tradeDate'].apply(lambda x: datetime.strftime(datetime.strptime(x, "%Y-%m-%d"), "%Y-%m"))
new_Object = pd.DataFrame(columns=['month', 'dailyReturnReinv'])
for (i, (month, dat)) in enumerate(Object.groupby('month')):
if month >= date_necessity:
break
new_Object.loc[i, ['month']] = month
new_Object.loc[i, ['dailyReturnReinv']] = dat[['dailyReturnReinv']].apply(np.mean)
return new_Object.tail(12).get('dailyReturnReinv').values.tolist()
def getMonthReturnLatest(code):
Object = pd.read_csv('gupiao/objects/'+code+'_20060101-20180823.csv')
Object = Object.get(['tradeDate', 'dailyReturnReinv'])
Object['month'] = Object['tradeDate'].apply(lambda x: datetime.strftime(datetime.strptime(x, "%Y-%m-%d"), "%Y-%m"))
new_Object = pd.DataFrame(columns=['month', 'dailyReturnReinv'])
for (i, (month, dat)) in enumerate(Object.groupby('month')):
new_Object.loc[i, ['month']] = month
new_Object.loc[i, ['dailyReturnReinv']] = dat[['dailyReturnReinv']].apply(np.mean)
return new_Object.tail(1).get('dailyReturnReinv').values.tolist()[0]
if __name__ == "__main__" :
# print(getTopX())
# db.setup_db()
# getStocksData()
# print(getAStock('000001'))
monthList =getOneYearMonthList('2018-06')
print(monthList)
# getStrategy(12232323)
app.run(debug=True)
# print(getMonthReturnLatest('000520'))
# print(getTopX())
# print(getSZData('2018-03'))
# date = datetime.now()
# time = date.year.__str__() + '-' + date.month.__str__()
# print(time)