【问题标题】:Determine Taker Price from Bid/Ask Order Book从买/卖订单簿中确定 Taker 价格
【发布时间】:2018-02-14 06:59:15
【问题描述】:

我正在使用 PHP 编写比特币交易脚本。要在实时数据上进行纸质交易,我必须从订单簿中确定买入/卖出价格,即接受者价格。

订单簿实时 json 数据看起来像 this

订单簿有两个主要数组 - 出价和要价。每个买/卖数组都有价格[0],数量[1],第三个参数[2]无关:

出价/要价样本数组

[0] => Array
    (
        [0] => 8848.99
        [1] => 9.89850469
        [2] => 7
    )

[1] => Array
    (
        [0] => 8848.2
        [1] => 0.05
        [2] => 1
    )

[2] => Array
    (
        [0] => 8848.02
        [1] => 0.274203
        [2] => 1
    )

[3] => Array
    (
        [0] => 8848.01
        [1] => 0.0012
        [2] => 1
    )

[4] => Array
    (
        [0] => 8847.47
        [1] => 0.5
        [2] => 1
    )

[5] => Array
    (
        [0] => 8846.99
        [1] => 0.28345
        [2] => 1
    )

[6] => Array
    (
        [0] => 8846.81
        [1] => 0.75
        [2] => 1
    )

[7] => Array
    (
        [0] => 8846
        [1] => 0.75181214
        [2] => 2
    )

[8] => Array
    (
        [0] => 8845.99
        [1] => 26.57694043
        [2] => 28
    )

根据以上数据,PHP 中如何计算 15 或 n 个硬币的平均价格?考虑到订单将在可用时从上到下执行/执行。

【问题讨论】:

  • 你能证明自己解决这个问题的努力吗?
  • price 元素是代表单个币的价格,还是该订单的总金额?
  • 这是每枚硬币的价格,数量是该价格的可用性。

标签: php math average


【解决方案1】:

我用这个 Excel 公式解决了这个问题: Avg. Cost = sumproduct(price series, qty series) / sum(qty series)

这是 PHP 代码:

$order_book = json_decode($order_book, true);
$bids = ($order_book['bids']);

$ordered = 15; //n number of coins
$filled = 0;
$fill_array = array();

foreach ($bids as $PriceQty) {

    $price = $PriceQty[0];
    $qty = $PriceQty[1];

    if ($ordered != $filled){

        $required = $ordered - $filled;

        if ($qty >= $required){
            $fill_array[] = array($price,$required);
            $filled = $filled + $required;
            break;
        }else{
            $filled = $filled + $qty;
            $fill_array[] = array($price,$qty);
        }   
    }   
}   

$totalQty = 0;
$totalSum = 0;

foreach ($fill_array as $PriceQty) {
    $totalSum = $totalSum + ($PriceQty[0] * $PriceQty[1]);
    $totalQty = $totalQty + $PriceQty[1];
}   

echo "$totalSum/$totalQty = ".($totalSum/$totalQty);

可能有更简单的方法,有人可以改进这个答案。目前它产生预期的平均值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 2022-10-08
    • 2021-09-17
    • 1970-01-01
    相关资源
    最近更新 更多