Using Grammatical Evolution to develop trading rulesPresentation Transcript
Case study: Using Grammatical Evolution to develop trading rules.
buy buy buy sell sell sell sell An automatic trading system decides when to buy and sell based on a certain rule set If we base the rules on moving averages (colored lines) one possible rule would be: buy, if yellow crosses above purple
The following BNF (Backus–Naur Form) grammar makes it easy to describe a whole universe of different rule sets. <S> ::= <tradingrule> <tradingrule> ::= sigdn = ifelse(<signal>,-1,0);sigup = ifelse(<signal>,1,0); <signal> ::= (<signal>) AND (<signal>)|(<signal>) OR (<signal>)|<var> <relop> <var> <relop> ::= <= | >= <var> ::= sma5|sma10|sma20|sma40|sma80|sma160
Such automatically generated trading rules can be tested on historical data. If we define a criterion for the quality of a trading rule we can then create an algorithm that searches for the best possible set of trading rules. Grammatical Evolution
GEVA 2.0 That is the setup used for the result on the following slides.
BNF grammar The encoding and the grammar distinguishes Grammatical Evolution from Evolutionary Algorithms / Programming.
The following settings were used to construct a simple trading system. BNF grammar Settings for the genetic search algorithm
sigdn = ifelse( ( sma40 <= sma20 ) | ( sma160 >= sma80 ) ,-1,0); sigup = ifelse( sma5 <= sma10 ,1,0); The winning trading rules after 100 generations are: The following formula has been used to calculate the fitness of each rule set.
The best trading rules applied to the S&P 500 from 2000-01-02 to 2008-12-05. This is not an realistic test for real world performance, please notice remarks on next slide.
The test doesn't give us information on the real world performance of this trading rules since:
The algorithm didn't actually forecast the price development, it already knew the future.
No commission was included, which might explain the many trades on the short side.
But, It clearly shows that the algorithm was capable to find trading rules that make intuitively sense and are profitable under the circumstances we provided. Further experiments with complexer grammar with more realistic settings is needed to obtain further insides.
Further examples of BNF to construction of trading rules.
Instruction on how to reproduce the show example. Requirements:
Packages: quantmod, TTR, rJava
In Eclipse: Edit/Import/Import Existing Project into Workspace
Edit run configuration for Main class “ Main.RunTradSys”
Extensions of the GEVA 2.0 code and files needed to modify the rule generation. Setup GEVA and starts instance of R Here get the rule sets pushed into R and feedback captured R files to setup the data and evaluate the rule sets. BNF grammar Properties for genetic search.