First day of vacation and I'm somehow at a Krispy Kreme -in Tokyo-

Learning By Building: Trading Platforms

Jun 30, 2026

I'm on a plane, on very few hours of sleep, as I write this. This trip is already turning out to be a doozy in terms of unexpected chaos and stress. Vacation!2 In a world where code itself is cheap but deep knowledge is increasingly difficult to obtain, my current approach to learning more is to use building to be my way of learning about systems. Essentially, I learn the most about how things work when I work and bump into all the rough edge cases that exist with any piece of software, so what better way to learn. This week is I'm talking about building a trading bot, because what kid doesn't grow up surrounded by the NYC finance economy and at least dream about building the magic printer. Grow up a bit and you learn that it's a crowded field where you're not going to be competing against extremely motivated and well financed people who do this for a living. At best you nibble along at the edges, which is fine by me. But regardless of whether it works in practice, it's cool to build something that interfaces with the many pieces of software and external systems involved to better learn how things tick. So a homemade equity trading bot has a bunch of systems that need to be in place for it to even function poorly as a bot that trades. First the obvious is we need a way for the bot to talk to financial markets and submit orders in real or paper money. Luckily there are now brokerages out there that cater to people who want to play with algorithmic trading. It's easy to sign up for a free trading account at someplace like Alpaca and get a paper trading account that can be controlled with an API. These accounts are about as simple as you expect, send in an order according to the API spec using the provided libraries and the reading platform pretty much handles the rest including important details like keeping track of positions and profit/loss. Some more traditional brokers and discount brokers will offer API access to data as well as trading functions too. But submitting orders doesn't mean anything unless you know what is actually going on in the markets. That's where brokerage accounts and these services provide the other half of their value, they give you access to one or more feeds of data. Data feeds come in various flavors, tiers, and types. They can offer data from one or more exchanges. The end effect is you may only see a small fraction of the currently active market order book. Depending on what you're doing the information this may be a problem or not. Either way, people pay good money for data feeds to support whatever project they are working for. Some brokerage accounts will give you access to more detailed feeds for free if you have an account with them or sign up for developer access, but now you need to figure out their specific API. Next you need a “when do we make a trade” system which can be anything you dream of, assuming you have the expertise and creativity to think up conditions that may work. In my case I have a bunch of technical signal calculations for momentum and trend that can fire independently and they all sorta vote. If enough signals in predefined combinations fire, then a trade fires. It's dumb and it undoubtedly loses money making bad decisions, but it's something concrete that I can put down just so the rest of the system that relies on them can function. Then to help see whether those dumb strategies can make sense, there's a back testing system. Take saved price data and replay it back against those strategies and see to what extent they work and or profitable. Make sure to use the same code paths, as the normal trading workflow so that weird bugs don't easily creep in. It's a fairly easy concept, but again, weeds bring complexity. Like how do you handle backfilling missing data? How far back do you go to try to avoid accidentally cherry picking market conditions that don't apply now? The potential parameter space is so huge that it's hard to search exhaustively, you need to make guesses. Next there's risk management decisions and models. This market stuff is probabalistic, so no strategy works 100% of the time. Or even 80%. You'd be happy if it was 55%. So the key to not losing your entire account value is making sure the system can't beat the farm. There's limits on the biggest amount it can out at risk at any time. There's limits on how big of a loss is allowed before the position is cut. Do you close out everything before the market closes, or do you hold overnight and take the risk that some random news event cause a huge change in value. There's lots of weird little things you have to account for like how certain big events are tied to the calendar. The Federal Reserve has scheduled meetings and press release sessions about interest rates that always to move the market instantly. There's economic data releases that are regular high volatility events. Companies hold their earnings releases. You can spend a lot of time collecting a list of these events and then plan to either trade or not during those specific days or hours. There's even government regulations to deal with. If you work for a publicly traded company, trading that stock can put you at risk for breaking insider trading regulations even if it's a bot doing so in your name. So to be safe you need to hard block those and make sure to update the information if you switch employers.

When I was originally writing the bot there was a “pattern day trader” rule where accounts that made too many day-trades in a week got their account flagged and had stricter limits on what they could do. For a small toy account, you wanted to avoid it so you had to build a system to track those trades to avoid the legal limit. Then the US government decided to eliminate those rules in June and all that code was scrapped. Then I had an excursion to deal with a practical problem, let's say I put a small amount like $1000 into an account. If I avoid dealing with fractional stock shares, then the absolute price of a single share can be a limiting factor – some stocks easily cost more than $1000 per share. On top of that if the bot can't risk more than 5% of the account at any point, one interpretation of that limitation is it cannot hold a stock worth more than $50. (Yes, there's other ways to interpret what is “at risk”). Regardless, I needed a tool that weeded through the universe of stocks to pick a short list of symbols to follow and test on. Otherwise trying to store and track thousands upon thousands of stock symbols becomes its own major engineering project. Finally there's a little fiddly details. For example, data comes from the server in ticks and all my signals work off 5-minute aggregated chunks. Depending on what I'm doing the fact that my system clock can drift away from the server clock may cause issues. There's also the problem of “slippage” where you put in an order for one price but close on a slightly different one when trading live. It's usually a small difference, but when you're scrapping for the difference in pennies, it starts to matter and requires you to account for it in model profitability calculations. All throughout building this, even when I'm not writing the literal lines of code, there's endless decisions to be made with little guidance as to what is correct. It's honestly my crazy little system and it can take whatever form my decisions collectively wind up at. While in theory there are possibilities of building a system that is “profitable” and thus can be deemed “correct”, I doubt my amateurish bungling can happen across such a combination for even a brief stretch. The market is constantly changing and from what I've heard, the pros constantly have to innovate to adjust to the conditions. So as of now, this system is just happily making paper trades. The first couple of months all it did was lose pennies on every trade. At some point after lots of random flailing and tweaking, it's clawed back a few of those pennies…. But I'm waiting for the inevitable drop again as it's lucky streak breaks. Right now my biggest issue is finding ways tohelp extract signal for the system to learn quickly without accidentally tipping too far into over fitting. This takes real wall clock time to test out how something works under line conditions and not historical data. I feel like that PM that wants an A/B test run with 5 daily visitors and wants a result by Tuesday. In fact, when there's an active position using up (paper) capital that would otherwise block trades, I'm already allowing paper trades to do “what if” scenarios in an effort to collect more data. So at a high level that's where this bot lives, a weird space between professional effort and results. I don't have any money to show for it, real normal imagined.

/emailfoote