Write It Like You Mean It: Blazing-Fast Algorithmic APIs in Rust
Because Sometimes You Need a Knife, Not a Blender
The problem with most APIs? They’re built like salad bars: slow, bloated, and full of shit nobody touches. Everything modular, everything abstract, everything wrapped in something else like a birthday gift you didn’t ask for.
What we need is a street cart.
Flame, steel, two ingredients, and zero apologies.
So today, we're making something fast, clean, and actually useful — a Rust API that takes in a stream of prices and gives you a direction: Buy, Sell, or Hold.
No database. No “microservice” folder hell.
Just an endpoint, an algorithm, and the truth.
🧪 What We’re Building
A REST endpoint:
POST /analyze
You feed it a JSON payload like this:
{
"prices": [102.5, 103.0, 105.0, 100.0, 98.5]
}
And it answers — immediately — with something like:
{
"signal": "Sell"
}
Simple moving average crossover.
Short-term vs long-term.
Like a heartbeat, only colder.
🦀 Step 1: Fire Up the Rust
If you don’t have Rust set up, you’re either new or lying. But let’s go anyway:
cargo new algo-api --bin
cd algo-api
In Cargo.toml
, throw down the essentials:
[dependencies]
axum = "0.7"
tokio = { version = "1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
That's your prep. Now we cook.
🍳 Step 2: Slice the Endpoint
Inside main.rs
:
use axum::{
routing::post,
Json, Router,
};
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
#[derive(Deserialize)]
struct PriceInput {
prices: Vec<f64>,
}
#[derive(Serialize)]
struct SignalOutput {
signal: String,
}
fn analyze(prices: Vec<f64>) -> String {
if prices.len() < 10 {
return "Hold".to_string();
}
let short_ma = prices[prices.len()-3..].iter().sum::<f64>() / 3.0;
let long_ma = prices[prices.len()-10..].iter().sum::<f64>() / 10.0;
if short_ma > long_ma {
"Buy".to_string()
} else if short_ma < long_ma {
"Sell".to_string()
} else {
"Hold".to_string()
}
}
async fn analyze_handler(Json(payload): Json<PriceInput>) -> Json<SignalOutput> {
let signal = analyze(payload.prices);
Json(SignalOutput { signal })
}
#[tokio::main]
async fn main() {
let app = Router::new().route("/analyze", post(analyze_handler));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("🔥 Listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
Run it:
cargo run
Hit it with curl or Postman — it’s alive.
💬 Let’s Break It Down
1. Structs:
We define two struct
s using serde
— one to receive input (PriceInput
) and one to send output (SignalOutput
).
PriceInput
expects a JSON array of floats.SignalOutput
returns a single string: the result of our algorithm.
2. The Algorithm:
The analyze
function does what every real trader has done in a spreadsheet at 3am:
- It takes the last 3 prices, averages them for a short-term moving average.
- It takes the last 10 prices, averages them for a long-term moving average.
- If short > long: Buy. If short < long: Sell. Else: Hold.
Real-time strategy. No promises. Just signals and steel.
3. The Handler:
analyze_handler
is the Axum endpoint. It:
- Accepts a
POST
with JSON. - Deserializes the body into
PriceInput
. - Runs it through
analyze()
. - Sends back a JSON
SignalOutput
.
4. The Server:
Axum + Tokio makes it easy:
- We create a router with one route:
/analyze
. - Bind to localhost on port 3000.
- Serve the app asynchronously.
This could scale. This could run in production. This could sit on the edge and not even blink.
💡 Why It Works
- No nonsense.
No layers of middleware trying to justify their existence. - Typed like a surgeon.
Vec<f64>
in,String
out. Rust keeps your hands on the wheel. - Fast.
Rust compiles down to something that hums. This thing could live on the edge and never sweat. - Extensible.
Add another route for RSI. Batch scoring. An entire drop-generation engine. It's yours. The core is clean.
🧠 You Now Have a Weapon
This isn’t a tutorial. This is a blueprint for violence — the good kind.
The kind that clears the plate, silences the noise, and lets the signal speak.
Rust doesn’t ask you to be a genius. It asks you to be deliberate.
And when you’re building something that might move money — real money — that’s exactly the mindset you want.
So go ahead. Fork this. Deploy it. Extend it.
Put it behind a drop button, a webhook, or a trading bot.
Just write it like you f*cking mean it.
Code will be up on GitHub soon. Drop a comment if you want to see it extended into a full drop engine or WASM build.
Stay sharp.