作者 karlet

feat:bn增加仓位初始化

@@ -349,10 +349,18 @@ class CmBroker @@ -349,10 +349,18 @@ class CmBroker
349 $positions = $this->exBroker->getAllPos(); 349 $positions = $this->exBroker->getAllPos();
350 $newPositions = []; 350 $newPositions = [];
351 foreach ($positions as $key => $value) { 351 foreach ($positions as $key => $value) {
352 - $item = Pos::transferOkxPos($value, $this->symbolInfos, function ($symbol) {  
353 - return $this->getSymbolSt($symbol);  
354 - });  
355 - $newPositions[$item->symbol . "_" . $item->posSide] = $item; 352 + if ($this->plat == self::PLAT_OKX) {
  353 + $item = Pos::transferOkxPos($value, $this->symbolInfos, function ($symbol) {
  354 + return $this->getSymbolSt($symbol);
  355 + });
  356 + $newPositions[$item->symbol . "_" . $item->posSide] = $item;
  357 + }
  358 + if ($this->plat == self::PLAT_BINANCE) {
  359 + $item = Pos::transferBinancePos($value, $this->symbolInfos, function ($symbol) {
  360 + return $this->getSymbolSt($symbol);
  361 + });
  362 + $newPositions[$item->symbol . "_" . $item->posSide] = $item;
  363 + }
356 } 364 }
357 return $newPositions; 365 return $newPositions;
358 } 366 }
@@ -395,4 +403,8 @@ class CmBroker @@ -395,4 +403,8 @@ class CmBroker
395 } 403 }
396 return []; 404 return [];
397 } 405 }
  406 + public function initPositions()
  407 + {
  408 + $this->positions = $this->getAllPos();
  409 + }
398 } 410 }
@@ -74,6 +74,13 @@ class Api @@ -74,6 +74,13 @@ class Api
74 $method = "POST"; 74 $method = "POST";
75 return $this->request($method, $url, $params, $this->apiInfo); 75 return $this->request($method, $url, $params, $this->apiInfo);
76 } 76 }
  77 + //查询仓位
  78 + public function accountV3($params)
  79 + {
  80 + $url = "/fapi/v3/account";
  81 + $method = "GET";
  82 + return $this->request($method, $url, $params, $this->apiInfo);
  83 + }
77 84
78 //------------------------------------ 85 //------------------------------------
79 86
@@ -133,4 +133,20 @@ class ExBroker @@ -133,4 +133,20 @@ class ExBroker
133 { 133 {
134 return $this->api->setLever(['symbol' => $symbol, 'leverage' => $lever]); 134 return $this->api->setLever(['symbol' => $symbol, 'leverage' => $lever]);
135 } 135 }
  136 + public function getAllPos()
  137 + {
  138 + $newPositions = [];
  139 + $res = $this->api->accountV3([]);
  140 + if ($res && isset($res['positions'])) {
  141 + $positions = $res['positions'];
  142 + foreach ($positions as $key => $value) {
  143 + if ($value['positionAmt'] != 0) {
  144 + $newPositions[] = $value;
  145 + }
  146 + }
  147 + } else {
  148 + output($res);
  149 + }
  150 + return $newPositions;
  151 + }
136 } 152 }
@@ -62,4 +62,18 @@ class Pos @@ -62,4 +62,18 @@ class Pos
62 { 62 {
63 return new Pos($wsDataPos->symbol, $wsDataPos->posSide, $wsDataPos->qty, $wsDataPos->lot, $wsDataPos->avgPrice, $wsDataPos->pnl, 0, 0); 63 return new Pos($wsDataPos->symbol, $wsDataPos->posSide, $wsDataPos->qty, $wsDataPos->lot, $wsDataPos->avgPrice, $wsDataPos->pnl, 0, 0);
64 } 64 }
  65 + public static function transferBinancePos($pos, $symbolInfos, callable $toSymbolSt)
  66 + {
  67 + $symbol = call_user_func($toSymbolSt, $pos['symbol']);
  68 + /** @var SymbolInfo $symbolInfo */
  69 + $symbolInfo = $symbolInfos[$symbol];
  70 + $posSide = strtoupper($pos['positionSide']);
  71 + $qty = abs($pos['positionAmt']);
  72 + $lot = round($qty / $symbolInfo->ctVal, $symbolInfo->qtyPrec);
  73 + $avgPrice = $pos['entryPrice'];
  74 + $pnl = $pos['unRealizedProfit'];
  75 + $lever = 0;
  76 + $margin = $pos['initialMargin'];
  77 + return new Pos($symbol, $posSide, $qty, $lot, $avgPrice, $pnl, $lever, $margin);
  78 + }
65 } 79 }