Exrem does not seem to work as it is suppose to in my code

I am a beginner at afl and programming as a whole. I am making a trading indicator based on Wave trend. Other indicators I used in this code are vfi , adx, dm+, dm- and OBV. I am having trouble with exrem as it seems it is having some issues as seen in the below image. I also tried debugging but to no avail.

isseu final

NOTE: You will have to turn off all the rules through param using on/off buttons and then only turn on ** rule named: 6.03 If first red signal's value is less than prev. red signal[OB zone] [On/Off] ** to replicate the above scenario.

param

The above param means that the value of vfi should be less than that of the previous wave trend signal that looks like the below image You will have to turn it on and turn off each and every parameter that you can turn off through param to replicate the issue I am facing.

wavetrend down

Any help would be appreciated.

Code(Note It is extremely long and I am sorry for that):

SystemName = "www";
SysVer ="v1.30";
SetFormulaName( SystemName );

SetTradeDelays(1,1,1,1);

#include <siff.afl>

paraCounter = 1;
_N(paraParam = "" + paraCounter + "." +  NumToStr(GetChartID(),1,false) + ".");

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat( "    |     {{DATE}} | Open: %.2f | Close: %.2f (%.2f%%) | Volume: %.0f |{{VALUES}}", O, C, SelectedValue( ROC( C, 1 ) ), V ));
///Plot( C, "Close", colorDefault, styleNoTitle | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("dashboard");

dashboard = ParamToggle("Show Dashboard","Off | On",1);
	
_SECTION_END();

_SECTION_BEGIN("VFI");

length = Param("VFI length",130,0,1000,1);//Param("VFI length",...
coef = Param("Coef",0.2,0,10,0.10);//Param("Coef",...
vcoef = Param("Max volume Cutoff",0.2,0,10,0.10);//Param("Max. vol. cutoff", ...
signalLength = Param("Signal length",5,0,100,1);; //Param(....
show_VFI = ParamToggle("Show VFI","Off | On",1); //ParamToggle(...
smoothVFI = ParamToggle("Smooth VFI","Off | On",1); //ParamToggle(...
showHisto = ParamToggle("Show histo","Off | On",1);;//ParamToggle(...

_SECTION_END();

_SECTION_BEGIN("ADX");

ADX_length = Param("ADX length",14,0,1000,1);//Param("VFI length",...
ExRem_on = ParamToggle("[0.00] exrem on//off [On/Off]","Off | On",1);

_SECTION_END();

_SECTION_BEGIN("Dm + and Dm _");

dm_positive_range = Param("Dm+ Period",14);
dm_negetive_range = Param("Dm- Period",14);

_SECTION_END();


_SECTION_BEGIN("OBV");
length_obv = Param("OBV + Len",10,1,1000);
Plot_obv = ParamToggle("[3.03] Plot obv [On/Off]","Off | On",1);

Aobv = OBV();

bobv = MA(aobv,length_obv);

if(Plot_obv)
	Plot(bobv - 100000000,"OBV",colorGreen,styleLine | styleThick);

_SECTION_END();


_SECTION_BEGIN("Timeframes");	
Timeframe1 = in1Minute *  Param("[TIMEFRAME 1] For Long",60,0,1000,1);
Timeframe2 = in1Minute *  Param("[TIMEFRAME 2] For Long",120,0,1000,1);
Timeframe3 = in1Minute *  Param("[TIMEFRAME 3] For Long",180,0,1000,1);

Timeframe1_p = ParamToggle("[TIMEFRAME 1] Plot [On/Off]","Off | On",1);
Timeframe2_p = ParamToggle("[TIMEFRAME 2] Plot [On/Off]","Off | On",1);
Timeframe3_p = ParamToggle("[TIMEFRAME 3] Plot [On/Off]","Off | On",1);
Timeframe4_p = ParamToggle("[TIMEFRAME 4] Plot [On/Off]","Off | On",1);

Timeframe_l = ParamToggle("[UNIVERSAL] Wave Trend - No. of MTF should be in Up trend for Long and Short [On/Off]","Off | On",1);
Timeframe_long = Param("[UNIVERSAL] Wave Trend - No. of MTF should be in Up trend  For Long and Short",2,0,4,1);
_SECTION_END();

//Buy rules	

_SECTION_BEGIN("[2] Long - VFI should be greater than");	
Vfi_greater_than_l = ParamToggle("[2.01] VFI value should be greater than [On/Off]","Off | On",1);
Vfi_greater_than_long = Param("[2.02] VFI value should be greater than",0.50,-10,10,0.10);
_SECTION_END();
	
_SECTION_BEGIN("[2] Long - VFI value should be more than pre. sell signal Buy signal");	
Vfi_greater_than_prev_l = ParamToggle("[2.03] VFI value should be more than pre. sell signal [On/Off],","Off | On",1);
//Vfi_greater_than_prev_long = Param("[2.04] VFI should be continuesly rising for given no. of Pre. Buy Signals (OS Zone)",0.50,-10,100,0.10);
_SECTION_END();

_SECTION_BEGIN("[2] Long - VFI should be rising for given look back  period");	
Vfi_rising_l = ParamToggle("[2.04] VFI should be rising for given look back  period [On/Off]","Off | On",1);
Vfi_rising_long = Param("[2.05] VFI should be rising for given look back  period",4,0,100,1);
_SECTION_END();

//////NEW//////////////////////

