Simple Coding Help - No Promise.

Nehal_s143

Well-Known Member
I am using below P&F afl, box size used in below afl is ok for Nifty but if used on Copper and Gold, due to wrong box size Chart becomes of no use.

I was reading ebook on P&F which says



Please box size to be used for copper and gold also how to use 2% instead of
boxsize=IIf(C<0.05, 0.001,
IIf(C>=0.05 AND C<0.1, 0.002,
IIf(C>=0.1 AND C<0.5, 0.005,
IIf(C>=0.5 AND C<2, 0.01,
IIf(C>=2 AND C<5, 0.02,
IIf(C>=5 AND C<10, 0.05,
IIf(C>=10 AND C<50, 0.1,
IIf(C>=50 AND C<100, 0.2,
IIf(C>=100 AND C<200, 0.5,
IIf(C>=200 AND C<500, 1,
2 ))))))))));

or any other suggestion so that same P&F afl can be used for all type of stocks


// dont trail stop loss in range bound market, keep recent high/low as stop loss
// re-entry as P&F signal in same direction after fake opposite signal
_SECTION_BEGIN("Point and Figure1");
//AFL P&F Chart for Amibroker Indicator window. Based on High/low prices.
//Based on code in AB help files
//Reverse is 3 boxes.

SetBarsRequired(100000,100000);

//Size for P&F boxes
boxsize=IIf(C<0.05, 0.001,
IIf(C>=0.05 AND C<0.1, 0.002,
IIf(C>=0.1 AND C<0.5, 0.005,
IIf(C>=0.5 AND C<2, 0.01,
IIf(C>=2 AND C<5, 0.02,
IIf(C>=5 AND C<10, 0.05,
IIf(C>=10 AND C<50, 0.1,
IIf(C>=50 AND C<100, 0.2,
IIf(C>=100 AND C<200, 0.5,
IIf(C>=200 AND C<500, 1,
2 ))))))))));
Box = LastValue(boxsize);
HX = round((H/box)*10)/10;
LX = round((L/box)*10)/10;
RH = floor(HX);
FL = ceil(LX);


// initialize first element
j = 0;

Reverse = 3; // reversal requirement

PFC[j] = FL[0];
PFO[j] = PFC[j] + 1;
down = 1; // By default the first bar is a down bar.
up = 0;
swap = 0;

// perform the loop that produces PF Chart
for( i = 1; i < BarCount; i++ )
{

if( FL <= PFC[j]-1 && down) //continue down
{
PFC[j] = FL;
PFO[j] = PFC[j] + 1;
}
else
{
if( RH >= PFC[j] + Reverse && down) //Change direction to up
{
j++;
swap = 1;
PFC[j] = RH;
PFO[j] = PFC[j]-1;
}
}
if( RH >= PFC[j] + 1 && up) //Continue up
{
PFC[j] = RH;
PFO[j] = PFC[j] - 1;
}
else
{
if( FL <= PFC[j] - Reverse && up) //Change direction to down
{
j++;
PFC[j] = FL;
PFO[j] = PFC[j] + 1;
swap = 1;
}
}
if( swap )
{
swap = 0;
if( up )
{
up = 0;
down = 1;
}
else
{
up = 1;
down = 0;
}
}
}
delta = BarCount - j-1;


PFO = Ref( PFO, -delta );
PFC = Ref( PFC, -delta );

// High-Low range sets the height of the P&F bar
H = IIf(Ref(PFC,-1)>Ref(PFO,-1),Ref(HHV(PFC,1),-1)-1,Max(PFO,PFC))*Box;
L = IIf(Ref(PFC,-1)<Ref(PFO,-1),Ref(LLV(PFC,1),-1)+1,Min(PFO,PFC))*Box;
O =
IIf(Ref(PFC,-1)>Ref(PFO,-1),Ref(HHV(PFC,1),-1)-1,IIf(Ref(PFC,-1)<Ref(PFO,-1),Ref(LLV(PFC,1),-1)+1,PFO))*Box;

