Buy above swing high and sell below swing low?

Hello all,
I am using a moving average trend following system on Intraday. I have tried to find range market by checking if in the last 10 bars, the two moving average have crossed more than 4 times (meaning market is in range and I won't trade). Now I will buy only when the prices close above the recent swing high or it closes below the recent swing low.
Capture
Here if I am at (1) point, how to buy above (2) or sell below (3)? I know zig zag , peak and trough shall be used here but I am not able to get a comfort on these functions.
Regards
Thanks

1 Like

AB is a technical software, so your first goal is to translate points 2/3/4 into mathematical expressions or atleast pseudocode in terms of what it can encompass before you will get a start on it.

Sorry for that . Lets say if point 2 and point 3 and 1 % above and below.

I apologize for this. It was truly not my intention to be lazy and expecting a complete code for the above. I just need a start of it.
I have seen zig zag function which catches the pivot highs and lows. So it will also trace point 2) and point 3) which I actually want. I have also tried to understand trough and peak and got to know that they also do something very similar to this.
Now how do I get a buy signal when I get closing above these pivot highs and lows? Should I use zig zag or peak ?

Lets say you have your moving averages providing some sort of trend info.

Now for upside breakouts, you can get the recent most Peak (n =1), and then have the other Buy conditions combined with the following:

Plot( C, "Close", colorDefault, ParamStyle("Style") | GetPriceStyle() );

Peak1 = Peak( C, 0.5, 1);

Buy1 = Cross( C, Peak1);

Plot( Peak1, "peak1", colorAqua);
PlotShapes( Buy1*shapeUpArrow, colorLime, 0, Low , -20 );

Use this code to get you started.

image

same logic can be used for Short.

Do note that all future looking codes have limitations like inappropriate backtesting etc so you need to acquaint yourself.

2 Likes

You are a great person bro. Thanks for the help. I really appreciate it

Your post has given me a good start. Just wanted to ask what is the difference between zig zag and peak ? I have plotted zig zag and its connecting all the swings highs and low but in the case above (buying above swing high) can we also use zig zag instead of peak ?

Zig has both combined whereas Peak and Trough are separate arrays of the same.
I think you should stick with both separately otherwise you'll have to keep a track of which is active in the last leg.

1 Like

Thanks. Lets say that we are just below point 2) (pic in the first post). Now at what time will AFl recognize point 2) as a peak ? is % change what we mention in the peak and trough formula is this only? LIke if goes below 0.5% (if we write change as 0.5%) from highest point, it will show it as peak or its something else ?

You cant rely on such assumption, you have to check if a peak value, that is Close price for example, has occurred in the Trough.

So if Lowest peak is 100 and highest Trough is 97, if the current Lowest peak becomes less than 97 for a recent trough, you will know that a sharp dropped has occurred. so similar logic to this is needed.

its a great hep bro can you create sell signal i mean down arrow also regards...