Momentum Trading Strategy (Amibroker Code)

You can find the original article here.

Logic in plain English:

Strategy 1:

  • When the close crosses above the 100-day high of the close, we go long at the close.
  • When the close crosses below the lowest close in the last 100 days, we sell at the close.

Strategy 2:

  • When the close crosses above the 25-day high of the close, we go long at the close.
  • When the close crosses below the 25-day high of the close, we sell at the close.

Strategy 3:

  • It’s based on monthly quotes in the ETFs SPY, TLT, EFA, and EEM.
  • We rank all four ETFs every month based on last month’s performance/momentum.
  • We go long the ONE ETF with the best performance the prior month (it doesn’t matter if negative or positive return).
  • Hold for one month and rinse and repeat (or continue being long the same instrument).

Code:

Strategy 1:

Amibroker:

oddis=Optimize("Trading day",100,5,100,5);

Buy= Cross(C,Ref(HHV(C,oddis),-1)) ;
buyPrice= Close;
Sell= Cross(Ref(llV(C,oddis),-1),Close);
sellPrice= Close ;

Tradestation:

{
Strategy - Momentum trading 1
https://www.quantifiedstrategies.com/lessons/momentum-trading-strategy-amibroker-code/
}

Inputs:
	Lookback(100);

if Close > Highest(Close, Lookback)[1] Then
	buy this bar on close;

if Close < Lowest(Close, Lookback)[1] Then
	sell this bar on close;

Strategy 2:

Amibroker:

oddis=Optimize("Trading day",25,5,100,5);

Buy= Cross(C,Ref(HHV(C,oddis),-1)) ;
buyPrice= Close;
Sell= Cross(Ref(hhV(C,oddis),-1),Close);
sellPrice= Close ;

Tradestation:

{
Strategy - Momentum trading 2
https://www.quantifiedstrategies.com/lessons/momentum-trading-strategy-amibroker-code/
}

Inputs:
	Lookback(25);

if Close > Highest(Close, Lookback)[1] Then
	buy this bar on close;

if Close < Highest(Close, Lookback)[1] Then
	sell this bar on close;

Strategy 3:

setOption("holdminbars",1);

Leverage=1.0 ;
SetOption("accountmargin",100/leverage);
setPositionSize(99.75*leverage,spsPercentOfEquity);
SetBacktestMode( backtestRotational );
SetOption("initialequity",100000);
//SetOption("commissionMode",1);
//SetOption("commissionAmount",0.1);
SetOption("MaxOpenPositions",1);
SetOption("WorstRankHeld",1);

PositionScore = 1000 +ROC(C,1) ;