Archive for the 'auto trading system' Category

*.*.*2 Bug Fix Release

Interactive Broker’s specification for "m_right" is

String m_right

Specifies a Put or Call. Valid values are: P, PUT, C, CALL.

 

I chose to make the RightType enumeration translate to "PUT" and "CALL". A bug report from the yahoo forums illustrated that this is no longer true, and that "P" and "C" are the only accepted values.

Please download the bug fix versions

9.2.0.2 and 9.1.0.2

Both also available under utilities.

Genetic Optimization and Maximization – Fitness Function

This is part 3 in a series on Genetic Optimization, please visit part 1 and part 2 to catch up.

What Does the Fitness Function Do?

The fitness function is the basis of the “survival of the fittest” premise of genetic algorithms. It is responsible for evaluating the parameter set, and choosing which parameter sets mate. The most difficult part of the fitness function is designing the function to produce parameters that are reliable and effective on data outside of the training set.

It helps to consider nature’s fitness function, we are the result of millions of years of genetic optimization, yet do not retain the brawn of a gorilla, nor the size of a sauropods (dinosaur that weighed 209 tons), nor the predatorial skills of a Tyrannosaurus. A genetic function does not just optimize for the strongest creature, but for the creature that can survive and thrive in all circumstances. Dinosaurs were clearly at the top of the food chain and thriving 65 million years ago, but were easily outlived by insects for their ability to survive the harsh aftermath of the Cretaceous-Tertiary extinction event. (Can you tell I have been researching a lot about dinosaurs since starting this blog?).

My point is that you need a fitness function which results in a set of parameters that performs well during a bull run, bear run, and also survives a market crash. A parameter set that makes a fortune on rallies, but bleeds on sideways patterns and reversals is no better than the dinosaurs, ultimately they will perish, taking a lot of your equity with them.

What Makes a Good Fitness Function?

A fitness function can be as simple as the profit generated by running your rules over training data, but this is likely to exploit onetime events in the data, and not to place an emphasis on reliability.

A good fitness function does the following

  • Understands Risk – does not evaluate only profit, but how much capital the rules placed at risk to earn that profit
  • Punishes Losses Heavily – by punishing the parameter set more heavily for losses than profits, you are training it to focus on consistent profits over volatile returns.
  • Punishes High Risk – any rules can earn a lot on a good day by loading up on beta, you want to train your algorithm to seek true alpha.
  • Does not punish zero gains – it is important to let your algorithm learn when to enter the market, and when to stay clear. Providing some incentive to simply not take a loss can be just as important as proving incentives to take a large gain.
  • Run on a reasonable time frame – A fitness function should evaluate each day (or possibly shorter) of sample data on its own, accumulating the results for a particular parameter set.

Following these guidelines the fitness function must rank each parameter set, and select mates.

Mate Selection

Once the parameter sets have been ranked they must undergo selection. The obvious solution would be to only select the top ranked parameters to mate, but this may ignore other minima that lesser parameter sets are exploring.

The chart above illustrates the importance of occasionally exploring lesser ranked parameter sets. The green lines represent the highest ranked parameter sets, but as we can see on the parameter space the red line is at the base of the global minima, while the green lines are just exploring local minima. The best way to allow for this is to select mates with an absolute valued normal distribution. The choice of probability distribution and standard deviation has a large effect on how fast a genetic algorithm converges, an analysis of which will be in a future article. For now the normal distribution proves to be more than adequate.

As you can see the fitness function has a huge impact on the output of your maximization, it defines what the ideal function should do.

Tune in for more Genetic Optimization in Part 4 where I will talk about Training.

Optimization

Rules based Optimization

Before I discuss my second Automated Trading System (ATS), I need to explain the principal in which it operates. A rule based ATS depends on carefully chosen thresholds and parameters to determine when a particular stock should be entered and exited (long or short). Experience and theory can provide an excellent starting point, but to perform really well for a particular stock, it is useful to maximize these parameters on historical data.

Think about your set of rules as though they are a function.

function rules (double shortMATime, double longMATime)
{
“Enter Long when the shortMATime minute moving average crosses above the longMATime minute moving average.”
“Exit when the longMATime minute moving average crosses above the shortMATime minute moving average.”
}

In this example, there are two rules which are executed over live or historical data, with two parameters, shortMATime and longMATime. We would like to select values for these parameters such that they would have made the most money over the last week (or any time frame), assuming this represents closely what values will make the most money tomorrow. This function is very difficult to maximize, as it is not continuous. Small adjustments to either parameter can cause huge swings in the profitability of the system.

In this particular case, you may consider running the entire variable space through the function, setting each parameter from 0 to 1000 minutes, incrementing by one second, and taking the maximum output when you are done. This turns out to be roughly (1000 * 60)2 = 3,600,000,000 runs. Assuming a very fast 5 seconds per run for a week’s worth of data, this would take 570.77 years to process. Clearly a better maximization function is needed, but it cannot depend on the derivative of the function, nor can it require continuity of the first order. This is exactly where genetic algorithms shine.

My next series of articles will cover Genetic Optimization in detail. Stay tuned for more updates.

My ATS History (Part 1)

I took a course in college on Portfolio Analysis, and while the course itself was interesting, what really inspired me was my professor’s research. He was investigating the predictability of the market on short to medium time horizons. His research suggested that it is certainly possible to forecast a stock’s performance, and back test to ascertain a statistical likelihood of this forecast. This was day and night to me(one might say a Jurassic change of mind!), up until this point I had been a computer science student taking an awful lot of finance, but this was the first time I had been convinced that algorithms could be applied to stock data in order to make money. (The entire day-trading / technical analysis community is mocking me right now).

In any case, this spurred a lot of research on my part. Up until now I had been investing in the best discount broker available, scottrade, and as a software engineer, the first thing I did was to write an html parsing interface to scottrade to allow my soon to be automated framework to make buy and sell orders. It wasn’t long before I did my research on the difference between a direct access broker and a brokerage firm. The former allowing me to place direct orders with an exchange, and the latter attempting to take a piece of my cake by being a market maker. My research immediately led me to Interactive Brokers, which offers an API (what you say? No HTML parsing to interface?), as well as much lower fees (we’re talking half a penny per share baby!). Later I learned that they do not offer the best margin rates, nor do they offer the highest leverage… but lets just say for my level of sophistication, they are perfect.

After establishing my new brokerage account, I needed to test the waters. I developed an incredibly simple trading infrastructure in C# using the interface provided in http://finance.groups.yahoo.com/group/twsapi/. This first cut interface simply parsed news events at the fastest possible speed, allowing my application to track it, and rapidly trade on it. As you can see from the chart below, there was some news event at 12:00. If the news event was detected at 12:00:10, one could make a tidy profit simply by going long early, and selling at around 12:04 with a stop at 43.55, this would represent a profit of 45 cents/share at a risk of 15 cents/share, or a nice 3R.

I implemented this strategy and ran for two months before trading live. It worked flawlessly, I back tested it on old news releases for about two years, and there was not a single case where the momentum did not follow the news. Then I turned it on last July, and got one good month of running, and come August it started out alright, right before the bull came. Then it hit (possibly like triceratops butting heads), the uptrend in all of the stocks I was following was proceeded by erratic trading behavior. Momentum on the in the news stocks was no longer carrying. After a news release I may see a large down tick followed be an instant up tick, so fast that I could not liquidate and close positions in time to make money. Clearly my strategy had to change. I shutdown my strategy in September, and went to work on version 2.0 of my ATS. (Part II to follow…)