_SECTION_BEGIN("[2] Long - VFI should be in up trend from selected moving average");	
Vfi_ma_l = ParamToggle("[2.06] VFI should be in up trend from selected moving average [On/Off]","Off | On",1);
Vfi_ma_long = Param("[2.07 , UNIVERSAL] VFI should be in up trend from selected moving average",20,0,1000,1);
vfi_ma_plot = Paramtoggle("[OTHER] VFI moving average plot [On/Off]","Off | On",1);
_SECTION_END();

_SECTION_BEGIN("[3] Long - OBV should be continuesly rising for given no. of Pre. Buy Signals (OS Zone) Buy signal");	
Obv_greater_than_prev_l = ParamToggle("[3.01] OBV should be continuesly rising for given no. of Pre. Buy Signals (OS Zone) [On/Off],","Off | On",1);
//Vfi_greater_than_prev_long = Param("[2.04] VFI should be continuesly rising for given no. of Pre. Buy Signals (OS Zone)",0.50,-10,100,0.10);
_SECTION_END();


_SECTION_BEGIN("[3] Long - OBV should be rising for given look back  period");	
obv_rising_l = ParamToggle("[3.01] OBV should be rising for given look back  period [On/Off]","Off | On",1);
obv_rising_long = Param("[3.01] OBV should be rising for given look back  period",0.50,-10,100,0.10);
_SECTION_END();

//////////////////////////

_SECTION_BEGIN("[4] Long - ADX value should be greater than");	
ADX_greater_than_l = ParamToggle("[4.01] ADX value should be greater than [On/Off]","Off | On",1);
ADX_greater_than_long = Param("[4.02] ADX value should be greater than",20,0,100,1);
_SECTION_END();

_SECTION_BEGIN("[4] Long - ADX should be continuesly rising for given no. of Pre. Buy Signals");	
ADX_greater_than_pre_l = ParamToggle("[4.03] ADX should be continuesly rising for given no. of Pre. Buy Signals [On/Off]","Off | On",1);
ADX_greater_than_pre_long = Param("[4.04] ADX should be continuesly rising for given no. of Pre. Buy Signals ",20,-10,100,0.10);
_SECTION_END();

_SECTION_BEGIN("[4] Long - ADX should be rising for given look back  period");	
ADX_rising_l = ParamToggle("[4.04] ADX should be rising for given look back  period [On/Off]","Off | On",1);
ADX_rising_long = Param("[4.04] ADX should be rising for given look back  period ",4,0,100,1);
_SECTION_END();

_SECTION_BEGIN("[5] Long - DM + Value should be greater than");	
Dm_p_more_than_l = ParamToggle("[5.01] DM + Value should be greater than [On/Off]","Off | On",1);
Dm_p_more_than_long = Param("[5.02] DM + Value should be greater than",20,0,100,0.10);
_SECTION_END();

_SECTION_BEGIN("[5] Long - DM - Value should be lesser than");	
Dm_n_less_than_l = ParamToggle("[5.03] DM - Value should be lesser than [On/Off]","Off | On",1);
Dm_n_less_than_long = Param("[5.04] DM - Value should be lesser than",20,0,100,0.10);
_SECTION_END();

_SECTION_BEGIN("[5] Long - Gap between DM + and DM -  should be higher than");	
gap_more_than_l = ParamToggle("[5.05] Gap between DM+ and DM- should be higher than [On/Off]","Off | On",1);
gap_more_than_long = Param("[5.06] Gap between DM+ and DM- should be higher than",20,0,100,0.10);
_SECTION_END();



// FOR SELL

_SECTION_BEGIN("[6.00] Sell - VFI -  If 1st Red Signal's look back value is FALLING for given period");	
Vfi_falling_s = ParamToggle("[6.01] VFI -  If 1st Red Signal's look back value is FALLING for given period [On/Off]","Off | On",1);
Vfi_falling_sell = Param("[6.02] VFI -  If 1st Red Signal's look back value is FALLING for given period",20,0,100,1);
_SECTION_END();

_SECTION_BEGIN("[6.00] Sell - VFI - If first red signal's value is less than prev. red signal [OB Zone]");	
prev_red_compare_s = ParamToggle("[6.03] If first red signal's value is less than prev. red signal [OB Zone] [On/Off]","Off | On",1);
_SECTION_END();

_SECTION_BEGIN("[7.00] Sell - OBV - 1st Red Signal's look back value is FALLING for period");	
obv_falling_s = ParamToggle("[7.01] OBV - 1st Red Signal's look back value is FALLING for period [On/Off]","Off | On",1);
obv_falling_sell = Param("[7.02] VFI - 1st Red Signal's look back value is FALLING for period",20,0,100,1);
_SECTION_END();

_SECTION_BEGIN("[7.00] Sell - OBV - If any Red signal'OBV value get lower than Prev. Red signal's OBV");	
prev_obv_compare_s = ParamToggle("[7.03] If any Red signal'OBV value get lower than Prev. Red signal's OBV [On/Off]","Off | On",1);
_SECTION_END();

//FOR SHORT
_SECTION_BEGIN("[8.00] Short - VFI  should be lesser than");	
Vfi_lesser_than_sh = ParamToggle("[8.01] VFI  should be lesser than [On/Off]","Off | On",1);
Vfi_lesser_than_short = Param("[8.02] VFI  should be lesser than",4,-10,100,0.10);
_SECTION_END();

_SECTION_BEGIN("[8.03] Short - VFI value should be lesser than pre. sell signal (OB Zone");	
Vfi_prev_sh = ParamToggle("[8.03] VFI value should be lesser than pre. sell signal (OB Zone)[On/Off]","Off | On",1);
_SECTION_END();

