Random formula from Internet changes dynamically evertime new Data Arrives

Hi All - I am using PnF chart Afl by Mr. Graham Kavanagh, I have edited the code to some extent for my purposes. This code uses Percentage to define boxvalue.

My Concern is that this code (original & updated one) both cause the PnF historical Chart changes dynamically evertime new Data Arrives. Infact the problem is to the level that a newly created Column of Os or Xs also disappears when a new data arrives.

Others softwares which has native support for PnF does not change historical chart when new value arrives.

So what is intended is to update only the last column or create a new column based on the Trend that the new data point cause else new data should not cause any change in previous Columns.

If this Change can be done, All can Leverage this code as Master code for plotting PnF via AFL.

This level of change is beyong my AFL skills. Can someone please help here on this?

// P&F chart based on closing price
// Date & Bar Number of start & end of columns
// Graham Kavanagh  17 Apr 2004
// Interpretation window data included
// 1:1 Line drawn when column selected with slope=boxsize
// Box size based on ATR of closing price 
// Modified by Abnash Singh abnash1978@yahoo.co.uk on 3rd January 2012
// you may also see Bollinger bands if you uncomment the commented section 
Version(4.53);

SetBarsRequired(100000,100000);

range = Min(260,BarCount/2);
BoxonATR=ParamList("Use ATR to calculate Boxsize?","No|Yes");
bsize=Param("Boxsize if ATR is not used",2,1,50,1);
if (BoxonATR=="Yes")box = LastValue( round(ATR(14)*100)/100 );
else box= (Param("% Box",1,0.25,10,0.25)/100) * LastValue( C );
Reverse_=Param("Reversal",3,1,10,1);

CX = round(C/box);
CF = ceil(Cx);
CR = floor(Cx);

Bar = BarIndex();
BarDate = DateNum();
BarTurn = 0;
BarEnd = 0;

Vol = 0;

// initialize first element
j = 0;
PFC[j] = CF[0];
PFO[j] = CF[0]+1;

down = 1;                  // By default the first bar is a down bar.
up = 0;

// perform the loop that produces PF Chart
for( i = 1; i < BarCount; i++ )
{
 if( CF[i] <= PFC[j]-1 && down)         //continue down
 {
  PFC[j] = CF[i];
  PFO[j] = CF[i]+1;
  BarEnd[j] = Bar[i];
 }
 else
 {
  if( CR[i] >= PFC[j] + Reverse_ && down)  //Change direction to up
  {
   j++;
   up = 1;
   down = 0;
   PFC[j] = CR[i];
   PFO[j] = CR[i] - 1;
   BarTurn[j] = Bar[i];
   BarEnd[j] = Bar[i];
  }
 }
 if( CR[i] >= PFC[j] + 1 && up)         //Continue up
 {
  PFC[j] = CR[i];
  PFO[j] = CR[i] - 1;
  BarEnd[j] = Bar[i];
 }
 else
 {
  if( CF[i] <= PFC[j] - Reverse_ && up)   //Change direction to down
  {
   j++;
   up = 0;
   down = 1;
   PFC[j] = CR[i];
   PFO[j] = CR[i]+1;
   BarTurn[j] = Bar[i];
   BarEnd[j] = Bar[i];
  }
 }
}

delta = BarCount-1 - j;

BarTurns = Ref( BarTurn, -delta); // Starting BarIndex of New Column
BarEnds = Ref( BarEnd, -delta); // Ending BarIndex of New Column

trend_lookback = abs(BarTurns - BarEnds) + 1;




	


PFO = Ref( PFO, -delta );
PFC = Ref( PFC, -delta );
H = IIf( Ref(PFC,-1)>Ref(PFO,-1), Ref(HHV(PFC,1),-1)-1, Max(PFO,PFC) )*box +
box / 2;
L = IIf( Ref(PFC,-1)<Ref(PFO,-1), Ref(LLV(PFC,1),-1)+1, Min(PFO,PFC) )*box -
box / 2;
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 is set to box size
// the sign decides if X or O are plotted
C = O + box * IIf( PFC > PFO, 1, -1 );

top = H - box/2;
bot = L + box/2;





function SumSinceInclusive( condition, array )
	{
	  return SumSince( condition, array ) + ValueWhen( condition, array );
	}
	
//vol_accl = sum(V, trend_lookback);
vol_accl = 0;
box_counts = round((Top - Bot) / Box);

for( i = BarCount - 1; i >= 0; i-- )

{
	
		for( j = BarTurns[i]; j <= BarEnds[i]; j++ )
		{
		
		vol_accl[i] = vol_accl[i] + v[j]; 

		
		
		}
	
	


}













GraphXSpace = 5;
//Title = Name() + " " + Now(1) + ": PF System, H: $" + Top + (for today's date)

_N( 
Title = Name() + " " + Date() + ": PF System, H: $" + Top + ", L: $" + Bot + ", Box $" + Box + " Reversal " + Reverse_ + "\n" + " NewCol bar: " + BarTurns + " Date: " + WriteVal(
ValueWhen(BarIndex()==SelectedValue(BarTurns),DateTime()),formatDateTime)  + " ColEnd bar: " + BarEnds + " Date: " + 
WriteVal(ValueWhen(BarIndex()==SelectedValue(BarEnds),DateTime()),formatDateTime) + "\n" 
+ "\n" + "Box Counts: " +  NumToStr(box_counts,1)
+ "\n" + "Days Counts: " +  NumToStr(trend_lookback,1)
+ "\n" + "Vol : " + NumToStr(SelectedValue(vol_accl), 1)
+ "\n" + "BarIndex : " + NumToStr(BarIndex()+1, 1))

;

Plot(C, "P&F Close", IIf( PFC > PFO, colorGreen, colorRed ), styleCandle + stylePointAndFigure, Null, Null, 0, 0, 1);

This is the question not about AmiBroker but about formula. The reason why it changes is that the formula is written specifically to do so. It uses last bar ATR as box size which obviously will change. I don't know where did you get that formula from and what modifications were done, but original P&F formula uses constant box size. This is crucial.

Long story short - don't download random codes from Internet without understanding what is going on.