Skip to main content

Range Constriction for Amibroker (AFL)

davidh over 14 years ago Amibroker (AFL)

  • Rating:
    3 / 5 (Votes 3)
  • Tags:
    trading system, amibroker, exploration

This is a complete trading system which can be applied to a chart, backtested or used as an exploration for buy/sell signals.

It has backtested profitably on all markets I have tested it on.

Basically it looks for stocks that are trading in a reduced range compared to the previous period. For stocks that are trading at recent highs it will buy when the stock moves up within this range, it shorts when the stock moves lower within this range. For exits it either sells into increasing strength or if when the price pulls back (opposite for shorts).

When run as an exploration it will produce a -1 for short, 1 for buy, -99 for sell, 99 for cover and 0 for hold.

Indicator / Formula

Copy & Paste Friendly
PosQty = 1000; // You can define here how many open positions you want
SetOption("MaxOpenPositions", PosQty);
//PositionSize = 1000/PosQty; // invest 100% of portfolio equity divided by Max. position count
SetPositionSize( 1, spsShares ); 


///////////////////////////////////////////////////////////////////////////////
// Range Constriction
// Finds stocks that are trading in a reduced range compared to previous period.
// Buys when in the reduced range at a recent high price, Shorts when in the reducred range at a recent low price.
// Sells when the stock slows or it makes a sharp move up, Covers when the stock rises or makes a sharp move down


H5 = HHV(H,5); L5 = LLV(L,5);
H10 = HHV(H,10); L10 = LLV(L,10);
H20 = HHV(H,20); L20 = LLV(L,20);
H30 = HHV(H,30); L30 = LLV(L,30);

Range1 = H5-L5 < (Ref(H5,-5)-Ref(L5,-5))*0.5;
Range2 = H10-L10 < (Ref(H10,-10)-Ref(L10,-10))*0.5;
Range3 = H20-L20 < (Ref(H20,-20)-Ref(L20,-20))*0.5;
Range4 = H30-L30 < (Ref(H30,-30)-Ref(L30,-30))*0.5;

Consolidation = Ref(Range1,-1) OR Ref(range2,-1) OR Ref(range3,-1) OR Ref(range4,-1) ;

Top = C == HHV(C,5);
Bottom = C == LLV(C,5);


Buy = top AND Consolidation AND C>1  AND C*MA(V,30)>10000000 AND V>1000000 AND C>Ref(C,-1)*1.015  
AND C>O AND C>MA(C,50) AND C>LLV(C,200)*2 AND Trix(12)>0;

Sell = C<MA(C,5) AND NOT Buy OR C>MA(C,10)*1.25 AND NOT Buy OR RSI(2)>95 AND NOT Buy;
InLong = Flip(Buy, Sell);

Short = bottom AND Consolidation  AND C>5  AND V>1000000 AND C*MA(V,30)>10000000 AND C<O 
AND C<HHV(C,200)*0.5 AND Trix(9)<0;

Cover = C>MA(C,5) AND NOT Short OR C<MA(C,10)*0.75 AND NOT Short  OR RSI(2)<5 AND NOT Short;
Inshort = Flip(Short, Cover);

   // METHOD END

NewLong = Buy;
NewShort = Short;

Any =  Newlong + newshort + inlong + inshort; 
Short = ExRem(Short,Cover);
Buy=ExRem(Buy,Sell);

Sell = ExRem(Sell,Buy) ;
Cover = ExRem(Cover, Short);

bColorDefault = colorLightGrey;


//Filter = 1;
Filter = any!=0 ;

AddColumn( Close, "Close", 1.2 );
AddTextColumn( FullName(), "IB Symbol,IB Exchange,IB Currency");
AddColumn(IIf(Sell, -99, (IIf(Cover, 99,(IIf(Buy>0, 1,(IIf(Short>0, -1,0))))))),"Buy/Sell",1.0);

6 comments

6. davidh
over 14 years ago

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

_SECTION_END();



PosQty = 1000; // You can define here how many open positions you want
SetOption("MaxOpenPositions", PosQty);
//PositionSize = 1000/PosQty; // invest 100% of portfolio equity divided by Max. position count
SetPositionSize( 1, spsShares ); 


///////////////////////////////////////////////////////////////////////////////
// Range Constriction
// Finds stocks that are trading in a reduced range compared to previous period.
// Buys when in the reduced range at a recent high price, Shorts when in the reducred range at a recent low price.
// Sells when the stock slows or it makes a sharp move up, Covers when the stock rises or makes a sharp move down


H5 = HHV(H,5); L5 = LLV(L,5);
H10 = HHV(H,10); L10 = LLV(L,10);
H20 = HHV(H,20); L20 = LLV(L,20);
H30 = HHV(H,30); L30 = LLV(L,30);

Range1 = H5-L5 < (Ref(H5,-5)-Ref(L5,-5))*0.5;
Range2 = H10-L10 < (Ref(H10,-10)-Ref(L10,-10))*0.5;
Range3 = H20-L20 < (Ref(H20,-20)-Ref(L20,-20))*0.5;
Range4 = H30-L30 < (Ref(H30,-30)-Ref(L30,-30))*0.5;

Consolidation = Ref(Range1,-1) OR Ref(range2,-1) OR Ref(range3,-1) OR Ref(range4,-1) ;

Top = C == HHV(C,5);
Bottom = C == LLV(C,5);


Buy = top AND Consolidation AND C>1  AND C*MA(V,30)>10000000 AND V>1000000 AND C>Ref(C,-1)*1.015  
AND C>O AND C>MA(C,50) AND C>LLV(C,200)*2 AND Trix(12)>0;

Sell = C<MA(C,5) AND NOT Buy OR C>MA(C,10)*1.25 AND NOT Buy OR RSI(2)>95 AND NOT Buy;
InLong = Flip(Buy, Sell);

Short = bottom AND Consolidation  AND C>5  AND V>1000000 AND C*MA(V,30)>10000000 AND C<O 
AND C<HHV(C,200)*0.5 AND Trix(9)<0;

Cover = C>MA(C,5) AND NOT Short OR C<MA(C,10)*0.75 AND NOT Short  OR RSI(2)<5 AND NOT Short;
Inshort = Flip(Short, Cover);

   // METHOD END

NewLong = Buy;
NewShort = Short;

Any =  Newlong + newshort + inlong + inshort; 
Short = ExRem(Short,Cover);
Buy=ExRem(Buy,Sell);

Sell = ExRem(Sell,Buy) ;
Cover = ExRem(Cover, Short);

bColorDefault = colorLightGrey;


//Filter = 1;
Filter = any!=0 ;

AddColumn( Close, "Close", 1.2 );
AddTextColumn( FullName(), "IB Symbol,IB Exchange,IB Currency");
AddColumn(IIf(Sell, -99, (IIf(Cover, 99,(IIf(Buy>0, 1,(IIf(Short>0, -1,0))))))),"Buy/Sell",1.0);


PlotShapes(IIf(Cover,shapeSmallCircle,shapeNone), colorRed, 0, Close, Offset=0);
PlotShapes(IIf(Sell, shapeSmallCircle,shapeNone), colorBlue, 0, Close, Offset=0);
PlotShapes(IIf(Buy,shapeUpTriangle,shapeNone), colorBlue, 0, Close, Offset=-7);;
PlotShapes(IIf(Short,shapeDownTriangle,shapeNone), colorRed, 0, Close, Offset=-7);

Leave Comment

Please login here to leave a comment.