_SECTION_BEGIN("[8.04] Short - VFI should be falling for given look back period");	
Vfi_falling_sh = ParamToggle("[8.04] VFI Average value is Falling for look back period [On/Off]","Off | On",1);
Vfi_falling_short = Param("[8.05] VFI Average value is Falling for look back period",4,1,100,1);
_SECTION_END();

_SECTION_BEGIN("[8.06] Short - VFI should be in Down Trend from selected moving average");	
vfi_ma_sh = ParamToggle("[8.07] VFI should be in Down Trend from selected moving average [On/Off]","Off | On",1);
//vfi_ma_short = Param("[8.08] VFI should be in Down Trend from selected moving average",4,1,100,1);
_SECTION_END();

_SECTION_BEGIN("[8.04] Short - VFI should be falling for given look back period");	
Vfi_falling_sh = ParamToggle("[8.04] VFI Average value is Falling for look back period [On/Off]","Off | On",1);
Vfi_falling_short = Param("[8.05] VFI Average value is Falling for look back period",4,1,100,1);
_SECTION_END();

_SECTION_BEGIN("[9.01] Short - OBV should be continuesly falling for given no. of Pre. Sell Signals (OB Zone");	
obv_prev_sh = ParamToggle("[9.01] OBV should be continuesly falling for given no. of Pre. Sell Signals (OB Zone)[On/Off]","Off | On",1);
_SECTION_END();

_SECTION_BEGIN("[9.02] Short - OBV should be falling for given look back period");	
OBV_falling_sh = ParamToggle("[9.02] OBV Average value is Falling for look back period [On/Off]","Off | On",1);
OBV_falling_short = Param("[9.03] OBV Average value is Falling for look back period",4,1,100,1);
_SECTION_END();

_SECTION_BEGIN("[10.00] Short - ADX should be greater than");	
ADX_greater_sh = ParamToggle("[10.01] ADX should be greater than [On/Off]","Off | On",1);
ADX_greater_short = Param("[10.02] ADX should be greater than",20,-10,100,0.10);
_SECTION_END();

_SECTION_BEGIN("[10.04] Short - ADX value should be greater than pre. Sell signal (OB Zone)");	
adx_prev_sh = ParamToggle("[10.04] ADX value should be greater than pre. Sell signal (OB Zone) [On/Off]","Off | On",1);
//adx_prev_short = Param("[10.05] ADX value should be greater than pre. Sell signal (OB Zone)",20,-10,100,0.10);
_SECTION_END();

_SECTION_BEGIN("[10.05] Short - DM + Value should be lesser than ");	
dm_p_lesser_than_sh = ParamToggle("[10.06] DM+ value should be lesser than [On/Off]","Off | On",1);
dm_p_lesser_than_short = Param("[10.07] If DM+ value should be lesser than ",20,-10,100,0.10);
_SECTION_END();

_SECTION_BEGIN("[10.08] Short - DM- value should be more than");	
dm_n_more_than_sh = ParamToggle("[10.08] DM- value should be more than [On/Off]","Off | On",1);
dm_n_more_than_short = Param("[10.09] If DM- value should be more than",20,-10,100,0.10);
_SECTION_END();

_SECTION_BEGIN("[10.10] Short - Gap between DM + and DM -  should be higher than");	
gap_more_than_sh = ParamToggle("[10.10] Gap between DM + and DM -  should be higher than [On/Off]","Off | On",1);
gap_more_than_short = Param("[10.11] Gap between DM + and DM -  should be higher than",20,0,100,0.10);
_SECTION_END();

//FOR Cover

_SECTION_BEGIN("[11.00] Cover - VFI value is rising for given look back period");	
Vfi_rising_c = ParamToggle("[11.00] VFI value is rising for given look back period for Cover [On/Off]","Off | On",1);
Vfi_rising_cover = Param("[11.01] VFI value is rising for given look back period For Cover",4,0,100,1);
_SECTION_END();

_SECTION_BEGIN("[11.03] Cover - VFI  - When any Blue Signal's value become higher than Pre. Blue Signal (O S Zone)");	
prev_vfi_c = ParamToggle("[11.03] VFI  - When any Blue Signal's value become higher than Pre. Blue Signal (O S Zone) [On/Off]","Off | On",1);
//prev_vfi_cover = Param("[11.04] If diff. between 1st Red Signal - Pre.Blue Signal is lesser than given value",20,-10,100,0.10);
_SECTION_END();

_SECTION_BEGIN("[11.04] Cover - OBV value is rising for given look back period");	
OBV_rising_c = ParamToggle("[11.04] OBV value is rising for given look back period for Cover [On/Off]","Off | On",1);
OBV_rising_cover = Param("[11.05] OBV value is rising for given look back period For Cover",4,0,100,1);
_SECTION_END();

_SECTION_BEGIN("[11.06] Cover - OBV  - When any Blue Signal's value become higher than Pre. Blue Signal (O S Zone)");	
prev_obv_c = ParamToggle("[11.06] OBV  - When any Blue Signal's value become higher than Pre. Blue Signal (O S Zone) [On/Off]","Off | On",1);
_SECTION_END();


