新增cross_order.py 文件
# -*- coding: utf-8 -*-import pandas as pd
from gate_api import ApiClient, Configuration, FuturesApi, FuturesOrder
from gate_api.exceptions import GateApiException
from gate_api.config import key, secret, host
import weixin
import time
pd.set_option('expand_frame_repr', False)
config = Configuration(key=key, secret=secret, host=host)
futures_api = FuturesApi(ApiClient(config))
# 交易对集合
# symbol:交易对
symbol_pool = ['BTC_USDT', 'ETH_USDT']
def get_orderbook_ask(symbol='EOS_USDT'):
"""
查看买一的价格
@param symbol: 币种
@return:
"""
result = futures_api.list_futures_order_book(settle='usdt', contract=symbol)
return float(result.asks[0].p)
def get_orderbook_bid(symbol='EOS_USDT'):
"""
查看卖一的价格
@param symbol: 币种
@return:
"""
result = futures_api.list_futures_order_book(settle='usdt', contract=symbol)
return float(result.bids[0].p)
def get_available_cash(symbol='usdt'):
"""
查看账户余额
@param symbol: 币种
@return:
"""
available = "0"
try:
futures_account = futures_api.list_futures_accounts(settle=symbol)
available = futures_account.available
except GateApiException as ex:
if ex.label != "USER_NOT_FOUND":
raise ex
return float(available)
def get_candlesticks(symbol='EOS_USDT', interval='5m', limit='30'):
"""
查看历吏价格
@param symbol:交易对
@param interval: K线数据
@param limit: 显示条数
@return:
"""
limits = int(limit)
tickers = futures_api.list_futures_candlesticks(settle='usdt', contract=symbol, limit=limits, interval=interval)
output_list = []
for item in tickers:
sub_dict = {'time': item.t, 'open': item.o, 'high': item.h, 'low': item.l, 'close': item.c, 'vol': item.v}
output_list.append(sub_dict)
df = pd.DataFrame(data=output_list, columns=['time', 'open', 'high', 'low', 'close', 'vol'])
df['symbol'] = symbol
df['time'] = pd.to_datetime(df['time'], unit='s', utc=True).dt.tz_convert('Asia/Shanghai')
# 删除重复数据
df.drop_duplicates(['time'], inplace=True)
# 将数值数据转为float型,便于后续处理
convert_list = ['open', 'high', 'low', 'close', 'vol']
df[convert_list] = df[convert_list].astype(float)
#df.sort_values(by=['symbol', 'time'], ignore_index=True, ascending=True, inplace=True)
# 重置索引
df.reset_index(drop=True, inplace=True)
return df
def get_long_positions(symbol='EOS_USDT'):
"""
查看合约多仓持仓信息
@param symbol:交易对
@return:
"""
position_size = 0
try:
position = futures_api.get_dual_mode_position(settle='usdt', contract=symbol)
for pos in position:
if pos.mode == 'dual_long':
position_size = pos.size
except GateApiException as ex:
if ex.label != "POSITION_NOT_FOUND":
raise ex
return position_size
def get_short_positions(symbol='EOS_USDT'):
"""
查看合约空仓持仓信息
@param symbol:交易对
@return:
"""
position_size = 0
try:
position = futures_api.get_dual_mode_position(settle='usdt', contract=symbol)
for pos in position:
if pos.mode == 'dual_short':
position_size = pos.size
except GateApiException as ex:
if ex.label != "POSITION_NOT_FOUND":
raise ex
return position_size
def set_leverage(symbol='EOS_USDT', leverage='25'):
"""
设置合约杠杆倍数
@param symbol:交易对
@param leverage:倍数
@return:
"""
long_positions = get_long_positions(symbol)
short_positions = get_short_positions(symbol)
if not long_positions and not short_positions:
futures_api.update_dual_mode_position_leverage(settle='usdt', contract=symbol, leverage=leverage)
else:
print('已持仓中,无法修改杠杆倍数')
标签:return,symbol,futures,param,api,io,position,gate,区块 From: https://www.cnblogs.com/nbjjy/p/17489125.html