Need help to rectify code


I have a trading strategy that buys @ ADX >= 20 and sells (take profit) @ ADX >= 50. Code given below:

Buy = ADX(10) >= 20;
BuyTarget = ADX(10) >= 50;
BuyStop = ADX(10) <= 10;
Sell = BuyTarget OR BuyStop;

Problem is that after achieving the target, the above code starts buying everyday after the target is achieved. Eg. Buy 100 shares on the day ADX closes at or above 20. When ADX reaches 50, the 100 shares are sold. So far so good.

Suppose ADX rises to 52 on next bar, the AFL buys again and sells the next day (this must not happen!). Then when ADX moves up to 54 on the next bar, the AFL buys again and sells the next day (this must not happen).

Grateful if someone can help with this :slight_smile:

you can add below lines of code and it will ignores the continues buy or sell signals, if you get 2 buy signals continuously it ignores second and waits till sell
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);


thank you AG.

my code buys when ADX(10)>=20. next it sells when ADX(10)>=50. this covers the trade. problem is that since my trigger is ADX(10)>=20, if the stock price rises further, resulting in ADX moving even higher - amibroker opens a fresh purchase - i want to stop this. any help is appreciated :slight_smile:

Hi Nitya,
im new to algo if i use your code for backtest, im getting error as Undefined variable ‘20’ why ??

what code did you use please?

i think undefined variable is an error that arises when an array is used without being inititated…