TimeFrameSet(Timeframe1);

	#include <myfunctions.afl>

	n1 = param("n1",14);
	n2 = param("n2",14);
	reaction_wt  = param("recation",1);



	nsc = param("nsc",50);
	nsv = Param("nsv",-50,-100,0);

	upper = Param("upper",1,0,1,1);
	lower = Param("lower",1,0,1,1);

	red=colorRed;
	green=colorBlue;

	ap = (H+L+c)/3; 
	esa = ema(ap, n1);
	d = ema(abs(ap - esa), n1);
	ci = (ap - esa) / (0.015 * d);
	tci = ema(ci, n2);
	 
	wt1 = tci;
	wt2 = ma(wt1,4);

	direction11 = 0;
	direction11 = IIf(rising(wt1,reaction_wt), 1, IIf(falling(wt1,reaction_wt),-1, 0));
	direction2 = IIf(direction11 == 0,Ref(direction11,-1),direction11);
	direction = IIf(direction2 == 0,Ref(direction11,-1),direction2);
	cambio_direccion= change(direction,1);
	pcol = IIf(direction>0 , green ,IIf( direction<0,red,0));



	
	midpoint = (nsc + nsv) / 2;
	ploff = (nsc - midpoint) / 8;

	down = crossunder(wt1, wt2) and wt1 >= nsc and upper == True;

	up = crossover(wt1, wt2) and wt1 <= nsv and lower == True;
	higherTf = inHourly*4;
	data =wt1;
	data1=wt2;
	chartOnLowerTf = 1;
	tfIsintraday = 1;
	
	sec1ft = Ref(wt1,-1); 

	sec1ft1 = Ref(wt2,-1); 
	//
	down1 = crossunder(sec1ft, sec1ft1) and sec1ft >= nsc and upper == True;

	up1 = crossover(sec1ft, sec1ft1) and sec1ft <= nsv and lower == True;
	//
	direction1 = 0;
	direction1 = IIf(rising(sec1ft,reaction_wt) , 1 ,IIf( falling(sec1ft,reaction_wt) , -1 , nz(Ref(direction1,-1))));
	cambio_direccion1= change(direction1,1);
	pcol1 = IIf(direction1>0 , green , IIf(direction1<0 , red , 0));
	
	isdirect = direction1 == 1;
	isdirects = direction1 == -1;
	valueoncrossoverb = ValueWhen(isdirect,sec1ft);
	valueoncrossovers = ValueWhen(isdirects,sec1ft);
	valueoncrossover = IIf(isdirect,valueoncrossovers ,IIf(isdirects,valueoncrossoverb,0));
	
	barsincecrossover = BarsSince(direction1 == 1 AND Ref(direction1,-1) == -1);
	is_p_or_n = IIf(isdirect,1,0);
	
	
	

	//

TimeFrameRestore();


sec1ft_timeframe_1 = TimeFrameExpand(sec1ft,Timeframe1);
pcol1_timeframe_1 = TimeFrameExpand(pcol1,Timeframe1);
is_p_or_n_timeframe_1 = TimeFrameExpand(is_p_or_n,Timeframe1);



TimeFrameSet(Timeframe2);

	

	n1 = param("n1",14);
	n2 = param("n2",14);
	reaction_wt  = param("recation",1);



	nsc = param("nsc",50);
	nsv = Param("nsv",-50,-100,0);

	upper = Param("upper",1,0,1,1);
	lower = Param("lower",1,0,1,1);

	red=colorRed;
	green=colorBlue;

	ap = (H+L+c)/3; 
	esa = ema(ap, n1);
	d = ema(abs(ap - esa), n1);
	ci = (ap - esa) / (0.015 * d);
	tci = ema(ci, n2);
	 
	wt1 = tci;
	wt2 = ma(wt1,4);

	direction11 = 0;
	direction11 = IIf(rising(wt1,reaction_wt), 1, IIf(falling(wt1,reaction_wt),-1, 0));
	direction2 = IIf(direction11 == 0,Ref(direction11,-1),direction11);
	direction = IIf(direction2 == 0,Ref(direction11,-1),direction2);
	cambio_direccion= change(direction,1);
	pcol = IIf(direction>0 , green ,IIf( direction<0,red,0));



	
	midpoint = (nsc + nsv) / 2;
	ploff = (nsc - midpoint) / 8;

	down = crossunder(wt1, wt2) and wt1 >= nsc and upper == True;

	up = crossover(wt1, wt2) and wt1 <= nsv and lower == True;
	higherTf = inHourly*4;
	data =wt1;
	data1=wt2;
	chartOnLowerTf = 1;
	tfIsintraday = 1;
	
	
	sec1ft = Ref(wt1,-1); 

	sec1ft1 = Ref(wt2,-1); 
	//
	down1 = crossunder(sec1ft, sec1ft1) and sec1ft >= nsc and upper == True;

	up1 = crossover(sec1ft, sec1ft1) and sec1ft <= nsv and lower == True;
	//
	direction1 = 0;
	direction1 = IIf(rising(sec1ft,reaction_wt) , 1 ,IIf( falling(sec1ft,reaction_wt) , -1 , nz(Ref(direction1,-1))));
	cambio_direccion1= change(direction1,1);
	pcol1 = IIf(direction1>0 , green , IIf(direction1<0 , red , 0));
	
	isdirect = direction1 == 1;
	isdirects = direction1 == -1;
	valueoncrossoverb = ValueWhen(isdirect,sec1ft);
	valueoncrossovers = ValueWhen(isdirects,sec1ft);
	valueoncrossover = IIf(isdirect,valueoncrossovers ,IIf(isdirects,valueoncrossoverb,0));
	
	barsincecrossover = BarsSince(direction1 == 1 AND Ref(direction1,-1) == -1);
	is_p_or_n = IIf(isdirect,1,0);
	
	
	

	//

TimeFrameRestore();


sec1ft_timeframe_2 = TimeFrameExpand(sec1ft,Timeframe2);
pcol1_timeframe_2 = TimeFrameExpand(pcol1,Timeframe2);
is_p_or_n_timeframe_2 = TimeFrameExpand(is_p_or_n,Timeframe2);


