R is very speedy statistical package that's like an F-18A Hornet, versus Excel which is like a paper airplane. R is professional sports, Excel is Pop Warner. R is Mona Lisa, Excel is stick figures. R is ... okay, you get the idea. I'm long R, and short Excel. For traders that are analyzing vasts amount of data, it's time you made the switch to the trader's statistical package of choice. You have to work a little at understanding the scripting language, but it's nothing that you can't wrap your mind around in a few nights after the kids go to bed. I'm just getting acquainted with R, and I'm already putting my copy of Excel on eBay.
Cut-and-paste versus read-in:
After you've generated vast amounts of data from an optimization run, you find yourself with the loathsome task of cutting and pasting the data into an Excel spreadsheet that you've probably already pre-formatted to look at the data. It requires you to select the data and not miss any rows, and it opens up the possibility that something gets left out. With R, you don't touch it once you've converted it to a .csv file. You simply reference it in your script.
Manipulating columns of data:
In Excel, you know the drill. Select a cell to compute the mean of a row and then select the elements of a column that you'd like to apply the formula to. When you have over 1,000 rows, it takes a while to drag that stupid mouse. Just as you approach the end of your values, the cursor goes wild and runs down to infinity. Ugh. Well, with R that is no longer an issue since you define the column of data (called a vector) to a script that quickly returns the value you're looking for.
Add-in packages:
Remember how excited you were to install the Data Analysis pack in Excel and how it gave summary statistics for a column of data that included mean, standard deviation and other stuff you had no idea what they were? Well, that's a standard part of R. And as you graduate from Statistics 101, you'll find that there are add-in packages in R that range from neural networks to Bayesian statistics, from genetic algorithms to copulas (side note: the geniuses who created tranched derivatives that came home to roost last year used gaussian copulas, amongst other things).
Cost:
I think I paid in the neighborhood of $250 for my copy of Excel. I paid nothing for R, since it is open-source and free.
The issue of crashing Windows:
I have crashed my computer by asking Excel to compute too much data. So far, R hasn't produced the same results. And based on its architecture, I'm not expecting it.
Graphs and dynamic data:
If you want to create a chart or graph of something in Excel, you need to navigate through that stupid menu of crazy pictures to get something that's clunky and difficult to maintain. In R, you use scripts to create charts and graphs, and the data file feeding the chart is dynamic so that each time data is added, R will incorporate the new data. No offsetting shenanigans.
There are video tutorials available at the usual places you'd find that sort of thing, and a very robust community of R users to help get you started. So put down your toys and get to work. There is much data that needs analyzing.
Thursday, October 29, 2009
Wednesday, October 28, 2009
System Trading is Quicksand without the Quick
Perhaps the best way to describe the path to system trading is slogging. It's like one of those dreams where you're trying to run in the sand, and it keeps sinking. And then it starts to rain and the wind starts blowing you backwards. Like many things related to trading, system trading sounds simple on its face, but becomes increasingly elusive as one pursues it.
System trading distinguishes itself from other forms of trading in that it has a high threshold for capital allocations. Feelings, notions, guru tips and gut instinct are not sufficient for the system trader. There needs to be a formalized approach to the trade that can be back-tested on historical data. This first step is not all that involved, really. There are many off-the-shelf programs that can perform a back-test of a trading system. But once you get the results, what do you do with it?
You ponder, perhaps, and cast a skeptical eye upon promising results. You start wondering (and rightfully so) whether past performance has any correlation with future returns. Well, how would you know? At this point you can either sell the program to others or try it out yourself.
A way to test the back-test is to perform an optimization. This is running the back-test with different parameters (ie, moving averages with varying length for an MA crossover system). If it performs well under this process, you can gain some more confidence in your system. But how would one define 'performs well'? There are many metrics to look at including net profit, drawdown and ratios of those two. How many permutations were profitable as a percentage of all the permutations that were run? What percentage of permutations had a net profit that exceeded maximum drawdown? Is that a good number? What is the best parameter set? What was the measuring stick used to determine the winning set? Now we see where the quagmire begins.
You are now the proud owner of stacks of statistics that you need to sort out. It's like going through your coupon drawer. You know, the one that has mostly expired coupons, and some for junk you wouldn't buy if it were free. The optimization results are not a self-evident validation or rejection of a trade system. It takes a thinking person to evaluate what they've got.
And after this is sorted out, there is still another step of testing the system on out-of-sample data. Why? Well, because your optimization process found good parameters, but it fit the data to do it. The process is called curve-fitting in statistics and involves making things fit. Not necessarily bad, but it can be overdone. That's why we walk-forward, or test on out-of-sample data. It's the same thing as trading the system in real-time. After this process is complete, you find yourself with another truckload of disorganized coupons.
Besides the programming skills needed to do the basics, you need a decent grasp of statistics, good organization skills (where did I put that file on the Coffee market between 1998 and 1990?), and the basic understanding of how markets work. Understanding markets is crucial, as many nerdy statisticians with excellent programming skills have proven by losing large sums of money. Don't be afraid of having to learn a lot of stuff, though. Trading is not for the lazy.
System trading distinguishes itself from other forms of trading in that it has a high threshold for capital allocations. Feelings, notions, guru tips and gut instinct are not sufficient for the system trader. There needs to be a formalized approach to the trade that can be back-tested on historical data. This first step is not all that involved, really. There are many off-the-shelf programs that can perform a back-test of a trading system. But once you get the results, what do you do with it?
You ponder, perhaps, and cast a skeptical eye upon promising results. You start wondering (and rightfully so) whether past performance has any correlation with future returns. Well, how would you know? At this point you can either sell the program to others or try it out yourself.
A way to test the back-test is to perform an optimization. This is running the back-test with different parameters (ie, moving averages with varying length for an MA crossover system). If it performs well under this process, you can gain some more confidence in your system. But how would one define 'performs well'? There are many metrics to look at including net profit, drawdown and ratios of those two. How many permutations were profitable as a percentage of all the permutations that were run? What percentage of permutations had a net profit that exceeded maximum drawdown? Is that a good number? What is the best parameter set? What was the measuring stick used to determine the winning set? Now we see where the quagmire begins.
You are now the proud owner of stacks of statistics that you need to sort out. It's like going through your coupon drawer. You know, the one that has mostly expired coupons, and some for junk you wouldn't buy if it were free. The optimization results are not a self-evident validation or rejection of a trade system. It takes a thinking person to evaluate what they've got.
And after this is sorted out, there is still another step of testing the system on out-of-sample data. Why? Well, because your optimization process found good parameters, but it fit the data to do it. The process is called curve-fitting in statistics and involves making things fit. Not necessarily bad, but it can be overdone. That's why we walk-forward, or test on out-of-sample data. It's the same thing as trading the system in real-time. After this process is complete, you find yourself with another truckload of disorganized coupons.
Besides the programming skills needed to do the basics, you need a decent grasp of statistics, good organization skills (where did I put that file on the Coffee market between 1998 and 1990?), and the basic understanding of how markets work. Understanding markets is crucial, as many nerdy statisticians with excellent programming skills have proven by losing large sums of money. Don't be afraid of having to learn a lot of stuff, though. Trading is not for the lazy.
Labels:
system development
Thursday, October 8, 2009
Is your equity curve underwater this year?
Is your equity curve underwater so far this year? Okay, you may be a loser, but you can still sing a very fine tune. It's not the same as singing joyfully and proudly about your ability to follow a trend, but it's close.
Are you related to Perfect Profit?
Okay, by now you've probably discovered you are not the perfect trader. You are but a fraction (if that) of the money-banking machine. He is no longer your friend, this creature known as Perfect Profit is your competition. Now that you've formally met your competition, what is the family resemblance between your equity curve and Perfect Profit's equity curve? Are you brother and sister, second cousins or do you need to go all the way back to Tiktaalik roseae to find a common gene? On the practical level, what is the correlation between your equity curve and Perfect Profit's equity curve?
The formula for correlation is actually quite simple. I could derive it for you, but this is not high-school statistics, so I'll just show it below:

