# Algorithm to understand if a market is trend following or mean reversal

10 replies

1 year ago #280948

Hello,
I recently started using Strategy Quant X. I’m trying to create an EA that analyzes a market and understands whether it is a trend following or reversing.
I was trying with AlgoWizard to compose the logic and, based on the backtest results, understand the market typology.
The basic idea to check if a market is trend following is as follows:
– Enter a buy stop if the next bar breaches the previous day’s high.
– Enter a sell short stop if the bar is lower than the previous day’s low.

The idea for testing a mean reverting market is as follows:
– Buy limit if the current bar is lower than the previous day’s low.
– Short limit if the current bar is lower than the previous day’s high.

But I can’t get these ideas to work.

Do you have a suggestion to implement an algorithm to check if a market is trend following or mean reverting?

Thank you very much.

Lorenzo

1 year ago #281025

Hi,

can you make a screenshot of a chart example for both rules? I can make some example on this

1 year ago #281049

Hi Thomas,
I appreciate your support. Right now, I’m not using the computer where Strategy Quant X is installed. Tomorrow, I’ll gladly send you a screenshot of what I was trying to do.

Thanks, you are very kind.

Lorenzo

1 year ago #281070

If you go to the getting started or Home screen of SQX there are two options to Start with default generation <button id=”home-start-generating-forex” class=”btn btn-primary
sq-btn-build-main” type=”button”>for forex<i class=”fa
fa-chevron-circle-right” aria-hidden=”true”></i></button> <button id=”home-start-generating-futures” class=”btn btn-primary
sq-btn-build-main” type=”button”>for futures<i class=”fa
fa-chevron-circle-right” aria-hidden=”true”></i></button> and get feel of the platform.

Forex is generally considered to be trend following and futures indices at least are generally considered to be mean reversion markets. Not so sure about futures commodities.

The above are not hard and fast rules. Just general market descriptions.

If you run your select market in forex and futures equally SQX likely has their building blocks reasonably apportioned and your Databank with the better results is a fair indication of what posture your chosen instrument displays more of.

Realize all markets experience regime changes or periods of consolidation (Mean Reversion) as well as periods of momentum (trend following). How you tune your strategies to filter out low probability trades can be almost as important as limiting your leverage in correlated exposure.

1 year ago #281082

Hi Tomas,
I apologize for my delayed response.
I’m creating a straightforward algorithm for testing the typology of a market. In the screenshot, I’m attempting to implement a simple break-out algorithm to verify from a point of view purely qualitatively if a market is a trend following. To confirm if a market is mean reverting the order type will be limit.
In the strategy that I show in the screenshot you below, I attempted to run the algorithm but I obtained a generic error and needed help understanding what was wrong.
In the backtest, if a market is trend following, I’m expecting an equity line increasing for both long and short.

Thank you very much for your kind and attention.

Lorenzo

1 year ago #281083

Hi James,
The important observation in a trend-following market in the long term, there are moments, such as those of consolidation, in which there are mean reverting behaviors. I was trying a method to understand the nature of a market for educational and indicative purposes, especially a hypothetical market that I had never used.
If you want, see the reply post I sent to Tomas.

Thank you very much for your valuable intervention.

Lorenzo

1 year ago #281084

I’m sorry Tomas,

I forgot the screenshot, here it is!

1 year ago #281089

I know you are looking for an answer from Thomas and he is better qualified to answer.

I wonder of having a buytocover exit from a buy order or a sell to exit a sellshort is viable. It seems counter intuitive to me.

Your logic to buy on a gap up also seems to be something that would or should be instantaneous on bar open. If so, why have a zero in the order valid for selector. Wouldn’t a 1 or even a 2 make sense?

Perhaps one or both details are causing issues.

1 year ago #281091

Hi James,
Excuse me. I need your help to understand your reflection.
Given that the attempt does not intend to generate a strategy to be put to the market, it only wants to try to study the market type on a sufficiently sizeable historical series. So the basic idea (to check if the market historically is a trend following) is to buy whether the bar just formed exceeds the maximum of the previous bar and sell if the new bar is lower than the minimum of the last bar. So in the backtest on a large time interval, I should get a growing equity line for both long and short purchases. Do you share? If you have time, I need clarification because you told me about the Shift of 1 or 2.

My idea is likely wrong. I am still gaining experience on the platform; my basic reasoning could be wrong.

Thank you again for your kindness and the time you have dedicated to helping me.

10 months ago #284092

Ciao Lorenzo,

did you find a solution to this?
i’’mn not sure if that helps but I took an algo trading course based on SQX. Didn t get my far for multiple reasons, now I m considering giving it a try again.

during this course, I was suggested that to identify if a market is trend following or reversal, you could launch a builder for a modest number of strategies, eg 500, with both limit and stop orders.

Then you look at the pseudo code of a good number of strategies, e.f. 250-300. If you more frequently encounter STOP orders, then market if better suited to breakout/trend following. If you find more LIMIT orders, then it s more suited for reversal. You can then continue the generation with only the type of order than works the most and, I would add, selecting indicators or stestegies most suited to the type of a market, to make generation most efficient.

I didn’t try that approach consistently I have to admit, it’s quite some work. But actually, thinking twice, it would just been once and provides a good piece of information.

Does this make sense? Did you try anything like this? Does it stimulate you any idea on how to automate?

I hope it helps, let me know,

cheers

Diego

9 months ago #284470

Hope this helps and also i used 1hour time frame.

###### Attachments:
You must be logged in to view attached files.

Viewing 10 replies - 1 through 10 (of 10 total)