Martingale Trading Strategy (Amibroker Code)

The original article can be found here.

The strategy in plain English:

  • We buy at the close when the 4-day RSI indicator cross below 30 (33% of equity).
  • If the 4-day RSI drops below 20 we double the bet (buy 66% of equity)
  • We sell at the close when the close is higher than yesterday’s high.

Amibroker code:

MaxPos = 2;
SetOption( "InitialEquity", 100000 );
SetOption( "MaxOpenPositions", MaxPos );

RSIFour1=30;
RSIFour2=20;

Entry1 = Cross(RSIFour1, RSI(4)) ; // First Entry
Entry2 = Cross(RSIFour2, RSI(4)) ; // Second entry

ExitSignal = C>Ref(H,-1);

BothOnSameBar = Entry1 AND Entry2; 
Entry2 = ExRem(Entry2, ExitSignal); // Remove Entry2 signals until ExitSignal
OnlyEntry2 = Entry2 AND NOT Entry1; 

InitialEntry = entry1;//BothOnSameBar OR Entry1;
ScaleInEntry = OnlyEntry2;

Buy = 	IIf(InitialEntry , 1, 
		IIf(ScaleInEntry, sigScaleIn, 0
		));

Sell = ExitSignal;

// Position sizing

EntryPosSize1 = 33.3; // Size for first entry
EntryPosSize2 = IIf(Cross(RSIFour2,RSI(4)),66,0); // Size for second (Martingale) entry
PosSize =  (EntryPosSize1) + (EntryPosSize2);
SetPositionSize(PosSize, spsPercentOfEquity);