Programming question regarding mean-reversion trade filtering

Discussion in 'App Development' started by PleasantDays, Jul 23, 2014.

  1. Hello all...

    I'm wondering if anyone here has come up with an elegant solution for filtering out unwanted trades in a mean-reversion automated strategy.

    For example, I use 6 range bars in my trading. I'm in the process of developing an automated strategy around them. One thing I'm having difficulty with, on the creative side, is coming up with an elegant way of filtering out counter-trend trades without killing all the other trades in that direction. I'll explain...

    Let's say I have a fast-flush down trend (morning session on the YM).

    This particular strategy I'm designing, which is written using NinjaScript for NinjaTrader, is designed to trade the reversion to the mean. So it is designed to focus on weakness in peaks and troughs.

    The problem is, when a fast-flush down occurs and you quickly get six red bars, and then there is a one-bar pullback in that flush (as so often happens), the strategy attempts to buy that one bar, only to have the market continue its flush on the next six to eight bars... followed by another one-bar pullback, each time canceling the short position (causing me to have to re-enter), and triggering the stop loss multiple times on the way down.

    Many times, this can result in 6 to 8 stopouts in a row. It's not the end of the world, and I can disengage the strategy during these periods, which are pretty easy to see coming, but I would be interested to find a way to programmatically "edit" these trades out, so I don't have to intervene in the running of the strategy.

    I'm wondering if anyone has come up with a way to deal with these fast-flush situations without killing the other mean reversion trades in their system.

    Any creative ideas and input would be much appreciate. (I've considered MA crossovers, slope calculations, etc., but I haven't come up with anything yet which works well... and I'm open to hear ideas regarding MA crossovers, falling or rising MA's, oscillator tricks, etc.).

    Thank you in advance.

    -PD
     
  2. dartmus

    dartmus

    Write a simple description (in plain language or pseudo-code between comment brackets) ....of what it is you see when it's easy to see the scenario coming. Then you will have your solution and will just need to convert that description of the events into code and get the sequencing calibrated correctly to match the description of the events that trigger your manual intervention.
     
  3. panzerman

    panzerman

    While price itself does not mean revert, derivatives of price called oscillators do mean revert. I like the work of John Ehlers. Check out his roofing filter (a combination of a low pass and a high pass filter), and his bandpass filter.
     
  4. Hello dratsum, and thank you for your reply and for your great recommendation regarding pseudo-coding the idea to assist with brainstorming the final result. This is very good advice, not only for me, but for anyone wishing to convert ideas into executable code.

    I've got (as you've so clearly suggested), the basic structure of the trade logic, and I've tried different scenarios to try and realize it in the automated trading environment. But I'm painfully aware of my limitations (and the fact that I, as everyone, have a certain preset list of solutions which restrict my creativity), so I was curious to read what other coders have done in the same situation, in an attempt to break my "writer's block" and come up with fresh ideas.

    I was thinking last night of a new angle of approach to this. It has to do with the timing of the formation of the bars. The great thing about range bars is they let you know right away when the market has decided to get up and move. And, when the market is ranging, it can take hours for a bar to form and move on to the next bar.

    When I say it is "easy to see coming," this timing of the bars' formation is what I'm referring to. When the market opens on the YM cash session (or whenever the momentum picks up), these bars begin to form at a rapid pace. I'm going to be working today on something which could time the formation of the bars, and based on that timing, limit counter-move trades for the duration of the period.

    I'm still interested to read what others have done or what ideas they may have contemplated relating to this scenario. Even if they have not tackled the situation themselves, I'd be interested to learn how they would tackle it if they encountered it.

    Thank you again for your great suggestion.

    -PD
     
  5. Very well-stated.

    The core around which my system is built consists of modified versions of two popular oscillators for this very reason.

    I'm curious as to why you specifically pointed out the two John Ehlers oscillators you've mentioned. Was there something specific about these particular oscillators which came to mind upon reading my original post? Any input regarding them, as they may relate specifically to my scenario, and based on your own personal experience with them, would be greatly appreciated.

    What unique characteristics have you noticed regarding them? Your input may spark yet more creativity in my own mind.

    Thanks for your reply.

    -PD
     
  6. Arti

    Arti

    You may try to switch to longer time frames, these type of patterns that you describe are more consistent on longer time frames.
     
  7. panzerman

    panzerman

    His work is generally predicated on well established engineering methods. That of course doesn't mean you will make money by trading his indicators, but they aren't some BS contrivance like Elliot Wave.

    If you stick to your oscillators, consider the Fisher Transform or Inverse Fisher Transform of those indicator outputs, which Ehlers also talks about. I would also encourage you to look at his work on finding and using the Dominant Cycle, in your chosen indicator calculation.

    I would also suggest you do some searching on criticisms of Ehlers use of DSP methods, just so you can have a more rounded picture of their applicability to trading.
     
  8. tom_czr

    tom_czr

    to PleasantDays:
    Maybe you should focus rather on stuff like autocorrelation of time series, or trade basket of markets. Using this approaches is much more easier than trying to have stable performance on only one market (YM as you mentioned) using MA crossovers and simple oscilalators...
     
  9. panzerman

    panzerman

    Funny you should mention that because in some of Ehlers' latest work, he uses an autocorrelation periodogram to calculate the dominant cycle.
     
  10. Thanks for your reply tom. I appreciate it.

    "Easier" is not actually what I'm focusing on at the moment. I'm focusing on a certain "edge," and sometimes that's actually "harder."

    About a year ago, quite by accident, as I was fiddling with the math of two very common "indicators," and one of JM Hurst's creations, and I noticed something in the chart formations of the YM on the 6 range chart that caught my attention. Since that day, I've been developing a trading system around those very unique and highly regular patterns as captured via the mathematical combination of these "indicators." I've only seen it on the 6 range bar charts and on the YM.

    The Achilles heel of the system at the moment are these occasional "fakeouts" which occur during the YM's "Russell-inspired" flushes. They throw off my testing numbers and I'd like to find an elegant way of eliminating them. I'm certain there is a way, and I'm looking for creative sparks from other sharper minds here at EliteTrader.

    With regards to using oscillators and crossovers, there's actually a very good reason to use both. The fact that 90% of the traders out there (including some of the biggest names in the business) are using them, and usually all around the same pivot numbers and formations (i.e. flags, fibs, 2/20 Bollingers, Keltners, Donchian, 30MA, 50MA, 200MA, and 12/26/9 MACD, etc.). There's actually quite an edge in studying the math of the herd, which is what I'm doing at the moment.

    Think about it this way: If everyone bought the market when Eastenders came on television at it's regularly scheduled time, and then sold the market when the BBC came on the air in the morning... What would be a good time to buy the market? Would you buy at noon, or would you wait until just before Eastenders came on the air? Now... imagine instead of just two television shows, there are 25. The concept is still the same. It's just the math that's more complicated. But that's why we have computers. :)

    Mathematically, what Ehlers and others are doing, is capturing the "oscillation" of all these traders using their different methods. At some point, there exists a "harmonic" of all this activity. A certain frequency at which they all harmonize. If everyone was using stochastics set at the same settings, along with the same type chart series, we could more easily see the frequency. Same with Bollinger bands, etc.

    Crossovers and oscillators probably don't really say anything about what's actually happening at the price level (maybe Market Delta does... maybe). But, they are very accurately telling me what the herd is doing. The herd is a powerful force (just turn on the news sometime).

    With regard to autocorrelation of time series and baskets, I agree with your assertion, provided you don't already have an edge with the system you've developed. As a trader, I'm not looking to take home a million dollars everyday... because I know that's not going to happen, even at the best of times, but... a consistent percentage gain in my portfolio is what I'm after... a simple, repeatable and consistent percentage gain, day after day, and week after week. If a trader finds a way to achieve this... an edge... my recommendation is (especially to myself): "stop looking around, put your head down, and plow, plow, plow until that edge is no longer producing." Linda Bradford Raschke is a perfect example of this mode of thinking.

    Just a few thoughts.

    I'm curious to know how you would approach getting rid of those momentary price reversals I mentioned in my original post. Have you given that any thought? What would you do in my case (keeping in mind that I want to keep the system I have)? Your thoughts would be much appreciated.

    -PD
     
    #10     Jul 24, 2014