Intraday Stochastic Trading System: Amibroker AFL

Day trading can be tricky and unpredictable if you do not understand the basics behind it. You need to be armed with reliable indicators and patterns to be successssfuly at intraday trading. Stochastic is one such indicator which has been round for a long time. It is suitable for both intraday trading as well as swing trading. In this post, we would explore one of the Intraday Stochastic trading system which has given 63%~ annual compounded returns in last 3 years. Also, Amibroker AFL code for this system has been provided.

What is Stochastic Indicator and How it is calculated?

Stochastic is a momentum indicator which shows the strength of current trend. It does so by comparing current price to the range of prices within a definite lookback period. It is an oscillator and its value ranges from 0 to 100, hence it can also identify overbought and oversold price levels very accurately.

Stochastic Indicator

Reference: https://tradingstrategyguides.com/best-stochastic-trading-strategy/

Stochastic Indicator was developed by George C Lane in 1950’s. According to an interview with Lane, the Stochastic Oscillator “doesn’t follow price, it doesn’t follow volume or anything like that. It follows the speed or the momentum of price. As a rule, the momentum changes direction before price.”

Stochastic Indicator is calulcated using below formula:

%K = (Current Close – Lowest Low)/(Highest High – Lowest Low) * 100
%D = 3-day SMA of %K

Lowest Low = lowest low for the look-back period
Highest High = highest high for the look-back period

The default look-back period is 14 days which can be changed depending on the security traded. Trading signals are generated when %K line of Stochastic crosses %D line.

Stochastic Divergances can also very accurately determine price reversals. Divergence occurs when the direction of the price trend and the direction of the indicator trend are moving in the opposite direction.

Read on to see a p

Read our article on AFL tutorial here.

Intraday Stochastic Trading System – AFL Overview

Parameter Value
Preferred Time-frame
5 Minutes
Indicators Used StochD, StochK
Buy Condition
  • Stochastic K line crosses over Stochastic D line
  • Stochastic K line crosses over 20
Short Condition
  • Stochastic K line crosses below Stochastic D line
  • Stochastic K line crosses below 80
Sell Condition
  • Same as Short
  • Stop Loss Hit
  • Target met
  • End of Trading Day
Cover Condition
  • Same as Buy
  • Stop Loss Hit
  • Target met
  • End of Trading Day
Stop Loss 0.5%
Targets 1%
Position Size 100% of equity
Initial Equity 200000
Brokerage 100 per order
Margin 10%

Intraday Stochastic Trading System – AFL Code

//------------------------------------------------------
//
//  Formula Name:    Intraday Stochastic System
//  Author/Uploader: Trading Tuitions
//  E-mail:          support@tradingtuitions.com
//  Website:         www.tradingtuitions.com
//------------------------------------------------------

_SECTION_BEGIN("Intraday Stochastic System"); 

SetTradeDelays( 1, 1, 1, 1 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",100);
SetOption("AccountMargin",10);
SetPositionSize(100,spsPercentOfEquity);
SetOption( "AllowPositionShrinking", True );
BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
Plot( Close, "Price", colorWhite, styleCandle );

NewDay = (Day()!= Ref(Day(), -1)) OR BarIndex() == 0;  
Plot(NewDay,"",colorlightGrey,styleHistogram|styleDots|styleNoLabel|styleOwnScale);

FirstTradeTime=093000;
SquareOffTime = 151500;

periods = Param( "Periods", 14, 1, 200, 1 );
Ksmooth = Param( "%K avg", 12, 1, 20, 1 );
Dsmooth = Param( "%D avg", 10, 1, 20, 1 );
myStochD =StochD( periods , Ksmooth, DSmooth );
myStochK =StochK( periods , Ksmooth);
Overbought = 80 ;
Oversold =20 ;
Center = 50 ;

Buy = Cross(myStochK, myStochD) AND Cross(myStochK, Oversold) AND TimeNum()>= FirstTradeTime AND TimeNum()<SquareOffTime;
Short = Cross(myStochD,myStochK) AND Cross(Overbought,myStochK) AND TimeNum()>= FirstTradeTime AND TimeNum()<SquareOffTime;

Sell=short OR TimeNum() >= SquareOffTime;
Cover=buy OR TimeNum() >= SquareOffTime;

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

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


StopLoss=Param("stop",0.5,0.5,2,0.5);
ApplyStop(Type=0,Mode=1,Amount=StopLoss);

Target=Param("Target",1,1,5,1);
ApplyStop(Type=1,Mode=1,Amount=Target);

printf("\nBuy : " + Buy );  
printf("\nSell : " + Sell );  
printf("\nShort : " + Short );  
printf("\nCover : " + Cover ); 
printf("\nmyStochK : " + myStochK ); 
printf("\nmyStochD : " + myStochD ); 

/* 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();




AFL Screenshot

Intraday Stochastic Trading System

Also Read: Ichimoku Trading System AFL Code

Intraday Stochastic Trading System – Backtest Report

Parameter Value
Nifty
Initial Capital 200000
Final Capital 1342878.53
Scrip Name NSE Nifty
Backtest Period 01-Jan-2014 to 20-Nov-2017
Timeframe 5 Minutes
Net Profit % 571.44%
Annual Return % 62.64%
Number of Trades 265
Winning Trade % 56.60%
Average holding Period 31.57 periods
Max consecutive losses 5
Max system % drawdown -22.90%
Max Trade % drawdown -9.32%

Download the detailed backtest report here.

Equity Curve

Intraday Stochastic Equity Curve

Profit Table

This intraday trend following strategy has been profitable every year from 2014 to 2017

Intraday Stochastic Profit Table

Additional Amibroker settings for backtesting

Goto Symbol–>Information, and specify the lot size and margin requirement. The below screenshot shows lot size of 75 and margin requirement of 10% for NSE Nifty:

Symbol Info_Nifty

Disclaimer:

All the AFL’s posted in this section are for learning purpose. Trading Tuitions does not necessarily own these AFL’s and we don’t have any intellectual property rights on them. We might copy useful AFL’s from public forums and post it in this section in a presentable format. The intent is not to copy anybody’s work but to share knowledge. If you find any misleading or non-reproducible content then please inform us at support@tradingtuitions.com

Leave a Reply

Your email address will not be published. Required fields are marked *