def trade(): if 'user_id' not in session: return redirect(url_for('home')) if request.method == 'POST': #---Add order--- res = _add_order(g.db,session['user_id'],long(request.form['contract_id']),request.form['b_s'], request.form['point'], request.form['lots']) session['bs'] = request.form['b_s'] flash(res['msg'],res['category']) if res['category'] == 'suc': if 'Deal' in res['msg']: _update_contract(g.db,request.form['contract_id'],'D') else: _update_contract(g.db,request.form['contract_id'],'C') return redirect(url_for('trade',c=request.form['contract_id'])) else: co = request.args.get('co', 0,type=int) contract_id = request.args.get('c', 0, type=int) if co >= 1: #Cancel order res = _cancel_order(g.db,session['user_id'],co) flash(res['msg'],res['category']) _update_contract(g.db,contract_id,'C') return redirect(url_for('trade',c=contract_id)) else: g.u = _update_user(g.db,session,[]) if contract_id == 0 and 'latestcont' in session: contract_id = session['latestcont'] return render_template('trade.html',default_cid = contract_id )
def api(): uid = _apikeyvalidate(g.db,request.args.get('key', 0)) if uid == 0: abort(404) type = request.args.get('t', 0) if type == 'A': #add order contract_id = request.args.get('cid', 0,type=int) b_s = request.args.get('bs', 0) point = request.args.get('pt', 0) lots = request.args.get('lt', 0,type=int) res = _add_order(g.db,uid,contract_id,b_s,point,lots,'S') return jsonify(res) elif type == 'C': #cancel order orderid = request.args.get('oid', 0) res = _cancel_order(g.db,uid,orderid) return jsonify(res) elif type == 'O': #get orderlist res = _update_user(g.db,{'user_id':uid},['orders']) return jsonify(res) elif type == 'U': #Update server contract_id = request.args.get('cid', 0,type=int) _update_contract(g.db,contract_id,'D') return jsonify({'category':'suc'}) else: abort(404)
def forced_close(): cur.execute( "select user_id,balance + p_l - pmargin,omargin from v_userbtc where balance + p_l - omargin - pmargin < 0" ) ccur = db.cursor() ocur = db.cursor() for u in cur.fetchall(): # cancel all orders; ocur.execute("SELECT order_id FROM orders WHERE status ='O' and user_id = %s ", u[0]) for o in ocur.fetchall(): ccur.callproc("p_exchange", (o[0], u[0], "C")) print >>sys.stderr, ccur.fetchone() ocur.nextset() if u[1] <= 0: preqamt = -u[1] ocur.execute("SELECT contract_id,buy_sell,lots FROM v_pos WHERE user_id = %s ORDER BY p_l Desc", u[0]) for p in ocur.fetchall(): lt = min( ( preqamt / ( gv_contract[p[0]]["latestpoint"] * gv_contract[p[0]]["btc_multi"] * gv_contract[p[0]]["leverage"] ) ).to_integral_exact(rounding=ROUND_CEILING), p[2], ) if p[1] == "B": _add_order( db, u[0], p[0], "S", gv_contract[p[0]]["bp"] * float(1 - gv_contract[p[0]]["movelimit"]), lt, "F", ) elif p[1] == "S": _add_order( db, u[0], p[0], "B", gv_contract[p[0]]["bp"] * float(1 + gv_contract[p[0]]["movelimit"]), lt, "F", ) preqamt = ( preqamt - gv_contract[p[0]]["latestpoint"] * gv_contract[p[0]]["btc_multi"] * gv_contract[p[0]]["leverage"] * lt ) print >>sys.stderr, gv_contract[p[0]]["name"], lt, preqamt, " Forced Close remained" if preqamt <= 0: break # todo send email to notify user db.commit()