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)
def check(keyvalue): try: tools.time_print('多个期货账户持仓检测') account = tools.get_account_bykeyvalue(keyvalue) if (account is not None): # 如果账户不为空,说明该输入值存在一个账户 api_key, seceret_key, passphrase, buy_instrument, order_size = account # 拆包 # 单个合约持仓信息 # long_qty short_qty 空仓数量 # long_avg_cost short_avg_cost 开仓平均价 futureAPI = future.FutureAPI(api_key, seceret_key, passphrase, True) # buy_result = futureAPI.take_order(buy_instrument, '4','',size='2', order_type='4')#1:开多2:开空3:平多4:平空 # buy_result2 = futureAPI.take_order(buy_instrument, '1','',size='2', order_type='4')#1:开多2:开空3:平多4:平空 # time.sleep(3) result = futureAPI.get_specific_position(buy_instrument) mail_text, text2 = '', '' my_future = result['holding'][0] # 我的量化数据 if my_future['long_qty'] == '0' and my_future['short_qty'] == '0': # 如果持仓为空,则该账户为开仓 mail_text = '{}账户{}未开仓'.format(keyvalue, buy_instrument) else: # 如果持仓不为空,判断是多还是空,输出持仓情况。 if my_future['long_qty'] == '0': mail_text = '{}账户目前在{}价格套保{}张'.format( keyvalue, my_future['short_avg_cost'], my_future['short_qty']) elif my_future['short_qty'] == '0': mail_text = '{}账户目前在{}价格开多{}张'.format( keyvalue, my_future['long_avg_cost'], my_future['long_qty']) else: mail_text = '{}账户目前在{}价格开多{}张,在{}价格套保{}张'.format( keyvalue, my_future['long_avg_cost'], my_future['long_qty'], my_future['short_avg_cost'], my_future['short_qty']) # 判断账户类型是否全仓 margin_mode = '全仓' if my_future[ 'margin_mode'] == 'crossed' else '逐仓' # 三元运算符 text2 = ',账户类型:{},杠杆倍数:{}。'.format(margin_mode, my_future['leverage']) print(mail_text + text2) # # 无法设置,api有bug https://www.okex.com/docs/zh/#futures-leverage # "error_message":"instrument_id cannot be blank" # result12 = futureAPI.set_leverage('BTC-USD','10') # result11 = futureAPI.get_leverage('BTC-USD') # print(result11) # else: # 如果不存在该输入账户,则输出所有账户持仓情况。 account_list = tools.get_buy_account_list() for account in account_list: keyvalue, api_key, seceret_key, passphrase, buy_instrument, order_size = account # 拆包 futureAPI = future.FutureAPI(api_key, seceret_key, passphrase, True) result = futureAPI.get_specific_position(buy_instrument) print(result) ''' 检查所有的账户对应取值是否正确,设置是否正确 不正确的话 代码进行设置 ''' time.sleep(6) except Exception as e: print('记录多个期货账户持仓检测异常:', e)
import json import time import sys import os o_path = os.getcwd() # 返回当前工作目录 sys.path.append(o_path) # 添加自己指定的搜索路径 from utils import tools, sms_send from okex_sdk_api.okex import futures_api as future with open(o_path + "/json/accounts.json", 'r', encoding='UTF-8') as load_f: myokapi_info = json.load(load_f)['myokapi'] api_key = myokapi_info['api_key'] seceret_key = myokapi_info['seceret_key'] passphrase = myokapi_info['passphrase'] futureAPI = future.FutureAPI(api_key, seceret_key, passphrase, True) instrument = ['BTC-USD-210625', 'BTC-USD-210924'] last_mail_text = ['', ''] # 记录okex期货下单 并发邮件 def bd(index): mail_text = '' # 邮件内容 sms_text = '' # 短信内容 qty_type = 0 try: result = futureAPI.get_specific_position(instrument[index]) # long_qty short_qty 空仓数量 # long_avg_cost short_avg_cost 开仓平均价
def job(): inputval=input("请输入查询:") key = inputval.split('询') # key=msg['Text'].split('询') if(len(key)==2 and key[0]=='查'): keyvalue=key[1] account = ms.ExecQueryOne( " select api_key,seceret_key,passphrase from tab_accounts where keyvalue='" + keyvalue + "' ") if (account is not None): api_key=str(account[0]) seceret_key=str(account[1]) passphrase=str(account[2]) row = ms.ExecQueryOne( "select top 1 * from okex where name='" + keyvalue + "' and DateDiff(dd,create_time,getdate())<=1 order by create_time asc ") lastday = '0' lastday_btc = '0.0' lastday_eth = '0.0' lastday_eos = '0.0' lastday_etc = '0.0' sys.exit(0) if (row is not None): lastday = str(row[1]) lastday_btc = str(row[4]) lastday_eth = str(row[5]) lastday_eos = str(row[6]) lastday_etc = str(row[7]) print('' + keyvalue + ': ') spotAPI = spot.SpotAPI(api_key, seceret_key, passphrase, True) spotresult = spotAPI.get_coin_account_info('USDT') futureAPI = future.FutureAPI(api_key, seceret_key, passphrase, True) btc_amunt = futureAPI.get_coin_account('btc') etc_amunt = futureAPI.get_coin_account('etc') eth_amunt = futureAPI.get_coin_account('eth') eos_amunt = futureAPI.get_coin_account('eos') swapAPI = swap.SwapAPI(api_key, seceret_key, passphrase, True) btc_price = swapAPI.get_mark_price('BTC-USD-SWAP')['mark_price'] etc_price = swapAPI.get_mark_price('ETC-USD-SWAP')['mark_price'] eth_price = swapAPI.get_mark_price('ETH-USD-SWAP')['mark_price'] eos_price = swapAPI.get_mark_price('EOS-USD-SWAP')['mark_price'] # 查询价格 btc = float(str(btc_price)) * float(str(btc_amunt['equity'])) etc = float(str(etc_price)) * float(str(etc_amunt['equity'])) eth = float(str(eth_price)) * float(str(eth_amunt['equity'])) eos = float(str(eos_price)) * float(str(eos_amunt['equity'])) all = str(eos + eth + btc + etc + float(str(spotresult['balance']))) # 盈利比率 bfb = '%.2f%%' % ((float(all) - float(lastday)) / float(all) * 100) if (lastday == '0'): bfb = '0%' res = keyvalue + ':\n当前币币账户USDT:' + spotresult['balance'] + '\n' + '当前合约账户btc:' + btc_amunt[ 'equity'] + ';\n昨日' + lastday_btc + '\n' + '当前合约账户etc: ' + etc_amunt['equity'] res = res + ';\n昨日' + lastday_etc + '\n' + '当前合约账户eth: ' + eth_amunt[ 'equity'] + ';\n昨日' + lastday_eth + '\n' + '当前合约账户eos: ' + eos_amunt[ 'equity'] + ';\n昨日' + lastday_eos + '\n账户总计USDT约: ' + all + ';\n昨日: ' + lastday + '\n' + '今日USDT本位盈利率' + bfb # return res print(res) else: print('查询口令错误')