Bateman
(ABANDONED) Simple stock trading system that optimizes its parameters with particle swarm optimization
Install / Use
/learn @fearofcode/BatemanREADME
Bateman

Bateman is a very simple trading system meant to screen a subset of the US equities markets and tests out how well a simple long-only trading strategy described below will work.
It's based off the observation that many symbols display sufficient daily volatility that their high will generally be significantly above their open, regardless of their price at the close of trading.
The strategy has its parameters refined by particle swarm optimization, a simple continuous optimization algorithm, so that you don't have to figure out the parameters for each stock you're interested in by hand.
I never really got around to making this robust, but would like to come back to it someday soon. See below for a sample run of it.
Obviously, don't use this for trading real money.
Who are you?
My name is Warren Henning. I'm a 29 year old software developer living in Berkeley, California.
I don't have any professional experience in the finance or investment banking industries. I've never worked for a bank. This is just a hobby project I've wanted to do for a long time.
Have you traded this with real money? Does it work?
I haven't risked any money on this. It produces profitable simulated results on historical data I've tried it against recently (see a sample result below), but that's definitely not a guarantee of actual trading performance.
This is Open Source software, which basically means I owe you nothing and this software doesn't necessarily work. Please read the LICENSE file carefully. It's not my fault if on the off chance you use this and lose money! You have no recourse against me. Again, refer to the license.
Why are you doing this? How can you write a program to pick stock trades?
I'm doing this for fun, nothing more. I'm not selling anything or expecting to make money off this. I'm giving it away because I think the idea of it is more interesting than the practical reality of it as a money-making tool. I don't have the capital to trade it, and it's common even for seemingly foolproof trading systems to not do well under actual circumstances compared to their simulated performance.
I'd like to discuss the idea of relying on a computer to place trades now. A program like Bateman falls under the category of "algorithmic trading", which has been practiced by hedge funds and Wall Street firms for quite a while now. See Wikipedia's page on algorithmic trading for more information.
Note that "algorithmic trading" should be distinguished from "high-frequency trading", which is what has become the real focus of the quants and hedge fund rocket scientists nowadays. Bateman is not a high-frequency trading app; in fact, it simulates placing trades only once a day. Compared to the crazy shit Wall Street is doing now, Bateman is, I would think, old hat. From what I know, Wall Street has shifted its focus from discretionary proprietary trading to market-making, statistical arbitrage, and high-frequency trading operations. We're living in a crazy world, and depending on who you ask, high-frequency trading definitely has its dark side. I have nothing to do with that.
I'll also emphasize again that I'm not a trader and, while I am a professional programmer, I've never worked on any trading infrastructure code.
Additionally, Bateman isn't really a fully algorithmic trading app, since it doesn't actually place trades itself; it just tries to find numbers that would allow a human trader to trade successfully. So it enables "systematic trading", where a program emits outputs that suggest a rigid, objective course of action that the human trader is then supposed to follow. Many people are unable to follow through with this and generally lose money as a result. So, Bateman has nothing for talking to brokers or actually making trades itself. If you wanted to use the results of a program like this, you'd have to do that by hand.
Prior to the rise of ubiquitous high-performance computers, trading was done in what is now often called, by contrast, a discretionary fashion, with traders generally trying to combine macroeconomic and fundamental financial information ("fundamental analysis"; think Warren Buffett) with technical analysis/charting to figure out what to trade or invest in. It's generally agreed that due to the psychological factors involved in trading, humans are less skilled at executing trades than pre-configured trading systems. This is due to factors like second-guessing, over-thinking, being indecisive or changing one's mind, etc., almost always to one's own detriment. This compliments recent psychological studies about the flawed nature of memory and the human mind.
If you haven't ever tried to trade and haven't experienced this psychological aspect, I suggest getting a free paper trading account that lets you do simulated trading without actually giving anyone any financial information or money and actually trying it for yourself. If you're like most people, you'll have a hard time waiting for the results of a trade to play out and want to basically dick around. Paper trading has a certain stress to it, even when no real money is changing hands -- now imagine if it was cash you had busted your ass to save and you were going to try to live the dream and "trade for a living". Most people fail, badly. There is almost certain no reason to believe you or I are any better or different. Most "successful traders" who appear to be beating the market are just lucky.
Hence, to somewhat alleviate the unreliability and anxiety associated with discretionary trading, traders look for rigorous, quantitative "trading systems" that enable "systematic trading" if used and followed properly. If the program makes trades automatically without human intervention, this removes the obligation to follow the decisions made by the system everytime, as well, and the psychological aspects of the system are largely removed; all that then remains for such an algorithmic trading system is to monitor it to ensure it's working as intended.
There have been a few cases of high-frequency trading systems gone awry to disastrous results; one such system at Knight Capital racked up over US$400 million in losses in 30 minutes. Imagine being the first person at the firm to learn what had happened, and having to be the messenger of such horrible news. Yikes.
To conclude these thoughts on algorithmic and high-frequency trading, it's clear that scrutiny and rigorous testing are crucial for success, as Knight Capital learned.
On model complexity
Any time you build a system with parameters that get "learned" or "optimized" with some kind of underlying assumption behind it, you're basically building a statistical model. Other financial models often have much stronger assumptions than Bateman's, such as a normal distribution of returns or mean reversion.
Bateman is intended to have good "generalization" and future performance by being limited in its assumptions. Bateman's assumption is that some stocks go up a little sometimes. A plot of a "Bateman model" consists of a couple of horizontal lines, nothing more.
Compared to the moving averages and other indicators many traders use, this is very simple: wait for the stock to go up a little bit (to get some evidence that it won't just go straight down that day), buy, wait for it to go up a little more, sell, then do it again the next day. This can be carried out through a limit order with pre-determined profit targets and stop losses, and a model like that is perfect for someone who isn't a professional trader. If it no longer goes up during the day the way it used to, stop trading that stock and look for another one. If no stocks display this volatility property regularly enough, don't use the system. Simple as that.
But, you might be asking yourself, isn't computer AI kind of lousy at complex tasks like this? Well, as imperfect as computers are at complex decision-making tasks, their systematic nature gives them a certain edge versus humans in financial markets. As simple as a small set of numerical parameters to guide trades is, therein lies its strength: the assumptions behind Bateman are minimal, and not meant to be universally applicable; it's instead intended to be used on stocks that have in the past displayed a specific property on a frequent basis. It's well-known in machine learning that models with too many parameters or of too great complexity that are tested on historical data wind up being overtrained to that data, and effectively just memorizing it, with the consequence t
