Example #1
0
    def create_pfhl_order(self, data):
        stp_p = float(data['price'])
        sym = data['symbol']
        pside = data['positionSide']

        for an in data['vas']:
            pp = brkrs.sym_infos[sym]['pp']
            qp = brkrs.sym_infos[sym]['qp']
            close_rate = 0.5
            pos_amt = 0
            brkrs.fetch_postion(an)
            for pos in brkrs.all_valid_pos[an]:
                if pos['symbol'] == sym and pos['positionSide'] == pside:
                    pos_amt = float(pos['positionAmt'])
                    entry_p = float(pos['entryPrice'])
            if not pos_amt:
                Lgr.log('bnews', f"账户 {an} {sym} 没有 {pside} 仓位")
                continue
            if pside == 'LONG':
                price = round(
                    abs(entry_p - stp_p) + entry_p *
                    (1 + Settings['slippage']), pp)
            else:
                price = round(
                    entry_p * (1 - Settings['slippage']) -
                    abs(entry_p - stp_p), pp)
            qty = round(abs(pos_amt) * close_rate, qp)
            qty = max(qty, 1 / pow(10, qp))
            rstr = base62.encode(int(datetime.now().timestamp()))
            param = {
                'symbol': sym,
                'side': data['side'],
                'positionSide': pside,
                'price': price,
                'type': 'LIMIT',
                'timeInForce': 'GTC',
                'quantity': qty,
                'newClientOrderId': f"{app}_{data['op']}_{rstr}",
            }
            res = brkrs.place_order(an, param)
            if res['orderId']:
                op_ch = get_op(data['op'])
                Lgr.log(data['op'], f"账户:{an} {op_ch}: {param}")
            else:
                Lgr.log('ERROR', 'place order has error')
Example #2
0
def get_order_param(event,values):
    estrs = event.split('-')
    coin = estrs[1]
    op_ch = get_op(estrs[2]) # operation: ol,os,cl,cs
    vas = get_valid_accts(values) # acct list
    mr = get_mr(values)
    lev = get_lev(values)
    sym = f"{coin}USDT"
    if lev == 0: 
        lev_str = ''
        for v in vas:
            lev_str += f"{v}-{output.levs[v][sym]}; "
    else:
        lev_str = lev
    cr = get_cr(values) # close position ratio
    price_str = '市价' if not values[f"{coin}-p"] else values[f"{coin}-p"]
    if event[-2] == 'o':
        pstr1 = f"操作账户: {vas} \n\n交易对: {sym} \n\n方向: {op_ch} \n\n保证金使用比例: {mr}"
        pstr2 = f" \n\n杠杆倍数: {lev_str}\n\n价格: {price_str}\n"
        pstr = pstr1+pstr2
        param = {
            'symbol': sym,
            'side': 'BUY' if event[-1]=='l' else 'SELL',
            'positionSide': 'LONG' if event[-1]=='l' else 'SHORT',
            'vas': vas,
            'mr': mr,
            'lev': lev,
            'op': estrs[2],
            'price': price_str
        }
    if event[-2] == 'c':
        pstr = f"操作账户: {vas} \n\n交易对: {sym} \n\n方向: {op_ch} \n\n关仓比例: {cr}\n\n价格: {price_str}\n"
        param = {
            'symbol': sym,
            'side': 'SELL' if event[-1]=='l' else 'BUY',
            'positionSide': 'LONG' if event[-1]=='l' else 'SHORT',
            'vas': vas,
            'cr': cr,
            'op': estrs[2],
            'price': price_str
        }
    return pstr,param
Example #3
0
 def create_stop_order(self, data):
     sym = data['symbol']
     for an in data['vas']:
         rstr = base62.encode(int(datetime.now().timestamp()))
         param = {
             'symbol': sym,
             'side': data['side'],
             'positionSide': data['positionSide'],
             'type': 'STOP_MARKET',
             'newClientOrderId': f"{app}_stplos_{rstr}",
             'stopPrice': data['price'],
             'closePosition': True,
             'workingType': 'MARK_PRICE'
         }
         res = brkrs.place_order(an, param)
         if res['orderId']:
             op_ch = get_op(data['op'])
             Lgr.log(data['op'], f"账户:{an} {op_ch}: {param}")
         else:
             Lgr.log('ERROR', 'place order has error')
