bateman-master

所属分类:数值算法/人工智能
开发工具:Java
文件大小:162KB
下载次数:1
上传日期:2014-04-02 20:15:34
上 传 者4668612
说明:  粒子群模型的代码实现 粒子群模性是描述动物群体和大数据无规律运动的理论
(bateman module)

文件列表:
LICENSE (1066, 2013-04-09)
plot (0, 2013-04-09)
plot\AAPL_series_201303242306.csv (154567, 2013-04-09)
plot\AAPL_trades_201303242306.csv (1053, 2013-04-09)
plot\sample_plotting_script.r (1239, 2013-04-09)
pom.xml (3401, 2013-04-09)
src (0, 2013-04-09)
src\main (0, 2013-04-09)
src\main\java (0, 2013-04-09)
src\main\java\org (0, 2013-04-09)
src\main\java\org\wkh (0, 2013-04-09)
src\main\java\org\wkh\bateman (0, 2013-04-09)
src\main\java\org\wkh\bateman\fetch (0, 2013-04-09)
src\main\java\org\wkh\bateman\fetch\GoogleQuoteFetcher.java (1735, 2013-04-09)
src\main\java\org\wkh\bateman\fetch\Quote.java (1283, 2013-04-09)
src\main\java\org\wkh\bateman\fetch\QuoteCollection.java (2184, 2013-04-09)
src\main\java\org\wkh\bateman\fetch\QuoteFetcher.java (1616, 2013-04-09)
src\main\java\org\wkh\bateman\fetch\YahooQuoteFetcher.java (3029, 2013-04-09)
src\main\java\org\wkh\bateman\model (0, 2013-04-09)
src\main\java\org\wkh\bateman\model\BuyZoneModel.java (3337, 2013-04-09)
src\main\java\org\wkh\bateman\model\BuyZoneOptimizer.java (6761, 2013-04-09)
src\main\java\org\wkh\bateman\pso (0, 2013-04-09)
src\main\java\org\wkh\bateman\pso\FitnessFunction.java (108, 2013-04-09)
src\main\java\org\wkh\bateman\pso\MersenneTwisterFast.java (54774, 2013-04-09)
src\main\java\org\wkh\bateman\pso\SimpleParticleSwarmOptimizer.java (4023, 2013-04-09)
src\main\java\org\wkh\bateman\trade (0, 2013-04-09)
src\main\java\org\wkh\bateman\trade\Account.java (1734, 2013-04-09)
src\main\java\org\wkh\bateman\trade\Asset.java (481, 2013-04-09)
src\main\java\org\wkh\bateman\trade\Conditions.java (569, 2013-04-09)
src\main\java\org\wkh\bateman\trade\FixedPercentageAllocationStrategy.java (1088, 2013-04-09)
src\main\java\org\wkh\bateman\trade\MoneyManagementStrategy.java (171, 2013-04-09)
src\main\java\org\wkh\bateman\trade\Rule.java (3094, 2013-04-09)
src\main\java\org\wkh\bateman\trade\Session.java (6384, 2013-04-09)
src\main\java\org\wkh\bateman\trade\TimeSeries.java (2415, 2013-04-09)
src\main\java\org\wkh\bateman\trade\TimeSeriesType.java (135, 2013-04-09)
src\main\java\org\wkh\bateman\trade\Trade.java (3655, 2013-04-09)
src\main\java\org\wkh\bateman\trade\TradeSignal.java (385, 2013-04-09)
... ...