TimeFrameSet(Timeframe3);

	

	n1 = param("n1",14);
	n2 = param("n2",14);
	reaction_wt  = param("recation",1);



	nsc = param("nsc",50);
	nsv = Param("nsv",-50,-100,0);

	upper = Param("upper",1,0,1,1);
	lower = Param("lower",1,0,1,1);

	red=colorRed;
	green=colorBlue;

	ap = (H+L+c)/3; 
	esa = ema(ap, n1);
	d = ema(abs(ap - esa), n1);
	ci = (ap - esa) / (0.015 * d);
	tci = ema(ci, n2);
	 
	wt1 = tci;
	wt2 = ma(wt1,4);

	direction11 = 0;
	direction11 = IIf(rising(wt1,reaction_wt), 1, IIf(falling(wt1,reaction_wt),-1, 0));
	direction2 = IIf(direction11 == 0,Ref(direction11,-1),direction11);
	direction = IIf(direction2 == 0,Ref(direction11,-1),direction2);
	cambio_direccion= change(direction,1);
	pcol = IIf(direction>0 , green ,IIf( direction<0,red,0));



	
	midpoint = (nsc + nsv) / 2;
	ploff = (nsc - midpoint) / 8;

	down = crossunder(wt1, wt2) and wt1 >= nsc and upper == True;

	up = crossover(wt1, wt2) and wt1 <= nsv and lower == True;
	higherTf = inHourly*4;
	data =wt1;
	data1=wt2;
	chartOnLowerTf = 1;
	tfIsintraday = 1;
	
	sec1ft = Ref(wt1,-1); 

	sec1ft1 = Ref(wt2,-1); 
	//
	down1 = crossunder(sec1ft, sec1ft1) and sec1ft >= nsc and upper == True;

	up1 = crossover(sec1ft, sec1ft1) and sec1ft <= nsv and lower == True;
	//
	direction1 = 0;
	direction1 = IIf(rising(sec1ft,reaction_wt) , 1 ,IIf( falling(sec1ft,reaction_wt) , -1 , nz(Ref(direction1,-1))));
	cambio_direccion1= change(direction1,1);
	pcol1 = IIf(direction1>0 , green , IIf(direction1<0 , red , 0));
	
	isdirect = direction1 == 1;
	isdirects = direction1 == -1;
	valueoncrossoverb = ValueWhen(isdirect,sec1ft);
	valueoncrossovers = ValueWhen(isdirects,sec1ft);
	valueoncrossover = IIf(isdirect,valueoncrossovers ,IIf(isdirects,valueoncrossoverb,0));
	
	barsincecrossover = BarsSince(direction1 == 1 AND Ref(direction1,-1) == -1);
	is_p_or_n = IIf(isdirect,1,0);
	
	
	

	//

TimeFrameRestore();


sec1ft_timeframe_3 = TimeFrameExpand(sec1ft,Timeframe3);
pcol1_timeframe_3 = TimeFrameExpand(pcol1,Timeframe3);
is_p_or_n_timeframe_3 = TimeFrameExpand(is_p_or_n,Timeframe3);


TimeFrameSet(inHourly*4);

	

	n1 = param("n1",14);
	n2 = param("n2",14);
	reaction_wt  = param("recation",1);



	nsc = param("nsc",50);
	nsv = Param("nsv",-50,-100,0);

	upper = Param("upper",1,0,1,1);
	lower = Param("lower",1,0,1,1);

	red=colorRed;
	green=colorBlue;

	ap = (H+L+c)/3; 
	esa = ema(ap, n1);
	d = ema(abs(ap - esa), n1);
	ci = (ap - esa) / (0.015 * d);
	tci = ema(ci, n2);
	 
	wt1 = tci;
	wt2 = ma(wt1,4);

	direction11 = 0;
	direction11 = IIf(rising(wt1,reaction_wt), 1, IIf(falling(wt1,reaction_wt),-1, 0));
	direction2 = IIf(direction11 == 0,Ref(direction11,-1),direction11);
	direction = IIf(direction2 == 0,Ref(direction11,-1),direction2);
	cambio_direccion= change(direction,1);
	pcol = IIf(direction>0 , green ,IIf( direction<0,red,0));



	
	midpoint = (nsc + nsv) / 2;
	ploff = (nsc - midpoint) / 8;

	down = crossunder(wt1, wt2) and wt1 >= nsc and upper == True;

	up = crossover(wt1, wt2) and wt1 <= nsv and lower == True;
	higherTf = inHourly*4;
	data =wt1;
	data1=wt2;
	chartOnLowerTf = 1;
	tfIsintraday = 1;
	
	sec1ft = Ref(wt1,-1); 

	sec1ft1 = Ref(wt2,-1); 
	//
	down1 = crossunder(sec1ft, sec1ft1) and sec1ft >= nsc and upper == True;

	up1 = crossover(sec1ft, sec1ft1) and sec1ft <= nsv and lower == True;
	//
	direction1 = 0;
	direction1 = IIf(rising(sec1ft,reaction_wt) , 1 ,IIf( falling(sec1ft,reaction_wt) , -1 , nz(Ref(direction1,-1))));
	cambio_direccion1= change(direction1,1);
	pcol1 = IIf(direction1>0 , green , IIf(direction1<0 , red , 0));
	
	isdirect = direction1 == 1;
	isdirects = direction1 == -1;
	valueoncrossoverb = ValueWhen(isdirect,sec1ft);
	valueoncrossovers = ValueWhen(isdirects,sec1ft);
	valueoncrossover = IIf(isdirect,valueoncrossovers ,IIf(isdirects,valueoncrossoverb,0));
	
	barsincecrossover = BarsSince(direction1 == 1 AND Ref(direction1,-1) == -1);
	
	
	is_p_or_n = IIf(isdirect,1,0);

	//

