How to find days lapse since last trough

/* Hello AB Programmers.

I need to code for the days lapse since the last Zig Zag Buy Signal (Last Trough) and am currently using 
"AddColumn( IIf( Buy, Daysback, Null ),   "Days since trough",  1.3, textColor = colorWhite, bkgndColor = colorGreen ); " 
but this does not give me what I require.
How should I code this to print correct info in AddColumn
Any and all assistance is greatly appreciated
*/

  _SECTION_BEGIN("ElliottWave-Buy-Sell-Troughs. My Portfoliotest");
SECUpColor=ParamColor("SEC-UpColor",colorGreen); 
Sens=Param("Sens",4.75, 0.5, 15, 0.25); 
BarsSincePeak=BarsSince(Zig(C,Sens)==Peak(C,Sens)); 
BarsSinceTrough= BarsSince(Zig(C,Sens)==Trough(C,Sens)); 
BarsSinceTurnpoint=Min(BarsSincePeak,BarsSinceTrough); 
Daysback = ValueWhen(BarsSinceTurnpoint==0,(Max(PeakBars(C,Sens,1),TroughBars(C,Sens,1))))+1; 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Large Elliott Wave
P1 = ParamField( "ZIG Price field" );
change1 = Param("% change1",4.5,0.25,25,0.25);  // Param( ''name'', defaultval, min, max, step, sincr = 0 ) 
Optimizechange1 = Optimize( "Change1", 5, 1, 30, 2 );   //Optimize("Name", DefaultValue, MinValue, MaxValue, StepValue)
change2 = Param("% change2",6.0 ,1.0 ,12.0,0.25);  // Param( ''name'', defaultval, min, max, step, sincr = 0 ) 

P1 = ParamField( "ZIG Price field" );
change1 = Param("% change1",4.5,0.25,25,0.25);
 
Color3 =(ParamColor("Larger Elliott Wave1",coloryellow));
Plot( zz=TEMA(Zig(P1, change1),1), _DEFAULT_NAME(), Color3, styleLine) ;
 
Color4 =(ParamColor("Larger Elliott Wave2",colorRed));
Plot( zz=TEMA(Zig(P1, change1),2), _DEFAULT_NAME(), Color4, styleLine) ;
 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Buy  = daysback > 2 AND turn < 0 AND C > Ref(C,-1);       
Sell =  C < Ref(C,-1) and  turn >  0 ; //Cross(Slope<0,Slope>0) OR  turn >  0 ;

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

Plot( C, "", colorDefault, styleBar ); // Plots the chart 
PlotShapes(IIf(Buy,  shapeCircle,  shapeNone),colorblue,       0,L, Offset= -70);  
PlotShapes(IIf(Buy, shapeHollowCircle,  shapeNone),colorwhite, 0,L, Offset= -70);                   
PlotShapes(IIf(Buy,  shapeUpArrow, shapeNone),colorwhite,      0,L, Offset=-58); 
PlotShapes(IIf(Sell, shapecircle,  shapeNone),colorred,        0,H, Offset= 60); 
PlotShapes(IIf(Sell, shapeHollowCircle,  shapeNone),colorwhite,0,H, Offset= 60);                       
PlotShapes(IIf(Sell, shapeDownArrow,shapeNone),colorblue,     0,H, Offset= -46); 

if(Status("action") == actionExplore);
Filter = ( Buy OR Sell );
// Comment Only -Add Signal Dates and Prices Headings
average = (H+L+C)/3;
SetOption("NoDefaultColumns", True);
AddTextColumn(Name(), "TICKER");
AddColumn(DateTime(), "Signal Date", formatDateTime);
AddColumn( IIf( Buy, BuyPrice, Null ),   "Buy Signal Close",  1.3, textColor = colorWhite, bkgndColor = colorGreen );
AddColumn( IIf( Buy, Daysback, Null ),   "Days since trough",  1.3, textColor = colorWhite, bkgndColor = colorGreen );
AddColumn( IIf( Sell, SellPrice, Null ), "Sell Signal Close",  1.3, textColor = colorWhite, bkgndColor = colorred );
AddColumn(iif(Buy,Average ,null),"BuyPrice=Days Ave ",1.3,textColor = colorblack, bkgndColor = colorlightblue );
AddColumn( Ref( DateTime(), 1 ), "next day DT", formatDateTime );

// Comment Only -Add Prices and Next day PricesLastClose = EndValue(C);
LV = LastValue(C);
AddColumn(LV,"Last Bar Close ",1.3,textColor = colorblack, bkgndColor = colorlightGrey );
AddColumn(ref(O,1),"ND Open ",1.3,textColor = colorblack, bkgndColor = colorGreen );
AddColumn(ref(H,1),"ND High ",1.3,textColor = colorblack, bkgndColor = colorGreen );
AddColumn(ref(L,1),"ND Low ",1.3,textColor = colorblack, bkgndColor = colorGreen );
AddTextColumn(FullName(), "  Name "); 
_SECTION_END();

Trough() & Peak() functions return continuous trough/peak values in the Array relative to each bar.

If you want to "Detect" changes for a new trough, then you can do it so:

tr = Trough( C, 0.5);
trb = tr != Ref( tr, -1); // New Trough bars element set to 1

bst = BarsSince( trb );   // Bars since recent Trough
// _TRACEF( "%g", LastValue( bst ));
/* Thank you Travick,
Your suggestion works for me in part! so it appears not to report correctly due to my buy condition being something more than tr = Trough( C, 4.75); Please see below and I have tried unsuccessfully to incorporate my buy condition into your code without success so can you please suggest how to do that? */
Buy  = daysback > 2 AND turn < 0 AND C > Ref(C,-1);       
Sell =  C < Ref(C,-1) and  turn >  0 ; //Cross(Slope<0,Slope>0) OR  turn >  0 ;
Buy  = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

// I print the resulting bars since using:
AddColumn( IIf( Buy,bst, Null ),   "Days since trough",  1.3, textColor = colorWhite, bkgndColor = colorGreen );
//Your code
//If you want to "Detect" changes for a new trough, then you can do it so:
tr = Trough( C, 4.75);
trb = tr != Ref( tr, -1); // New Trough bars element set to 1
bst = BarsSince( trb );   // Bars since recent Trough
//_TRACEF( "%g", LastValue( bst ));   

@travick
Read about TroughBars() , it's original built-in function giving the same result as that of your code

1 Like
/*  Thanks greatly  for  the assistance of Travick and Sebastian which has brought me to this point. Clearly I have been asking the wrong question so I will try harder. 
What I needed to know is:-
1	“how many bars have lapse since my code generated a buy signal until today.
2	How to Print 1 above using the AddColumn command
Currently, as per my code AddColumn produces zero’s bars which at the date of signal is correct so
3. How do I overcome this so that AddColumn displays the bars lapsed since the buy signal?
Thanks programmers, I’m learning but very slowly
Keith
*/