Feb 1

A couple articles in the February 2007 TAS&C caught me eye. One of them talked about a method to attempt to predict a simple moving average crossover the bar before it happens. Basically, they figure out what the price would have to be in the next bar, in order to make the MAs cross. Then, they compare the needed price to the current price. If the MAs would cross even if the stock remained unchanged in the next bar, they predict that it will happen.

I liked the idea, and made a few enhancements while playing with it:

  1. I extended the technique to exponential moving averages.
  2. I changed the indicator to a +1/-1 oscillator, so it’s easy to read (the indicator in the magazine had a huge range, and you had to watch for it to cross the stock price)
  3. I added a way to make the indicator tunably optimistic, if you wanted earlier warning at the cost of more false alerts

If you want to know how it all works, and was derived, you can read the Mathematica notebook I used. I tried to explain things as I went so it’s easy enough to follow. I’ll put some of the graphs from that file below.

Here’s some (obviously extremely fake) price data, with two moving averages. The fast MA is the red line, and the slow MA is the blue line.

I zoomed in on a couple of the crossings, and superimposed the oscillator in green on top of it. You can see that it tends to go from positive to negative (or vice versa) the bar before the crossing actually happens:


The Formulas

For EMAs, the formula for tomorrow’s needed price for crossover is:

.. where ‘f’ and ’s’ are the ‘fast’ and ’slow’ ema periods, respectively. If you plug in 8 and 20, you’ll get (57ema[20] - 49ema[8]) / 8.

For SMAs, the formula is:

Note that for SMAs, you wind up doing the computation with faster MAs than the ones you are predicting. For instance, the formula for 8 and 20 SMAs is: (38*MA[19] - 35*MA[7]) / 3. So, for 8 and 20, you end up using 7 and 19. The reasons are explained in the Mathematica file.

MA Crossover Prediction on Real Stock Data

I turned my version of the indicator into a QuoteTracker PaintBar setting. The one I made looks for 8EMA vs. 20EMA crossings. The formulas above tell you how to make settings for other MAs.

Here’s an example from the daily chart for SLAB:
slab daily

The 2 ema’s are on this chart in red and blue. You can see that the crossover was correctly predicted, and that going long the day of the trigger would have worked pretty well (although in this case there really wasn’t much advantage in getting in early). That’s not to say I recommend jumping in front of MA crossovers as your complete trading system! If you turn on the paintbars, and look at lots of charts, you’ll see that it is a pretty standard mix for a trend-following system… lots of little losses and occasional huge wins. Using other indicators and chart patterns to inform the trades would probably increase the win rate substantially.

The other thing you’ll notice when you look at lots of charts, is that it often predicts a big move, but in the wrong direction. I haven’t done any thorough analysis, but I think this is more likely when the price is very close to the MAs. So, instead of pushing through and causing the crossover, the price is repelled and runs the other way. Here’s an example of this:
sirf wrongway
The indicator predicts a down crossover on 8/9 and 8/11, but the crossover doesn’t happen, and instead there’s an explosive move the other direction. Again, other indicators or chart patterns would help make more informed trading decisions. Or, it seems just taking an early loss on the short trade and going long would be profitable in most situations like this.

Here’s an example of the indicator applied to a 15-minute chart:
slab intraday example
You can see two triggers on this one (the red and green box at the bottom). The first you’d likely scratch or take a loss, and the second would make some money. Sorry, the 2 EMAs aren’t on that intraday chart (I don’t like messy intraday charts), but I did confirm that both crossover predictions were correct in this case.

The Paintbar Code

Here is the code for the “conservative” version of the paintbar code, which matches the TAS&C article’s approach:

Rule 1:
if (EMA(8) < EMA(20)) AND ((57*EMA(20) - 49*EMA(8))/8 <= Bar Close) AND ((57*EMA(20)[1] - 49*EMA(8)[1])/8 > Bar Close[1]) set color to Lime and stop

Rule 2:
if (EMA(8) > EMA(20)) AND ((57*EMA(20) - 49*EMA(8))/8 >= Bar Close) AND ((57*EMA(20)[1] - 49*EMA(8)[1])/8 < Bar Close[1]) set color to $2B2BFF

…. and here is the code for the “eager” version, that is more optimistic about possible crossings, but also cries wolf more often:
Rule 1:
if (EMA(8) < EMA(20)) AND ((57*EMA(20) - 49*EMA(8))/8 <= (Bar Close + 0.5*Average True Range(14,3).Signal)) set color to Lime and stop

Rule 2:
if (EMA(8) > EMA(20)) AND ((57*EMA(20) - 49*EMA(8))/8 >= (Bar Close - 0.5*Average True Range(14,3).Signal)) set color to $2B2BFF

You can tune the optimism by adjusting that 0.5 factor on the Average True Range. This basically says, assume it will cross if the crossing point is within half an ATR of the current price. The “conservative” version assumes a cross will happen if the crossing point is at the current price, or better.

Jan 28

