AFL based on two buy conditions

hi i am a newbie to afl programming
would like to seek help regarding a query

do help if you find the time thx in advance

my basic buy/sell is based on rsi crossing above 60 to buy and below 40 to sell
would like to add on a another condition wherein the buy happens only when previous bar high price is broken and vice versa
it does not need to close above previous bar high just breaking that price would do
the buy / sell needs to happen only when condi 1 and condi 2 are satisfied

kindly help me code this along with plots for buy and sell trigger

regards
sadayappan

Firstly, you should post your code to get better help because your description is still ambiguous.

Should the Bar H be greater than previous Bar on the crossover of RSI for Buy or only after and when that new H occurs?
same for sell.

Also, you can post an image if your finding it hard to explain in text alone.

1 Like

Below is a code that should do it for you. It assumes that the RSI cross and high above the previous high occurred yesterday and that you are buying on the next day open. Travick is right, you should be very clear with your description and you should post your own attempt at writing a code.

SetTradeDelays(1,1,1,1);
BuyPrice = SellPrice = Open;
HighAboveYdayHigh = High > Ref(High,-1);
LowBelowYdayLow = Low < Ref(Low,-1);
RSIEntryCross = Cross(RSI(),60);
RSIExitCross = Cross(40,RSI());
Buy = RSIEntryCross AND HighAboveYdayHigh;
Sell = RSIExitCross AND LowBelowYdayLow;
Short = Cover = 0;

//exploration
Filter = 1;
AddColumn(Open,"Open",1.2);
AddColumn(High,"High",1.2);
AddColumn(Low,"Low",1.2);
AddColumn(Close,"Close",1.2);
AddColumn(RSIEntryCross,"RSIEntryCross",1.0); 
AddColumn(HighAboveYdayHigh,"HighAboveYdayHigh",1.0); 
AddColumn(RSIExitCross,"RSIExitCross",1.0);
AddColumn(LowBelowYdayLow,"LowBelowYdayLow",1.0); 
AddColumn( IIF( Buy, 'B', ' ' ), "Buy", formatChar, colorDefault, IIf( Buy, colorLime, colorDefault ) );
AddColumn( IIF( Sell, 'S', ' ' ), "Sell", formatChar, colorDefault, IIf( Sell, colorRed, colorDefault ) );

//charting
Type = ParamList("Type","BarChart,IndicatorChart"); 
if(Type =="BarChart") 
{
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( Close, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
Plot( HighAboveYdayHigh, "HighAboveYdayHigh", colorAqua, styleOwnScale | styleArea | styleNoTitle, -3, 50 );
Plot(RSIEntryCross, "RSIEntryCross", colorSeaGreen, styleOwnScale | styleArea | styleNoTitle, -2, 50 ); 
Plot(LowBelowYdayLow, "LowBelowYdayLow", colorRose, styleOwnScale | styleArea | styleNoTitle, -1, 50 );
Plot(RSIExitCross, "RSIExitCross", colorPink, styleOwnScale | styleArea | styleNoTitle,0, 50 );
}
if(Type =="IndicatorChart") 
{
Plot(RSI(),"RSI",colorBlue, styleLine|styleThick );
PlotGrid( 60, colorLime, 10, 2, False ); // solid line 2 pixels thick, no label
PlotGrid( 40, colorRed, 10, 2, False ); // solid line 2 pixels thick, no label
}
3 Likes

hello travick

apologize for the ambiguous request

will go in detail

buy condition one : when rsi closes above 60 wait for candle to close above rsi 60 then check for condition two in the next bar ,

condition two : this bar breaks previous bar high , just moving over high price is enough to
trigger buy ...we need not wait for it to close

likewise short condition one : when rsi closes below 40 then look for condition two , the next bar
prices cuts previous bar low . in both cases just moving above or below the high or low is
sufficient.
i want the trade to trigger only when both conditions are full filled .
condi one is to close above or below rsi / condi 2 is to cut high or low price of the previous rsi bar

sorry again for the ambiguous request

and thx for helping

hi marcel

i agree , sorry for not being clear

thx for the code u have shared

thank you for the time taken to help

i trade in 15 mins and i have replied to travick in detail

kindly have a look and help .

would be greatly appreciated .

regards
sadayappan

SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "Close", ParamColor( "Color", colorBlack ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

SetBarsRequired( 100000, 0 );

per1 = Param( "RSI", 14, 1, 100, 1 );
per2 = Param( "OverBought Level", 70, 1, 100, 1 );
per3 = Param( "OverSold Level", 30, 1, 100, 1 );

SetPositionSize( 1, spsShares );

RSI1= RSI(per1);

Buy = Cross(RSI1,per2);
Sell = Cross(40, RSI1);
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

Short = Cross(per3, RSI1) ;
Cover = Cross(RSI1, 60);
Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );

Title = NumToStr( DateTime(), formatDateTime ) + " O " + O + " H " + H + " L " + L + " C " + C
;

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, shapeDownArrow, shapeNone ), colorRed, 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 );
PlotShapes( IIf( Cover, shapeUpArrow, shapeNone ), colorBlue, 0, L, Offset = -45 );

_SECTION_END();

this is the current code that i am using kindly help me to change the buy according condition one

and condition two

thx

@sadayappan, did you try @Marcel's code?

Did you LEARN how he included the additional condition?

If you learn how he "Ref"erenced (the quotes are a hint) previous day (bars) you can adapt your code to do the same.

Hopefully you take the ideas given and learn and grow.