Divergence Trading Strategy (Amibroker And Tradestation Code)

The original article can be found here.

The strategy in plain English:

  1. The close must set an N-day low of the close.
  2. The RSI indicator must NOT set an N-day low.
  3. If 1 and 2 are correct, we buy at the close.
  4. We sell at the close when the close ends higher than yesterday’s high.

Amibroker code:

oddis=Optimize(“N-days”,2,1,30,1) ;

Buy= C<Ref(LLV(C,oddis),-1) AND RSI(oddis)>Ref(LLV(RSI(oddis),oddis),-1) ;
buyPrice= Close;
Sell= C>Ref(H,-1) ;
sellPrice= Close ;

Tradestation code:

{
Divergence strategy (no 143):
The close must set an N-day low of the close.
The RSI indicator must NOT set an N-day low.
If 1 and 2 are correct, we buy at the close.
We sell at the close when the close ends higher than yesterday's high.
}

Inputs:
	lookback(2);

Vars:
	llvClose(0),
	rsiClose(0),
	llvRsi(0);
	
llvClose = Lowest(Close, lookback);
rsiClose = RSI(Close, lookback);
llvRsi = Lowest(rsiClose, lookback);

if (Close<llvClose[1]) and (rsiCLose > llvRsi[1]) then
	Buy this bar at close;
	
if (Close > High[1]) then
	Sell this bar at close;