// the difference between Open AND Close should be set to box size
// the sign decides if X or O are plotted
C = O + Box * IIf( PFC > PFO, 1,-1);




GraphXSpace = 2;
Title ="" + Name()+ " PF H: " + H+ ", L: " + L+", Box: "+ box
+ ", Reversal: " + reverse;

Plot( C, "P&F Chart Close", IIf( PFC > PFO, colorBlue, colorRed ),styleCandle+styleNoLabel+stylePointAndFigure);

hbar1 = H ;
hbar2 = Ref(H,-2) ;
lbar1 = L ;
lbar2 = Ref(L,-2) ;
Buy = IIf( hbar1 > hbar2 AND PFC > PFO ,True,False);
Sell = IIf(lbar1 < lbar2 AND PFC < PFO ,True,False);


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

PlotShapes(shapeSmallUpTriangle * Buy, colorGreen, 0, L -2);
PlotShapes(shapeSmallDownTriangle * Sell, colorOrange, 0, H -2);


_SECTION_END();
 

Nehal_s143

Well-Known Member
Hi all,

I need a little help.
I have written some rules(buy sell logic) and i want that after 3:00 if there is any signal according to the rules that should not be traded.
Is this possible?

Thanks in advance
you can use below afl for reference

// Open Range Breakout AFL
// This is a simple AFL which will be developed further based on user input.
// SL and re-entry signals will be added.
// Switching from candle to bar chart will be added
// Profit levels will be added
// Current ORB level time is set as 9.30 a.m. which can be easily changed
SetBarsRequired(1000,0);
ORBtype=ParamList("ORB Single or Two levels?","Single|Two");
Style=ParamList("Plot Bars or Candle","Bars|Candle");
BarColor = IIf(Close > Open, colorGreen, colorRed);
SetBarFillColor(BarColor);
if(style=="Bars")Cstyle=styleBar;
else Cstyle=styleCandle;
Plot(C,"Close",colorWhite,Cstyle);
timesetting=093000;
tn=TimeNum();
Datey=DateNum();
fastprice=lev=0;
avp=(O+C)/2;
r2=s2=ar1=ar2=ar3=bs1=bs2=bs3=0;
fastbuy=fastsell=0;
gapzz=10;
Lastsig=Buycond=Shortcond=dayopen=dayclose=dayhigh=daylow=0;
for (i=2;i<BarCount;i++)
{

//PlotText(WriteVal(1,1.0),i,H+15,colorWhite);

//day high low logic
if (Datey!=Datey[i-1])
{
Dayopen=O;
Dayclose=C;
Buycond=Shortcond=lastsig=0;
ar1=ar2=ar3=bs1=bs2=bs3=0;
fastprice=lev=0;
}
if (Datey==Datey[i-1])
{
Dayopen=Dayopen[i-1];
Dayclose=Dayclose[i-1];
Dayhigh=Dayhigh[i-1];
Daylow=Daylow[i-1];
}
if (H>Dayhigh)Dayhigh=H;
if (Daylow==0)Daylow=L;
if (L<Daylow)Daylow=L;
// day high low logic end
//PlotText(WriteVal(i,1.0),i,H+10,colorWhite);

if (tn<timesetting )
{
//PlotText(WriteVal(1,1.0),i,H+10,colorWhite);

r2=Dayhigh;
s2=Daylow;
if (ORbtype=="Single")fastprice=r2=s2=(Dayhigh+Daylow)/2;
}
else
{
//PlotText(WriteVal(1,1.0),i,H+10,colorWhite);

r2=r2[i-1];
s2=s2[i-1];
}
Lev=0;
if (ORbtype=="Single")
{
fastprice=r2;
if (avp>r2)Lev=1;
if (avp<s2)Lev=8;

}
else
{
if (avp>r2){fastprice=r2;Lev=1;}
if (avp<s2){fastprice=s2;Lev=8;}
if (avp<r2 AND avp>s2 AND avp>(r2+s2)/2){fastprice=r2;}
if (avp<r2 AND avp>s2 AND avp<(r2+s2)/2){fastprice=s2;}
}

if (Lev==1)
{
ar1=avp-r2;ar2=avp[i-1]-r2;ar3=avp[i-2]-r2;
//passthrough
if (avp>r2 AND avp[i-1]<r2)Buycond=1;
//kiss and go above
if (ar2<gapzz AND ar1>ar2 AND ar3>ar2) Buycond=1;
}
if (Lev==8)
{
bs1=s2-avp;bs2=s2-avp[i-1];bs3=s2-avp[i-2];
//pass through
if (avp[i-1]>s2 AND avp<s2)Shortcond=1;
//kiss and go
if (bs2<gapzz AND bs1>bs2 AND bs3>bs2) Shortcond=1;
}
if (Lev==1 AND Buycond==1 AND Lastsig!=1)
{
fastbuy=1;
Lastsig=1;
Buycond=shortcond=0;
}
if (Lev==8 AND Shortcond==1 AND Lastsig!=-1)
{
fastsell=1;
Lastsig=-1;
Buycond=shortcond=0;
}
}
shape = fastBuy * shapeUpArrow + fastSell * shapeDownArrow;
PlotShapes( shape, IIf( fastBuy, colorLime, colorRed ), 0, IIf( fastBuy, Low, High));
if (ORbtype=="Single")
{
Plot(r2,"R2",colorYellow,styleLine|styleDashed|styleNoTitle);
}
else
{
Plot(r2,"R2",colorYellow,styleLine|styleDashed|styleNoTitle);
Plot(s2,"S2",colorRed,styleLine|styleDashed|styleNoTitle);
}
 