TimeFrameRestore();

is_p_or_n_timeframe_4 = TimeFrameExpand(is_p_or_n,inHourly*4);

typical = Avg;
inter = log( typical ) - log(Ref(typical,-1));
vinter = StDev(inter, 30);
cutoff = coef * vinter * Close;
vave = Ref(MA(Volume, length ),-1);
vmax = vave * vcoef;
vc = Min( Volume, vmax );
mf = typical - Ref(typical,-1);
vcp = IIf( mf > cutoff, vc, IIf( mf < -cutoff, -vc, 0 ) );

x = SafeDivide(Sum(vcp, length),vave);
vfi = IIf(smoothVFI, MA(x, 3), x);
vfima = EMA( vfi, signalLength );
d = vfi-vfima;

if(show_vfi)
	Plot( vfi, "vfi", colorGreen, styleLine, Null, Null, 0, 0, 2);
	
if ( showHisto AND show_vfi) 
	Plot(d, "d", colorGrey40, styleHistogram, Null, Null, 0, 0, 3 );
dm_positive = PDI(dm_positive_range);
dm_negetive = mdi(dm_negetive_range);

greater = MDI(dm_negetive_range)-PDI(dm_positive_range);
gap= IIf(greater>0,MDI(dm_negetive_range)-PDI(dm_positive_range),PDI(dm_positive_range)-MDI(dm_negetive_range));

n1 = param("n1",14);
n2 = param("n2",14);
reaction_wt  = param("recation",1);

nsc = param("nsc",50);
nsv = Param("nsv",-50,-100,0);

upper = Param("upper",1,0,1,1);
lower = Param("lower",1,0,1,1);

red1=colorRed;
green1=colorBlue;

ap1 = (H+L+c)/3; 
esa1 = ema(ap1, n1);
d1 = ema(abs(ap1 - esa1), n1);
ci1 = (ap1 - esa1) / (0.015 * d1);
tci1 = ema(ci1, n2);

wt11 = tci1;
wt21 = ma(wt11,4);

direction111 = 0;
direction111 = IIf(rising(wt11,reaction_wt), 1, IIf(falling(wt11,reaction_wt),-1, 0));
direction21 = IIf(direction111 == 0,Ref(direction111,-1),direction111);
direction1 = IIf(direction21 == 0,Ref(direction111,-1),direction21);
cambio_direccion1= change(direction1,1);
pcol111 = IIf(direction1>0 , green1 ,IIf( direction1<0,red1,0));
pcol111cus = IIf(direction1>0 , colorWhite ,IIf( direction1<0,colorYellow,0));
pcol1111 = IIf(direction1>0 , colorWhite ,IIf( direction1<0,colorRed,0));


Plot(wt11,"wt11",pcol111);
Plot(wt21,"wt11",pcol1111);

Plot(nsv,"nsv",colorred);
Plot(nsc,"nsc",colorBlue);

sellmini = Cross(wt21,wt11) AND wt21 > nsc AND wt11 > nsc;
buymini = Cross(wt11,wt21) AND wt21 < nsv AND wt11 < nsv;


if(timeframe4_p)
	plot(TimeFrameExpand(sec1ft,inHourly*4),"t 4",TimeFrameExpand(pcol1,inHourly*4));
if(timeframe3_p)
	plot(sec1ft_timeframe_3,"t 3",pcol1_timeframe_3);
if(timeframe2_p)
	plot(sec1ft_timeframe_2,"t 2",pcol1_timeframe_2);
if(timeframe1_p)
	plot(sec1ft_timeframe_1,"t 1",pcol1_timeframe_1);


Plot(nsv,"nsv",colorred);
Plot(nsc,"nsc",colorBlue);

//////////
offset = Param("ofset",5,0,100,1,0);



buymega1 = (TimeFrameExpand(valueoncrossover,inHourly*4) - TimeFrameExpand(sec1ft,inHourly*4)) > offset AND TimeFrameExpand(sec1ft,inHourly*4) > nsc AND TimeFrameExpand(valueoncrossover,inHourly*4) > nsc;
sellmega = Ref(buymega1,-1) == 0 AND buymega1 == 1 ;

sellmega1 = TimeFrameExpand(barsincecrossover,inHourly*4) == 0 AND  TimeFrameExpand(valueoncrossover,inHourly*4) < nsv AND (TimeFrameExpand(valueoncrossover,inHourly*4) - TimeFrameExpand(sec1ft,inHourly*4)) <	 (offset/-1);
buymega = Ref(sellmega1,-1) == 0 AND sellmega1 == 1 ;
 
timeframes_p = is_p_or_n_timeframe_1 + is_p_or_n_timeframe_2 + is_p_or_n_timeframe_3 + is_p_or_n_timeframe_4;
timeframes_n = IIf(timeframes_p == 4,0,IIf(timeframes_p == 3,1,IIf(timeframes_p == 2,2,IIf(timeframes_p == 1,3,IIf(timeframes_p == 0,4,null)))));

prev_lc = BarsSince(buymini) - BarsSince(sellmini);


