Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Strategy optimization

Hi all,

I have been trading using mechanical systems since the last few years on my own account. I started by trying various technical analysis indicators but found that most of them don't make money directly and then moved on to stat arb. Quite a bit of edges have been reduced to almost zero if not gone entirely. However, a few changes to the original unprofitable strategy can turn it into a very profitable one. A flawed strategy by itself is unlikely to make money no matter what you do, but I've found that good performing strategies stop working and some bad performing strategies perform very well at times. This is a (long) collection of what I learnt and hopefully will save you some time when you experiment :)

Any strategy can be broken down into

  1. Entry conditions
  2. Risk management
  3. Exit conditions
  4. Portfolio construction

  5. If a significant number of positions are not showing a Mark to market profit at any point until they are closed, then the entries are either too early or too late. Entry conditions should be either based on a model or data. When you trade on a model, remember that it is ultimately a representation and is unlikely to ever be accurate 100%. Putting too many parameters in a model will make it harder to debug and take longer to fix when it breaks down. Data based strategies have the risk of a variety of biases. It is essential that if you are using patterns in data to trade, there should be a sound reason for the pattern to stay consistent over time.

  6. Keeping stop losses too close to the entry, or too wide from the entry point will result in hitting the stops frequently or big drawdowns. I think risk management matters more to the overall Pnl in the long run than the specific strategy. Trading using algorithms is a great way to get rid of emotions affecting the trade. A stop loss should not be placed based solely on risk tolerance or performance constraints. If you can tolerate a maximum loss of 5%, keeping the stop at 5% regardless of the strategy or security is a bad idea. Consider the hypothesis for the trade. You should ideally exit a position only when the reason for entering the trade has been invalidated. So this could be 2% below the entry price, or even 20%. After considering this, size your trade size accordingly so that the stop loss is still within risk limits. Volatility should also affect your stop. As an example, oil futures typically move 2-3% on a volatile day. Currency futures rarely move 3% a day. Keeping the same stop for both is not optimal. This is especially true for stocks.

  7. I think most strategies fall ultimately into two categories- direction based and value based. If the strategy makes money betting on the direction of something, it is direction based. If you trade a spread or bet on the fair value of some security being a specific value, it is value based trading. The exit strategy should ideally be separable from the entry. If you have separate entry and exit strategies, you can then mix them to create new strategies.

  8. Portfolio construction is running your strategy on multiple securities or building a portfolio of multiple strategies. Since some strategies perform well at some times and others poorly, by combining the strategies into a weighed portfolio, you can reduce the volatility and smooth the returns. A simple way to do it would be to use correlations between the strategies. Put together different strategies that are uncorrelated to each other in a portfolio.

Hope this helps!

12 responses

@Prathamesh,

Thank you for the share! Point #8 above really struck me, and I'd love to wire up an example. Is there a canonical example of two offsetting strategies that would really illustrate your point?

Taking points 7 and 8 together, I wonder if you see a way for us to make this easy in Quantopian. @Sanz has a good multi-strat example here: https://www.quantopian.com/posts/multi-strategy-example and he's working on making one strat a signal and the other an entry/exit. Maybe we can reformulate his example to split entry and exit?

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

I have similar experiences regarding flawed strategies. Thanks for sharing your thoughts.

@Prathamesh,

+1. Valuable advice. Similar experience trading my own, too.

@John
" I wonder if you see a way for us to make this easy in Quantopian"
I will try to extend my multi-strategy example but I thought I 'll share some thoughts for further development since others could code them better that I can ( I am learning as I go)
A friendly environment would:
1. "Fit" a strategy into a structure. The simplest form is to have inputs for
a. Instruments to be traded by the strategy
a Parameters that initiate a version of this strategy (.e. period for a moving average)
b,Buy Rule (that uses these parameters)
c.Sell rule
2. "Hide" the strategy under a structure and represent it just like any other asset/stock : A time-series of its equity. Like a stock, it has returns, stdevs, correlations to other assets, etc. It's just another time-series
3. Be able to buy or sell these strategies (or "allocate" money to them" ) and built a portfolio from them.
The idea is to be able to buy
20% TrendFollow(LargeDJISTocks)_Strategy,
30% MeanReversion(myVolatile_Assets)_Strat
and 50% APPLE

Just sharing my thoughts...

@Sanz, thanks for the ideas!

For 1b, 1c, could you provide an example of the rule? Or would these be functions you pass into the strategy?
2 and 3 sound pretty awesome. I think you could prototype this in a script today, and it would really help illustrate the benefits of the design.

@John

Rules could be anything as simple or as complicated as needed, as long as it returns a boolean. Could be a function, too.
buy=closemavg(5)
I will try to implement a script. I am getting runtime errors passing parameters to data[cur_sid].mavg(param).

@Sanz, sorry, but we have a limitation right now that prevents parameters being passed to the transforms (mavg, stddev, returns). We pre-parse the code to find the transformations you need, and then configure the simulation to include just those. Our current parsing requires a primitive be passed into the function.

No problem. Will do without for now. Thanks for the response.

@John, regarding Prathamesh's 7 and 8 and Sanz's 2 and 3, it seems that quantopian would need a way to run a strategy in a sandbox that runs without affecting the positions of the master strategy and allowing it to yield the equity as a series as it tracks it's own equity, positions, etc., and then trigger orders as a percentage of the sandboxed strategies' positions. Maybe "tracker strategy" is a better way to put it.

Without sandboxing, and to use what's already available on quantopian, one way of course is to just run a strategy at an absolute minimum size (whatever makes sense for that instrument and strategy) but make it so it can track it's performance regardless of the allocation. Once it's "active" just bump up the traded sizes to match the requested allocation amount.

Thinking through it, perhaps these tracker strategies are for tracking performance purely and provide access to their equity or whichever values are needed to make decisions. If these trackers could broadcast events like position_size_delta a "trader" strategy could subscribe to these, take the requested allocation percentage or value, figure out the shares needed based on the trackers and adjust as the tracker strategy makes changes to its own positions. Is it possible to do simple events or perhaps pubsub within a strategy?

Ah rather some sort of a message queue. Trackers could pop their messages onto the queue, the actual trading strategy would be last to process in the handle_data to see what's needed and act accordingly. That's simpler, too, I would think.

@Michael, I'm thinking something that takes advantage of the generator internals of the simulation. Have you looked at http://docs.python.org/2/library/itertools.html before? You can see how a simulation is assembled from generators in the zipline source. Maybe it will spark some more ideas for you.

I am only superficially familiar with itertools but that will be changing now. That's a great set of recipes.

I was going to ask how much of the internals are accessible within a simulation, until I realized the help docs show zipline as a module we can import into a simulation. And this is why Sanz's 2 and 3 can be prototyped today.

(some time later) Been going over the source and also stepping through it.

This certainly spawns ideas like Sanz's adaptive strategy post from the blog. I will have to ponder it a bit as I wrap my head around it.

thanks for share your thoughts