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
  1. Step-by-step walkthroughs
  2. All worksheets
  3. Ideas to try

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.

All worksheets

WorksheetWhat it showsWalkthrough
simple_trading_strategy.cljA basic buy and sell loop with order and portfolio outputGet Started
golden_cross.clj15-day and 30-day moving average crossoverGolden Cross
bollinger_bands.cljMean reversion with Bollinger BandsBollinger Bands
relative_strength_index.cljOverbought and oversold signals from the RSI
rate_of_change.cljA momentum strategy using the rate of change
automation.cljStop and limit orders as automationsAutomation
buying_on_margin.cljLeveraged positions, short selling and margin requirementsBuying on Margin
bad_strategy.cljA losing short position through a dividend
Plotting_frame.cljLine charts of security prices and returns
Fundamental analysis.cljFiling visibility by report date and a return-on-equity screenFundamental Analysis
yahoo_finance_data.cljBuilding a Yahoo dataset: traded prices, point-in-time fundamentals, ratiosYahoo Finance Data
value_strategy.cljA monthly earnings-yield strategy against an indexValue Strategy
quality_and_earnings.cljThe Piotroski F-score, and trading on earnings announcementsQuality 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) before init-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.