Hi,
Anyone has any experiences how to code if we do not want automatic trading running if the market moves sideways?
Thanks
George
There are several ways, a pair of Moving Averages with small (you have to tell what small is for you) differences, Bollinger Bands narrowing for, say, two consecutive bars, no price movement over “n” pips for at least “x” bars….It’s up tou you to tell when a range is starting.
It’s a lot easier to spot it on a chart, especially when it ends!
Thanks Roberto, would you post a short example if you have time!? Thanks again
This Nicolas‘post when, as a novice, I needed some help https://www.prorealcode.com/topic/detecting-a-range/#post-10338.
The following example uses narrowing Bollinger Bands for just 1 (or whatever number you want to set) bar.
First you have to define a BB:
// Set up a Bollinger Band
ONCE BBavg = 20 //20-period BB
ONCE BBdev = 2.0 //2.0 BB Deviation
BBmean = average[BBavg,0](close) //BB mean (middle line)
BollUP = BBmean + ((std[BBavg](close)) * BBdev) //BB Upper Band
BollDN = BBmean - ((std[BBavg](close)) * BBdev) //BB Lower Band
Then you have to determine when the BB starts narrowing, because the price is likely to enter a range:
// Determine current status of BB (Narrowing)
ONCE NarrowingBars = 1 //You may want to change this to suit your needs
Narrow = (summation[NarrowingBars]((BollUP < BollUP[1]) AND (BollDN > BollDN[1])) = NarrowingBars)
Now you can use the variable NARROW to check whenever you want:
IF Your_Conditions AND not Narrow THEN....
I find the best practise is using highs and lows. By definition a trend can only exist (up) if prices are making higher highs and higher lows.
ema = exponentialaverage[48](close)
ema2 = exponentialaverage[24](close)
//set trend using highs and lows
Trend1= highest[2](high)
Trend2= highest[5](high)
Trend3 = trend1-trend2
///
Trend4= lowest[2](low)
Trend5= lowest[5](low)
Trend6 = trend4-trend5
////add trend3 and trend6 commands to code
Long = (emaabove and trend3 <0)
Short = (emabelow and trend6 >-1)
I use the above code. I have found it does well at keeping you out of sideways markets but is not perfect so any edits that people might have would be useful. Different FX pairs behave differently for the second value [n] depending on what time or other variables control your entry criteria.