# How to encode trading strategies mathematically

If you have a bunch of different econometric data (e.g. indexes, FX, commodities, interest rates...) you can try to find a formula to see if there is any relationship in the data - e.g. to forecast it by this discovered pattern.

What I am asking here is a little bit different: Is there another way in the sense that you can search for a formula f() such that the given form represents a trading strategy where certain indicators are found when to go long or short (or any derivative combinations)? The idea is that the formula itself lives in n-dimensional space of indicators/ trading-strategies and tries to survive as best as it can.

This must be a standard procedure for multi-agent systems simulating artificial stock markets. Alas, I am unable to find a simple approach to do just that...

Yes, you use an implementation of each signal and then use a statistical package like sas to generate a factor model for you. It generates a mathematical formula, with coefficients, and signals(variables) and even tells you the efficacy(R^2)

However you quickly find yourself exposed to data snooping bias by choosing this approach. Similar to the results outlined in this paper: http://www.eco.sdu.edu.cn/jrtzx/uploadfile/pdf/empiricalfinance/10.pdf

Data-snooping bias, is why people stress the economic reasoning for their strategies over the historic statistical efficacy, which often fails to replicate going forward.

• Thank you. Could you please give an example for a trading rule (e.g. moving average) and its mathematical encoding. – vonjd Mar 2 '11 at 12:27
• make a function like this: MA(int numdaysavg){return sum(x[i] thru x[i-numdaysavg] / numdaysavg} then use the results of this function over time, [a time series] along with a time series of returns or prices... (this wouldn't be much of a factor model because you only have one factor (moving avg). You can add as many as you like from there. – glyphard Mar 2 '11 at 13:46

Maybe I completely misunderstood the question, but it seems to me that you are looking to find a model structure as opposed to fit a specified/known model. In your context the model specification (the trading rules) are unknown... Am I right?

If that Is the case, maybe genetic programming:

http://en.wikipedia.org/wiki/Genetic_programming

Is what you need?

In a nutshell, it is a sub-class of GA which applies evolutionary approach for finding a model structure (a program) which is most fit... Throughout generations of evolutionary improvements.

My guess is that a language dictionary in this case is a set of constructs (variables) you have at your disposal, and the language grammar are the rules...

Just a thought!

Btw. Good Question!

• @user40: Thank you, in fact this was my original reference. Could you give me some ideas or references how to encode trading rules within the alphabet of GP? – vonjd Mar 8 '11 at 19:48

Here is an example of the 75% trading rule coded in R: Can one beat the random walk

This is how the author describes the rule:

The following script will generate a random series of data and follow the so called 75% rule which says, Pr[Price>Price(n-1) & Pr<(n-1) < Price_median] Or [Price < Price(n-1) & Price(n-1) > Price_median] = 75%.

There is a new paper "A meta-grammatical evolutionary process for portfolio selection and trading" which evolves trading strategies with genetic algorithms (unfortunately behind a paywall):

Contreras, I., Hidalgo, J.I., Nuñez-Letamendía, L. et al. Genet Program Evolvable Mach (2017) 18: 411. https://doi.org/10.1007/s10710-017-9304-1

Abstract
This study presents the implementation of an automated trading system that uses three critical analyses to determine time-decisions and portfolios for investment. The approach is based on a meta-grammatical evolution methodology that combines technical, fundamental and macroeconomic analysis on a hybrid top-down paradigm. First, the method provides a low-risk portfolio by analyzing countries and industries. Next, aiming to focus on the most robust companies, the system filters the portfolio by analyzing their economic variables. Finally, the system analyzes prices and volumes to optimize investment decisions during a given period. System validation involves a series of experiments in the European financial markets, which are reflected with a data set of over nine hundred companies. The final solutions have been compared with static strategies and other evolutionary implementations and the results show the effectiveness of the proposal.

In the paper two grammars are being used to encode trading strategies (in BNF):

One to encode a portfolio of companies: The other to encode investment signals over a specific period: More details can be found in the paper.