It's the best depiction I found on the Internet. Look at the bottom part of the equation and just substitute that gobblygook with standard deviation of X and standard deviation of Y and you have a much cleaner looking equation.
Oh, yawn. Where's the stock tip? I need to know what stock I need to buy tomorrow so please, stop with the algebra already.
These highly trending equity markets will not sustain themselves indefinitely, and you need to start thinking about the range-bound scenario that may happen in the next couple years. For that you need a system. And I offer you a metric now to compare your system's equity curve with Perfect Profit's equity curve.
What does a high correlation indicate? Well, that you are extracting profits (albeit at a slower pace than Perfect) in-line with what your competition is doing.
What does low correlation indicate? Your system is not achieving its destined returns based on what the market offers.
If your system has low correlation with your ultimate competitor, Perfect Profit, then you probably are just lucky right now. There is nothing wrong with being lucky and being rich, but consider your fortune as a potential predicament.
The formula for correlation is actually quite simple. I could derive it for you, but this is not high-school statistics, so I'll just show it below:

It's the best depiction I found on the Internet. Look at the bottom part of the equation and just substitute that gobblygook with standard deviation of X and standard deviation of Y and you have a much cleaner looking equation.
Oh, yawn. Where's the stock tip? I need to know what stock I need to buy tomorrow so please, stop with the algebra already.
These highly trending equity markets will not sustain themselves indefinitely, and you need to start thinking about the range-bound scenario that may happen in the next couple years. For that you need a system. And I offer you a metric now to compare your system's equity curve with Perfect Profit's equity curve.
What does a high correlation indicate? Well, that you are extracting profits (albeit at a slower pace than Perfect) in-line with what your competition is doing.
What does low correlation indicate? Your system is not achieving its destined returns based on what the market offers.
If your system has low correlation with your ultimate competitor, Perfect Profit, then you probably are just lucky right now. There is nothing wrong with being lucky and being rich, but consider your fortune as a potential predicament.
Labels:
correlation,
perfect profit
Wednesday, October 7, 2009
How close are you to perfect?
Becoming the perfect trader is no easy task, and I daresay that nobody has been able to achieve this great feat. The perfect trader buys at the absolute low of the day and sells at the absolute high. And depending on whether the high happens first or the low happens first determines if he is long or short for the day. It's really easy to calculate this metric. It is simply the absolute value of the daily range or the high minus the low.
To get an accurate handle on the concept, we first ask what time frame we will be using. For end-of-day traders or swing traders, the daily bar is your competition. If you day-trade off the five-minute bar, well then use the five-minute bar to gauge your performance.
This concept is used in system trading and because vast amounts of data are typically used, we system traders need to resort to our super-duper calculators, also referred to as our programming language. This is an example of what code looks like for our daily perfect trader. The language is TradersStudio's version of Visual Basic, but you can get the idea and use it with any program you wish. It's the basic loop function.
For i = FirstBar to LastBar step 1
Next
PerfectProfit = PerfectProfit + Range [i]
You'll need to dimension your PerfectProfit variable as an array if you want it to tally up daily ranges.
But you don't need to be a systems trader to get some use from the concept. You can simply take a sheet of paper and tally the sum of all the bar ranges for your time frame. Take your net profit and divide it by perfect profit for a sobering assessment of how far from perfect you really are.
If you are 20% of perfect, then you have an impressive system. Don't be discouraged to find out you're more in the 2% range. What they say about flyfishing applies. First you try to catch a fish (profitable trade), then you try to catch a lot of fish (consistently profitable), then you try to catch big fish (participating in large moves) and then you seek the fish that has never yet been caught.
Good luck fishing.
To get an accurate handle on the concept, we first ask what time frame we will be using. For end-of-day traders or swing traders, the daily bar is your competition. If you day-trade off the five-minute bar, well then use the five-minute bar to gauge your performance.
This concept is used in system trading and because vast amounts of data are typically used, we system traders need to resort to our super-duper calculators, also referred to as our programming language. This is an example of what code looks like for our daily perfect trader. The language is TradersStudio's version of Visual Basic, but you can get the idea and use it with any program you wish. It's the basic loop function.
For i = FirstBar to LastBar step 1
Next
PerfectProfit = PerfectProfit + Range [i]
You'll need to dimension your PerfectProfit variable as an array if you want it to tally up daily ranges.
But you don't need to be a systems trader to get some use from the concept. You can simply take a sheet of paper and tally the sum of all the bar ranges for your time frame. Take your net profit and divide it by perfect profit for a sobering assessment of how far from perfect you really are.
If you are 20% of perfect, then you have an impressive system. Don't be discouraged to find out you're more in the 2% range. What they say about flyfishing applies. First you try to catch a fish (profitable trade), then you try to catch a lot of fish (consistently profitable), then you try to catch big fish (participating in large moves) and then you seek the fish that has never yet been caught.
Good luck fishing.
Labels:
perfect profit,
system development
Thursday, October 1, 2009
Rusty Apple
The main objective of system trading is to create a trade system that has predictive value and can make money for years to come. If a system historically doesn't make money, it's not worth pursuing. Or is it? In the curious world of trade system development, some have stumbled upon good trading systems by fading their original idea, which performed so abysmally that you couldn't lose that much money unless you started throwing it out the window as fast as you can. System trading can also provide other insights into market behavior that, if not yielding a tradeable system, can at least give you some ideas about developing one. From the twitter world, I got the following idea from AshRust: How about each time the VIX spikes X% buy AAPL, then sell after 5/15/30 days w/ 10% stop loss? It's an interesting idea that's contrarian and uses intermarket analysis to trigger a long trade. Does it work? Can you trade it? Or is it nothing more than a curious data-mined notion?
My first impression is that it's not an idea one would actually trade real money with. But is it an interesting factoid? Well, to pursue the idea, I quickly coded the idea (I use TradersStudio) with three parameters that include VIX Spike, N Days to Hold and Percent Stop Loss. Preliminary research showed that fine-tuning a stop loss percentage was not all that meaningful, so I focused on varying only the amount of VIX spike that would trigger the trade, and the number of days to hold the long Apple stock. Since I'm not developing this idea as a trade system, I did not protect any data from the perils of optimization's curve-fitting process. I didn't even really backtest the idea since I'm not sure what parameter sets would make sense but instead ran straight into optimization. The period is from October 1, 1999 to October 1, 2009 (10 years). I stepped the N days to hold from 2 to 30 days in chunks of 2 days, and the VIX spike from 2% to 20% in chunks of 1%. That's 285 permutations that yields the following space with respect to profitability (above zero line is profitable):

