feat: 后台一键平仓自动化(止盈止损同步)
一键平仓自动根据每个订单的止盈/止损价格确定平仓价: - 做多: 市场价>=止盈价→以止盈价平; 市场价<=止损价→以止损价平 - 做空: 市场价<=止盈价→以止盈价平; 市场价>=止损价→以止损价平 - 其余情况以当前市场价平仓 自动写入平仓价、时间、收益,无需手动输入
This commit is contained in:
@@ -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]);
|
||||
|
||||
Reference in New Issue
Block a user