Back to Community
Automatic code generation for Quantopian

We are pleased to announce that Price Action Lab has developed automatic code generation capability for the Quantopian platform and that will allow our customers to join this community and take advantage of the functionality it has to offer, such as generating more accurate backtests, developing live trading systems and participating in contests and profit sharing in case a profitable strategy.

The strategy code in the attached backtest was generated by Price Action Lab, a program that identifies price patterns that fulfill user-defined performance criteria and risk/reward parameters. The in-sample for the identification was from SPY inception to 12/31/2008 and the results were first published in an article on April 6, 2015. The Quantopian backtest shows realistic backtest results in the out-of-sample from 01/02/2009 to 08/15/2016 based on 1-minute data with the code that was automatically generated by Price Action Lab.

There are 30 price patterns in the strategy, 23 long and 7 short. The patterns were identified in daily bars. The profit target is 2% and the stop-loss is at 4% and in the Quantopian code they are monitored in 1-minute bars. This particular strategy uses only the close of daily OHLC bars. Although the performance is lower than buy and hold, it is out-of-sample performance and free from look-ahead bias. Note that this system is just an example and it is not recommended in its present form for live trading but something similar could serve as a foundation for further development.

Price Action Lab users can just copy and paste generated code in an algorithm in Quantopian. We will continue improving the code generation of Price Action Lab based on feedback received. This is the first version, it has been challenging and the code is basic but it offers a good starting ground.

Edit on 07/23/2018: backtest and code were removed due to being outdated.

24 responses

Cool. Does the demo include this compiler?

Yes, this is really cool.

Clever stuff Michael.

Michael
I have read and enjoyed a lot of your articles in the past but never really looked at PAL. I see a lot on your website about what PAL is NOT: eg neural networks. Can you tell us a little bit more about what PAL IS?

Or is that difficult to do without giving away proprietary information?

Actually as phrased that question sounds weak and lazy. I can see in general what it does (just not HOW it does) - perhaps further reading of your website will answer the question.

Anthony,

PAL employs a proprietary deterministic algo that identifies price patterns in OHLC data that fulfill user-defined criteria (win fraction, consecutive losers, profit factor, etc.) There are no other parameters except OHLC. PAL has three functions: search, scan and p-indicator.

The search function can be used to develop trading strategies. The developer should try to minimize selection bias using the tools available in the program and proper validation methods. Here is where code for popular platforms, including Quantopian, is generated for complete strategies.

The scan function can be used to identify short-term price action anomalies. There is an advanced portfolio backtesting option for attempting to minimize selection bias. Here is an example: http://www.priceactionlab.com/Blog/2016/03/data-mining-price-anomalies/

The p-indicator function measures short-term directional bias of single securities and of groups of securities, such such indexes. This can can be used in many different ways. The P-Dow indicator is just one: http://www.priceactionlab.com/Blog/2016/02/procedure-for-generating-weekly-p-dow-indicator-results/

There are no pre-programmed patterns in PAL but the algo to identify price patterns is deterministic (no random input).

Best.

Here is a list of a few related articles from PAL blog: http://www.priceactionlab.com/Blog/quant-trading/#pal

The ability to quickly generate code looks amazing! If anyone decides to pick up a license, let us know how intuitive it is to develop strategies with!

Are you planning on offering any sort of specials or incentives to users on Quantopian? My wallet/heart broke a little bit when I compared the cost of a license to the total value of my portfolio.

Delman,

it is best to try the demo to determine whether the program suits your needs and then contact PAL for any specials and discounts. I do not handle sales and also this forum is not the proper place for that I think you would agree to that.

Michael,

Agreed. Does the demo include the Quantopian compiler, so that we may use it to generate independent backtests when evaluating?

I have just used the code for a DIA strategy from this blog post to enter the contest. There is also a brief discussion of data-mining and selection bias in that article.

The strategy was developed by Price Action Lab with in-sample data from DIA inception to 12/31/2009. The out-of-sample performance in the Quantopian platform using the code generated by Price Action Lab shows 147.5% total return versus 121.6% for the benchmark (SPY). Sharpe is 1.78 in the out-of-sample, resulting in a t-statistic of 4.54, which is exceptionally good.

Simon, No compiler and the demo does not generate the full code (a few details are masked by *) otherwise it would not be a demo but a free program. But it takes few seconds to copy and paste the generated code in a new algorithm in Quantopian (indentation taken care of.) The purpose of the demo is to get an idea of how strategies based on price patterns can be generated and evaluated. The evaluation part is fully functional in the demo. Just the code is not fully available.

I have added another strategy to the contest. It was generated by Price Action Lab in 20 minutes. Here is a Twitter post with out-of-sample performance. Sharpe in out-of-sample is 1.27, resulting in a t-statistic of 3.23. Strategy trades SPY short-term.

