Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

SDA2 Trend Trading System – AFL code

1 min read

SDA2 Trend Trading system is nothing but a trend trading system made using Standard Deviation(SD) and ATR(2) – Average True range. This is just a variation of the AFBI Channel Trading system with better performance and backtesting results.

Nifty Daily Charts


[click on the picture for Bigger Charts -Older Chart has been replaced with the new one due to historical data discrepancy]

 
The Above chart is for Educational Purposes only not for any kind of Buy or Sell Recommendation in Nifty

The channel has been Built using the following formula

Derived=MA(((H+L)/2)+(O-C),3);
Upper=(Derived+StDev(Derived,7))+ATR(2)/1.5;
Lower=(Derived-StDev(Derived,7))-ATR(2)/1;

The Colour of the Candlestick will be toggled between Green and Red each time when it breaks the Upper Channel or the Lower Channel.

1)Candlestick turns green if the Candle stick breaks and closes above the Upper Channel which indicates the buy signal

2)Candlestick turns red if the Candle stick breaks and closes above the Lower Channel which indicates the sell signal

3)The stop loss for the Buy Signal is the lower Channel and the stop loss for the Sell Signal is the Upper Channel

The following code is used to toggle the Candlestick color between Green/Red

color=IIf(BarsSince(Cross(Lower,C))>BarsSince(Cross(C,Upper)), colorBrightGreen,colorRed);
Plot(C,"",Color,64);

Further, Buy or Sell Signals, Scanning Functionality and Exploration have been added to the AFL code as usual

SDA2 Trend Trading System – Amibroker AFL Code

_SECTION_BEGIN("SDA2 Channel Trading System");

Derived=WMA(((H+L)/2)+(O-C),3); 
i=WMA(Derived,3); 
Upper=(Derived+StDev(Derived,7))+ATR(2)/1.5; 
Lower=(Derived-StDev(Derived,7))-ATR(2)/1; 
color=IIf(BarsSince(Cross(Lower,C))> 
BarsSince(Cross(C,Upper)), colorBrightGreen,colorRed); 
Plot(C,"",Color,64); 
Plot(Upper,"upper band",colorGreen); 
Plot(Lower,"lower band",colorDarkRed); 
Title=Name()+" "+Date()+" "+EncodeColor(colorBlack)+" SDA2 Channel System "+EncodeColor(colorRed)+" Vol="+NumToStr(Volume,1.0)+" "+" O="+Open+" H="+HHV(H,1)+" L="+LLV(L,1)+
"Close="+ Close+""+"("+NumToStr((Close-Ref(C,-1))/Ref(C,-1)*100,1.2)+" %)";


/* Buy or Sell Condition */
Buy = Cross(Close,Upper);
Sell = Cross(Lower,Close);
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Filter = Buy OR Sell;
/* Exploration Parameters */
AddTextColumn( FullName(), "Company Name" );
AddColumn( Buy, "Buy", 1 );
AddColumn( Sell, "Sell", 1 );
AddColumn( C, "Close", 1.3 );
AddColumn( H, "High", 1.3 );
AddColumn( Lower, "Lower Band", 1.3 );
AddColumn( Upper, "Upper Band", 1.3 );

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


_SECTION_END();
Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

[Webinar] Understanding and Mitigating Curve Fitting in System Trading

"Understanding and Mitigating Curve Fitting in System Trading"! This dynamic session aims to equip novice to intermediate traders, quantitative analysts, and financial engineers with...
Rajandran R
1 min read

P-Signal Strategy Long Only Strategy – Amibroker AFL Code

This tutorial provides an overview of the P-Signal reversal strategy, a quantitative trading strategy that utilizes statistical parameters and error functions to generate probabilistic...
Rajandran R
2 min read

