接针


创建日期: 2026-01-12 09:57:56 最后修改: 2026-01-12 09:57:56
复制: 9 点击次数: 155
avatar of zmjzzy zmjzzy
0
关注
1
关注者
策略源码
'''backtest
start: 2026-01-01 00:00:00
end: 2026-01-12 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Hyperliquid","currency":"ETH_USDC"}]
'''

import time

def main():
    # ---- 策略参数设置 (在FMZ界面也可以直接设置) ----
    symbol = "ETH_USDC"          # 交易对 (需在添加交易所时匹配)
    direction = "buy"            # 挂单方向: buy (买入/接针), sell (卖出/摸顶)
    offset_percent = 2.0         # 偏离度: 2.0 表示距离当前价格 2% 的位置
    interval_x = 10              # X秒: 每10秒检查一次
    amount = 0.5                 # 每次挂单的数量 (根据你的9.4万USDC底池自行分配)
    
    Log(f"🚀 脚本启动,目标交易对: {symbol}, 方向: {direction}, 偏离度: {offset_percent}%")
    
    # 设置交易对
    exchange.SetCurrency(symbol)
    
    while True:
        # 1. 获取最新价格
        ticker = _C(exchange.GetTicker)
        current_price = ticker.Last
        
        # 2. 计算挂单价格
        if direction == "buy":
            # 抄底挂单:在当前价下方
            target_price = current_price * (1 - offset_percent / 100)
        else:
            # 摸顶挂单:在当前价上方
            target_price = current_price * (1 + offset_percent / 100)
            
        Log(f"当前最新价: {current_price}, 目标挂单价: {target_price}")
        
        # 3. 下限价单
        order_id = None
        if direction == "buy":
            order_id = exchange.Buy(target_price, amount)
        else:
            order_id = exchange.Sell(target_price, amount)
            
        if not order_id:
            Log("❌ 下单失败,请检查余额或 API 权限")
            return

        # 4. 等待 X 秒
        time.sleep(interval_x)
        
        # 5. 检查成交情况
        order = exchange.GetOrder(order_id)
        
        if not order:
            Log("无法获取订单状态,跳过...")
            continue
            
        # 订单状态判定: 0为未成交, 1为部分成交, 2为完全成交, 3为已取消
        if order.Status == 1 or order.Status == 2:
            # 全部成交或部分成交,直接关闭程序
            fill_type = "全部成交" if order.Status == 2 else "部分成交"
            Log(f"✅ {fill_type}!成交均价: {order.AvgPrice}, 成交数量: {order.DealAmount}。程序停止。")
            break
            
        else:
            # 未成交,取消订单,准备下一次循环
            Log(f"⏳ {interval_x}秒内未成交,撤单并重新执行...")
            exchange.CancelOrder(order_id)
            # 等待撤单确认,防止占用资金导致下次下单失败
            time.sleep(1) 

    Log("🏁 任务完成,脚本已退出。")