2
2
3
3
# 配置BotVS平台的AccessKey与SecretKey
4
4
5
- BOTVS_ACCESS_KEY = 'xxxxx'
6
- BOTVS_SECRET_KEY = 'yyyyy'
7
5
8
6
import os , time , urllib , md5 , json , sqlite3 , datetime
9
7
from flask import jsonify , request , Flask , render_template , redirect , url_for
10
8
from flask_bootstrap import WebCDN , Bootstrap
11
- from flask_wtf import Form
9
+ from flask_wtf import FlaskForm
12
10
from wtforms import StringField , PasswordField , BooleanField
13
11
from wtforms .validators import InputRequired , Email , Length
14
12
from flask_sqlalchemy import SQLAlchemy
15
13
from flask_login import LoginManager , UserMixin , login_user , login_required , logout_user , current_user
16
14
15
+ BOTVS_ACCESS_KEY = os .getenv ('BOTVS_ACCESS_KEY' , 'xxxxx' )
16
+ BOTVS_SECRET_KEY = os .getenv ('BOTVS_SECRET_KEY' , 'yyyyy' )
17
+
17
18
18
19
class cached (object ):
19
20
def __init__ (self , * args , ** kwargs ):
@@ -49,7 +50,11 @@ def get_exchange_list():
49
50
global exchanges_list
50
51
if exchanges_list is None :
51
52
# init exchanges
52
- exchanges_list = api ('GetExchangeList' , cache = True )['data' ]['result' ]['exchanges' ]
53
+ #exchanges_list = api('GetExchangeList', cache=True)['data']['result']['exchanges']
54
+ exchanges_list = [
55
+ {u'website' : u'https://www.binance.com/' , u'name' : u'\u5e01 \u5b89 ' , u'meta' : u'[{"desc": "Access Key", "required": true, "type": "string", "name": "AccessKey", "label": "Access Key"}, {"encrypt": true, "name": "SecretKey", "required": true, "label": "Secret Key", "type": "password", "desc": "Secret Key"}]' , u'eid' : u'Binance' , u'logo' : u'binance.png' , u'stocks' : u'LTC_BTC,ETH_BTC,WTC_BTC' , u'id' : 0 },
56
+ {u'website' : u'https://www.okex.com/' , u'name' : u'OKEX' , u'meta' : u'[{"desc": "Access Key", "required": true, "type": "string", "name": "AccessKey", "label": "Access Key"}, {"encrypt": true, "name": "SecretKey", "required": true, "label": "Secret Key", "type": "password", "desc": "Secret Key"}]' , u'eid' : u'OKEX' , u'logo' : u'okex.png' , u'stocks' : u'LTC_BTC,ETH_BTC,WTC_BTC' , u'id' : 1 },
57
+ ]
53
58
print ' * Initialize %d exchanges' % (len (exchanges_list ), )
54
59
return exchanges_list
55
60
@@ -58,10 +63,12 @@ def get_default_stock(eid):
58
63
if e ['eid' ] == eid :
59
64
return e ['stocks' ].split (',' )[0 ]
60
65
61
- def plugin_run (exchanges , code ):
66
+ def plugin_run (exchanges , code , pair = None ):
62
67
settings = { "period" : 60 , "source" : code , "exchanges" : []}
63
68
for e in exchanges :
64
- settings ["exchanges" ].append ({"eid" : e .eid , "pair" : get_default_stock (e .eid ), "meta" :{"AccessKey" : e .accessKey , "SecretKey" : e .secretKey }})
69
+ if pair is None :
70
+ piar = get_default_stock (e .eid )
71
+ settings ["exchanges" ].append ({"eid" : e .eid , "pair" : pair , "meta" :{"AccessKey" : e .accessKey , "SecretKey" : e .secretKey }})
65
72
return api ('PluginRun' , settings )
66
73
67
74
def robot_run (robotId , appId , exchanges ):
@@ -124,11 +131,11 @@ class Exchange(db.Model):
124
131
def load_user (user_id ):
125
132
return User .query .get (int (user_id ))
126
133
127
- class LoginForm (Form ):
134
+ class LoginForm (FlaskForm ):
128
135
username = StringField (u'用户名' , validators = [InputRequired (), Length (min = 4 , max = 15 )])
129
136
password = PasswordField (u'密码' , validators = [InputRequired (), Length (min = 8 , max = 80 )])
130
137
131
- class RegisterForm (Form ):
138
+ class RegisterForm (FlaskForm ):
132
139
email = StringField (u'邮箱' , validators = [InputRequired (), Email (message = '无效的Email' ), Length (max = 50 )])
133
140
username = StringField (u'用户名' , validators = [InputRequired (), Length (min = 4 , max = 15 )])
134
141
password = PasswordField (u'密码' , validators = [InputRequired (), Length (min = 8 , max = 80 )])
@@ -229,18 +236,52 @@ def assets():
229
236
db .session .delete (Exchange .query .filter_by (user_id = current_user .id , id = request .args .get ('pid' , - 1 )).first ())
230
237
db .session .commit ()
231
238
return jsonify (results = True )
239
+ platforms = Exchange .query .filter_by (user_id = current_user .id ).all ()
240
+ return render_template ('dashboard.html' , current_user = current_user , platforms = platforms )
241
+
242
+ @app .route ('/hub' , methods = ['GET' , 'POST' ])
243
+ @login_required
244
+ def hub ():
245
+ if request .method == "GET" :
246
+ action = request .args .get ('action' , None )
247
+ symbol = request .args .get ('symbol' , None )
248
+ if action is not None :
249
+ action = action .lower ().strip ()
250
+ if symbol is not None :
251
+ symbol = symbol .split ('.' )[1 ]
252
+ if action == "buy" or action == "sell" :
253
+ args = json .loads (request .args .get ('args' ))
254
+ r = plugin_run ([Exchange .query .filter_by (user_id = current_user .id , id = request .args .get ('pid' , - 1 )).first ()], '''
255
+ function main() {
256
+ exchange.SetTimeout(2000);
257
+ var pfn = '%s' == 'buy' ? exchange.Buy : exchange.Sell;
258
+ return pfn(%f, %f);
259
+ }
260
+ ''' % (action , args [0 ], args [1 ]), symbol )
261
+ return jsonify (r )
262
+ elif action == "cancel" :
263
+ args = json .loads (request .args .get ('args' ))
264
+ r = plugin_run ([Exchange .query .filter_by (user_id = current_user .id , id = request .args .get ('pid' , - 1 )).first ()], '''
265
+ function main() {
266
+ exchange.SetTimeout(2000);
267
+ return exchange.CancelOrder(%s)
268
+ }
269
+ ''' % (args [0 ]), symbol )
270
+ return jsonify (r )
232
271
elif action == "balance" :
233
272
# 运行查询脚本(也可以定制实现任何想要的功能, 比如资产统计,一键平仓)
234
273
r = plugin_run ([Exchange .query .filter_by (user_id = current_user .id , id = request .args .get ('pid' , - 1 )).first ()], '''
235
274
function main() {
236
275
exchange.SetTimeout(2000);
237
- return exchange.GetAccount();
276
+ return [ exchange.GetOrders(), exchange. GetAccount()] ;
238
277
}
239
- ''' )
278
+ ''' , symbol )
240
279
return jsonify (r )
241
280
platforms = Exchange .query .filter_by (user_id = current_user .id ).all ()
242
- return render_template ('dashboard.html' , current_user = current_user , platforms = platforms )
243
-
281
+ arr = []
282
+ for ele in platforms :
283
+ arr .append ({'id' : ele .id , 'eid' : ele .eid , 'label' : ele .label })
284
+ return render_template ('dashboard.html' , current_user = current_user , platforms = json .dumps (arr ))
244
285
@app .route ('/logout' )
245
286
@login_required
246
287
def logout ():
0 commit comments