WsDataPos.php 1.7 KB
<?php

namespace trader\struct;

require_once __DIR__ . '/SymbolInfo.php';

use trader\struct\SymbolInfo;

class WsDataPos
{
    public string $symbol;
    public string $posSide; //大写
    public float $qty; //币种真实数量
    public float $lot; //合约张数
    public float $avgPrice; //平均持仓价
    public bool $isSnapshot; //是否快照;
    public float $pnl; //盈亏

    public function __construct($symbol, $posSide, $qty, $lot, $avgPrice, $isSnapshot, $pnl)
    {
        $this->symbol = $symbol;
        $this->posSide = $posSide;
        $this->qty = $qty;
        $this->lot = $lot;
        $this->avgPrice = $avgPrice;
        $this->isSnapshot = $isSnapshot;
        $this->pnl = $pnl;
    }

    public function toArray()
    {
        return [
            'symbol' => $this->symbol,
            'posSide' => $this->posSide,
            'qty' => $this->qty,
            'lot' => $this->lot,
            'avgPrice' => $this->avgPrice,
            'isSnapshot' => $this->isSnapshot,
            'pnl' => $this->pnl,
        ];
    }

    public static function TransferOkxPos(array $data, array $symbolInfos, callable $toSymbolSt): WsDataPos|null
    {
        $symbol = call_user_func($toSymbolSt, $data['instId']);
        /** @var SymbolInfo $symbolInfo */
        $symbolInfo = $symbolInfos[$symbol] ?? null;
        if ($symbolInfo === null) {
            return null;
        }
        $posSide = strtoupper($data['posSide']);
        $lot = (float)$data['pos'];
        $qty = round($lot * $symbolInfo->ctVal, $symbolInfo->qtyPrec);
        $avgPrice = (float)$data['avgPx'];
        $pnl = (float)$data['upl'];
        return new WsDataPos($symbol, $posSide, $qty, $lot, $avgPrice, false, $pnl);
    }
}