I think I am right in saying to qualify in the Quantopian competition an algo has to be hedged by running both long and short positions at the same time. Trading one instrument I am assuming you will be net long or net short at any given time.

As a follow up question, what is your personal aim using PAL for stock trading? Or perhaps your recommendation? There seems to be a refreshing realism in many of your posts: the DIA and SPY strategies look to be aiming to equal an index return with slightly lower volatility and draw down? Is this your personal take on algorithmic trading? Or at least the type of probabilistic algorithmic trading promoted here. Rather than the arbitrage or structural trading practised by the HFT industry.

The lunatic fringe is always keen to shoot the lights out using leverage but that seems happily absent from your style.

I must say Michael I am also baffled as to why trading the patterns discovered by PAL (or indeed any other method such as ANNs) should be SO different from the sort of market timing practised by the CTAs based on momentum.

Momentum

Philosophically speaking I wonder if the two methods have, in fact, a lot in common? A "trend" is merely a pattern observed in past data which the CTAs hope will re-occur in the future.

I am particularly interested by the following statement:

Finally, I have never said that market timing does not work. It works for those few who do it correctly. Note that market timing is an anomaly and must be treated as such.

Who are " those few who do it correctly" and how should it be done correctly?

If people on this forum want to time the markets by going long the SPY when it is going up and in exit to cash when it is going down what is the correct method to use? Bearing in mind perfect timing is impossible.

Would I be right in saying that perhaps any sort of probabilistic trading is a futile exercise in the long term (including PAL?) and that those who "are aware of this problem hope that they will manage to retire before they hit uncle point" ?

Let me be absolutely clear: you may well be right about momentum over the longer term but if so, I am puzzled as to why PAL's pattern searching is any more valid. In particular we both know that market data can look alarmingly similar to random sequences. Perhaps markets are a true random walk, mere brownian motion. Although it is difficult to reconcile that with the performance of stock indices over the past 100 years. And of course stock indices are a form of momentum investing. You may well argue however that 100 years is nothing and that it may just represent a fluke. And you may be right: mean reversion may well rule in the long term as the US empire inevitably declines, as have all other past empires, taking its stock market with it.

Please excuse the meandering and abtruse musing. My real point is that perhaps the HFT guys are the only ones who are actually correct. They take advantage of the bid offer spread. Their algos sniff out large orders which they can deal ahead of. They receive (apparently) inside information.

In short those guys have the dice heavily loaded in their favour. They don't really trade on probability at all. I'm not at all sure that pattern traders have anything loaded in their favour. Whether those patterns are PAL patterns or "trend", or "momentum" patterns.

Anthony,

"I think I am right in saying to qualify in the Quantopian competition an algo has to be hedged by running both long and short positions at the same time. Trading one instrument I am assuming you will be net long or net short at any given time."

I think that has to do with judging of who gets the prize, not qualifying. I am interested in seeing how these algorithms work and not so much about winning a prize. However, you can do that if you wish:

  1. Develop a long system for security XYZ1
  2. Develop a short system for security XYZ2
  3. Trade the system when 1 AND 2 signal

But it is wrong to think that because an algorithm is both long and short at the same time, it is hedged or has less risk. The long security can fall and the short can gain. This has happened many times in the past and pairs traders got wiped out. It is mistake to think pairs trading is a hedge, actually it can be double the risk.

Totally agree!

Anthony,

"strategies look to be aiming to equal an index return with slightly lower volatility and draw down?"

This is the primary goal, higher risk-adjusted returns. But there is a more important motivation: limiting the exposure to market. These systems have 50% - 75% exposure. If I can get higher risk-adjusted returns with half the exposure, this is highly desirable.

"The lunatic fringe is always keen to shoot the lights out using leverage but that seems happily absent from your style."

I was trading for a Hedge Fund in 2008. Instead of using leverage I limited total risk of open positions to less than 2% of my allocated equity. I said to my boss to avoid leverage. He did not. By November he was wiped out. I lost 3 years investment in that fund of hard work because of his actions. Leverage will get you at the end because everyone has a small probability of ruin, this is never 0. Leverage amplifies this parameter, the risk of ruin, when in reality you should try to eliminate it. It is going the other way, inviting ruin.

Anthony,

"I must say Michael I am also baffled as to why trading the patterns discovered by PAL (or indeed any other method such as ANNs) should be SO different from the sort of market timing practiced by the CTAs based on momentum."

because the former is short-term and possibly intraday and the latter is longer-term. See CTAs cannot control tracking error. Momentum is a risky strategy in mean-reverting markets. http://www.priceactionlab.com/Blog/2015/08/the-day-momentum-died/

"Who are " those few who do it correctly" and how should it be done correctly?"

Well, I was thinking about overfitting, selection bias and data-snooping bias, as well as, look-ahead bias. It is not Quantopian's problem or failure but 7 out of 10 algos I have looked at here suffer from look ahead bias and most suffer for selection bias. When you mix a large number of factors with a large number of securities you get overifitting and selection bias. This is why I prefer systems that trade one symbol with one factor. KISS

