NAME

App::cryp::arbit::Strategy::merge_order_book - Using merged order books for arbitration

VERSION

This document describes version 0.010 of App::cryp::arbit::Strategy::merge_order_book (from Perl distribution App-cryp-arbit), released on 2021-05-26.

SYNOPSIS

Using this strategy

In your cryp.conf:

[program=cryp-arbit arbit]
strategy=merge-order-book

or in your cryp-arbit.conf:

[arbit]
strategy=merge-order-book

This is actually the default strategy, so you don't have to explicitly set strategy to this strategy.

Configuration

In your cryp.conf:

[arbit-strategy/merge-order-book]
...

DESCRIPTION

This arbitration strategy uses information from merged order books. Below is the description of the algorithm. Suppose we are arbitraging the pair BTC/USD. E1, E2, ... En are exchanges. P* are prices. S* are sizes. i denotes exchange index.

First step: get order books from all of the involved exchanges, for example:

# buy orders on E1            # sell orders on E1
price  size                   price  size
-----  ----                   -----  ----
P1b1   S1b1                   P1s1   S1s1
P1b2   S1b2                   P1s2   S1s2
P1b3   S1b3                   P1s3   S1s3
...                           ...

# buy orders on E2            # sell orders on E2
price  size                   price  size
-----  ----                   -----  ----
P2b1   S2b1                   P2s1   S2s1
P2b2   S2b2                   P2s2   S2s2
P2b3   S2b3                   P2s3   S2s3
...                           ...

...

Note that buy orders are sorted from highest to lowest price (Pib1 > Pib2 > Pib3 > ...) while sell orders are sorted from lowest to highest price (Pis1 < Pis2 < Pis3 < ...). Also note that P1b* < P1s*, unless something weird is going on.

Second step: merge all the orders from exchanges into just two lists: buy and sell orders. Sort buy orders, as usual, from highest to lowest price. Sort sell orders, as usual, from lowest to highest. For example:

# buy orders                  # sell orders
price  size                   price  size
-----  ----                   -----  ----
P1b1   S1b1                   P2s1   S2s1
P2b1   S2b1                   P3s1   S3s1
P2b2   S2b2                   P3s2   S3s2
P1b2   S1b2                   P1s1   S1s1
...

Arbitrage can happen if we can buy cheap bitcoin and sell our expensive bitcoin. This means P1b1 must be above P2s1, because we want to buy bitcoins on E1 from trader that is willing to sell at P2s1 then sell it on E1 to the trader that is willing to buy the bitcoins at P2b1. Pocketing the difference (minus trading fees) as profit.

No actual bitcoins will be transferred from E2 to E1 as that would take a long time and incurs relatively high network fees. Instead, we maintain bitcoin and USD balances on each exchange to be able to buy/sell quickly. The balances serve as "working capital" or "inventory".

The minimum net profit margin is min_net_profit_margin. We create buy/sell order pairs starting from the topmost of the merged order book, until we can't get min_net_profit_margin anymore.

Then we monitor our order pairs and cancel them if they remain unfilled for a while.

Then we retrieve order books from the exchanges and start the process again.

Strengths

Order books contain information about prices and volumes at each price level. This serves as a guide on what size our orders should be, so we do not have to explicitly set order size. This is especially useful if we are not familiar with the typical volume of the pair on an exchange.

By sorting the buy and sell orders, we get maximum price difference.

Weaknesses

Order books are changing rapidly. By the time we get the order book from the exchange API, that information is already stale. In the course of milliseconds, the order book can change, sometimes significantly. So when we submit an order to buy X BTC at price P, it might not get fulfilled completely or at all because the market price has moved above P, for example.

CONFIGURATION

  • max_order_size_as_book_item_size_pct

    Number 0-100. Default is 100. This setting is used for more safety since order books are rapidly changing. For example, there is an item in the merged order book as follows:

    type  exchange   price  size     item#
    ----  --------   -----  ----     -----
    buy   exchange1  800.1  12       B1
    buy   exchange1  798.1  24       B2
    ...
    sell  exchange2  780.1   5       S1
    sell  exchange2  782.9   8       S2
    ...

    If `max_order_size_as_book_item_size_pct` is set to 100, then this will create order pairs as follows:

    size  buy from   buy price  sell to    sell price  item#
    ----  --------   ---------  -------    ----------  -----
    5     exchange2  780.1      exchange1  800.1       OP1
    7     exchange2  782.9      exchange1  800.1       OP2
    ...

    The OP1 will use up (100%) of item #S1 from the order book, then OP2 will use up (100%) item #B1 from the order book.

    However, if `max_order_size_as_book_item_size_pct` is set to 75, then this will create order pairs as follows:

    size  buy from   buy price  sell to    sell price  item#
    ----  --------   ---------  -------    ----------  -----
    3.75  exchange2  780.1      exchange1  800.1       OP1
    5.25  exchange2  782.9      exchange1  800.1       OP2

    OP1 will use 75% item S1 from the order book, then the strategy will move on to the next sell order (S2). OP2 will also use only 75% of item B1 (3.75 + 5.25 = 9, which is 75% of 12) before moving on to the next buy order.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/App-cryp-arbit.

SOURCE

Source repository is at https://github.com/perlancar/perl-App-cryp-arbit.

BUGS

Please report any bugs or feature requests on the bugtracker website https://github.com/perlancar/perl-App-cryp-arbit/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

SEE ALSO

App::cryp::arbit

Other App::cryp::arbit::Strategy::* modules.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2021, 2018 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.