十二、区块量化 gate.io 合约操作文件
新增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 Config
import weixin
import time
from datetime import datetime
pd.set_option('expand_frame_repr', False)
config = Configuration(key=Config.GATEIO_API_KEY, secret=Config.GATEIO_API_SECRET, host=Config.GATEIO_HOST)
futures_api = FuturesApi(ApiClient(config))
# 交易对集合
# symbol:交易对
symbol_pool = ['BTC_USDT', 'ETH_USDT']
# symbol_pool = ['BTC_USDT', 'ETH_USDT', 'BCH_USDT', 'ZEC_USDT', 'DASH_USDT', 'LTC_USDT', 'FIL_USDT', 'XMR_USDT',
# 'DOT_USDT', 'SOL_USDT']
def get_orderbook(symbol='EOS_USDT'):
"""
查看买一的价格
@param symbol: 币种
@return:
"""
result = futures_api.list_futures_order_book(settle='usdt', contract=symbol)
return float(result.asks[0].p), float(result.bids[0].p)
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:
"""
try:
result = futures_api.list_futures_accounts(settle=symbol)
return float(result.available)
except GateApiException as ex:
if ex.label != "USER_NOT_FOUND":
raise ex
return 0
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 = {'datetime': item.t, 'open': item.o, 'high': item.h, 'low': item.l, 'close': item.c, 'volume': item.v}
output_list.append(sub_dict)
df = pd.DataFrame(data=output_list, columns=['datetime', 'open', 'high', 'low', 'close', 'volume'])
df['symbol'] = symbol
df['datetime'] = pd.to_datetime(df['datetime'], unit='s', utc=True).dt.tz_convert('Asia/Shanghai')
# 删除重复数据
df.drop_duplicates(['datetime'], inplace=True)
# 将数值数据转为float型,便于后续处理
convert_list = ['open', 'high', 'low', 'close', 'volume']
df[convert_list] = df[convert_list].astype(float)
# 重置索引
df.reset_index(drop=True, inplace=True)
# 增加id列
df.index = df.index + 1
df = df.reset_index()
df = df.rename(columns={'index': 'id'})
return df
def get_historicalticks(symbol='EOS_USDT', interval='15m', start_str='2023-06-01 08:00:00', end_str='2023-06-09 08:00:00'):
"""
查看历吏价格
@param symbol: 交易对
@param interval: K线数据
@param start_str: 开始时间 注意时间格式为秒(s)精度的 Unix 时间戳 10位
@param end_str: 结束时间 注意时间格式为秒(s)精度的 Unix 时间戳 10位
@return:
"""
after = datetime_to_timestamp10(start_str)
before = datetime_to_timestamp10(end_str)
tickers = futures_api.list_futures_candlesticks(settle='usdt', contract=symbol, interval=interval, _from=after, to=before)
output_list = []
for item in tickers:
sub_dict = {'datetime': item.t, 'open': item.o, 'high': item.h, 'low': item.l, 'close': item.c, 'volume': item.v}
output_list.append(sub_dict)
df = pd.DataFrame(data=output_list, columns=['datetime', 'open', 'high', 'low', 'close', 'volume'])
df['symbol'] = symbol
df['datetime'] = pd.to_datetime(df['datetime'], unit=
import pandas as pd
from gate_api import ApiClient, Configuration, FuturesApi, FuturesOrder
from gate_api.exceptions import GateApiException
from gate_api.config import Config
import weixin
import time
from datetime import datetime
pd.set_option('expand_frame_repr', False)
config = Configuration(key=Config.GATEIO_API_KEY, secret=Config.GATEIO_API_SECRET, host=Config.GATEIO_HOST)
futures_api = FuturesApi(ApiClient(config))
# 交易对集合
# symbol:交易对
symbol_pool = ['BTC_USDT', 'ETH_USDT']
# symbol_pool = ['BTC_USDT', 'ETH_USDT', 'BCH_USDT', 'ZEC_USDT', 'DASH_USDT', 'LTC_USDT', 'FIL_USDT', 'XMR_USDT',
# 'DOT_USDT', 'SOL_USDT']
def get_orderbook(symbol='EOS_USDT'):
"""
查看买一的价格
@param symbol: 币种
@return:
"""
result = futures_api.list_futures_order_book(settle='usdt', contract=symbol)
return float(result.asks[0].p), float(result.bids[0].p)
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:
"""
try:
result = futures_api.list_futures_accounts(settle=symbol)
return float(result.available)
except GateApiException as ex:
if ex.label != "USER_NOT_FOUND":
raise ex
return 0
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 = {'datetime': item.t, 'open': item.o, 'high': item.h, 'low': item.l, 'close': item.c, 'volume': item.v}
output_list.append(sub_dict)
df = pd.DataFrame(data=output_list, columns=['datetime', 'open', 'high', 'low', 'close', 'volume'])
df['symbol'] = symbol
df['datetime'] = pd.to_datetime(df['datetime'], unit='s', utc=True).dt.tz_convert('Asia/Shanghai')
# 删除重复数据
df.drop_duplicates(['datetime'], inplace=True)
# 将数值数据转为float型,便于后续处理
convert_list = ['open', 'high', 'low', 'close', 'volume']
df[convert_list] = df[convert_list].astype(float)
# 重置索引
df.reset_index(drop=True, inplace=True)
# 增加id列
df.index = df.index + 1
df = df.reset_index()
df = df.rename(columns={'index': 'id'})
return df
def get_historicalticks(symbol='EOS_USDT', interval='15m', start_str='2023-06-01 08:00:00', end_str='2023-06-09 08:00:00'):
"""
查看历吏价格
@param symbol: 交易对
@param interval: K线数据
@param start_str: 开始时间 注意时间格式为秒(s)精度的 Unix 时间戳 10位
@param end_str: 结束时间 注意时间格式为秒(s)精度的 Unix 时间戳 10位
@return:
"""
after = datetime_to_timestamp10(start_str)
before = datetime_to_timestamp10(end_str)
tickers = futures_api.list_futures_candlesticks(settle='usdt', contract=symbol, interval=interval, _from=after, to=before)
output_list = []
for item in tickers:
sub_dict = {'datetime': item.t, 'open': item.o, 'high': item.h, 'low': item.l, 'close': item.c, 'volume': item.v}
output_list.append(sub_dict)
df = pd.DataFrame(data=output_list, columns=['datetime', 'open', 'high', 'low', 'close', 'volume'])
df['symbol'] = symbol
df['datetime'] = pd.to_datetime(df['datetime'], unit=