def take_order(qty_type):
    try:
        account_list = tools.get_buy_account_list()
        for account in account_list:
            keyvalue, buy_api_key, buy_seceret_key, buy_passphrase, buy_instrument, order_size = account  # 拆包
            buy_futureAPI = future.FutureAPI(buy_api_key, buy_seceret_key,
                                             buy_passphrase, True)
            result = buy_futureAPI.get_specific_position(buy_instrument)
            buy_future = result['holding'][0]  # 该账户量化数据
            buy_result1, buy_result2 = '', ''
            if buy_future['long_qty'] == '0' and buy_future['short_qty'] == '0':
                # 如果持仓为空
                if qty_type != 0:
                    buy_result1 = buy_futureAPI.take_order(buy_instrument,
                                                           str(qty_type),
                                                           '',
                                                           size=order_size,
                                                           order_type='4')
            elif (buy_future['long_qty'] == '0'
                  and qty_type == 1) or (buy_future['short_qty'] == '0'
                                         and qty_type == 2):  # 目前开空
                buy_result1 = buy_futureAPI.take_order(buy_instrument,
                                                       str(qty_type),
                                                       '',
                                                       size=order_size,
                                                       order_type='4')
                time.sleep(2)
                buy_result2 = buy_futureAPI.take_order(buy_instrument,
                                                       str(5 - qty_type),
                                                       '',
                                                       size=order_size,
                                                       order_type='4')
            elif (qty_type == 0 and (buy_future['long_qty'] == '0'
                                     or buy_future['short_qty'] == '0')):
                if buy_future['long_qty'] == '0':
                    buy_result2 = buy_futureAPI.take_order(buy_instrument,
                                                           '4',
                                                           '',
                                                           size=order_size,
                                                           order_type='4')
                else:
                    buy_result2 = buy_futureAPI.take_order(buy_instrument,
                                                           '3',
                                                           '',
                                                           size=order_size,
                                                           order_type='4')
            else:
                pass
            tools.warning(keyvalue, buy_result1, buy_result2, sep='\n')
            time.sleep(5)
    except Exception as e:
        print("future_record.py -take_order()出現异常:", e)
        sms_text = '请查看异常天气'
        sms_result = sms_send.send(sms_text, True)
Exemplo n.º 2
0
    sched = BlockingScheduler(job_defaults=job_defaults)

    # 有date, interval, cron可供选择,其实看字面意思也可以知道,date表示具体的一次性任务,interval表示循环任务,cron表示定时任务

    # 检测下单
    def my_record():
        record.okex_v5()

    def my_listener(event):
        if event.exception:
            print('任务出错了。')
            # sms.send_wrong_sms()
            sched.shutdown()
            time.sleep(5)
            sched.start()
        else:
            pass

    my_record()

    sched.add_job(func=my_record, trigger='interval', seconds=150)

    sched.add_listener(my_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR)
    sched._logger = logging
    sched.start()
except Exception as e:
    tools.warning(str(e))
    sched.shutdown()
    time.sleep(5)
    sched.start()
def okex_v5():
    try:
        global last_mail_text, last_usdt_balance
        mail_text = {}
        text = ''
        sms_text = ''
        accountAPI = account.AccountAPI(api_key, secret_key, passphrase, False)

        # 现货版本

        last_order_history = accountAPI.get_last_order_history()
        if last_order_history['code'] == '0':
            # print(last_order_history)
            # print(len(last_order_history['data']))
            if len(last_order_history['data']) == 0:
                tools.warning('持仓无变化')
            else:
                data = last_order_history['data'][0]
                uTime = timestamp_to_str(int(data['fillTime']))

                mail_text['SPOT'] = '{}:{}-开单:{}-{},{}。'.format(
                    uTime, data['instId'], data['side'], data['fillPx'],
                    data['accFillSz'])

                if last_mail_text == mail_text:
                    tools.warning('持仓无变化')
                else:
                    last_mail_text = mail_text
                    tools.warning(str(last_mail_text))
                    sms_send.send_wecaht(sms_text, mail_text)
                    sms_send.send_to_wecom(str(mail_text))

        else:
            print('获取出错' + last_order_history)

        # 期货版本

        # if result['code'] != '0':
        #     for position in result['data']:
        #         instType = position['instType']
        #         instId = position['instId']
        #         posSide = position['posSide']
        #         liqPx = position['liqPx']
        #         last = position['last']
        #         uTime = timestamp_to_str(int(position['uTime']))
        #         context = '{}:{}-{}-开单:{}-{}。开仓后,清算价变为{}。'.format(
        #             uTime, instType, instId, posSide, last, liqPx)
        #         sms_text = '量化'

        #         if instType == 'FUTURES':
        #             text = text + context
        #         else:
        #             text = context

        #         mail_text[instType] = text

        #     if last_mail_text == mail_text:
        #         tools.warning('持仓无变化')
        #     else:
        #         last_mail_text = mail_text
        #         tools.warning(str(last_mail_text))
        #         # sms_send.send_wecaht(sms_text, mail_text)
        #         # sms_send.send_to_wecom(str(mail_text))
        """
        instType	String	产品类型
        SPOT:币币
        MARGIN:币币杠杆
        SWAP:永续合约
        FUTURES:交割合约
        OPTION:期权
    
        instId	String	产品ID

        posSide	String	持仓方向
        long:双向持仓多头
        short:双向持仓空头
        net:单向持仓(交割/永续/期权:pos为正代表多头,pos为负代表空头。币币杠杆:posCcy为交易货币时,代表多头;posCcy为计价货币时,代表空头。)


        liqPx	String	预估强平价
        不适用于跨币种保证金模式下交割/永续的全仓
        不适用于期权


        last	String	最新成交价


        uTime	String	最近一次持仓更新时间,Unix时间戳的毫秒数格式,如 1597026383085
        """

    except Exception as e:
        print('记录期货账户持仓检测异常:', str(e))