新增cross_order.py文件
# -*- coding: utf-8 -*-
import pandas as pd
from binance.client import Client
from binance.config import api_key, api_secret
import time
client = Client(api_key, api_secret)
pd.set_option('expand_frame_repr', False)
# 交易对集合
# symbol:交易对
symbol_pool = ['BTCUSDT', 'ETHUSDT']
def get_orderbook_ask(symbol='EOSUSDT'):
"""
查看买一的价格
@param symbol: 币种
@return:
"""
result = client.get_orderbook_ticker(symbol=symbol)
return float(result['askPrice'])
def get_orderbook_bid(symbol='EOSUSDT'):
"""
查看卖一的价格
@param symbol: 币种
@return:
"""
result = client.get_orderbook_ticker(symbol=symbol)
return float(result['bidPrice'])
def get_available_cash(symbol='USDT'):
"""
查看账户余额
@param symbol: 币种
@return:
"""
result = client.futures_account_balance()
for value in result:
if value['asset'] == symbol:
balance = float(value['balance'])
return balance
def get_candlesticks(symbol='EOSUSDT', interval='5m', limit='30'):
"""
查看历吏价格
@param symbol: 交易对
@param interval: K线数据
@param limit: 显示条数
@return:
"""
limits = int(limit)
result = client.futures_klines(symbol=symbol, interval=interval.lower(), limit=limits)
for line in result:
del line[6:]
df = pd.DataFrame(data=result, columns=['time', 'open', 'high', 'low', 'close', 'vol'])
df['symbol'] = symbol
# 时区转换为+8.00区域
df['time'] = pd.to_datetime(df['time'], unit='ms', 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.reset_index(drop=True, inplace=True)
return df
def get_long_positions(symbol='EOSUSDT'):
"""
查看合约多仓持仓信息
@param symbol: 交易对
@return:
"""
result = client.futures_position_information(symbol=symbol)
for value in result:
if value['positionSide'] == 'LONG':
return float(value['positionAmt'])
def get_short_positions(symbol='EOSUSDT'):
"""
查看合约空仓持仓信息
@param symbol: 交易对
@return:
"""
result = client.futures_position_information(symbol=symbol)
for value in result:
if value['positionSide'] == 'SHORT':
return float(value['positionAmt'])
def set_leverage(symbol='EOSUSDT', leverage='5'):
"""
设置合约杠杆倍数
@param symbol: 交易对以-SWAP为结尾
@param leverage: 倍数
@return:
"""
long_positions = get_long_positions(symbol)
short_positions = get_short_positions(symbol)
if not long_positions and not short_positions:
leverages = int(leverage)
result = client.futures_change_leverage(symbol=symbol, leverage=leverages)
return result
else:
print('已持仓中,无法修改杠杆倍数')