Of all the permutations, 41% were profitable. Hmmm, maybe we can improve on the idea. What if no trades were taken when there was a positive correlation between AAPL and VIX? After all, it is a contrarian trade idea. Well, a little line in the code does the trick and the following optimization space is the result:

It yields less trades with the filter (obviously) but there is also a decrease in the percentage of profitable parameter sets with the total coming to 36%. Oh well, at least now we know. Anyone who made money using this system over the past ten years did so on the basis of pure luck. If , however, you have a time machine that can go back to 1999, I suggest you buy APPL whenever the VIX spikes at least 4% and hold the long stock for 2 days. You will have been a genius and could brag about an annualized return of 47% over ten years.
My first impression is that it's not an idea one would actually trade real money with. But is it an interesting factoid? Well, to pursue the idea, I quickly coded the idea (I use TradersStudio) with three parameters that include VIX Spike, N Days to Hold and Percent Stop Loss. Preliminary research showed that fine-tuning a stop loss percentage was not all that meaningful, so I focused on varying only the amount of VIX spike that would trigger the trade, and the number of days to hold the long Apple stock. Since I'm not developing this idea as a trade system, I did not protect any data from the perils of optimization's curve-fitting process. I didn't even really backtest the idea since I'm not sure what parameter sets would make sense but instead ran straight into optimization. The period is from October 1, 1999 to October 1, 2009 (10 years). I stepped the N days to hold from 2 to 30 days in chunks of 2 days, and the VIX spike from 2% to 20% in chunks of 1%. That's 285 permutations that yields the following space with respect to profitability (above zero line is profitable):

Of all the permutations, 41% were profitable. Hmmm, maybe we can improve on the idea. What if no trades were taken when there was a positive correlation between AAPL and VIX? After all, it is a contrarian trade idea. Well, a little line in the code does the trick and the following optimization space is the result:

It yields less trades with the filter (obviously) but there is also a decrease in the percentage of profitable parameter sets with the total coming to 36%. Oh well, at least now we know. Anyone who made money using this system over the past ten years did so on the basis of pure luck. If , however, you have a time machine that can go back to 1999, I suggest you buy APPL whenever the VIX spikes at least 4% and hold the long stock for 2 days. You will have been a genius and could brag about an annualized return of 47% over ten years.
Labels:
Rusty Apple,
system development
Subscribe to:
Posts (Atom)