How to create algo strategy to automatically trigger reversal trades when a stock moves a certain %?

How to create algo strategy to automatically trigger reversal trades when a stock moves a certain %?

This can be used as base for 52 Week HL Strategy and as a base for any breakout trade as well.

But in this episode, we will be focussing on triggering reversal trades when a stock moves by 3%. So if a stock moves up by 3%, we will short it and viceversa. This is a simple mean reversion strategy.

So going into the script, the first thing we will do is store stock’s daily open and closing price into a variable.

price_close = close

price_open = open

Now we can then perform a calculation to determine the percentage price change.

price_change = price_close / price_open

The price_change variable now holds the calculation. So for example, if a stock opened at 100 and rallied 3% to close at 103, the price_change variable would be 103/100 which is 1.03.

But if the same stock opened at 100, and declined 3% to close at 97, the variable would read 97/100 which is 0.97.

As per the strategy, we already know that if a stock declined 3% or more, the price_change variable would be 0.97 or less, and we want to go long as we are trading reversal.

So let’s write the code for taking the trades now.

if price_change < 0.97

strategy.entry(“long”, strategy.long, 100)

And the code to go short if a stock rallies more than 3%.

if price_change > 1.03

strategy.entry(“short”, strategy.short, 100)

Lastly, we will plot the price_change variable in the data window to see and we can confirm that the trades are being executed.

plot(price_change)

And here are the results of our strategy.

This can be used as base for 52 Week HL Strategy and as a base for any breakout trade as well.


Check Out Script Library to get script used for this episode:
https://marketsecrets.in/script-library/

To know more, checkout:

https://youtu.be/iclkrl47jgE

 

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!