gramseyer / speedex-standalone

Implementing SPEEDEX price computation engine in Golang as a standalone binary that exchanges can call

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

speedex-standalone

Implementing SPEEDEX price computation engine in Golang as a standalone binary that exchanges can call.

Notes from Geoff About Tatonnement:

SCS Lab Implementation: https://github.com/scslab/speedex/blob/master/price_computation/

Stellar Implementation: https://github.com/gramseyer/stellar-core/blob/master/src/speedex/TatonnementOracle.cpp

Simple Simplex LP Solver: https://github.com/gramseyer/stellar-core/tree/master/src/simplex

Tatonnement

  1. Collect DB of transactions for a given block 1a) Create one vector for each group <sell A, buy B> (which is a different vector than <sell B, buy A>) as transactions come in 1b) As we are building these vectors, keep each one in sorted order from minimum price. 1c) Also keep track of cumulative amount up to that price.
  2. Start with a price (ideal is integer - maybe 64 bit or less for edge cases and overflow and speed), but MVP can use floating point
  3. Go through these vectors using binary search O(log n) to find how much would be bought/sold for each asset.
  4. Update prices. 4a) New price = Old price * (1 + Old price * asset quantity * (demand - supply) * STEP_SIZE) 4b) Why weight by old price * asset quantity? It's to make sure when things are denominated differently they still take similar size steps. IE 1 dollar vs 100 pennies. 4c) How to set STEP_SIZE? 4ci) Pick a small constant 4cii) Run multiple Tatonnement in parallel with different constants 4ciii) Use a heurtistic (such as L2 Norm or Infinity Norm) of Net Demand Vector (demand - supply) for Old price and New price and see if the norm decreases. If it increases then use a smaller STEP_SIZE.
  5. Stopping criteria 5a) Fixed number of iterations 5b) Net Demand Norm less than some fixed amount.
  6. LP Solver

Notes:

  1. When collecting transactions from multiple threads, create this sorted DB (sell A, buy B, price, cumulative, metadata) per thread. Then do one MergeSort to combine them all together to get the entire transaction set.
  2. Don't want demand to be too cliff-y, so smooth it out using a parameter alpha. 2a) Example: alpha = 0.01. Then if limit price is 99% of current price then sell in full. If limit price is 99.5% of current price sell half of it. If limit price equals current price then sell none. 2b) Will need to have 2 cumulative values in vector: cumulative amount and cumulative amoount * limit price offer

To Do (Small Items):

  1. Change appropriate methods/variables from public to private and add getters/setters as needed.
  2. Check to make sure functions are taking pointers instead of underlying to save on memory
  3. Add in smoothing for demand curve using alpha
  4. Layout project as https://github.com/golang-standards/project-layout with appropriate folders, etc.
  5. Overflow/underflow (floats/ints?) - package: https://pkg.go.dev/github.com/shopspring/decimal
  6. Test cases
  7. Change step size to be a heuristic rather than a fixed number.
  8. Add Stopping criteria
  9. Add parallelism/multithreading for collecting transactions. Locks/mutexes accordingly. Then Mergesort them together.

To Do (Large Items):

  1. LP Solver after Tatonnement
  2. Create charts to get metrics on throughput with different trials

About

Implementing SPEEDEX price computation engine in Golang as a standalone binary that exchanges can call

License:MIT License


Languages

Language:Go 100.0%