Skip to content

Ladder Algorithm Explained

adamant-al edited this page Jun 19, 2023 · 13 revisions

Optimal ladder/grid trade strategy: when a bot places many orders to buy and sell tokens with prices starting from the spread. When the closest to spread order is filled, a bot adds the same order to the opposite side, following the rule "buy lower than you sell, and sell higher than you buy". It works best in a volatile market.

Abbreviations

  • hb — highest bid
  • la — lowest ask
  • COIN1/COIN2 — a bot’s trade pair
  • ld — ladder module order type

Ladder orders

Ladder orders are intended to realize a trading grid strategy, where the bot places many orders to buy and sell tokens with prices starting from the spread:

Ladder Explained Picture 1

When the closest to spread ladder order is filled, a bot adds the same order to the opposite side. For example, if someone takes the 100 COIN1 @0.105 COIN2 ladder sell-order:

Ladder Explained Picture 2

Purpose

Ladder orders:

  • Gives a profit, as a bot sells higher than it buys or buys lower than it sells
  • Makes the liquidity higher and order books richer

Work algorithm

See the Ladder algorithm code.

Example

Current order book:

Ladder Explained Picture 3

Command: /start ld 100 USDT 6 1%

Bot steps:

  • Calculate middle price: (0.100 + 0.090) /2 = 0.095
  • Place the first bid as 0.095 – 1% = 0.094, amount ~99 USDT
  • Place the second bid as 0.094 – 1% = 0.093, amount ~100 USDT
  • Place the third bid as 0.093 – 1% = 0.092, amount ~101 USDT
  • Place the 4th bid as 0.092 – 1% = 0.091, amount ~101 USDT
  • Place the 5th bid as 0.091 – 1% = 0.090, amount ~100 USDT
  • Place the 6th bid as 0.090 – 1% = 0.089, amount ~100 USDT
  • Place the first ask as 0.095 + 1% = 0.096, amount ~99 USDT
  • Place the second ask as 0.096 + 1% = 0.097, amount ~100 USDT
  • Place the third ask as 0.097 + 1% = 0.098, amount ~101 USDT
  • Place the 4th ask as 0.098 + 1% = 0.099, amount ~101 USDT
  • Place the 5th ask as 0.099 + 1% = 0.100, amount ~101 USDT
  • Place the 6th ask as 0.100 + 1% = 0.101, amount ~101 USDT

Result order book:

Ladder Explained Picture 4

Comments

  • ld-orders are stored in local DB with their states:
    • Not placed, with a reason:
      • Unable to calculate amount
      • Not enough balances
      • No order id returned
    • Open
    • Filled
    • Partly filled
    • Cancelled
    • Missed
    • To be removed
    • Removed
  • A bot constantly checks if all of ld-orders open
    • If not, it restores orders in Cancelled and Not placed states
    • If there is not enough balance to place ld-order, a bot notifies, sets it in Not placed state, and tries to place it again on the next check
  • A bot replaces fully filled orders according to ladder/grid strategy
  • /clear all and /clear ld commands close all open ld-orders
    • Set them in Cancelled state
    • If the ladder module is active, it will restore ld-orders on the next check
  • /stop ld will disable the module and reset all ladder orders' states and history
    • It will not close opened ld-order. To close them, an admin can run /clear all or /clear ld additionally
    • Next, /start ld will create a new grid of ld-orders, not connected to the previous one

Enable & disable ladder module

Command to enable ladder module has parameters:

/start ld {AMOUNT} {COIN} {COUNT} {STEP%} [mid {MIDPRICE} COIN2]

  • AMOUNT — order value in COIN. AMOUNT will be with a random factor of 2%. It’s a float number with precision depending on an exchange. AMOUNT can be in COIN1 or COIN2.
  • COUNT — number of orders each side, buy and sell. Total ld-orders count is COUNT*2.
  • STEP% — how much consequent orders will differ by price
  • [mid {MIDPRICE} COIN2] — optional middle price setting. If not set, midprice will be (hb + la) / 2.

The command asks for confirmation before running.

Examples

To start the Ladder/ Grid strategy with 30 ~100 USDT orders on each side with a 3% step, starting from the current middle token price:

/start ld 100 USDT 30 1%

To disable the ladder module:

/stop ld