Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Algorithmic Trading for Fun and Profit (Red Dot...

Algorithmic Trading for Fun and Profit (Red Dot Ruby Conf 2014)

Sheng-Loong Su

June 27, 2014
Tweet

More Decks by Sheng-Loong Su

Other Decks in Programming

Transcript

  1. DISCLAIMERS This talk is intended for educational purposes only This

    talk is not a demonstration of how to trade This talk is not intended as investment advice and must not be relied upon
  2. WHAT IS ALGORITHMIC TRADING? “The use of a predefined set

    of rules written into computer code to automate the process of buying or selling.” - AutomatedTrader.net
  3. ALGORITHMIC TRADING PROCESS 1. Formulate objective and hypothesis 2. Develop

    trading strategy 3. Backtesting with historical data 4. Measure and optimize 5. Deploy to production
  4. FEEDER - DATA SOURCES Free Commercial 1. Yahoo/Google Finance 2.

    Quandl 3. Twitter/StockTwits 1. DTN IQFeed 2. QuantQuote 3. EODData
  5. Red line - Fast Moving Average (15 days) Green line

    - Slow Moving Average (50 days) Technical Indicators
  6. def moving_average bars, lookback_period if bars.length < lookback_period raise "insufficient

    number of bars" end sum_of_prices = bars.last(lookback_period) .inject(0.0) { |sum, bar| sum + bar[:adj_close].to_f } sum_of_prices / lookback_period end fast_ma = moving_average bars, 15 slow_ma = moving_average bars, 50
  7. Fast MA > Slow MA = Long Signal (Uptrend) Fast

    MA < Slow MA = Short Signal (Downtrend) Moving Average Crossover
  8. PORTFOLIO Make trading decisions based on signals generated by strategy

    Keep track of cash, market positions and holdings Assess returns and risks
  9. THE LOOPS loop do # Feeder gets next bar break

    if bar.nil? loop do # Dequeue from event queue break if event.nil? case event.type when :market # Strategy analyzes market data # Portfolio updates holdings when :signal # Portfolio places order when :order # Broker executes order when :fill # Portfolio updates holdings end end