Examples
Examples
Complete strategies that run on the bundled sample data, from a simple buy-and-sell loop to margin trading, value investing and earnings announcements.
On this page
Every example is a Gorilla REPL worksheet in the examples folder of the library. Open them with make start (see Get Started), or read them as plain Clojure files and paste the code into lein repl. The outputs shown on this site come from running them. The first ten use the 1990 sample dataset; the last three use the synthetic Yahoo snapshots, which have company reports.
Step-by-step walkthroughs
These pages explain the code and the results line by line. If you are new, read them roughly in this order.
1A first strategyBuy once, sell a little every other day, and read the order record, portfolio and evaluation report.Basics →2Golden CrossTrend following with two moving averages, on two securities at once.Moving averages →3Bollinger BandsMean reversion: buy far below the average, sell far above it. Shows why buying every dip can go wrong.Volatility bands →4Stop and limit ordersAutomations that sell by themselves when the price crosses a level.Automation →5Buying on MarginBorrowed cash, short selling, and what the initial and maintenance margins do.Leverage →6Fundamental AnalysisRank securities by return on equity from Compustat-style filings, using only those that were public at the time.Compustat →7Yahoo Finance DataBuild a dataset from Yahoo snapshots: prices as traded, report dates, currencies and ratios.Real-world data →8Value StrategyEvery month, hold the two companies with the highest earnings yield, and compare with an index.Rank and rebalance →9Quality and EarningsPiotroski's F-score, growth rates, and buying after earnings beat the estimate.Events →10Strategy RecipesMomentum, combined factors, screens, cash buffers and parameter sweeps, as code to reuse.Patterns →
All worksheets
| Worksheet | What it shows | Walkthrough |
|---|---|---|
| simple_trading_strategy.clj | A basic buy and sell loop with order and portfolio output | Get Started |
| golden_cross.clj | 15-day and 30-day moving average crossover | Golden Cross |
| bollinger_bands.clj | Mean reversion with Bollinger Bands | Bollinger Bands |
| relative_strength_index.clj | Overbought and oversold signals from the RSI | |
| rate_of_change.clj | A momentum strategy using the rate of change | |
| automation.clj | Stop and limit orders as automations | Automation |
| buying_on_margin.clj | Leveraged positions, short selling and margin requirements | Buying on Margin |
| bad_strategy.clj | A losing short position through a dividend | |
| Plotting_frame.clj | Line charts of security prices and returns | |
| Fundamental analysis.clj | Filing visibility by report date and a return-on-equity screen | Fundamental Analysis |
| yahoo_finance_data.clj | Building a Yahoo dataset: traded prices, point-in-time fundamentals, ratios | Yahoo Finance Data |
| value_strategy.clj | A monthly earnings-yield strategy against an index | Value Strategy |
| quality_and_earnings.clj | The Piotroski F-score, and trading on earnings announcements | Quality and Earnings |
Ideas to try
Once an example runs, change one thing at a time and compare the evaluation reports:
- Change the windows: 10 and 20 days instead of 15 and 30 in the Golden Cross, or 1.5 standard deviations instead of 2 in the Bollinger Bands.
- Trade a different security, or all of them with
(available-permnos). - Add costs with
(update-transaction-cost 0.001)beforeinit-portfolio, and see how much of the profit survives. - Protect each position with a stop-loss automation.
- In the Yahoo examples, rank by another ratio, or combine two.
- Download real tickers with
download-yahoo!and run the same strategy on them.