If you are trading with a goal of maximizing your long term profits, you should be concerned with maximizing your expectancy. But, if you are like me, and you want to keep your electricity on via trading stocks, expectancy isn’t the right measurement to use. In this article, I’ll explain how to gauge your consistency. In other words, what percentage of your days/weeks/months can you expect to be profitable? This is useful if you are designing a system, and also useful if you want to know more about the system you already use.

Note: Many of the ideas in this article are largely derived from the first 6 pages of this elitetrader thread, started by user Acrary. I’ve tried to expand on that material with more detail and graphs, to make it easier to understand. Plus, it’s a lot more fun to read without all the elitetrader noise…

Expectancy: the Premier Profitability Measure

If you want to know if your trading system is profitable over the long haul, you want to know about your expectancy. You can find lots of articles on the web about expectancy, so I won’t spend long explaining it here. Briefly, it is computed as:

Expectancy = win_rate * avg_win - loss_rate * avg_loss

For the purposes of this article, I am going to ignore exact breakeven trades. This means that the loss_rate is directly related to the win_rate, and expectancy becomes:

Expectancy = win_rate * avg_win - (1 - win_rate) * avg_loss

A positive expectancy means that if you took an infinite number of trades, you would have more money than you started with when you finished (assuming, of course, that you didn’t bust your account during a bad drawdown–see this article for more about the risk of ruin, and this article for more about comparing the risk at different account sizes). Similarly, a negative expectancy means you’d have less money after an infinite number of trades, and a 0 expectancy means you’d break even.

That’s great, but when’s the last time you made an infinite number of trades? :-) What I’d really like to know is, am I going to make money most weeks? months? years? Profitablility and consistency are not the same thing!

Consistency Graphs

So, to get started, let’s look an example consistency graph for a system with positive expectancy. There’s going to be a lot of graphs like this one in this article, so let me explain what they mean.

The horizontal axis describes a number of trades taken during a given time period. The vertical axis tells what percentage of those time periods will be profitable, given that you took that number of trades. This was computed via a monte-carlo type analysis, with 2000 trials per dot on that chart.

So, let’s say you trade the depicted system, and you tend to trade 20 times a month. This graph tells you that you will be profitable around 95% of months you trade. If you trade 30 times a day, then this graph tells you that you will be profitable about 97% of your trading days. If you trade 10 times an hour, then you will be profitable around 85% of the hours that you trade. So it works on any timeframe… you get the idea by now.

See how the graph starts out in the 70% range, and converges on 100% consistency as the number of trades goes up? In general, all graphs of positive expectancy systems look like this, in that they start out near the win rate % consistency, and converge on 100% consistency. The width of the path, and the speed of the convergence will vary.

Now, let’s look at a graph for a system with negative expectancy:

Pretty much the inverse of the positive expectancy chart. It basically says, the more you trade, the less chance you have of being profitable. As an aside, this is exactly why your best bet at a roulette table is to bet everything on one round. Roulette has a negative expectancy for the player, so the more you play, the less consistent your profits will be.

Finally, if your system has an expectancy of 0, the graphs all look like this:

… as the number of trades increases, the consistency % forms a band around 50%. Makes sense.

Effect of Expectancy Size on Consistency

So, by now, you should know that a positive expectancy is necessary both for long term profits, and consistent profits. You might wonder if a larger expectancy system will be more consistent than a smaller expectancy system. The answer, which surprises a lot of people, is: no.

Here are consistency graphs for a range of expectancies from $40/trade to $440/trade. They are only marginally different:

If you think about the meaning of expectancy, you will realize that the $440/trade system will make a lot more money than the $40/trade system. But, for any given set of trades, expectancy is clearly not the aspect of the system that governs the consistency of the profits. We’ll just have to keep looking….

Effect of Win Rate on Consistency

Well, surely, if my system wins more often, I will have more consistent profits? It turns out, winning more often increases expectancy, but does not necessarily do much for consistency of profits. Here are several consistency graphs for systems with increasing win rates. You can see that, after you get above 10 to 15 trades per day/week/month/whatever, the graphs all look about the same.




It makes sense that the win rate would have sway over the answers when there are fewer than 10 trades per trial. So few inputs go into the profitability calculation, that a little luck in either direction changes the answer. Also, at the extreme end, any 1-trade-per-trial run will have a consistency % equal to the win rate (since the single trade is profitable at exactly its win rate).

Effects of Profit Factor on Consistency

Now, if you are an astute reader, you’ve noticed that in the preceding two sections, each graph had a constant “Pf” label at the top. And, since each set of graphs had fairly constant conistency, you might conclude that this “Pf” is what really gauges the consistency of a trading system. You’d be right.

“Pf” stands for “Profit Factor.” The equation for it has the same terms as the expectancy equation, arranged differently:

Profit Factor = (win_rate * avg_win) / (loss_rate * avg_loss)

Since the equation is so similar, you can see by simple transformation that the profit factor will be 1 whenever the expectancy is 0:

  • (win_rate * avg_win) / (loss_rate * avg_loss) = 1
  • win_rate * avg_win = loss_rate * avg_loss
  • win_rate * avg _win - loss_rate * avg_loss = 0

Similarly, the profit factor will be greater than 1 whenever expectancy is greater than 0, and less than 1 whenever expectancy is less than 0.

