SymbolInfo.php 4.7 KB
<?php

namespace trader\struct;

require_once __DIR__ . '/../../jiaoyin/func.php';

use function Jiaoyin\getPrecision;

class SymbolInfo
{
    public string $symbolOri; // 原始交易对
    public string $symbol; // 标准交易对
    public string $ctVal; // 合约面值
    public string $pricePrec;
    public string $qtyPrec;
    public string $lotPrec;
    public string $minLot; // 最小下单张数
    public string $minQty; // 最小下单数量
    public int $mul; // 合约乘数//放大倍数,比如币安 1000CATUSDT,乘数为1000

    public function __construct($symbolOri, $symbol, $ctVal, $pricePrec, $qtyPrec, $lotPrec, $minLot, $minQty, $mul = 1)
    {
        $this->symbolOri = $symbolOri;
        $this->symbol = $symbol;
        $this->ctVal = $ctVal;
        $this->pricePrec = $pricePrec;
        $this->qtyPrec = $qtyPrec;
        $this->lotPrec = $lotPrec;
        $this->minLot = $minLot;
        $this->minQty = $minQty;
        $this->mul = $mul;
    }
    public function toArray()
    {
        return [
            'symbolOri' => $this->symbolOri,
            'symbol' => $this->symbol,
            'ctVal' => $this->ctVal,
            'pricePrec' => $this->pricePrec,
            'qtyPrec' => $this->qtyPrec,
            'lotPrec' => $this->lotPrec,
            'minLot' => $this->minLot,
            'minQty' => $this->minQty,
            'mul' => $this->mul,
        ];
    }
    public static function transferOkx($data, callable $getSymbolSt): SymbolInfo
    {
        $symbolSt = $getSymbolSt($data["instId"]); //转换为标准交易对
        $pricePrec = getPrecision($data['tickSz']);
        $ctVal = $data['ctVal'];
        $minLot = $data['minSz'];
        $minQty = $minLot * $ctVal;
        $qtyPrec = getPrecision($minQty);
        $lotPrec = getPrecision($minLot);
        $info = new SymbolInfo($data["instId"], $symbolSt, $ctVal, $pricePrec, $qtyPrec, $lotPrec, $minLot, $minQty);
        return $info;
    }
    public static function transferBinance($data, callable $getSymbolSt): SymbolInfo
    {
        $symbolOri = $data['symbol'];
        $symbolSt = $getSymbolSt($symbolOri); //转换为标准交易对
        $ctVal = 1;
        $pricePrec = 0;
        $qtyPrec = 0;
        $lotPrec = 0;
        $minLot = 0;
        foreach ($data['filters'] as $v) {
            if ($v['filterType'] == 'PRICE_FILTER') {
                $pricePrec = getPrecision($v['tickSize']);
            }
            if ($v['filterType'] == 'LOT_SIZE') {
                $minLot = $v['minQty'];
                $minQty = $minLot * $ctVal;
                $qtyPrec = getPrecision($minLot);
                $lotPrec = getPrecision($minLot);
            }
        }
        $mul = self::extractNumber($symbolOri);
        $info = new SymbolInfo($symbolOri, $symbolSt, $ctVal, $pricePrec, $qtyPrec, $lotPrec, $minLot, $minQty, $mul);
        return $info;
    }
    public static function transferBybit($data, callable $getSymbolSt): SymbolInfo
    {
        $symbolOri = $data['symbol'];
        $symbolSt = $getSymbolSt($symbolOri); //转换为标准交易对
        $ctVal = 1;
        $pricePrec = getPrecision($data['priceFilter']['tickSize']);
        $qtyPrec = getPrecision($data['lotSizeFilter']['qtyStep']);
        $lotPrec = $qtyPrec;
        $minQty = $data['lotSizeFilter']['minOrderQty'];
        $minLot = $minQty * $ctVal;
        $mul = self::extractNumber($symbolOri);
        $info = new SymbolInfo($symbolOri, $symbolSt, $ctVal, $pricePrec, $qtyPrec, $lotPrec, $minLot, $minQty, $mul);
        return $info;
    }
    public static function transferBitget($data, callable $getSymbolSt): SymbolInfo
    {
        $symbolOri = $data['symbol'];
        $symbolSt = $getSymbolSt($symbolOri); //转换为标准交易对
        $ctVal = 1; // Bitget合约面值为1
        $pricePrec = (int)$data['pricePlace'];
        $qtyPrec = (int)$data['volumePlace'];
        $lotPrec = $qtyPrec;
        $minQty = $data['minTradeNum'];
        $minLot = $minQty * $ctVal;
        $mul = self::extractNumber($symbolOri);

        return new SymbolInfo(
            $symbolOri,
            $symbolSt,
            $ctVal,
            $pricePrec,
            $qtyPrec,
            $lotPrec,
            $minLot,
            $minQty,
            $mul
        );
    }
    public static function extractNumber($str)
    {
        // 示例
        // echo extractNumber("1000CATUSDT") . PHP_EOL;     // 输出:1000
        // echo extractNumber("1000PEPEUSDT") . PHP_EOL;    // 输出:1000
        // echo extractNumber("1000000MOGUSDT") . PHP_EOL;  // 输出:1000000
        if (preg_match('/^\d+/', $str, $matches)) {
            return (int)$matches[0]; // 返回整数
        }
        return 1;
    }
}