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]);