"Would I be right in saying that perhaps any sort of probabilistic trading is a futile exercise in the long term (including PAL?) and that those who "are aware of this problem hope that they will manage to retire before they hit uncle point" ?"

Yes, as long as the probability of ruin is not zero. Then, everyone will fail no matter the method used. The method is irrelevant. What is relevant is how you do it.

"Let me be absolutely clear: you may well be right about momentum over the longer term but if so, I am puzzled as to why PAL's pattern searching is any more valid."

It is fine, you have perfectly legitimate and intelligent points to make.

I never said PAL is better. I also use momentum systems and they are doing quite well this year, I mean cross-sectional momentum because price-series momentum is a curve-fit in most cases. I prefer PAL because it minimizes the bias. It cannot eliminate it but it is lower. But even with lower bias losses are possible if market conditions change. http://papers.ssrn.com/sol3/papers.cfm?abstract_id=2810170

"My real point is that perhaps the HFT guys are the only ones who are actually correct. "

HFT is modern market making. Market makers almost always profited in the past because they had access to information about order flow.

"They take advantage of the bid offer spread. "

This has still non null probability of ruin if the offer is taken and the bid is pulled. Eventually, HFT algos will destroy each other and a few market makers will remain.

"In short those guys have the dice heavily loaded in their favour. They don't really trade on probability at all. I'm not at all sure that pattern traders have anything loaded in their favour. Whether those patterns are PAL patterns or "trend", or "momentum" patterns."

HFT is part of market microstructure now and they get paid for that exactly. Everyone else has higher non null probability of ruin, probably a few orders of magnitude higher. Most savvy quants have prob of ruin ~ 0.05. For naive quants this goes to 0.5. For worse-than-random traders it is 1 and there are many of those. HFT is down to ~0.001 But it is still non null and something can go wrong.

You have a choice: You can join the HFT group and become a market maker or try to be a more intelligent quant. Both are difficult. The former also requires substantial investment. Chances of success are 95%. The latter requires no investment but chances of success are < 0.05%.

Always enjoy your questions Anthony. I may not have good answers but they are good.

I find it hard to disagree with a word of what you have to say. And I too have only questions, no answers. It may well be (to veer back to my usual pseudo scientific / philosophical mode) that mean reversion is a fundamental physical law of the universe and that all reverts to zero eventually. Before rising, phoenix like, to re-commence the cycle.

We are probably all doomed to making hay while the sun shines and hoping to retire before the weather mean reverts and the rain gets us.

Suns are born, shine for a few billions of years and die. The universe itself may be subject to the same eternal cycles. Why should financial markets be any different?

"We are probably all doomed to making hay while the sun shines and hoping to retire before the weather mean reverts and the rain gets us."

Then move to another country (strategy) and repeat the same. if you know how to anticipate mean-reversion, you have an advantage. This is one reason some profitable funds close down and people wonder why the decision was made. The owners move to another business or another fund and start all over gain. Staying long enough with same strategy and objectives invites ruin.

You may want to take a look at what has happened to CTAs: http://www.priceactionlab.com/Blog/2016/04/cta-woes-continue/

The smart ones moved to HFT or short-term trading. The naive ones still fight mean reversion and ruin.

No one should expect to stay profitable in any business including trading with same old method. If you can discover a new edge every two to three years and you control risk, you can probably profit from the naive ones who do not follow this route. After all, if we exclude QE, markets are mostly a zero-sum game.

Hilarious article and my own trend following models in the futures markets confirm what you say. I ceased trading in disgust when MF Global blew up and probably just as well. Look what happened to Dunn and JWH.

The financial markets are full of prospectors digging for gold. Probably always will be. Better sell them shovels.

I note with interest that the long feted Tudor Jones has not done well in recent years. And of course Steve Cohen is looking to diversify through Quantopian.

The best model however is to sell the punters something: Interactive Brokers have a good line in this. And in the investment world generally of course most people make a living by selling useless products rather than trading for themselves: ratings (Moody's etc), active fund managers (fat fees for less than index performance). And the list goes on.

Sell the punters the rope and let them hang themselves.

What a cynical old misery I am.

Limitations of Quantitative Claims About Trading Strategy Evaluation

No correction for significance and backtest overfitting would have
provided an indication of the coming failure of this strategy

So very true.
And unless it is discovered we live in a purely deterministic universe;
and we know all the variables;
and we have vast computing power;
and our name is Harri Seldon

That is never going to change.

Even in a deterministic universe, lack of knowledge of initial conditions (intentions of all market participants, or at least ,major ones) of very complex financial systems can lead to high uncertainty, equivalent to that of purely stochastic systems.

Indeed. Edward Lorenz.