Crypto data analysis on an ESP32

#projects#finance

So I finally pulled the trigger on the ESP32. Been wanting to buy this for a long time to do all kinds of stuff with a display attached to it, but for now I just have the board, and I tried building something to understand what its capable of.

I wanted to sorta build something in the space that I currently have my eyes on - quantitative finance. Now granted I don’t know jack shit about how any of that works, but I have been practicing writing C++ code and understanding how stuff works inside a computer when your code runs.

Lucky for me, the ESP32 runs on C++, so it was kinda a no brainer to build something quant-y?

What did I build?

I ended up building a script that pulls live trade data from a Binance websocket for BTC/USDT, and then calculates 3 things: VWAP, or the volume weight adjusted price - basically the average price of the stock, normalised with the volume or quantity of each transaction, simple formula

VWAP = (price * quantity) / quantity

Trade flow imbalance - a way to show which side the market is leaning, more buying or more selling

IMB = (buyVolume - sellVolume) / (buyVolume + sellVolume)

Volatility - self explanatory, how much the index (or the coin in this case) deviates over a certain time period. This is the interesting bit. So I used three formulas here

return = log(currentPrice / previousPrice)
EWMA = (λ * oldVariance) + (1 - λ) * return^2
volatility = EWMA^2

With the lambda set to 0.99 (basically keeping 99% of its old value and mixing in 1% of the new value we calculated).

Now since we have the EWMA, we don’t need to store the historical values of the returns, reducing our space complexity.

Now once it does this, it hosts this data on a small little dashboard (yeah I used AI for this shit cuz which idiot writes vanilla JS anymore), and updates it 4 times a second so you have like a live view of whats happening with the volatility.

What did I learn?

A lot.

  • The hardest thing was to not Claude my way through everything and let it run on autopilot. I’ve been practicing how to write C++ for a bit, so I was kinda familiar with the syntax, writing my own header files and fixing small errors myself.
  • I learnt what a WebSocket is, I know I’m really late to the party but I did get a hold of why it’s different from an API.
  • How big the gap is between what I built and what the big trading firms do. Latency for me was milliseconds, which by their terms is years. HFTs rely on programs with nano, or microsecond latency, and it kinda throws light on the amount of engineering going on there. I’ll get there soon enough.

Photos

The Dashboard

The Dashboard

The ESP32

Each blink of the blue light shows an incoming order on the Binance websocket

The ESP32

What’s Next?

I have built a limit order book in SpringBoot and I kinda wanna use this incoming market data to make my own order book and match trades as they enter the queue. Sounds really simple but I want to explore how much you can push this thing. Maybe not stop at just one index, maybe span it over more, see how I can reduce latency, space complexity and more. Maybe even do some more calculations on this thing.

Until next time.