• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
金记缘
博客园    首页    新随笔    联系   管理    订阅  订阅

十三、区块量化 gate.io 合约操作文件续


def create_order(symbol='EOS_USDT', quantity='1', message=''):
"""
全仓合约市价下单
@param symbol:交易对
@param quantity:委托数量 正数为买入,负数为卖出
@param message: 判断依据
@return:
"""
quantitys = float(quantity)
order = FuturesOrder(contract=symbol, size=quantitys, price="0", tif='ioc', iceberg=0, stp_act='-')
order_response = futures_api.create_futures_order(settle='usdt', futures_order=order)
weixin.senddata('@all', 'Gate.io 市价下单成功,\n 订单ID:' + str(
order_response.id) + '\n 交易对:' + order_response.contract + '\n 委托数量:' + str(quantity) + '\n 判断依据:' + message)
return order_response


def close_positions(symbol='EOS_USDT', message=''):
"""
平仓
@param symbol:交易对
@return:
"""
order = FuturesOrder(contract=symbol, size='0', price="0", tif='ioc', iceberg=0, stp_act='-',close= True)
order_response = futures_api.create_futures_order(settle='usdt', futures_order=order)
weixin.senddata('@all', 'Gate.io 平仓成功,\n 订单ID:' + str(
order_response.id) + '\n 交易对:' + order_response.contract + '\n 判断依据:' + message)
return order_response


def up_cross_order(symbol, message=''):
"""
平空做多
@param symbol:交易对
@param message: 消息处理
@return:
"""
print('可做多的交易对:' + symbol)
# 获取标的可平多仓
long_position = get_long_positions(symbol=symbol)
print('可平多仓:' + str(long_position))
# 获取标的可平空仓
short_position = get_short_positions(symbol=symbol)
print('可平空仓:' + str(short_position))
if (long_position + short_position) > 5:
print('持仓数最多5个')
return
free_money = get_available_cash('USDT')
print('可用USDT:' + str(free_money))
if free_money > 0:
# 如果当时持有空仓,则先平仓,再开多仓;
if short_position != 0:
print('Gate.io 如果当时持有空仓,则先平仓,再开多仓;' + symbol)
close_positions(symbol=symbol, message=message)
create_order(symbol=symbol, quantity='1', message=message)
# 如果没有持仓,则直接开多仓;
if (short_position == 0) and (long_position == 0):
print('Gate.io 如果没有持仓,则直接开多仓;' + symbol)
create_order(symbol=symbol, quantity='1', message=message)


def down_cross_order(symbol, message=''):
"""
平多做空
@param symbol:交易对
@param message: 消息处理
@return:
"""
print('可做空的交易对:' + symbol)
# 获取标的可平多仓
long_position = get_long_positions(symbol=symbol)
print('可平多仓:' + str(long_position))
# 获取标的可平空仓
short_position = get_short_positions(symbol=symbol)
print('可平空仓:' + str(short_position))
if (long_position + short_position) > 5:
print('持仓数最多5个')
return
free_money = get_available_cash('USDT')
print('可用USDT:' + str(free_money))
if free_money > 0:
# 如果当时持有多仓,则先平仓,再开空仓;
if long_position != 0:
print('Gate.io 如果当时持有多仓,则先平仓,再开空仓;' + symbol)
close_positions(symbol=symbol, message=message)
create_order(symbol=symbol, quantity='-1', message=message)
# 如果没有持仓,则直接开空仓;
if (short_position == 0) and (long_position == 0):
print('Gate.io 如果没有持仓,则直接开空仓;' + symbol)
create_order(symbol=symbol, quantity='-1', message=message)
 

 

posted @ 2023-06-18 14:44  一生所悟  阅读(136)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3