From 6a8ab13b7fdc39ca243888415245ea5540f13f95 Mon Sep 17 00:00:00 2001 From: li Date: Tue, 31 Mar 2026 10:53:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=8E=E5=8F=B0=E4=B8=80=E9=94=AE?= =?UTF-8?q?=E5=B9=B3=E4=BB=93=E8=87=AA=E5=8A=A8=E5=8C=96(=E6=AD=A2?= =?UTF-8?q?=E7=9B=88=E6=AD=A2=E6=8D=9F=E5=90=8C=E6=AD=A5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 一键平仓自动根据每个订单的止盈/止损价格确定平仓价: - 做多: 市场价>=止盈价→以止盈价平; 市场价<=止损价→以止损价平 - 做空: 市场价<=止盈价→以止盈价平; 市场价>=止损价→以止损价平 - 其余情况以当前市场价平仓 自动写入平仓价、时间、收益,无需手动输入 --- .../admin/controller/app/AppContract.php | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/application/admin/controller/app/AppContract.php b/application/admin/controller/app/AppContract.php index 9861089..84a6d45 100755 --- a/application/admin/controller/app/AppContract.php +++ b/application/admin/controller/app/AppContract.php @@ -30,6 +30,10 @@ class AppContract extends Backend /** * 一键平仓 - 关闭所有持仓中的合约订单 + * 自动根据止盈(zy_price)/止损(zs_price)价格平仓: + * - 做多(more): 当前价 >= 止盈价 → 以止盈价平仓; 当前价 <= 止损价 → 以止损价平仓 + * - 做空(free): 当前价 <= 止盈价 → 以止盈价平仓; 当前价 >= 止损价 → 以止损价平仓 + * - 其他情况以当前市场价平仓 */ public function closeall() { @@ -69,10 +73,35 @@ class AppContract extends Backend foreach ($contracts as $contract) { Db::startTrans(); try { - $pc_price = $contract['close']; + $market_price = $contract['close']; // 当前市场价 + $zy_price = floatval($contract['zy_price']); // 止盈价 + $zs_price = floatval($contract['zs_price']); // 止损价 + + // 根据止盈止损自动确定平仓价格 + $pc_price = $market_price; // 默认以市场价平仓 + if ($contract['type'] == 'more') { + // 做多: 止盈价 > 0 且 当前价 >= 止盈价 → 以止盈价平仓 + if ($zy_price > 0 && $market_price >= $zy_price) { + $pc_price = $zy_price; + } + // 做多: 止损价 > 0 且 当前价 <= 止损价 → 以止损价平仓 + elseif ($zs_price > 0 && $market_price <= $zs_price) { + $pc_price = $zs_price; + } + } else { + // 做空: 止盈价 > 0 且 当前价 <= 止盈价 → 以止盈价平仓 + if ($zy_price > 0 && $market_price <= $zy_price) { + $pc_price = $zy_price; + } + // 做空: 止损价 > 0 且 当前价 >= 止损价 → 以止损价平仓 + elseif ($zs_price > 0 && $market_price >= $zs_price) { + $pc_price = $zs_price; + } + } + $diffnum = $pc_price - $contract['price']; - // 计算收益 + // 计算收益(与pingcang API保持一致) if ($contract['type'] == 'more') { $income = $diffnum * $contract['num'] * $shizhi; } else { @@ -135,7 +164,7 @@ class AppContract extends Backend ->where('id', $currency['id']) ->update(['num' => $lostnum, 'updatetime' => time()]); - // 更新订单状态 + // 更新订单状态:自动写入平仓价、平仓时间、收益 Db::name('app_contract') ->where('id', $contract['id']) ->update(['status' => '2', 'pc_price' => $pc_price, 'pctime' => time(), 'income' => $income]);