Bateman ------- ![Everything failed to subdue me. Soon everything seemed dull: another sunrise, the lives of heroes, falling in love, war, the discoveries people made about each other.](http://media.tumblr.com/tumblr_lmvx4uwp0h1qeqv7k.gif) Bateman is an in-progress trading system meant to screen a subset of the US equities markets and tests out how well a simple [long](http://www.investopedia.com/terms/l/long.asp)-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](http://en.wikipedia.org/wiki/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. It's currently in an experimental phase and shows some promise, but more research is needed. 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 27 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](https://github.com/fearofcode/bateman/blob/master/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](http://en.wikipedia.org/wiki/Algorithmic_trading) for more information. Note that "algorithmic trading" should be distinguished from ["high-frequency trading"](http://en.wikipedia.org/wiki/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](http://www.zerohedge.com/news/2012-12-15/high-frequency-trading-broken-market-primer-two-parts). 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 the infrastructure that powers the new HFT systems. 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](http://blog.nobletrading.com/2009/12/what-is-discretionary-trading.html) fashion, with traders generally trying to combine macroeconomic and fundamental financial information (["fundamental analysis"](http://en.wikipedia.org/wiki/Fundamental_analysis); think Warren Buffett) with [technical analysis](http://en.wikipedia.org/wiki/Technical_analysis)/charting (think voodoo snake oil, in my opinion) to figure out what to trade or invest in. It's generally agreed that due to the [psychological factors involved in trading](http://economix.blogs.nytimes.com/2011/02/17/forecasting-is-for-the-birds-and-rats/), 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](http://www.oanda.com/) 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. 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](http://dealbook.nytimes.com/2012/08/02/knight-capital-says-trading-mishap-cost-it-440-million/). 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](http://en.wikipedia.org/wiki/Mean_reversion_(finance). 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](http://en.wikipedia.org/wiki/Moving_average) and other [indicators](http://www.investopedia.com/terms/t/technicalindicator.asp) many traders use, this is very simple: wait for the stock to go up a little bit, 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](http://www.investopedia.com/terms/l/limitorder.asp) with pre-determined profit targets and [stop losses](http://www.investopedia.com/terms/s/stop-lossorder.asp), 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 that they do poorly on future, unforeseen data. This, unfortunately, is why it's not generally practical or effective to build huge models that handle every conceivable scenario and do lots of stuff. Why write something from scratch? Aren't there tools out there for this? ------------------------------------------------------------------------ There are many different ways to write a trading system. Many trading systems consist of an awful hell-world of Excel spreadsheets and VBA macros. Many others exist as scripts for tools like [MetaTrader](http://www.metaquotes.net/en/metatrader4), which have built in programming languages intended to be friendly to non-professional programmers. They also include optimization facilities for finding numerical parameters to trading systems, like genetic algorithms. Probably the easiest way to go in general would be to use [Quantopian](http://www.quantopian.com), which lets you build and test trading systems right in your browser. Most of these tools are inadequate because they don't give you the low-level control you need. Quantopian gives you access to libraries like [numpy](http://www.numpy.org/), so that may very well be your best option if you don't roll your own from scratch. I need to call a backtrading system in a loop and have it run as fast as possible. In other words, I can't see how to implement Bateman in terms of low-level linear algebra that can be called from Numpy. So, we're stuck with a general-purpose language like Java. Finally, I'd like to comment on the choice of optimization algorithm. I decided to use [particle swarm optimization](http://en.wikipedia.org/wiki/Particle_swarm_optimization) rather than genetic algorithms because PSO can often be better for continuous optimization tasks, whereas genetic algorithms seem, to me, more suited for discrete/combinatorial tasks like scheduling and routing. What is the idea behind the trading system? ------------------------------------------- As mentioned above, Bateman tries to buy a stock slightly above its open and below or near its daily high. Rather than trying to build a forecasting model, Bateman is intended for use with stocks that have a frequent high positive difference between daily high and open share price, so that regardless of what happens by the end of the day, at some point it will likely exhibit behavior that can be profitably exploited. There are three fixed numerical parameters Bateman tries to optimize when it runs: the "buy trigger", the "sell trigger", and a stop loss. 1. The *buy trigger* is the amount above the open price for the day that it will buy at. So if the stock opens at 100 and the buy trigger is taken to be 0.5, any price above 100.5 will be acted upon. 2. The *sell trigger* is the amount above the price shares were purchased at to sell. If the sell trigger is not met by the end of the day, the shares are sold so that no positions are carried overnight. 3. The *stop loss* is used in the normal sense as a [risk management](http://www.investopedia.com/articles/trading/09/risk-management.asp) procedure to cut losses. To find what the values of these constants should be for a given stock, it downloads recent data for that stock and tries to find the specific numbers that would be most profitable for that data. To compute this, it takes a given possible candidate set of constants and [backtests](http://en.wikipedia.org/wiki/Backtesting) them, simulating trading using the historical data it acquires. As it's an optimization algorithm, it gravitates towards more profitable constants. These three components -- buy trigger, sell trigger, stop loss -- are the numbers Bateman optimizes for, using data retrieved from Google to create a simulation of trading that data as if it were live. The results of the trade simulation are the particle swarm algorithm's *objective function*; every time it wants to find out how good a set of candidate parameters are, it runs a full simulation with those and gets back a number that lets it quantitatively compare different solutions so that it can find better solutions. The simulation of trading is what is used to drive the optimization process. So what does it actually give back to you? ------------------------------------------ After downloading data from the Internet and running through the optimization process, some sample values it might wind up spitting back would be something like "buy trigger = 0.1, sell trigger = 0.5, stop loss = 0.07". This would be interpreted to mean the following: > "If the stock price is currently up $0.10 or more since the beginning of trading and we haven't made any other trades today, then BUY; if, after buying, the stock has gone up an additional $0.50, SELL. Additionally, if, after buying, the stock price has gone down by $0.07 or more, SELL. If we still haven't sold shares of stock and the end of the trading day is imminent, SELL, regardless of current share price. If we have traded once today, don't trade again until the next trading day". Notice how this is systematic, quantitative, and could be automatically executed by a computer without human intervention. This is why Bateman is a "trading system" or enables "systematic trading": it suggests a course of action that completely removes qualitative judgment or fickle human decision-making. *Is it really worth the trouble to use an algorithm to find the buy and sell triggers?* Well, when I tried to figure out good buy and sell triggers by hand by looking at graphs of intraday data, my results were significantly worse than the numbers Bateman comes up with through its particle swarm algorithm, so I think this program adds real value. Besides, doing that by hand when you have a quad-core computer in front of you seems silly. How many shares do we buy? The model I use is to just take a fixed percentage of our capital and buy as many shares as we can buy on that. Currently I'm using 75% of available capital in the executable example described below, which I think would be considered pretty high, but Bateman is long-only and sets fairly tight stop losses. So I think the choice is reasonable. What range should we restrict our model parameters to? That's a matter of preference. I'll choose the following: * Set minimum buy trigger to 0, allowing buying at open price * Set minimum stop loss and sell trigger to 0.2% of the first opening price, which we'll use as a kind of proxy of the historical bid-ask spread * Set maximum buy trigger, sell trigger, and stop loss to the median difference between open price and high for the last year, so that we can estimate triggers that are reasonably in line with historical data To get the median figure referred to above, we download daily end-of-day data for the last year from Yahoo! Finance. See `YahooQuoteFetcher`. Our choice of ranges will influence the outcomes we get. See `BuyZoneOptimizer` for how all of this comes together and tinker around with it if you want to change the ranges. Currently, it does backtesting with a simulated starting amount of US$100,000 and what should be reasonable assumptions about trading costs: US$10 commissions one way for trading, slippage of 0.01%. These aren't currently user-configurable, other than the symbol to work with. It simulates placing a (long-only) [market order](http://www.investopedia.com/terms/m/marketorder.asp) (as opposed to a [limit order](http://www.investopedia.com/terms/l/limitorder.asp)) that it assumes it pretty much gets right where it buys at -- it assumes orders are placed fast enough to be considered immediate for the purposes of a simulation, at a price with small enough slippage to be quite small. It also assumes the spread between the bid and ask is small enough to be reasonably accounted for with the commissions and slippage calculation that is applied to every trade. Currently, trailing stop losses are not supported. It will also only trade once a day. It keeps trades of a fixed size, not changing them or undertaking new trades until the current trade has been closed. Hopefully the assumptions implemented here are reasonable enough to be useful for simulating the performance of a trading rule. Also, the specific metric it optimizes for is actually the [Sharpe ratio](http://en.wikipedia.org/wiki/Sharpe_ratio) of the simulated trades, rather than net profit; i.e., it is intended to optimize for *risk-adjusted* returns. Although the Sharpe ratio is imperfect and many other metrics could plausibly be used, it is widely known and is currently what is in place. To restate and summarize, it takes a given set of parameters as candidate triggers and stop loss, simulates that on historical data, and then returns the best one it finds. How to run it ------------- You will need the following software to run this: * [JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html) version 1.7 (note the version -- it uses some 1.7-specific I/O libraries) * [Maven](http://maven.apache.org/) 3 Then you'll want to start by cloning the repo: $ git clone https://github.com/fearofcode/bateman $ cd bateman Then you can build the project, which should be as simple as: $ mvn package Maven will download a lot of stuff the first time through. It should run the project's unit tests, then build a single fat JAR in the `target` directory. Assuming it built successfully, you should be able to run it like any other JAR: $ java -jar target/bateman-1.0-SNAPSHOT.jar This will then run the actual optimizer. Currently it is hardcoded to work on Apple's stock (AAPL). When you run this, a sequence of events will occur: * Download recent intraday quotes for the symbol in question (AAPL) from Google Finance * Run a particle swarm optimization to find the best triggers and stop loss * Print the parameters it comes up with and run a final simulation with these * Write out a simulated trading log with profit-and-loss calculations for each simulated trade to a CSV file ... ...

近期下载者

相关文件


收藏者