prev = vfi > Ref(ValueWhen(buymini,vfi,1),-1);
prev_obv = bobv > Ref(ValueWhen(buymini,bobv,1),-1);
prev_Adx = ADX(ADX_length) - ValueWhen(buymini OR sellmini,ADX(ADX_length),2);
prev_adx_2 = ADX(ADX_length) > Ref(ValueWhen(buymini,ADX(ADX_length),1),-1);

vfima12 = MA(vfi,Vfi_ma_long);

if(vfi_ma_plot)
	Plot(vfima12,"VFIMA12",colorYellow,styleLine|styleThick);


if(Vfi_greater_than_l)
	Vfi_greater_Than_long = VFI > Vfi_greater_than_long;
else 
	Vfi_greater_Than_long = 1;
	
if(Vfi_greater_than_prev_l)
	prev_compare_rule_long = prev;
else
	prev_compare_rule_long = 1;
	
if(Vfi_rising_l)
	vfi_rising_rule_l = rising(vfi,vfi_rising_long);
else
	vfi_rising_rule_l = 1;

if(vfi_ma_l)
	vfi_ma_l_rule = vfi > vfima12;
else
	vfi_ma_l_rule = 1;
	
if(OBV_rising_l)
	OBV_rising_l_rule = rising(bOBV,OBV_rising_long);
else
	OBV_rising_l_rule = 1;
	
if(OBV_greater_than_prev_l)
	OBV_greater_than_prev_l_rule = prev_obv;
else
	OBV_greater_than_prev_l_rule = 1;

if(ADX_greater_than_l)
	ADX_greater_than_rule_l = ADX(ADX_length) > ADX_greater_than_long;
else
	ADX_greater_than_rule_l = 1;
	
if(ADX_greater_than_pre_l)
	ADX_greater_than_rule_pre_l = prev_adx_2;// > ADX_greater_than_pre_long;
else
	ADX_greater_than_rule_pre_l = 1;
	
if(ADX_rising_l)
	ADX_rising_l_rule = rising(ADX(ADX_length),ADX_rising_long);// > ADX_greater_than_pre_long;
else
	ADX_rising_l_rule = 1;

if(Dm_p_more_than_l)
	Dm_p_more_than_l_rule = dm_positive > Dm_p_more_than_long;
else
	Dm_p_more_than_l_rule = 1;
	
if(Dm_n_less_than_l)
	Dm_n_less_than_l_rule = dm_negetive < Dm_n_less_than_long;
else
	Dm_n_less_than_l_rule = 1;
	
if(gap_more_than_l)
	gap_more_than_l_rule = gap >  gap_more_than_long;
else
	gap_more_than_l_rule = 1;
	
if(Timeframe_l)
	Timeframe_l_rule = timeframes_p >=  Timeframe_long;
else
	Timeframe_l_rule = 1;

Buy = buymini AND Vfi_greater_Than_long AND prev_compare_rule_long AND vfi_rising_rule_l AND vfi_ma_l_rule AND  OBV_rising_l_rule AND OBV_greater_than_prev_l_rule AND ADX_rising_l_rule AND  /* */ADX_greater_than_rule_l AND ADX_greater_than_rule_pre_l AND Dm_p_more_than_l_rule AND Dm_n_less_than_l_rule AND gap_more_than_l_rule AND Timeframe_l_rule;

prev_vfi = VFI - ValueWhen(buymini,vfi,2);
prev_red = VFI < ValueWhen(sellmini,vfi,2);
prev_red_sh = VFI - ValueWhen(sellmini,vfi,2);
prev_obv = bobv < Ref(ValueWhen(sellmini,bobv,1),-1);
prev_ADX = ADX(ADX_length) - ValueWhen(sellmini OR buymini ,ADX(ADX_length),2);
prev_ADX_sh = ValueWhen(sellmini ,ADX(ADX_length),2) - ADX(ADX_length);
prev_ADX_sht = ADX(ADX_length) < ValueWhen(sellmini ,ADX(ADX_length),2);


prev_lc_less = prev_lc < 0;

if(vfi_falling_s)
	vfi_falling_s_rule = falling(vfi,Vfi_falling_sell);
else	
	vfi_falling_s_rule = 1;
	
if(obv_falling_s)
	obv_falling_s_rule = falling(bobv,obv_falling_sell);
else	
	obv_falling_s_rule = 1;
	

if(prev_Red_compare_S)
	prev_Red_compare_S_rule = prev_red;
else	
	prev_Red_compare_S_rule = 1;
	
if(prev_OBV_compare_S)
	prev_OBV_compare_S_rule = prev_OBV;
else	
	prev_OBV_compare_S_rule = 1;



	
Sell = sellmini AND vfi_falling_s_rule AND   prev_Red_compare_S_rule AND prev_OBV_compare_S_rule AND obv_falling_s_rule ;

vfimash = MA(vfi,Vfi_ma_long);


if(vfi_lesser_than_sh)
	vfi_lesser_than_sh_rule = vfi < vfi_lesser_than_short;
else	
	vfi_lesser_than_sh_rule = 1;
	

prev_vfi_sh = vfi < Ref(ValueWhen(sellmini,vfi),-1);
prev_obv_sh = BOBV < Ref(ValueWhen(sellmini,bobv),-1);
prev_adx_sh12 = ADX(ADX_length) < Ref(ValueWhen(sellmini,ADX(ADX_length)),-1);

	
if(vfi_ma_sh)
	vfi_ma_sh_rule = vfi < vfimash;
else
	vfi_ma_sh_rule = 1;

if(vfi_prev_sh)
	vfi_prev_sh_rule = prev_vfi_sh;
else	
	vfi_prev_sh_rule = 1;
	
