hedgingExecutor.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { hedgeCalculator } from './hedgeCalculator'
  2. import { tradeRouter } from './router'
  3. import { HedgeRequest, ExecutionPlan, ExecutionResult } from './types'
  4. export class HedgingExecutor {
  5. async execute(req: HedgeRequest): Promise<ExecutionResult> {
  6. // 1) 计算对冲决策
  7. const decision = hedgeCalculator.decide(req)
  8. if (!decision.shouldHedge) {
  9. return { success: true, executedQuantity: 0, executedPrice: 0, orderId: undefined, txHash: undefined }
  10. }
  11. const side: 'buy' | 'sell' = decision.hedgeQuantity > 0 ? 'buy' : 'sell'
  12. const absQty = Math.abs(decision.hedgeQuantity)
  13. // 2) 路由报价(根据 method)
  14. const quote =
  15. decision.method === 'spot'
  16. ? await tradeRouter.quoteSpot(req.symbol, absQty)
  17. : await tradeRouter.quotePerp(req.symbol, absQty)
  18. // 3) 滑点检查(占位:总是通过)。实际应根据 req.config.thresholds.maxSlippage 与 quote 对比
  19. // 4) 生成执行计划
  20. const plan: ExecutionPlan = {
  21. method: decision.method,
  22. symbol: req.symbol,
  23. quantity: absQty,
  24. side,
  25. quote,
  26. }
  27. // 5) 执行
  28. const result = await tradeRouter.execute(plan)
  29. return result as ExecutionResult
  30. }
  31. }
  32. export const hedgingExecutor = new HedgingExecutor()