you can use below afl for reference

// Open Range Breakout AFL
// This is a simple AFL which will be developed further based on user input.
// SL and re-entry signals will be added.
// Switching from candle to bar chart will be added
// Profit levels will be added
// Current ORB level time is set as 9.30 a.m. which can be easily changed
SetBarsRequired(1000,0);
ORBtype=ParamList("ORB Single or Two levels?","Single|Two");
Style=ParamList("Plot Bars or Candle","Bars|Candle");
BarColor = IIf(Close > Open, colorGreen, colorRed);
SetBarFillColor(BarColor);
if(style=="Bars")Cstyle=styleBar;
else Cstyle=styleCandle;
Plot(C,"Close",colorWhite,Cstyle);
timesetting=093000;
tn=TimeNum();
Datey=DateNum();
fastprice=lev=0;
avp=(O+C)/2;
r2=s2=ar1=ar2=ar3=bs1=bs2=bs3=0;
fastbuy=fastsell=0;
gapzz=10;
Lastsig=Buycond=Shortcond=dayopen=dayclose=dayhigh=daylow=0;
for (i=2;i<BarCount;i++)
{

//PlotText(WriteVal(1,1.0),i,H+15,colorWhite);

//day high low logic
if (Datey!=Datey[i-1])
{
Dayopen=O;
Dayclose=C;
Buycond=Shortcond=lastsig=0;
ar1=ar2=ar3=bs1=bs2=bs3=0;
fastprice=lev=0;
}
if (Datey==Datey[i-1])
{
Dayopen=Dayopen[i-1];
Dayclose=Dayclose[i-1];
Dayhigh=Dayhigh[i-1];
Daylow=Daylow[i-1];
}
if (H>Dayhigh)Dayhigh=H;
if (Daylow==0)Daylow=L;
if (L<Daylow)Daylow=L;
// day high low logic end
//PlotText(WriteVal(i,1.0),i,H+10,colorWhite);

if (tn<timesetting )
{
//PlotText(WriteVal(1,1.0),i,H+10,colorWhite);

r2=Dayhigh;
s2=Daylow;
if (ORbtype=="Single")fastprice=r2=s2=(Dayhigh+Daylow)/2;
}
else
{
//PlotText(WriteVal(1,1.0),i,H+10,colorWhite);

r2=r2[i-1];
s2=s2[i-1];
}
Lev=0;
if (ORbtype=="Single")
{
fastprice=r2;
if (avp>r2)Lev=1;
if (avp<s2)Lev=8;

}
else
{
if (avp>r2){fastprice=r2;Lev=1;}
if (avp<s2){fastprice=s2;Lev=8;}
if (avp<r2 AND avp>s2 AND avp>(r2+s2)/2){fastprice=r2;}
if (avp<r2 AND avp>s2 AND avp<(r2+s2)/2){fastprice=s2;}
}

if (Lev==1)
{
ar1=avp-r2;ar2=avp[i-1]-r2;ar3=avp[i-2]-r2;
//passthrough
if (avp>r2 AND avp[i-1]<r2)Buycond=1;
//kiss and go above
if (ar2<gapzz AND ar1>ar2 AND ar3>ar2) Buycond=1;
}
if (Lev==8)
{
bs1=s2-avp;bs2=s2-avp[i-1];bs3=s2-avp[i-2];
//pass through
if (avp[i-1]>s2 AND avp<s2)Shortcond=1;
//kiss and go
if (bs2<gapzz AND bs1>bs2 AND bs3>bs2) Shortcond=1;
}
if (Lev==1 AND Buycond==1 AND Lastsig!=1)
{
fastbuy=1;
Lastsig=1;
Buycond=shortcond=0;
}
if (Lev==8 AND Shortcond==1 AND Lastsig!=-1)
{
fastsell=1;
Lastsig=-1;
Buycond=shortcond=0;
}
}
shape = fastBuy * shapeUpArrow + fastSell * shapeDownArrow;
PlotShapes( shape, IIf( fastBuy, colorLime, colorRed ), 0, IIf( fastBuy, Low, High));
if (ORbtype=="Single")
{
Plot(r2,"R2",colorYellow,styleLine|styleDashed|styleNoTitle);
}
else
{
Plot(r2,"R2",colorYellow,styleLine|styleDashed|styleNoTitle);
Plot(s2,"S2",colorRed,styleLine|styleDashed|styleNoTitle);
}


