WsDataTrade.php 6.1 KB
<?php

namespace trader\struct;

require_once __DIR__ . '/SymbolInfo.php';

use trader\struct\SymbolInfo;

class WsDataTrade
{
    public string $platform;
    public string $posSide; //大写
    public string $symbol; //大写
    public string $side; //大写
    public float $price; //成交价格
    public float $qty; //币种真实数量
    public float $lot; //合约张数
    public float $pnl; //盈亏
    public float $fee; //手续费
    public float $quoteVol; //计价货币数量 成交额
    public int $ts; //时间戳 ms
    public string $tradeId;
    public string $ordId;
    public string $cliOrdId;
    public int $lever;

    public function __construct($platform, $posSide, $symbol, $side, $price, $qty, $lot, $pnl, $fee, $quoteVol, $ts, $tradeId, $ordId, $cliOrdId, $lever)
    {
        $this->platform = $platform;
        $this->posSide = $posSide;
        $this->symbol = $symbol;
        $this->side = $side;
        $this->price = $price;
        $this->qty = $qty;
        $this->lot = $lot;
        $this->pnl = $pnl;
        $this->fee = $fee;
        $this->quoteVol = $quoteVol;
        $this->ts = $ts;
        $this->tradeId = $tradeId;
        $this->ordId = $ordId;
        $this->cliOrdId = $cliOrdId;
        $this->lever = $lever;
    }

    public function toArray()
    {
        return [
            'platform' => $this->platform,
            'posSide' => $this->posSide,
            'symbol' => $this->symbol,
            'side' => $this->side,
            'price' => $this->price,
            'qty' => $this->qty,
            'lot' => $this->lot,
            'pnl' => $this->pnl,
            'fee' => $this->fee,
            'quoteVol' => $this->quoteVol,
            'ts' => $this->ts,
            'tradeId' => $this->tradeId,
            'ordId' => $this->ordId,
            'cliOrdId' => $this->cliOrdId,
            'lever' => $this->lever,
        ];
    }

    public static function TransferOkxOrder(array $data, array $symbolInfos, callable $toSymbolSt): WsDataTrade|null
    {
        if ($data['fillSz'] != "" && $data['fillSz'] != "0") {
            $symbol = call_user_func($toSymbolSt, $data['instId']);
            /** @var SymbolInfo $symbolInfo */
            $symbolInfo = $symbolInfos[$symbol] ?? null;
            if ($symbolInfo == null) {
                return null;
            }
            $platform = 'okx';
            $posSide = strtoupper($data['posSide']);
            $side = strtoupper($data['side']);
            $price = (float)$data['fillPx'];
            $lot = (float)$data['fillSz'];
            $qty = round($lot * $symbolInfo->ctVal, $symbolInfo->qtyPrec);
            $pnl = (float)$data['fillPnl'];
            $fee = (float)$data['fillFee'];
            $quoteVol = $price * $qty;
            $ts = $data['uTime'];
            $tradeId = $data['tradeId'];
            $lever = (int)$data['lever'];
            $ordId = $data['ordId'];
            $cliOrdId = $data['clOrdId'];
            return new WsDataTrade($platform, $posSide, $symbol, $side, $price, $qty, $lot, $pnl, $fee, $quoteVol, $ts, $tradeId, $ordId, $cliOrdId, $lever);
        }
        return null;
    }
    public static function TransferBinanceTrade($data, $symbolInfos, callable $toSymbolSt): WsDataTrade|null
    {
        $platform = 'binance';
        $order = $data['o'];
        $symbol = call_user_func($toSymbolSt, $order['s']);
        $posSide = strtoupper($order['ps']);
        $side = strtoupper($order['S']);
        $price = (float)$order['L'];
        $qty = (float)$order['l'];
        $lot = $qty;
        $pnl = (float)$order['rp'];
        $fee = (float)$order['n'];
        if ($order['N'] == "BNB") {
            $fee = $fee * 600;
        }
        $quoteVol = $price * $qty;
        $ts = $order['T'];
        $tradeId = $order['t'];
        $ordId = $order['i'];
        $cliOrdId = $order['c'];
        $lever = 0;
        return new WsDataTrade($platform, $posSide, $symbol, $side, $price, $qty, $lot, $pnl, $fee, $quoteVol, $ts, $tradeId, $ordId, $cliOrdId, $lever);
    }
    public static function TransferBybitTrade($data, $symbolInfos, callable $toSymbolSt): WsDataTrade|null
    {
        $platform = 'bybit';
        $symbol = call_user_func($toSymbolSt, $data['symbol']);
        $posSide = "";
        $side = strtoupper($data['side']);
        $price = (float)$data['execPrice'];
        $qty = (float)$data['execQty'];
        $lot = $qty;
        $pnl = (float)$data['execPnl'];
        if (($pnl == 0 && $side == "BUY") || ($pnl != 0 && $side == "SELL")) {
            $posSide = "LONG";
        }
        if (($pnl == 0 && $side == "SELL") || ($pnl != 0 && $side == "BUY")) {
            $posSide = "SHORT";
        }
        $fee = (float)$data['execFee'];
        $quoteVol = $price * $qty;
        $ts = $data['execTime'];
        $tradeId = $data['execId'];
        $ordId = $data['orderId'];
        $cliOrdId = $data['orderLinkId'];
        $lever = 0;
        return new WsDataTrade($platform, $posSide, $symbol, $side, $price, $qty, $lot, $pnl, $fee, $quoteVol, $ts, $tradeId, $ordId, $cliOrdId, $lever);
    }
    public static function TransferBitgetOrder(array $data, array $symbolInfos, callable $toSymbolSt): WsDataTrade|null
    {
        $symbol = call_user_func($toSymbolSt, $data['symbol']);
        /** @var SymbolInfo $symbolInfo */
        $symbolInfo = $symbolInfos[$symbol] ?? null;
        if ($symbolInfo === null) {
            return null;
        }
        $plat = 'bitget';
        $side = strtoupper($data['side']);
        $price = (float)$data['price'];
        $qty = (float)$data['baseVolume'];
        $lot = $qty;
        $fee = (float)$data['feeDetail'][0]['totalFee'];
        $pnl = (float)$data['profit'];
        if (($pnl == 0 && $side == "BUY") || ($pnl != 0 && $side == "SELL")) {
            $posSide = "LONG";
        } else {
            $posSide = "SHORT";
        }
        $quoteVol = (float)$data['quoteVolume'];
        $ts = $data['uTime'];
        $tradeId = $data['tradeId'];
        $ordId = $data['orderId'];
        $cliOrdId = $data['clientOid'];
        $lever = 0;
        return new WsDataTrade($plat, $posSide, $symbol, $side, $price, $qty, $lot, $pnl, $fee, $quoteVol, $ts, $tradeId, $ordId, $cliOrdId, $lever);
    }
}