Here are some charts of systems with increasing profit factors. They are randomly-generated systems with respect to win_rate, avg_gain, and avg_loss… the only thing I’m controlling is that the profit factor of each is rising. It’s easy to see that the profit factor is highly correlated with speed of consistency convergence of a system, and that higher profit factors require fewer trades per period to give a good guarantee of consistency.











If you think the graphs from 1.5 to 3.5 look very similar, check the y-axis! The convergence is getting much faster! (On some of these graphs, Mathematica cut off the early numbers, because it converges on the 99% range so fast that it decided to blow up the 99-to-100% range. Recall that the point for 1 trade-per-period will always be very close to whatever the win rate happens to be, no matter how fast it converges after that.)

Reading that last chart, you can see that if you find a system with a profit factor of 6, and you can trade 15 times a day with it, you will make money 99.95% of days you trade. That means you will have a losing day once every 10 years, if you trade 200 days a year. Oddly enough, I bet you feel pretty bad that day… so try not to let it throw you off! :-)

These trials confirm the advice given in the elitetrader thread, which gives the following guidelines for trading frequency versus profit factor:

Profit Factor # Trades Needed
for 95% Consistency
1.5 60
1.75 40
2.0 30
2.5 20
4.0 10

By looking at my graphs, you can see what the guidelines are for any level you want to target, for profit factors from 1.5 up to 6.

Simplifications

I already mentioned that I ignored breakeven trades. This has only a marginal effect on the outcome, while simplifying my job a bunch. If you make 10 trades a day, and 9 of them are exactly breakeven, then maybe you should do some mental translation when using graphs like these. If your truly breakeven trades are relatively rare (like they are for most people), then you will be fine.

I also assumed during the random trials that a win is always the avg_win, and a loss is always the avg_loss. I could have been more exact by making a probability distribution of wins and losses, via a standard deviation from the averages. Assuming the variance in the wins and losses is not very large, doing this wouldn’t affect the message of this article, and would just make the graphs a bit more noisy. And, I could eliminate that noise by ramping up the number of trials per dot up from 2000 to more like 1,000,000 anyway. So, I didn’t bother. However, if your trading is all over the place, then you could have much wider swings in your profitability than depicted here (especially if you don’t trade often). Do try to keep the variance in your gains and losses as small as possible, if only because it makes it easier to reason about and predict your own performance.

Conclusions

So, here are some guidelines you can take away from all this.

If you are designing a system for consistency:

  1. Maximize the profit factor.
  2. Double-check your win rate to make sure your risk of ruin is acceptable
  3. Trade as many times as you can, within the parameters of the system.
  4. Get the variability of your returns under control (using something like the modified sharpe ratio, for example)
  5. Tune the expectancy (or add additional systems) in order to make enough money.

If you are designing a system for maximum profit:

  1. Maximize the expectancy.
  2. Double-check your win rate to make sure your risk of ruin is acceptable
  3. Trade as many times as you can, within the parameters of the system
  4. Use the profit factor to frame your month-to-month profitability expectations. No reason to be down on yourself for a losing month, if your system should only win 60% of all months!

If you already trade a system, and you want to be more consistently profitable:

  1. Find more trades to take within your system’s parameters. This doesn’t mean you start taking questionable trades, just to get the count up. Instead, you have to be more efficient about finding and exploiting opportunities to trade.

(this is why you’ve seen me post every now and then about needing to find more trades to take… it’s the only way I know of to be more consistent, without changing my system!)

If you’d like to see the Mathematica notebook that I used for this investigation, you can read the html version of it.

Jan 24

So, based on my TICK article, one would guess that the majority of randomly-chosen market situations would have low TICK values (since there are so many more ways to get a low TICK than a high TICK).

But, how low is “random low”? If you chose 1 million random markets, and calculated their TICKs, what percentage of them would have a TICK of 1000? 500? 300? Since we are talking about random selection, there is a non-zero probability that you’d hit the maximum value. But, I wouldn’t hold my breath…

Knowing where the distribution of random TICKs trails off would be valuable, since it would make a sensible absolute lower threshold for paying attention to the indicator in actual trading. I mean, you really only want to pay attention when the TICK is pointing out a non-random situation!

Today, since I didn’t have any trading prospects, I thought I’d take a minute and find out what that level might be. Here is the distribution of TICKs for 1 million randomly generated 3000-stock markets. The raw result:

… and the prettier histogram version:

As you can see, the vast majority of answers came out in the +/-150 range, while some answers ventured out toward +/-220. None of the million markets got a value in the 300 range. Of course, if I generated enough markets, eventually I’d make one with a TICK of 3000, but due to the normal-looking distribution in the histogram, that would take a long, long time.

So, no matter what, never base any decisions on TICKs smaller than 250. Maybe, if you are trading the index that underlies the TICK, you could justify watching values in the 300 to 500 range. If your position is in a single sector or stock, then it still makes sense to me that values in the 500 to 600 range are the first notable TICKs (because there has to be a broader market force at work to really affect you).

If you are curious, you can check out the html version of the Mathematica document I made. Mathematica’s great for playing around with little ideas like this.