Ejemplo n.º 1
0
def stop_profit(usr_id, instrument_id, ptlist, results, ctype, deal_num,
                highest, lowhest, client_oid):
    try:
        longnum, longcost, longpnl, shortnum, shortcost, shortpnl, long_pnl_ratio, short_pnl_ratio, last = ptlist
        if ctype == 0:
            if longnum == 0 and shortnum == 0:
                return
            id = get_random_no(E_R=0)
            sql = """insert into stop_profit(id,usr_id,instrument_id,
            longnum,longcost,longpnl,shortnum,shortcost,shortpnl,ctime,long_pnl_ratio,short_pnl_ratio,
            results,lasts,highest,lowhest,client_oid)
            values(%s,%s,%s,%s,%s,%s,%s,%s,%s,now(),%s,%s,%s,%s,%s,%s,%s);"""
            parm = [
                id, usr_id, instrument_id, longnum, longcost, longpnl,
                shortnum, shortcost, shortpnl, long_pnl_ratio, short_pnl_ratio,
                results, last, highest, lowhest, client_oid
            ]
            db.query(sql, parm)
        elif ctype == 1:
            if longnum != 0:
                #def spl_log(remark, symbol, deal_num, num, costs, pnl, lasts):
                stop_profit_log.push(results, instrument_id, deal_num, longnum,
                                     longcost, longpnl, last, highest, lowhest,
                                     client_oid)  #多单
            elif shortnum != 0:
                stop_profit_log.push(results, instrument_id, deal_num,
                                     shortnum, shortcost, shortpnl, last,
                                     highest, lowhest, client_oid)  #空单
    except:
        robot_bugerror(traceback, ctype='stop_profit')
    return
Ejemplo n.º 2
0
def update_earnings_price(usr_id, us_id, ctype, flag, last, pnl, client_oid):
    #ctype 0:空单,1:多单
    #flag  'H'高价,'L':低价
    try:
        if ctype == 0:  #空单
            if flag == 'H':  #更高价
                sql = """update user_strategy set htime=now(),h_last=%s where usr_id=%s and id=%s;
                update positions set highest=%s where client_oid=%s;
                """
                parm = [last, usr_id, us_id, last, client_oid]
                db.query(sql, parm)
            else:  #更低价
                sql = """update user_strategy set h_shortpnl=%s,htime=now(),l_last=%s where usr_id=%s and id=%s;
                update positions set lowhest=%s where client_oid=%s;
                """
                parm = [pnl, last, usr_id, us_id, last, client_oid]
                db.query(sql, parm)
        else:  #多单
            if flag == 'H':  #更高价
                sql = """update user_strategy set h_longpnl=%s,htime=now(),h_last=%s where usr_id=%s and id=%s;
                 update positions set highest=%s where client_oid=%s;
                """
                parm = [pnl, last, usr_id, us_id, last, client_oid]
                db.query(sql, parm)
            else:  #更低价
                sql = """update user_strategy set htime=now(),l_last=%s where usr_id=%s and id=%s;
                update positions set lowhest=%s where client_oid=%s;
                """
                parm = [last, usr_id, us_id, last, client_oid]
                db.query(sql, parm)
    except:
        robot_bugerror(traceback, ctype='update_earnings_price')
    return
Ejemplo n.º 3
0
def update_status(usr_id, us_id, status):

    try:
        sql = "update user_strategy set status=%s,utime=now() where usr_id=%s and id=%s;"
        db.query(sql, [status, usr_id, us_id])
    except:
        robot_bugerror(traceback, ctype='update_status')
    return
Ejemplo n.º 4
0
def update_reset(usr_id, us_id):
    #ctype 0空单,1多单
    try:
        sql = """update user_strategy set h_longpnl='0',h_shortpnl='0',
        htime=now(),h_last='0',l_last='0' where usr_id=%s and id=%s;"""
        parm = [usr_id, us_id]
        db.query(sql, parm)
    except:
        robot_bugerror(traceback, ctype='update_reset')
    return
Ejemplo n.º 5
0
def trading_log(remark, usr_id, symbol, num, tp_okexs):
    #us_id  user_strategy的id
    try:
        id = get_random_no(E_R=0)
        sql = """insert into trading_log(id,usr_id,deal_num,instrument_id,remark,ctime,tp_okexs)
        values(%s,%s,%s,%s,%s,now(),%s);"""
        db.query(sql, [id, usr_id, num, symbol, remark, tp_okexs])
    except:
        robot_bugerror(traceback, ctype='trading_log')
    return
Ejemplo n.º 6
0
def client_oid_update(us_id, client_oid, usr_id, symbol):
    try:
        id = get_random_no(E_R=0)
        sql = """update user_strategy set client_oid=%s where id=%s;
        insert into positions(id,usr_id,instrument_id,client_oid,ctime)values(%s,%s,%s,%s,now());
        """
        parm = [client_oid, us_id, id, usr_id, symbol, client_oid]
        db.query(sql, parm)

    except:
        robot_bugerror(traceback, ctype='client_oid_update')
    return