if(obv_prev_sh)
	obv_prev_sh_rule = prev_obv_sh;
else	
	obv_prev_sh_rule = 1;
	
if(vfi_falling_sh)
	vfi_falling_sh_rule = falling(vfi,vfi_falling_short);
else	
	vfi_falling_sh_rule = 1;
	
if(obv_falling_sh)
	obv_falling_sh_rule = falling(bobv,obv_falling_short);
else	
	obv_falling_sh_rule = 1;
	
if(ADX_greater_sh)
	ADX_greater_sh_rule = ADX(ADX_length) > ADX_greater_short;
else	
	ADX_greater_sh_rule = 1;
	
if(ADX_prev_sh)
	ADX_prev_sh_rule = prev_ADX_sh12;
else	
	ADX_prev_sh_rule = 1;
	
if(dm_p_lesser_than_sh)
	dm_p_lesser_than_sh_rule = dm_positive < dm_p_lesser_than_short;
else	
	dm_p_lesser_than_sh_rule = 1;
	
if(dm_n_more_than_sh)
	dm_n_more_than_sh_rule = dm_negetive > dm_n_more_than_short;
else	
	dm_n_more_than_sh_rule = 1;
	
if(gap_more_than_sh)
	gap_more_than_sh_rule = gap > gap_more_than_short;
else	
	gap_more_than_sh_rule = 1;
	
if(Timeframe_l)
	Timeframe_l_rulet = timeframes_n >=  Timeframe_long;
else
	Timeframe_l_rulet = 1;
	

	
	
Short = sellmini AND vfi_lesser_than_sh_rule and vfi_prev_sh_rule AND vfi_falling_sh_rule AND ADX_greater_sh_rule AND ADX_prev_sh_rule AND dm_p_lesser_than_sh_rule AND dm_n_more_than_sh_rule AND gap_more_than_sh_rule AND Timeframe_l_rulet AND vfi_ma_sh_rule AND obv_prev_sh_rule AND obv_falling_sh_rule;


prev_obv_c_c = Ref(ValueWhen(buymini,bobv),-1) < bobv;
prev_vfi_c_c1 =  Ref(ValueWhen(buymini,vfi,1),-1) < vfi;

if(Vfi_rising_c)
	vfi_rising_rule_c = rising(vfi,vfi_rising_cover) ;
else
	vfi_rising_rule_c = 1;

if(prev_vfi_c)
	prev_vfi_c_rule = prev_vfi_c_c1 ;
else
	prev_vfi_c_rule = 1;
	
	
if(OBV_rising_c)
	obv_rising_rule_c = rising(bobv,obv_rising_cover) ;
else
	obv_rising_rule_c = 1;

	

	
if(prev_obv_c)
	prev_obv_c_rule = prev_obv_c_c;
else
	prev_obv_c_rule = 1;
	


	
Cover = buymini AND vfi_rising_rule_c AND prev_vfi_c_rule  AND prev_obv_c_rule AND obv_rising_rule_c;// AND   prev_adx_c_rule;

	Buy = ExRem(Buy,sell);
	Sell = ExRem(Sell,buy);
	Short = ExRem(Short,cover);
	Cover = ExRem(Cover,Short);


if(dashboard)
{
	GfxTextOut("obv +"+ NumToStr(SelectedValue(bobv),True,true),250,250);
	GfxTextOut("dm+"+ NumToStr(SelectedValue(dm_positive)),250,270);
	GfxTextOut("dm -"+ NumToStr(SelectedValue(dm_negetive)),250,290);
	GfxTextOut("adx"+ NumToStr(SelectedValue(ADX(ADX_length))),250,310);
	GfxTextOut("timeframe p"+ NumToStr(SelectedValue(timeframes_p)),250,330);

}


PlotShapes(buymini*shapeHollowStar,colorAqua, 0, wt21, Offset=-20);
PlotShapes(sellmini*shapeHollowStar,colorRed, 0, wt21, Offset=20);

PlotShapes(Cover*shapeSquare,colorWhite, 0, wt21, Offset=-60);
PlotShapes(Short*shapeDownTriangle,colorWhite, 0, wt21, Offset=-20);
PlotShapes(buy*shapeUpTriangle,colorBlue, 0, wt21, Offset=-20);
PlotShapes(sell*shapeSquare,colorBlue, 0, wt21, Offset=60);

SetPositionSize(1,spsShares);

First you don't need ExRem for backtests. You should remove them at all. Backtester matches signals by itself, read the manual Back testing your trading ideas

Secondly, ExRem works as described in the manual AFL Function Reference - EXREM and it works perfectly here as well.
It works pair-wise.
If you use it like you do:

Buy=ExRem( Buy, Sell);

it would remove extra buys that occur between initial Buy and Sell.
And it won't look at the Short variable because that is precisely what you told it to do (by NOT including Short in the expression).

You just told it to work on Sell, not on Short.

ExRems should be avoided in code unless you know what you are doing.

Again, you don't need to do signal filtering AT ALL. Backtester by itself is doing that.

If for some reason you still want to filter signals and do it the way backtester is doing, you can use Equity(1) as it is backtester in the box and can perform signal filtering for you as well as evaluation of stops in regular mode. Just remove all those ExRem()s.

https://www.amibroker.com/f?equity

2 Likes

Thankyou For spending your valuable time to help me out

As per your instructions is tried using Equity(1); function. I noticed all my Short and Cover signals disappeared. What could be the reason for that.

shorts missing

You need to enable Short trades in the Analysis settings. Equity(1) function uses current analysis settings.

Thankyou for helping me out again. Your work really means a lot.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.