Buy Sell formula Modification

#1
Dear Sir

I want to add 2 more buy condition

If first buy and sell condition OCCURED then

Second Buy condition on if price cross Buy Line (above upper side) again or
Third Buy condition on if price cross Target0 Line (above upper side) again

Please add the Second and Third condition in AFL or modify afl according to condition.
I have tried but failed now I need Help.


_SECTION_BEGIN("Realtimetips ");
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", colorBlack , styleNoTitle | styleCandle | GetPriceStyle() );
//
messageboard = ParamToggle("Message Board","Show|Hide",0);
showsl = ParamToggle("Stop Loss Line", "Show|Hide", 0);
no=10;
res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
s5d=IIf(avn==1,sup,res);

if (showsl == 0)
{Plot(s5d,"Stop Loss",colorCustom14,styleDots);}
exitlong = Cross(s5d, C);
//PlotShapes(exitlong * shapeDownArrow, colorRed,0,H,-20);
exitshort = Cross(C, s5d);
PlotShapes(exitshort * shapeUpArrow, colorGreen,0,L,-20);


Buy = exitshort;
Sell = exitlong;
Short = exitlong;
Cover = exitshort;

Buytmp=Cross(Buy,0);
Buy = IIf(Ref(Buytmp, -1),1,0);
Selltmp=Cross(Sell,0);
Sell = IIf(Ref(Selltmp, -1),1,0);

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short, Cover);
Cover = ExRem(Cover, Short);
AlertIf( Buy, "", "BUY @ " + C, 1 );
AlertIf( Sell, "", "SELL @ " + C, 2 );


for(i=BarCount-1;i>1;i--)
{
if(Buy == 1)
{
entry = C;
sig = "BUY";
sl = s5d;
tar0 = entry + (entry * .10);
tar1 = entry + (entry * .25);
tar2 = entry + (entry * .50);
tar3 = entry + (entry * .75);
tar4 = entry + (entry * 1.00);
tar5 = entry + (entry * 1.25);
tar6 = entry + (entry * 1.50);


bars = i;
i = 0;
}
if(Sell == 1)
{
sig = "SELL";
entry = C;
sl = s5d;
tar0 = entry - (entry * .10);
tar1 = entry - (entry * .25);
tar2 = entry - (entry * .50);
tar3 = entry - (entry * .75);
tar4 = entry - (entry * 1.00);
tar5 = entry - (entry * 1.125);
tar6 = entry - (entry * 1.150);
bars = i;
i = 0;
}
}
Offset = 20;
Clr = IIf(sig == "BUY", colorLime, colorOrange);
ssl = IIf(bars == BarCount-1, s5d[BarCount-1], Ref(s5d, -1));
sl = ssl[BarCount-1];


Plot(LineArray(bars-Offset, tar0, BarCount, tar0,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar1, BarCount, tar1,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar2, BarCount, tar2,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar3, BarCount, tar3,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar4, BarCount, tar4,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar5, BarCount, tar5,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar6, BarCount, tar6,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, sl, BarCount, sl,1), "", colorCustom14, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, entry, BarCount, entry,1), "", colorTurquoise, styleLine|styleDots, Null, Null, Offset);


for (i=bars; i <BarCount;i++)
{
PlotText("" + sig + " @ " + entry, BarCount+3, entry, Null, colorTurquoise);
PlotText("Target 0 : @ " + tar0, BarCount+3, tar0, Null, Clr);
PlotText("Target 1 : @ " + tar1, BarCount+3, tar1, Null, Clr);
PlotText("Target 2 : @ " + tar2, BarCount+3, tar2, Null, Clr);
PlotText("Target 3 : @ " + tar3, BarCount+3, tar3, Null, Clr);
PlotText("Target 4 : @ " + tar4, BarCount+3, tar4, Null, Clr);
PlotText("Target 5 : @ " + tar5, BarCount+3, tar5, Null, Clr);
PlotText("Target 6 : @ " + tar6, BarCount+3, tar6, Null, Clr);
PlotText("Trailing SL @ " + sl, BarCount+3, sl, Null, colorCustom14);
}
//Plot(sl, "", colorCustom14, styleLine);
printf("Last " + sig + " Signal came " + (BarCount-bars) + " bars ago");
printf("\n" + sig + " @ : " + entry + "\nStop Loss : " + sl + " (" + WriteVal(IIf(sig == "SELL",entry-sl,sl-entry), 2.2) + ")"+ "\nTarget_1 : " + tar1 + "\nTarget_2 : " + tar2 + "\nTarget_3 : " + tar3);
printf("\nCurrent P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2));


if (messageboard == 0 )
{
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );


if ( sig =="BUY")
{
GfxSelectSolidBrush( colorGreen ); // this is the box background color
}
else
{
GfxSelectSolidBrush( colorRed ); // this is the box background color
}
pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 2;
x2 = 290;


y = pxHeight;

x = 290;
x2 = 570;
GfxSelectSolidBrush( colorTurquoise );
GfxSetTextColor( colorBlack);
GfxSelectFont( "Tahoma", 14, 100 );
GfxSetBkMode( 1 );
GfxSelectPen( colorLightBlue, 1); // broader color
//GfxRoundRect( x, y - 43, x2, y , 7, 7 ) ;


GfxSelectFont( "Tahoma", 10, 400 );

}