Ejemplo n.º 7
0
def stop_profit_log(remark, symbol, deal_num, num, costs, pnl, lasts, highest,
                    lowhest, client_oid):

    try:
        id = get_random_no(E_R=0)
        sql = """insert into stop_profit_log(id,instrument_id,deal_num,num,costs,pnl,lasts,remark,ctime,highest,lowhest,client_oid)
        values(%s,%s,%s,%s,%s,%s,%s,%s,now(),%s,%s,%s);"""
        db.query(sql, [
            id, symbol, deal_num, num, costs, pnl, lasts, remark, highest,
            lowhest, client_oid
        ])
    except:
        robot_bugerror(traceback, ctype='stop_profit_log')
    return
Ejemplo n.º 8
0
def get_symbol_check():
    try:
        sql = """select 
                    t_instrument_id,tp_okexs
                    from user_strategy where coalesce(del_flag,0)=0 and coalesce(status,0) not in (0,5)
                    """
        l, t = db.select(sql)
        if t > 0:
            for i in l:
                symbol, time_period = i
                GetData(symbol, time_period).signal_chekc()

    except:
        robot_bugerror(traceback, ctype='get_symbol_check')
    return
Ejemplo n.º 9
0
def save_position(usr_id, instrument_id, pt, us_id, result, ctype, client_oid,
                  remark):
    try:
        if ctype != 0:  #2永续
            robot_bugerror('获取持仓故障%s:%s:%s' %
                           (client_oid, instrument_id, result),
                           ctype='ok')
            return

        longnum, longcost, longpnl, shortnum, shortcost, shortpnl, long_pnl_ratio, short_pnl_ratio, last = pt

        if longnum > 0:
            profit_loss = longpnl
        else:
            profit_loss = shortpnl

        sql = """update user_strategy set longnum=%s,longcost=%s,longpnl=%s,
                shortnum=%s,shortcost=%s,shortpnl=%s,ptime=now(),long_pnl_ratio=%s,short_pnl_ratio=%s,
                hold_position=%s,profit_loss=%s where id=%s;"""
        parm = [
            longnum, longcost, longpnl, shortnum, shortcost, shortpnl,
            long_pnl_ratio, short_pnl_ratio, longnum, profit_loss, us_id
        ]

        db.query(sql, parm)
        if longnum == 0 and shortnum == 0:
            update_reset.push(usr_id, us_id)

        if longnum != 0 or shortnum != 0:
            sql = """update positions set holds=%s,results=%s,ctime=now(),longnum=%s,longcost=%s,longpnl=%s,
            shortnum=%s,shortcost=%s,shortpnl=%s,long_pnl_ratio=%s,short_pnl_ratio=%s,lasts=%s where client_oid=%s;"""
            parm = [
                remark,
                '%s' % result, longnum, longcost, longpnl, shortnum, shortcost,
                shortpnl, long_pnl_ratio, short_pnl_ratio, last, client_oid
            ]
            db.query(sql, parm)

    except:
        robot_bugerror(traceback, ctype='save_position')
    return
Ejemplo n.º 10
0
    def apikey_check(self, api_key, secret_key, passphrase):

        accountAPI = account.AccountAPI(api_key, secret_key, passphrase, False)
        try:
            result = accountAPI.coin_transfer('',
                                              '',
                                              1,
                                              1,
                                              5,
                                              sub_account='',
                                              instrument_id='',
                                              to_instrument_id='')
        except:
            robot_bugerror(traceback, 'apikey_check')
            return 3
        if result["code"] == 30006:
            #print("密钥校验有误")
            return 1
        elif result["code"] == 30012:
            #print("没有权限哦")
            return 2
        else:
            return 0
Ejemplo n.º 11
0
def fetch(a):

    try:
        RunTrade(a).run()
    except:
        robot_bugerror(traceback,'begin_run_error')
Ejemplo n.º 12
0
    except:
        robot_bugerror(traceback,'begin_run_error')


if __name__ == "__main__":

    try:
        while True:
            sql = """select convert_from(decrypt(bu_api::bytea, %s, 'aes'),'SQL_ASCII'),
            convert_from(decrypt(bu_secret::bytea, %s, 'aes'),'SQL_ASCII'),
            convert_from(decrypt(bu_passp::bytea, %s, 'aes'),'SQL_ASCII'),
            t_instrument_id,tp_okexs,deal_num, coalesce(status,0),usr_id,id,bu_title,
            coalesce(h_longpnl,'0'),coalesce(h_shortpnl,'0'),
            stop_pnl,coalesce(pnl_num,'0'),stop_kx,coalesce(kx_num,'0'),coalesce(h_last,'0')
            ,coalesce(pnl_tag,'0'),coalesce(l_last,'0'),
            coalesce(sy1,'0'),coalesce(sy2,'0'),coalesce(sy3,'0'),coalesce(sy4,'0'),coalesce(sy5,'0'),coalesce(sy6,'0'),
            coalesce(ht1,'0'),coalesce(ht2,'0'),coalesce(ht3,'0'),coalesce(ht4,'0'),coalesce(ht5,'0'),coalesce(ht6,'0'),
            coalesce(client_oid,'')
            from user_strategy where coalesce(del_flag,0)=0 and coalesce(status,0) not in (0,5)
            """
            l, t = db.select(sql, [md5code, md5code, md5code])
            if t>0:
                gevent.joinall([gevent.spawn(fetch, x) for x in l])
            time.sleep(2)
    except:
        robot_bugerror(traceback,'begin_user_strategy_error')