作者 karlet

feat:增加平仓

... ... @@ -214,4 +214,8 @@ class CmBroker
{
$this->exBroker->stopListen();
}
public function closeAllPos()
{
$this->exBroker->closeAllPos();
}
}
... ...
... ... @@ -84,6 +84,13 @@ class Api
$method = 'GET';
return $this->request($path, $method, $param, $this->apiInfo);
}
//查询持仓
public function getPositions($param)
{
$path = '/api/v5/account/positions';
$method = 'GET';
return self::request($path, $method, $param, $this->apiInfo);
}
//-------------------------------------
... ...
... ... @@ -178,4 +178,38 @@ class ExBroker
$this->wsAcc->close();
$this->wsKline->close();
}
public function closeAllPos()
{
//获取所有仓位
$param = [
'instType' => 'SWAP'
];
$res = $this->api->getPositions($param);
if ($res['code'] == 0) {
$positions = $res['data'];
foreach ($positions as $key => $value) {
if ($value['pos'] != 0) {
$this->clsoePos($value['instId'], $value['posSide'], abs($value['pos']), $value['mgnMode']);
}
}
}
}
// 平仓
private function clsoePos($instId, $posSide, $size, $tdMode)
{
$param = [
'instId' => $instId,
'tdMode' => $tdMode,
'side' => strtoupper($posSide) == 'LONG' ? 'sell' : 'buy',
'posSide' => $posSide,
'ordType' => 'market',
'sz' => $size,
];
$res = $this->api->placeOrder($param);
if ($res['code'] == 0) {
return true;
} else {
return false;
}
}
}
... ...