Example #4
0
 def close_pos(self, data):
     sym = data['symbol']
     if data['price'] == '市价':
         res = brkrs.fetch_symbol_price(sym)
         price = float(res['price'])
         order_type = 'MARKET'
     else:
         price = float(data['price'])  #fix price
         order_type = 'LIMIT'
     for an in data['vas']:
         qp = brkrs.sym_infos[sym]['qp']
         close_rate = float(data['cr'][:-1]) / 100
         pos_amt = 0
         for pos in brkrs.all_valid_pos[an]:
             if pos['symbol'] == sym and pos['positionSide'] == data[
                     'positionSide']:
                 pos_amt = float(pos['positionAmt'])
         if not pos_amt:
             Lgr.log('bnews', f'账户 {an} 没有找到对应仓位')
             return
         qty = round(abs(pos_amt) * close_rate, qp)
         qty = max(qty, 1 / pow(10, qp))
         rstr = base62.encode(int(datetime.now().timestamp()))
         param = {
             'symbol': sym,
             'side': data['side'],
             'positionSide': data['positionSide'],
             'type': order_type,
             'quantity': qty,
             'newClientOrderId': f"{app}_{data['op']}_{rstr}",
         }
         if order_type == 'LIMIT':
             param['price'] = price
             param['timeInForce'] = 'GTC'
         res = brkrs.place_order(an, param)
         if res['orderId']:
             op_ch = get_op(data['op'])
             Lgr.log(data['op'], f"账户:{an} {op_ch}: {param}")
         else:
             Lgr.log('ERROR', 'place order has error')
Example #5
0
def get_plan_order_param(event,values):
    estrs = event.split('-')
    coin = estrs[1]
    op_ch = get_op(estrs[2]) # operation: ol,os,cl,cs
    vas = get_valid_accts(values) # acct list

    price = values[f"{coin}-stp"]
    if not price:
        Lgr.log('bnews','必须设置止损价')
        return None,None

    pstr = f"操作账户: {vas} \n\n币: {coin} \n\n方向: {op_ch} \n\n止损价格: {price}\n"
    param = {
        'symbol': f"{coin}USDT",
        'side': 'SELL' if event[-1]=='l' else 'BUY',
        'positionSide': 'LONG' if event[-1]=='l' else 'SHORT',
        'vas': vas,
        'op': estrs[2],
        'price': price
    }

    return pstr,param
Example #6
0
    def create_pos(self, data):
        sym = data['symbol']
        if data['price'] == '市价':
            res = brkrs.fetch_symbol_price(sym)
            price = float(res['price'])
            order_type = 'MARKET'
        else:
            price = float(data['price'])  #fix price
            order_type = 'LIMIT'
        for an in data['vas']:
            self._set_lev(an, data)
            acct_equ = brkrs.all_equ[an]
            margin_rate = float(data['mr'][:-1]) / 100
            margin = acct_equ * margin_rate
            quote = margin * float(data['lev'])
            qp = brkrs.sym_infos[sym]['qp']
            qty = round(quote / price, qp)
            qty = max(qty, 1 / pow(10, qp))

            rstr = base62.encode(int(datetime.now().timestamp()))
            param = {
                'symbol': sym,
                'side': data['side'],
                'positionSide': data['positionSide'],
                'type': order_type,
                'quantity': qty,
                'newClientOrderId': f"{app}_{data['op']}_{rstr}",
            }
            if order_type == 'LIMIT':
                param['price'] = price
                param['timeInForce'] = 'GTC'
            res = brkrs.place_order(an, param)
            if res['orderId']:
                op_ch = get_op(data['op'])
                Lgr.log(data['op'], f"账户:{an} {op_ch}: {param}")
            else:
                Lgr.log('ERROR', 'place order has error')