作者 karlet

feat:bn增加仓位初始化

... ... @@ -349,11 +349,19 @@ class CmBroker
$positions = $this->exBroker->getAllPos();
$newPositions = [];
foreach ($positions as $key => $value) {
if ($this->plat == self::PLAT_OKX) {
$item = Pos::transferOkxPos($value, $this->symbolInfos, function ($symbol) {
return $this->getSymbolSt($symbol);
});
$newPositions[$item->symbol . "_" . $item->posSide] = $item;
}
if ($this->plat == self::PLAT_BINANCE) {
$item = Pos::transferBinancePos($value, $this->symbolInfos, function ($symbol) {
return $this->getSymbolSt($symbol);
});
$newPositions[$item->symbol . "_" . $item->posSide] = $item;
}
}
return $newPositions;
}
//获取某个品种的某个方向仓位
... ... @@ -395,4 +403,8 @@ class CmBroker
}
return [];
}
public function initPositions()
{
$this->positions = $this->getAllPos();
}
}
... ...
... ... @@ -74,6 +74,13 @@ class Api
$method = "POST";
return $this->request($method, $url, $params, $this->apiInfo);
}
//查询仓位
public function accountV3($params)
{
$url = "/fapi/v3/account";
$method = "GET";
return $this->request($method, $url, $params, $this->apiInfo);
}
//------------------------------------
... ...
... ... @@ -133,4 +133,20 @@ class ExBroker
{
return $this->api->setLever(['symbol' => $symbol, 'leverage' => $lever]);
}
public function getAllPos()
{
$newPositions = [];
$res = $this->api->accountV3([]);
if ($res && isset($res['positions'])) {
$positions = $res['positions'];
foreach ($positions as $key => $value) {
if ($value['positionAmt'] != 0) {
$newPositions[] = $value;
}
}
} else {
output($res);
}
return $newPositions;
}
}
... ...
... ... @@ -62,4 +62,18 @@ class Pos
{
return new Pos($wsDataPos->symbol, $wsDataPos->posSide, $wsDataPos->qty, $wsDataPos->lot, $wsDataPos->avgPrice, $wsDataPos->pnl, 0, 0);
}
public static function transferBinancePos($pos, $symbolInfos, callable $toSymbolSt)
{
$symbol = call_user_func($toSymbolSt, $pos['symbol']);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$symbol];
$posSide = strtoupper($pos['positionSide']);
$qty = abs($pos['positionAmt']);
$lot = round($qty / $symbolInfo->ctVal, $symbolInfo->qtyPrec);
$avgPrice = $pos['entryPrice'];
$pnl = $pos['unRealizedProfit'];
$lever = 0;
$margin = $pos['initialMargin'];
return new Pos($symbol, $posSide, $qty, $lot, $avgPrice, $pnl, $lever, $margin);
}
}
... ...