20 Replies to “SDA2 Trend Trading System – AFL code”

  1. Once again a great work done sir, hats off to you 🙂

    Sir can you help me with one thing, as its not possible to adjust all the stocks which have been splitted so is there any way one can arrange all the shares which have been splitted from the beginning till date, how can that be done sir?

    Any Afl to scan the circuit limits also plz?

    Thanks eagerly waiting for a reply

    Tc

    1. @Akki : Sure if i found any interesting work will update it here. Also iam trying to make database wil completely split/bonus adjusted will present here
      soon

      AFL to scan Circuit limits? What do you mean by that… already circuit limits are fixed by nseindia for each and every stock.
      I guess the data is available in nseindia itself. Why should one have to scan for circuit limits?

  2. Gm Sir Have a Nice Day, thx a lot for a quick reply sir, Sir by the Circuit scanner I meant to say that as you said the limits for circuits are available for each share in nse site, but wanted to make a scanner which can scan the closing price with the circuit filter point, and if the stock is near to uc then a yes option can be prompted on the share, is it possible with amibroker, or in excel, if I am able to get the whole list of circuit bands of all the shares, then I may work on an excel sheet….

    Can you provide me the list of bands of the circuits on all the shares sir in nse and bse both.

    And last thing sir, from where can i get 1 min and 5 min data of stocks and indexes?

    ThanksSir KeepRocking

    Have a Niceday

  3. yes in next data base ,pls see that the nse database can be updated with joy,s datadownloader and data base can be created outside c volume.

  4. Thx a lot sir 🙂

    Sir is it possible that during market hours we get the stocks which are hitting new highs and lows on intervals of 1 min in nse?

    This is a very good thing which must be there on the site sir, would request you to find some solution to this and make a stock screen er which can screen the stocks hitting new highs and lows on 1 min intervals

    Thx Sir

  5. Sir one more help can you provide a exploration afl for pivot and camrilla levels generation, which can be used on auto analysis and the camrilla and pivot levels can be plotted for all the shares which we select.

  6. Sir I found the Afl for camrilla here is the code

    _SECTION_BEGIN(“Price”);
    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 ) ) ));
    Plot( C, “Close”, ParamColor(“Color”, colorBlack ), styleNoTitle | ParamStyle(“Style”) | GetPriceStyle() );
    _SECTION_END();

    _SECTION_BEGIN(“Volume At Price”);
    PlotVAPOverlay( Param(“Lines”, 300, 100, 1000, 1 ), Param(“Width”, 5, 1, 100, 1 ), ParamColor(“Color”, colorCycle ), ParamToggle(“Side”, “Left|Right” ) | 4*ParamToggle(“Z-order”, “On top|Behind”, 1 ) );

    _SECTION_END();
    _SECTION_BEGIN(“Camrilla”);

    A=Ref(O,0);
    B=Ref(H,0);
    D=Ref(L,0);
    E=Ref(C,0);
    F=ATR(2);
    G=((100*F)/E);
    Pivot=((A+B+D+E)/4);
    range=B-D;
    H1=C+((range*(1.1*G))/12);
    H2=C+((range*(1.1*G))/6);
    H3=C+((range*(1.1*G))/4);
    H4=C+((range*(1.1*G))/2);
    H5=C+((range*(1.1*G))/1);
    L1=C-((range*(1.1*G))/12);
    L2=C-((range*(1.1*G))/6);
    L3=C-((range*(1.1*G))/4);
    L4=C-((range*(1.1*G))/2);
    L5=C-((range*(1.1*G))/1);
    Filter=1;
    AddColumn(range,”Range”,1.2,colorDefault,colorDefault,-1);
    AddColumn(F,”ATR”,1.2,colorDefault,colorDefault,-1);
    AddColumn(G,”ATR %”,1.2,colorDefault,colorDefault,-1);
    AddColumn(E,”Close”,1.2,colorDefault,colorGold,-1);
    AddColumn(Pivot,”Pivot”,1.2,colorGold,colorBlue,-1);
    AddColumn(H5,”H5″,1.2,colorDefault,colorDefault,-1);
    AddColumn(H4,”H4″,1.2,colorGold,colorBlue,-1);
    AddColumn(H3,”H3″,1.2,colorGold,colorRed,-1);
    AddColumn(H2,”H2″,1.2,colorDefault,colorDefault,-1);
    AddColumn(H1,”H1″,1.2,colorDefault,colorDefault,-1);
    AddColumn(L1,”L1″,1.2,colorDefault,colorDefault,-1);
    AddColumn(L2,”L2″,1.2,colorDefault,colorDefault,-1);
    AddColumn(L3,”L3″,1.2,colorGold,colorBlue,-1);
    AddColumn(L4,”L4″,1.2,colorGold,colorRed,-1);
    AddColumn(L5,”L5″,1.2,colorDefault,colorDefault,-1);

    _SECTION_END();

    Hope I am able to benefit all the family members 🙂

  7. Dear sir,

    I stumbled across this blog where I find both you and the members to be very helpful. thanks to you all. I am new to Amibroker. Could someone kindly show me how to create an AFL file from the above code? Thanks

  8. DEAR SIR I M TRADER AND I M USING AMIBROKER FOR TAKE MY TRADES
    SIR MY REQUEST TO U CAN U PLZ MAKE A AFL CODE FOR AMIBROKER . USING
    StochasticK AND Stochastic D CROSSOVE BUY SELL ARROW SIGNAL.
    SO PLZ DO IT SIR I NEED IT .IF U DO IT FOR ME THEN I VERY THANKFULL TO U
    SO PLZ SEND IT TO ME ON MY MAIL
    MY MAIL ID IS
    [email protected]

  9. Scanning Functionality and Exploration has been added to the AFL
    means in the EOD can it give the scanned names of stocks came under buy signal
    from the list (derived from the eod data of getbhavcopy into ami)

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More