i appreciate your help but i m very weak in language and didnt get how to make this code to use.. it would be helpful if you precisely solve my query..
thanks
 

dell

Well-Known Member
i am using candle close as a buy and short condition ,
whereas i have seen if we take trigger candle high +2 point as a filter , than we can avoid many bad trades ,

so , how to add condition in buy and short conditions , so it will take trigger candle high + 2 point price as a buy price and trigger candle low -2 points as a short price .....? here we will not take closing price of second candle , we take price just when price just hits or cross our (buy,short)price

also can we plot a horizontal line above our trigger candle for buy and below for short ?
 
Code:
tn = TimeNum();
timecond = 091500 < tn < 150000;
Place the above code any where before the buy/short signals and then below the Buy/Short statements add these lines.

Code:
buy = buy and timecond;
short = short and timecond;
Not tested but should work.
no this doesnt work.. plz suggest something else.
 
Hi all,

I need a little help.
I have written some rules(buy sell logic) and i want that after 3:00 if there is any signal according to the rules that should not be traded.
Is this possible?

Thanks in advance
mktclose=Param("Market Close Time",150000,000000,235959,1 );

x = EMA(Close,3);
y = EMA(Close,8);

Buy=( cross x,y) AND TimeNum() < mktclose ;
Sell=( cross Y,X) AND TimeNum() < mktclose ;

the above code will solve ur problem.
 
Last edited:

dell

Well-Known Member
If your code is something like this
Code:
Buy = Cross(variable, Close);
Short = Cross(variable, Close);
Replace it with
Code:
Buy = Cross(variable, (Close+2));
Short = Cross(variable, (Close-2));

buyprice = Close+2;
Shortprice = Close - 2;
I am not sure if Cross(variable, Open) can be made to work like you wanted.



Something like this should do the job for you.

Code:
// LineArray(starting x-axis, starting y-axis, end x-axis, end y-axis, rightextend=1)
Plot(LineArray(bars-Offset, tar2, BarCount, tar2,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Code not tested.
thanks for help , but i think some other thing or function is also needed as it is not working , maybe valueat or ref,1 -1 ....etc ....
 

Similar threads