/////////////M
//GfxRectangle(pxw/2,1,(pxw/2)-5,pxh);


BarB = BeginValue(BarIndex());//Beginvalue of Barindex
BarE = EndValue(BarIndex());//EndValue of Barindex
BarC = BarIndex();//Current Barindex
BarL = LastValue(BarC,1);//LastValue of Barindex
BarT = BarIndex() - Nz(ValueWhen(Day()!= Ref(Day(),-1) ,BarIndex()),0) ;


TrdBegin = ParamTime("Trading Begin Time","09:15",0);
TrdEnd = ParamTime("Trading End Time","15:10",0);
SqOff = ParamTime("Square Off Time","15:20",0);


Lossp = Param("Loss Percentage",20.0,0.05,20,0.05)/100;// 20% Final// Lossp = Param("Loss Percentage",0.6,0.05,20,0.05)/100;
Profitp = Param("Profit Percentage",10.0,0.05,20,0.05)/100;


BuyPrice = ValueWhen(Buy,C);
ShortPrice = ValueWhen(Short,C);//short


BuySL = BuyPrice - (BuyPrice*Lossp);
ShortSL = ShortPrice + (ShortPrice*Lossp);


Buy2 = BuyPrice + (BuyPrice*Profitp);

Buy = Cross(Buy,0) AND TimeNum() >= TrdBegin AND TimeNum() <= TrdEnd;
//FINAL-(Exception is only one Buy Signal comes on Sell side)
Buy2 = Cross(tar0,C) AND TimeNum() >= TrdBegin AND TimeNum() <= TrdEnd;


//TESTING-(Exception Buy2 Condition not works)
//Buy2 = Cross(Buy,0) AND Cross(tar0,C) AND TimeNum() >= TrdBegin AND TimeNum() <= TrdEnd;



Buytmp=Cross(Buy2,0);
Buy2 = IIf(Ref(Buytmp, -1),1,0);


Short = Cross(Short,0) AND TimeNum() >= TrdBegin AND TimeNum() <= TrdEnd;

Sell3 = Short;

Buy = IIf(Ref(Sum(Buy,BarT),-1)==0,Buy,ExRem(Buy,Sell));
Buy2 = IIf(Ref(Sum(Buy2,BarT),-1)==0,Buy2,ExRem(Buy2,Sell));


Short = IIf(Ref(Sum(Short,BarT),-1)==0,Short,ExRem(Short,Buy));


// exit rule
//Exit point
Sell = Cross(tar0,C) OR Cross(tar1,C) OR Cross(tar2,C) OR Cross(tar3,C) OR Cross(tar4,C) OR Cross(tar5,C) OR Cross(H,tar6) OR Cross(C,Short) OR Cross(BuySL,C) OR TimeNum() >= SqOff;;//
Cover = Cross(C,tar1) OR Cross(C,tar2) OR Cross(C,tar3) OR Cross(C,tar4) OR Cross(C,tar5) OR Cross(tar6,L) OR Cross(Buy,C) OR Cross(C,ShortSL) OR TimeNum() >= SqOff;;//
Sell2 = Cross(Short,0) AND Cross(tar1,C) OR Cross(tar2,C) OR Cross(tar3,C) OR Cross(tar4,C) OR Cross(tar5,C) OR Cross(H,tar6) OR Cross(Short,0) OR Cross(BuySL,C) OR TimeNum() >= SqOff;;//



//Selltmp=Cross(Sell,0);
//Sell = IIf(Ref(Selltmp, -1),1,0);


Sell = ExRem(Sell,Buy);
Cover = ExRem(Cover,Short);
//Sell2 = ExRem(Sell,Cover);
//Cover2 = ExRem(Cover,Sell);



PlotShapes(IIf(Buy2, shapeUpArrow, shapeNone),colorYellow, 0,L, Offset=-30);
PlotShapes(IIf (Sell, shapeCircle, shapeNone),colorRed);
PlotShapes(IIf(Sell2, shapeCircle, shapeNone),colorYellow, 0,L, Offset=-45);
//PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);








//MOMIN//////////////////
_SECTION_BEGIN("Magfied Market Price");


//Magfied Market Price
FS=Param("Font Size",30,11,100,1);
GfxSelectFont("Times New Roman", FS, 700, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor( ParamColor("Color",colorYellow) );
Hor=Param("Horizontal Position",1035,1,1200,1);//Hor=Param("Horizontal Position",800,1,1200,1);
Ver=Param("Vertical Position",112,1,830,1); //Ver=Param("Vertical Position",12,1,830,1);
GfxTextOut(""+C, Hor-790 , Ver );
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);
GfxSelectFont("Times New Roman", 11, 700, True );
GfxSetBkMode( colorBlack );
GfxSetTextColor(ParamColor("Color",colorYellow) );
GfxTextOut(""+DD+" ("+xx+"%)", Hor-790 , Ver+45 );


_SECTION_END();
 

Similar threads