A real-time adaptive trading system using Genetic Programming QF5205 : Topics in Quantitative Finance Donny Lee Nadim Mouchonnet Summary 1. 2. 3. 4. 5. Introduction Genetic Algorithm and Genetic Programming Trading System Genetic Program Results Introduction • Many traders use indicator based trading rules • … but they may be loss making (see chart) • Double Objective Create a system than trades and make profit Emulate the behavior of a technical trader Motivation: “Can a technical trader consistently make a profit?” and “should a technical trader or trading system adapt to market conditions or is it better to use a static system?” Introduction(2) • Results using only a single strategy, backtested on historical data. • Stochastic strategy: Buy when indicator crosses the 20% mark from below and sell when it crosses the 70% mark from above. • Simple moving average strategy: Buy when the slow lag (50 day look back) crosses the fast lag (20 day look back) from below and sell when vice versa. Introduction(3) • Using the stochastic strategy on SIA stock from 02/11/10 to 08/26/10. • Loss is $0.30. Introduction(4) • Using the SMA strategy on KEP stock from 02/11/10 to 08/26/10. • Loss is $0.27. Introduction(5) • Using the GBPUSD for a period of 3 months • Bloomberg can summarize a whole bunch of strategies Introduction(6) • Using the GBPUSD for a period of 3 months • Parabolic SAR Genetic Algorithm and Genetic Programming (1) • • • • • GA is an iterative system … aiming to find near-optimal solutions … to multi-extremal problems … by imitating the process of evolution Rule is : SURVIVAL OF THE FITTEST Genetic Algorithm and Genetic Programming (2) • GA representation consists of binary strings of fixed length. • Search space is finite • GA needs to be initialized … • … by random sampling • Difference between GA and GP is that GP allows string length to vary within the solution space. Trading System : Data • Analysis carried out on spot FX ticks for GBP/USD from CQG Data Factory and Future Source from 1994 to 1997 • CQG : gathered from various FX brokers • Future Source : live feed from the Omega TradeStation utility (major banks FX quotes) • Got bid and ask and transforms them into mid • Data aggregated into OHLC Trading System : Software • Imitating the technical trader i.e. chooses technical strategies from a range of popular trading rules • GP based rule selection engine choosing combinations of such strategies • Cash management filter exits strategies when losses > user-defined threshold • Each backtested strategy returns : Profit, max drawdown, Modified Stirling ratio, number of trades • Transaction costs are taken into account with various slippage depending on trading time Trading System : Software Trading System : Strategies • 6 Rules used : simple moving averages crossover, adaptive moving averages, price channel breakout, stochastic, relative strength index, commodity channel index • 3 Connectors : AND / OR / XOR (00/01/10) • System rules allowed to use different frequencies/lags • Strategies ranked by stirling ratio (when return is positive) or by absolute return when negative. Genetic Program • Representation of a strategy. • A strategy of ours can be represented graphically. Genetic Program(2) • From a graphical understanding of a strategy, we can translate to a strategy structure. or a binary string representation. Genetic Program(3) • Or a binary string representation. Genetic Program(4) • This is essential as we ultimately need to convert our strategy into a binary string for our GP to work. Each strategy can be written in the form RULE | CONDITION | CONNECTOR | RULE | CONDITION | CONNECTOR | ……… | ACTION Genetic Program(5) • From there, we can form our binary string representation for our GP. I.e., consider the rule “BUY if AMA TRUE AND CCI FALSE OR RSI TRUE” It would be represented in binary as, 1000011111 Genetic Program(6) • Now with each strategy represented as a binary string, we perform the following in each iteration. 1. 2. 3. 4. Initialize population Calculate Fitness Crossover Mutate Genetic Program(7) • We initial the population with a certain number of strings which will represents the strategies we will use. 1. Assume there are a maximum of k included indicators and a minimum of j. We may construct rules from a total of I indicators. 2. Generate U uniform pseudo-random integer variables V where 1 <= V <= I. For indicators to be used, we write a ‘1’ in the structure part, otherwise a ‘0’. 3. Generate U uniform pseudo-random binary variables corresponding to the indictor bit for each indicator. 4. Assume there are C connectors allowed. For each indicator, we generate a fixed width binary of D(1,C) and write it next to the corresponding indicator bit. Genetic Program(8) • At each trial over a user-defined period, each of these strategies is tested by simulating their trading performance over historical data. • There are many ways to assess performance. The Stirling ratio – profit divided by maximum drawdown – is often used by traders to assess performance. • For Crossover and Mutation, we would pick a strategies with a certain top % in their Stirling ratio. Genetic Program(9) • Crossover is the process of cutting strategy string pairs at points and exchanging tails and heads to make a new a pair. • Only the best s% are considered for crossover. For those strings rank i, the probability of selecting this string is • A cut point is selected uniformly pseudo-randomly and ‘head’ and ‘tail’ parts are exchange. Genetic Program(10) An example of a Crossover, suppose the following two pairs of strings are selected along with a uniformly distributed random number: 1010001010 and 1100111100 and 3 0100100111 and 0001101110 and 8 Crossover: 101|0001010 and 110|0111100 becomes 1100001010 and 1010111100 01001001|11 and 00011011|10 becomes 0100100110 and 0001101111 Genetic Program(11) • Mutation is the process of randomly changing appropriate bits in a strategy string and is executed in a bitwise manner. • Elitist model: top-ranked 5% of strings are not mutated. • Number of strings mutated = Mutation Rate x No. of Strings. • M uniform pseudo-random integer are generated between 1 and B, length of each string, without replacement. • Each corresponding numbered bit is then mutated (‘0’ becomes ‘1’ and ‘1’ becomes ‘0’) Genetic Program(12) • Example of a mutation. • Strings are of length 10 and four pseudo-random integers are generated and arranged in ascending order – 3, 5, 7, 8. • The following strings are mutated as follows: 1010001010 becomes 1000100110 1100111100 becomes 1110010000 0100100110 becomes 0110001010 0001101110 becomes 0011000010 Genetic Program : Summary Step 1 Step 2 Step 3 Step 4 • Initialize : Generate 100 strategies randomly • Crossover strategies using 50% of the best strategies • Mutate the strategies outside the best 5% • Repeat Steps 2 & 3 until convergence of the solutions Results : Test • Step 1 : In Sample backtesting using 15minute data used to determine the 20 best strategies during Q1 1994 • Step 2 : Out Sample trading using the best 20 strategies over the next quarters and assess the performance in live trading conditions Results : Improvement of solutions (1) Results : Improvement of solutions (2) Results : Profit Results : Further work • Periodic reoptimization as performance decreases as time out of sample increases • Parallel optimization Our experience • Unsuccessful Programming in the Java framework • 1 solution : using the Technical Analysis tool of Bloomberg’s Excel Add-in