Order.php 6.1 KB
<?php

namespace trader\struct;

class Order
{
    public string $symbol; //大写
    public string $ordType; // 订单类型 LIMIT 限价 IOC 不成则撤 FOK 全部成交或立即取消 MARKET 市价 ONLYMAKER 仅做maker单
    public string $posSide; //大写
    public string $side; //大写
    public float $price; //成交价格
    public float $qty; //币种真实数量
    private string $cliOrdId = ''; // 客户端订单号


    public function __construct($symbol, $ordType, $posSide, $side, $price, $qty)
    {
        $this->symbol = $symbol;
        $this->ordType = $ordType;
        $this->posSide = $posSide;
        $this->side = $side;
        $this->price = $price;
        $this->qty = $qty;
    }
    public function setCliOrdId($cliOrdId)
    {
        $this->cliOrdId = $cliOrdId;
    }
    public function getCliOrdId()
    {
        return $this->cliOrdId;
    }

    public function toArray()
    {
        $arr = [
            'symbol' => $this->symbol,
            'ordType' => $this->ordType,
            'posSide' => $this->posSide,
            'side' => $this->side,
            'price' => $this->price,
            'qty' => $this->qty,
        ];
        if ($this->cliOrdId != "") {
            $arr['cliOrdId'] = $this->cliOrdId;
        }
        return $arr;
    }

    public function toOkxOrder(array $symbolInfos, callable $toSymbolOri)
    {
        $instId = call_user_func($toSymbolOri, $this->symbol);
        /** @var SymbolInfo $symbolInfo */
        $symbolInfo = $symbolInfos[$this->symbol];
        $order = [
            'instId' => $instId,
            'tdMode' => 'cross',
            'posSide' => strtolower($this->posSide),
            'side' => strtolower($this->side),
            'ordType' => $this->ordType,
        ];
        if ($this->ordType == 'LIMIT' || $this->ordType == 'IOC') {
            $order['px'] = round($this->price, $symbolInfo->pricePrec);
        }
        $lot = $this->qty / $symbolInfo->ctVal;
        $order['sz'] = round($lot, $symbolInfo->lotPrec);
        if ($this->ordType == 'ONLYMAKER') {
            $order['ordType'] = 'POST_ONLY';
        }
        $order['ordType'] = strtolower($order['ordType']);
        if ($this->cliOrdId != "") {
            $order['clOrdId'] = $this->cliOrdId;
        }
        return $order;
    }
    public function toBinanceOrder(array $symbolInfos, callable $toSymbolOri)
    {
        $symbol = call_user_func($toSymbolOri, $this->symbol);
        /** @var SymbolInfo $symbolInfo */
        $symbolInfo = $symbolInfos[$this->symbol];
        $ordType = $this->ordType;
        $timeInForce = "GTC";
        if ($this->ordType == "IOC" || $this->ordType == "FOK") {
            $ordType = "LIMIT";
            $timeInForce = $this->ordType;
        }
        if ($this->ordType == "ONLYMAKER") {
            $ordType = "LIMIT";
            $timeInForce = "GTX";
        }
        $order = [
            'symbol' => $symbol,
            'positionSide' => $this->posSide,
            'side' => $this->side,
            'type' => $ordType,
            'timeInForce' => $timeInForce,
            'quantity' => round($this->qty, $symbolInfo->qtyPrec),
            'price' => round($this->price, $symbolInfo->pricePrec),
        ];
        if ($ordType == "MARKET") {
            unset($order['price']);
            unset($order['timeInForce']);
        }
        if ($this->cliOrdId != "") {
            $order['newClientOrderId'] = $this->cliOrdId;
        }
        return $order;
    }
    public function toBybitOrder(array $symbolInfos, callable $toSymbolOri)
    {
        $symbol = call_user_func($toSymbolOri, $this->symbol);
        /** @var SymbolInfo $symbolInfo */
        $symbolInfo = $symbolInfos[$this->symbol];
        $order = [
            'category' => 'linear',
            'symbol' => $symbol,
            'qty' => (string)round($this->qty, $symbolInfo->qtyPrec),
            'price' => (string)round($this->price, $symbolInfo->pricePrec),
            'orderLinkId' => $this->cliOrdId,
        ];
        if ($this->ordType == "MARKET") {
            $order['orderType'] = "Market";
            unset($order['price']);
        }
        if ($this->ordType == "LIMIT") {
            $order['timeInForce'] = "GTC";
            $order['orderType'] = "Limit";
        }
        if ($this->ordType == "IOC") {
            $order['timeInForce'] = "IOC";
            $order['orderType'] = "Limit";
        }
        if ($this->side == "BUY") {
            $order['side'] = "Buy";
        } else {
            $order['side'] = "Sell";
        }
        if ($this->posSide == "LONG") {
            $order['positionIdx'] = 1;
        } else if ($this->posSide == "SHORT") {
            $order['positionIdx'] = 2;
        } else {
            $order['positionIdx'] = 0;
        }
        return $order;
    }
    public function toBitgetOrder(array $symbolInfos, callable $toSymbolOri)
    {
        $symbol = call_user_func($toSymbolOri, $this->symbol);
        /** @var SymbolInfo $symbolInfo */
        $symbolInfo = $symbolInfos[$this->symbol];
        $tradeSide = "open";
        $side = $this->side;
        if ($this->posSide == "SHORT" && $this->side == "BUY") {
            $tradeSide = "close";
            $side = "SELL";
        }
        if ($this->posSide == "LONG" && $this->side == "SELL") {
            $tradeSide = "close";
            $side = "BUY";
        }
        $timeInForce = "GTC";
        if ($this->ordType == "IOC") {
            $timeInForce = "IOC";
            $ordType = "LIMIT";
        }
        if ($this->ordType == "FOK") {
            $timeInForce = "FOK";
            $ordType = "LIMIT";
        }
        $order = [
            'productType' => 'USDT-FUTURES',
            'marginMode' => 'crossed',
            'marginCoin' => 'USDT',
            'symbol' => $symbol,
            'side' => strtolower($side),
            'orderType' => strtolower($this->ordType),
            'price' => (string)round($this->price, $symbolInfo->pricePrec),
            'size' => (string)round($this->qty, $symbolInfo->qtyPrec),
            'tradeSide' => $tradeSide,
            'force' => $timeInForce,
            'clientOid' => $this->cliOrdId,
        ];
        return $order;
    }
}