89 lines
3.7 KiB
Python
89 lines
3.7 KiB
Python
import logging
|
||
from time import sleep
|
||
from core.base import QuantTrader
|
||
from core.strategy import QuantStrategy
|
||
from config import API_KEY, SECRET_KEY, PASSPHRASE, TRADING_CONFIG, TIME_CONFIG
|
||
|
||
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s')
|
||
|
||
|
||
class BizMain:
|
||
def __init__(self):
|
||
api_key = API_KEY
|
||
secret_key = SECRET_KEY
|
||
passphrase = PASSPHRASE
|
||
sandbox = TRADING_CONFIG.get("sandbox", True)
|
||
symbol = TRADING_CONFIG.get("symbol", "BTC-USDT")
|
||
position_size = TRADING_CONFIG.get("position_size", 0.001)
|
||
self.trader = QuantTrader(api_key, secret_key, passphrase, sandbox, symbol, position_size)
|
||
# self.strategy = QuantStrategy(api_key, secret_key, passphrase, sandbox, symbol, position_size)
|
||
|
||
def start_job(self):
|
||
"""
|
||
1. 合约开空单流程
|
||
1.1. 获取当前价格
|
||
1.2 设置tdmode为cross,张数为1,每张货币单位为0.01,杠杆为1,缓冲比例为0.3
|
||
1.2. 计算保证金
|
||
1.4. 开空单
|
||
|
||
2. 现货卖出虚拟货币流程
|
||
2.1 获取当前虚拟货币数量
|
||
2.2 卖出0.01单位虚拟货币
|
||
|
||
3. 合约平空单流程
|
||
3.1 设置tdmode为cross,张数为1
|
||
3.2 平空单
|
||
"""
|
||
td_mode = "cross"
|
||
quantity = 1
|
||
try:
|
||
# 1. 合约开空单流程
|
||
logging.info("[1] 合约开空单流程:")
|
||
# price = self.trader.get_current_price(self.trader.symbol_swap)
|
||
# logging.info(f"当前合约价格: {price}")
|
||
slot = 0.01
|
||
leverage = 1
|
||
buffer_ratio = 0.3
|
||
# margin, entry_price = self.trader.calculate_margin(quantity, leverage, slot, buffer_ratio)
|
||
# logging.info(f"所需保证金: {margin}, 开仓价格: {entry_price}")
|
||
order_id, entry_price = self.trader.place_short_order(td_mode, quantity, leverage, slot, buffer_ratio)
|
||
if order_id:
|
||
logging.info(f"开空单成功,订单ID: {order_id}, 开仓价格: {entry_price}")
|
||
else:
|
||
logging.error("开空单失败")
|
||
except Exception as e:
|
||
logging.error(f"合约开空单流程异常: {e}")
|
||
sleep(1)
|
||
try:
|
||
# 2. 现货卖出比特币流程
|
||
logging.info(f"[2] 现货卖出{self.trader.symbol_prefix}流程:")
|
||
balance = self.trader.get_account_balance()
|
||
btc_balance = balance.get(self.trader.symbol_prefix, 0)
|
||
logging.info(f"当前{self.trader.symbol_prefix}余额: {btc_balance}")
|
||
sell_amount = 0.01
|
||
if btc_balance >= sell_amount:
|
||
order_id = self.trader.place_market_order('sell', sell_amount)
|
||
if order_id:
|
||
logging.info(f"现货卖出{sell_amount}{self.trader.symbol_prefix}成功,订单ID: {order_id}")
|
||
else:
|
||
logging.error(f"现货卖出{self.trader.symbol_prefix}失败")
|
||
else:
|
||
logging.error(f"{self.trader.symbol_prefix}余额不足,无法卖出{sell_amount}{self.trader.symbol_prefix}")
|
||
except Exception as e:
|
||
logging.error(f"现货卖出{self.trader.symbol_prefix}流程异常: {e}")
|
||
sleep(1)
|
||
try:
|
||
# 3. 合约平空单流程
|
||
logging.info("[3] 合约平空单流程:")
|
||
result = self.trader.close_short_order(td_mode, quantity)
|
||
if result:
|
||
logging.info("平空单成功")
|
||
else:
|
||
logging.error("平空单失败")
|
||
except Exception as e:
|
||
logging.error(f"合约平空单流程异常: {e}")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
biz_main = BizMain()
|
||
biz_main.start_job() |