Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Mean Reversion Trading System for Amibroker (AFL)

Rating:
3 / 5 (Votes 2)
Tags:
adx, trading system, amibroker, bands

Mean Reversion systems assume that Stock prices oscillate in a Fixed range bounded by an upper and lower price bands. The price always tends to return to a median level in due course of time. In order to trade such system, Buy order is placed at the lower end of range and Sell order is placed at the higher end of range. Any breakout of this range with good volumes may lead to strong trends and trade should be avoided during that time. Strict stoploss is imperative in order to trade Mean Reversion systems. There are many indicators which can be handy for developing Mean Reversion Trading systems. Examples are Bollinger Bands, Donchian channels, RSI, CCI etc.

Read more about this trading system and detailed backtest report here.

Screenshots

Indicator / Formula

Copy & Paste Friendly
//------------------------------------------------------
//
//  Formula Name:    Mean Reversion Trading System
//  Author/Uploader: Trading Tuitions
//  E-mail:          support@tradingtuitions.com
//  Website:         www.tradingtuitions.com
//------------------------------------------------------

_SECTION_BEGIN("Mean Reversion Trading System");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

SetTradeDelays( 1, 1, 1, 1 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",100);
SetOption("AccountMargin",10);
SetOption("RefreshWhenCompleted",True);
SetPositionSize(150,spsShares);
SetOption( "AllowPositionShrinking", True );

BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;

Periods =optimize("Periods",110,10,200,20);
Width =optimize("width",2,1,10,1);
MAPeriods = 200;
BBColor = colorOrange;
Style = 0;
bTop=BBandTop( Close, Periods, Width );
bBottom=BBandBot( Close, Periods, Width );
ADXPeriods=optimize("ADXPeriods",10,2,20,1);


BuyCondition=IIf(Ref(C,-1)<bBottom AND close>bBottom AND ADX(ADXPeriods)<35,1,0);
ShortCondition=IIf(Ref(C,-1)>bTop AND close<bTop AND ADX(ADXPeriods)<35,1,0);


Buy = IIf(Ref(C,-1)<bBottom AND close>bBottom AND ADX(ADXPeriods)<35,1,0);
Short = IIf(Ref(C,-1)>bTop AND close<bTop AND ADX(ADXPeriods)<35,1,0);


BarsSinceBuy=BarsSince(BuyCondition);
BarsSinceShort=BarsSince(ShortCondition);

LastBuyPrice=ValueWhen(Buy AND BarsSinceBuy<BarsSinceShort,BuyPrice);
LastShortPrice=ValueWhen(Short AND BarsSinceShort<BarsSinceBuy,ShortPrice);

StopLoss=optimize("SL",0.5,0.2,1,0.2);
Target=optimize("Target",1,0.5,5,0.5);

BuySL=BarsSinceBuy<BarsSinceShort AND Close<=LastBuyPrice-LastBuyPrice*(Stoploss/100);
ShortSL=BarsSinceShort<BarsSinceBuy AND Close>=LastShortPrice+LastBuyPrice*(Stoploss/100);
BuyTarget=BarsSinceBuy<BarsSinceShort AND Close>=LastBuyPrice+LastBuyPrice*(Target/100);
ShortSL=BarsSinceShort<BarsSinceBuy AND Close<=LastShortPrice-LastBuyPrice*(Target/100);

Sell=Short OR Close<bBottom OR BuySL OR BuyTarget;
Cover=Buy OR Close>bTop OR ShortSL OR BuyTarget;



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



ApplyStop(Type=0,Mode=1,Amount=StopLoss);
ApplyStop(Type=1,Mode=1,Amount=Target);

printf("\nBuy : " + Buy );
printf("\nShort : " + Short );
printf("\nCover : " + Cover );
printf("\nSell : " + Sell );
printf("\nBuyPrice : " + BuyPrice );
printf("\nShortPrice : " + ShortPrice );
printf("\nLastBuyPrice : "+ LastBuyPrice);
printf("\nLastShortPrice : "+ LastShortPrice);
printf("\nBuyCondition : "+ BuyCondition);
printf("\nShortCondition : "+ ShortCondition);
printf("\nBuySL : "+BuySL); 
printf("\nShortSL : "+ShortSL);
 
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
Plot( bTop, "BBTop" + _PARAM_VALUES(), BBColor , Style ); 
Plot( bBottom, "BBBot" + _PARAM_VALUES(), BBColor , Style ); 


/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

_SECTION_END();	

1 comments

1. dhavalparikh

After the sell when to cover? Can we have target trigger added?

Leave Comment

Please login here to leave a comment.

Back