Support & Resistance

Looking AFL code for Price Action Breakout Strategy. i.e., Buy on the breach of Resistance Line and Sell on the breach of Support Line

Study

  • reference hand-drawn study Miscellaneous functions
    (AFL 1.5)
SYNTAX Study( STUDYID, CHARTID = 1, scale = -1 )
RETURNS ARRAY
FUNCTION generates an array equivalent to a trendline study drawn by the user - allows detecting trendline breakouts from AFL.
1 Like

Any afl code , please.

//Hi Anthony can you help me with the below code
I want to BUY on the breach of Resistance and SELL on the breach of Support

_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", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("SWING SUPPORT RESISTANCE");

LB= Param("Look Back Periods",10,1,30,1);
R=ValueWhen(Cross(MA(C,LB),C),HHV(H,LB),1);
S=ValueWhen(Cross(C,MA(C,LB)),LLV(L,LB),1);
Color=IIf(O>C,colorBlack,colorBlue);
Plot (R,"Res",colorLime,8+16);
Plot (s,"Sup",colorRed,8+16);
Plot (C,"Close",color,64,32);

GraphXSpace=4;
_SECTION_END();

Moderator comment: If you post code it should be surrounded in code tags as instructed in How to use this site I added proper formatting for you. Failure to comply with forum rules will result in post deletion.

Please use Code button ( </> ) when inserting code here. Thank you.

??

_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", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("SWING SUPPORT RESISTANCE");

LB= Param("Look Back Periods",10,1,30,1);
R=ValueWhen(Cross(MA(C,LB),C),HHV(H,LB),1);
S=ValueWhen(Cross(C,MA(C,LB)),LLV(L,LB),1);
Color=IIf(O>C,colorBlack,colorBlue);
Plot (R,"Res",colorLime,8+16);
Plot (s,"Sup",colorRed,8+16);
Plot (C,"Close",color,64,32);
////Added rules /////////////////////////////////////////////////////////
Buy=Cross(C,R);//Close price inserted 
Sell=Cross(s,C);//Close price inserted 
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
PlotShapes(Buy*shapeUpArrow,colorGreen);//visual to see signal
PlotShapes(Sell*shapeDownArrow,colorRed);//visual to see signal
////////////////////////////////////////////////////////////////////////
GraphXSpace=4;
_SECTION_END();

ok Thanks for the codes

Moderator comment: do NOT quote formulas. The code is just in the post above. There is no need to quote it. And if you post code it should be surrounded in code tags as instructed in How to use this site

Hi Anthony , I tried to backtest it says missing short/cover variable assignments. I needed to place a reversal order on change of Signal . PLEASE HELP

Hello,
I would suggest to do some reading specifically in the users guide under:

Back-testing your trading ideas

where this golden nugget could have been found:

Some in order to back-test short trades you need to assign short and cover variables.
If you use stop-and-reverse system (always on the market) simply assign sell to short and buy to cover

short = sell;    cover = buy;

Thanks Anthony really appreciate your time