Stability of Stochastic Fisher Plot -- plot values flash "on and off"

Thank you Tomasz, fxshrat, Guiseppe for your prior able and generous help, and Happy New Year. Fxshrat's help on the OSAKA sort and extraction of values was a major help in execution time in particular, and thank you to Guiseppe on matrix insights. As always, this is the best software for analysis, Tomasz.

I am a newbie to amateur, running 6.30.0 Amibroker (WILL UPGRADE SOON), windows 8.1, version 6.1.0.20 DTN (latest according to them), have a 2.4 GHz pentium, 16 MB Ram, using wi-fi 85gb and above download speed fiber optic, and plot 20 cent range bars typically on symbol TQQQ (triple leverage QQQ ETF). My decimal setting in "Miscellaneous" preferences is 4 for titles & tools

My full code is shown below. It is, in essence, several composite Ehlers' stochastic fisher calculations. There are four of them, three covering different periods, and one that is simply an average of two of the others. I have applied the AFL check, and no coding errors appear, nor do any "divide by zero" warnings appear. I have also applied the "Code Check and Profile", and it indicates "no future quotes" are used. Everything "appears" fine. However, at various times, one of the plots (the Blue Histogram noted in the plot section at the end of the code) will flash "off" and then "reappear", usually within an instant. If you hold the cursor over that bar, the Blue histogram value will remain in the data window even when the plot disappears (very rarely, but sometimes, the value will show zero). The other plot overlays noted at the bottom of my code remain intact. I would have provided a screenshot, but can't "grab" one quickly enough when this happens. I have tried the steps noted earlier to sort this out. Within my amateur ability, I have also looked for any other items that might lead to unstable numbers, but don't see anything. I have also searched on web and Amibroker for any graphical and/or numerical limits on plotting multiple "indicators" over each other (the indicators are all calculated within the same named indicator, and then plotted together). I also tried the Prec function in Amibroker, hoping it might limit any decimal errors.

I don't know how to proceed at this point.

Any help would be greatly appreciated!! Thank you......Mike

function SuperSmoother2(Price,Period)

{
a1 = 0;
b1 = 0;
coef1 = 0;
coef2 = 0;
coef3 = 0;
Result = 0;

Filt = Nz( Price );   // not in regular SuperSmoother code;  had to use Nz to eliminate infinite or undefined values that caused function to return an "Empty" data set

                  

for (i=1; i< 11; i++)
{
	Result[i] = Filt[i] ; 
}

for (i > 10; i < BarCount; i++)
{
 a1 = exp(-1.414*3.14159 /Period);
 b1 = 2*a1*cos(1.414*(3.14159/2) /Period);      
 coef2 = b1;
 coef3 = -a1*a1;
 coef1 = 1 - coef2 - coef3;



 Result[i] = coef1*Filt[i] + coef2*Result[i-1] + coef3*Result[i-2];   
                                                                       
}

return Result;

}

function Cycle(Input, Alphas)
{
Smooth = 0;
Result = 0;
/*
// Do we go to firstValid + 6 or firstValid + 4 ????
for (i = 2; i < 6; i++)
{
Result[i] = (Input[i] - 2 * Input[i-1] + Input[i-2]) / 4.0 ;
}

for (i = 4; i < 6; i++)
{
	// Seed Values...
	Smooth[i] =	(Input[i] 
						+ 2 * Input[i-1] 
						+ 2 * Input[i-2] 
						+ Input[i-3]) 
						/ 6.0 ;
}

*/
for (i = 6; i < BarCount; i++)
{
alpha = Alphas[i];
if (alpha < 0)
alpha = 0;
if (alpha > 1)
alpha = 1;

	Smooth[i] = 	(Input[i] 
				+ 2 * Input[i-1] 
				+ 2 * Input[i-2]
				+ Input[i-3]) 
				/ 6.0 ;

	Result[i] =	(1 - .5*alpha) * (1 - .5*alpha) * (Smooth[i] - 2*Smooth[i-1] + Smooth[i-2]) + 2*(1 - alpha)
					* Result[i-1] - (1 - alpha) * ( 1 - alpha) * Result[i-2];
}

return Result;

}

function StochFisher(Input, Periods, Alphas)
{

R = 0; 
R1 = 0;
R2 = 0;

TempHHV = HHV(Input, Periods);
TempLLV = LLV(Input, Periods);

for (i = 1; i < BarCount; i++)
{
	alpha = Alphas[i];

	if (alpha < 0)
		alpha = 0;
	if (alpha > 1)
		alpha = 1;
	
	if ((TempHHV[i] - TempLLV[i]) != 0)
	{
		R =(Input[i] - TempLLV[i]) / 
			(TempHHV[i] - TempLLV[i]); 
	}
	else
		R = 0.0;
	
	//extra smoothing of stochastic, via Mike's workaround code
	//R[0] = .5 * 2 * (R[0] - .5) + .5 * R[1];

	R1 = R - 0.5;

	R2[i] = 2 * R1;

	R2[i] = .5 * R2[i] + .5 * R2[i-1];

	// The number of 9's used here determines the range bound of the final indicator
	// using only 2 9's gives a bound around abs(2.64), using 5 9's gives bound around abs(6.01).
	if (R2[i] < -0.9999)
		R2[i] = -0.9999;
	if (R2[i] > 0.9999)
		R2[i] = 0.9999;

	if ((1-R2[i]) != 0)
		Result[i] =  .5 * log( (1+R2[i]) / (1-R2[i]) ) ;
	else
		Result[i] = Result[i-1];

	Result[i] = Result[i] * alpha + Result[i-1] * (1-alpha) ;
}
return Result;

}

Price = 2*(H+L)/2 - Ref((H+L)/2, -1) + 0.001;
Price1 = Cycle(2*(H+L)/2 - Ref((H+L)/2, -1) + 0.001, .005); //200
Price2 = Cycle(2*(H+L)/2 - Ref((H+L)/2, -1) + 0.001, .02); //50
Price3 = Cycle(2*(H+L)/2 - Ref((H+L)/2, -1) + 0.001, .01); //100

n=5;
k=4;

SF = Prec((StochFisher(Price, n , 0.50) +
StochFisher(Price, n + k , 0.50) +
StochFisher(Price, n + 2k , 0.50) +
StochFisher(Price, n + 3
k , 0.50) +
StochFisher(Price, n + 4k , 0.50) +
StochFisher(Price, n + 5
k , 0.50) +
StochFisher(Price, n + 6k , 0.50) +
StochFisher(Price, n + 7
k , 0.50) +
StochFisher(Price, n + 8k , 0.50) +
StochFisher(Price, n + 9
k , 0.50) +
StochFisher(Price, n + 10k , 0.50) +
StochFisher(Price, n + 11
k , 0.50) +
StochFisher(Price, n + 12k , 0.50) +
StochFisher(Price, n + 13
k , 0.50) +
StochFisher(Price, n + 14k , 0.50) +
StochFisher(Price, n + 15
k , 0.50) +
StochFisher(Price, n + 16k , 0.50) +
StochFisher(Price, n + 17
k , 0.50) +
StochFisher(Price, n + 18k , 0.50) +
StochFisher(Price, n + 19
k , 0.50) +
StochFisher(Price, n + 20k , 0.50) +
StochFisher(Price, n + 21
k , 0.50) +
StochFisher(Price, n + 22k , 0.50) +
StochFisher(Price, n + 23
k , 0.50) +
StochFisher(Price, n + 24k , 0.50) +
StochFisher(Price, n + 25
k , 0.50) +
StochFisher(Price, n + 26k , 0.50) +
StochFisher(Price, n + 27
k , 0.50) +
StochFisher(Price, n + 28k , 0.50) +
StochFisher(Price, n + 29
k , 0.50) +
StochFisher(Price, n + 30k , 0.50) +
StochFisher(Price, n + 31
k , 0.50) +
StochFisher(Price, n + 32k , 0.50) +
StochFisher(Price, n + 33
k , 0.50) +
StochFisher(Price, n + 34k , 0.50) +
StochFisher(Price, n + 35
k , 0.50) +
StochFisher(Price, n + 36k , 0.50) +
StochFisher(Price, n + 37
k , 0.50) +
StochFisher(Price, n + 38k , 0.50) +
StochFisher(Price, n + 39
k , 0.50) +
StochFisher(Price, n + 40k , 0.50) +
StochFisher(Price, n + 41
k , 0.50) +
StochFisher(Price, n + 42k , 0.50) +
StochFisher(Price, n + 43
k , 0.50) +
StochFisher(Price, n + 44k , 0.50) +
StochFisher(Price, n + 45
k , 0.50) +
StochFisher(Price, n + 46k , 0.50) +
StochFisher(Price, n + 47
k , 0.50) +
StochFisher(Price, n + 48k , 0.50) +
StochFisher(Price, n + 49
k , 0.50))/50, 4);

SFA = Prec((StochFisher(Price1, n , 0.50) +
StochFisher(Price1, n + k , 0.50) +
StochFisher(Price1, n + 2k , 0.50) +
StochFisher(Price1, n + 3
k , 0.50) +
StochFisher(Price1, n + 4k , 0.50) +
StochFisher(Price1, n + 5
k , 0.50) +
StochFisher(Price1, n + 6k , 0.50) +
StochFisher(Price1, n + 7
k , 0.50) +
StochFisher(Price1, n + 8k , 0.50) +
StochFisher(Price1, n + 9
k , 0.50) +
StochFisher(Price1, n + 10k , 0.50) +
StochFisher(Price1, n + 11
k , 0.50) +
StochFisher(Price1, n + 12k , 0.50) +
StochFisher(Price1, n + 13
k , 0.50) +
StochFisher(Price1, n + 14k , 0.50) +
StochFisher(Price1, n + 15
k , 0.50) +
StochFisher(Price1, n + 16k , 0.50) +
StochFisher(Price1, n + 17
k , 0.50) +
StochFisher(Price1, n + 18k , 0.50) +
StochFisher(Price1, n + 19
k , 0.50) +
StochFisher(Price1, n + 20k , 0.50) +
StochFisher(Price1, n + 21
k , 0.50) +
StochFisher(Price1, n + 22k , 0.50) +
StochFisher(Price1, n + 23
k , 0.50) +
StochFisher(Price1, n + 24k , 0.50) +
StochFisher(Price1, n + 25
k , 0.50) +
StochFisher(Price1, n + 26k , 0.50) +
StochFisher(Price1, n + 27
k , 0.50) +
StochFisher(Price1, n + 28k , 0.50) +
StochFisher(Price1, n + 29
k , 0.50) +
StochFisher(Price1, n + 30k , 0.50) +
StochFisher(Price1, n + 31
k , 0.50) +
StochFisher(Price1, n + 32k , 0.50) +
StochFisher(Price1, n + 33
k , 0.50) +
StochFisher(Price1, n + 34k , 0.50) +
StochFisher(Price1, n + 35
k , 0.50) +
StochFisher(Price1, n + 36k , 0.50) +
StochFisher(Price1, n + 37
k , 0.50) +
StochFisher(Price1, n + 38k , 0.50) +
StochFisher(Price1, n + 39
k , 0.50) +
StochFisher(Price1, n + 40k , 0.50) +
StochFisher(Price1, n + 41
k , 0.50) +
StochFisher(Price1, n + 42k , 0.50) +
StochFisher(Price1, n + 43
k , 0.50) +
StochFisher(Price1, n + 44k , 0.50) +
StochFisher(Price1, n + 45
k , 0.50) +
StochFisher(Price1, n + 46k , 0.50) +
StochFisher(Price1, n + 47
k , 0.50) +
StochFisher(Price1, n + 48k , 0.50) +
StochFisher(Price1, n + 49
k , 0.50))/50, 4);

n=4;
k=4;

SFX = Prec((StochFisher(Price2, n , 0.50) +
StochFisher(Price2, n + k , 0.50) +
StochFisher(Price2, n + 2k , 0.50) +
StochFisher(Price2, n + 3
k , 0.50) +
StochFisher(Price2, n + 4k , 0.50) +
StochFisher(Price2, n + 5
k , 0.50) +
StochFisher(Price2, n + 6k , 0.50) +
StochFisher(Price2, n + 7
k , 0.50) +
StochFisher(Price2, n + 8k , 0.50) +
StochFisher(Price2, n + 9
k , 0.50) +
StochFisher(Price2, n + 10k , 0.50) +
StochFisher(Price2, n + 11
k , 0.50) +
StochFisher(Price2, n + 12*k , 0.50))/13,4);

SFY = Prec((StochFisher(Price3, n , 0.50) +
StochFisher(Price3, n + k , 0.50) +
StochFisher(Price3, n + 2k , 0.50) +
StochFisher(Price3, n + 3
k , 0.50) +
StochFisher(Price3, n + 4k , 0.50) +
StochFisher(Price3, n + 5
k , 0.50) +
StochFisher(Price3, n + 6k , 0.50) +
StochFisher(Price3, n + 7
k , 0.50) +
StochFisher(Price3, n + 8k , 0.50) +
StochFisher(Price3, n + 9
k , 0.50) +
StochFisher(Price3, n + 10k , 0.50) +
StochFisher(Price3, n + 11
k , 0.50) +
StochFisher(Price3, n + 12k , 0.50) +
StochFisher(Price3, n + 13
k , 0.50) +
StochFisher(Price3, n + 14k , 0.50) +
StochFisher(Price3, n + 15
k , 0.50) +
StochFisher(Price3, n + 16k , 0.50) +
StochFisher(Price3, n + 17
k , 0.50) +
StochFisher(Price3, n + 18k , 0.50) +
StochFisher(Price3, n + 19
k , 0.50) +
StochFisher(Price3, n + 20k , 0.50) +
StochFisher(Price3, n + 21
k , 0.50) +
StochFisher(Price3, n + 22k , 0.50) +
StochFisher(Price3, n + 23
k , 0.50) +
StochFisher(Price3, n + 24*k , 0.50))/25,4);

SF1 = SuperSmoother2(SF, 5);
SF2 = SuperSmoother2(SFA, 5);
SF3 = SuperSmoother2(SFX, 5);
//SF4 = SuperSmoother2(SFY, 8);
Plot(SF1 ,"Fshavg", colorBlue, styleHistogram, styleThick, Null, Null, Null, 3);
Plot(SF2 ,"FshavgRf", colorRed, styleLine, styleThick, Null, Null, Null, 2);
Plot((0.33SF1 + 0.67SF2) ,"FshCmb2", colorWhite, styleHistogram, styleThick, Null, Null, Null, 2);
Plot(SF3 ,"FshavgQ", colorBlack, styleLine, styleThick, Null, Null, Null, 6);
//Plot(SF4 ,"FshavgTH", colorBlack, styleLine, styleThick, Null, Null, Null, 3);
Plot(1 ,"", colorWhite,styleThick);
Plot(-1 ,"", colorWhite,styleThick);
Plot(2 ,"", colorYellow,styleThick);
Plot(-2 ,"", colorYellow,styleThick);
Plot(3 ,"", colorRed,styleThick);
Plot(-3 ,"", colorRed,styleThick);

SupperSmoother does not require looping.
Super Smoother code is part of the manual

function SuperSmoother( Price, Periods ) {
	/// IIR SuperSmoother code from AmiBroker help
	/// @link https://www.amibroker.com/guide/afl/iir.html
	/// Copyright AmiBroker.com
	c1 = 1.41421 * 3.14159 / Periods;  
	c2 = 2.71828^-c1;  
	a1 = 2 * c2 * cos( c1 );  
	a2 = -c2^2;  
	b0 = (1 - a1 - a2)/2;  b1 = b0;  
	return  IIR( Price, b0, a1, b1, a2 ); 
}

Also StockFisher does not need looping, AFAICS.
Here I converted it to loopless code

function StochFisher( Input, Periods, Alphas )
{
   	/// https://forum.amibroker.com/t/stability-of-stochastic-fisher-plot-plot-values-flash-on-and-off
   	TempHHV = HHV( Input, Periods );
   	TempLLV = LLV( Input, Periods );
    
   	alpha = Max(0, Min(Alphas, 1));
	// SafeDivide requires AB 6.35 otherwise use line below
	R = SafeDivide(Input - TempLLV, TempHHV - TempLLV);
	//R = (Input - TempLLV) / (TempHHV - TempLLV+1e-9);

	R1 = R - 0.5;
   	R2 = 2 * R1;
   	R2 = AMA( R2, 0.5);
    
   	// The number of 9's used here determines the range bound of the final indicator
	// using only 2 9's gives a bound around abs(2.64), using 5 9's gives bound around abs(6.01).
	R2 = Max(-0.9999, Min(R2, 0.9999));
	
	// 1-R2 can not become zero, see line before
	Result = 0.5 * log((1 + R2)/(1 - R2)); 
   	return AMA(Result, alpha);
}

Now Cycle function you try yourself based on upper hints on how to remove loops.
Honestly I also refused to look at it because of presentation without proper code tags usage, see below.

But CyberCyle function can be found in members zone
http://www.amibroker.com/members/traders/05-2004.html
See Listing 2


Side note please insert code properly. You have a preview in every forum editor.
Why do you not check before sending?

2 Likes

thank you for your prompt reply. I appreciate the help very much. I did not mean to overlook the preview, and will check next time. I don't use the forum regularly, and it isn't second nature to me. I am unclear on the term "proper code tag usage" If this refers to tracking credit for who wrote the code, please understand that an acquaintance of mine wrote this a long time ago, and gave it to me. He wanted no money or credit. I do not intentionally pirate code from anyone!! I am a novice, and trying to learn my way around

image

See,

Ok....thank you TrendSurfer. I see what you mean. I thought I had violated some protocol regarding ownership and attribution, but now I understand what fxshrat was telling me. My understanding was off base, and I misunderstood

The image with the CODE BUTTON really helps!

I try, but coding seems foreign to me. For example, I thought since my code compiled in Amibroker, it was ok as is. So you can see my lack of familiarity. Thank you, fxshrat and Tomasz for the help! Happy New Year

All good! :+1:

Happy New Year!

Fxshrat:

I believe I see where you were going with the substantial improvements to my code. My code was probably causing the intermittent delays in my plot. After I made these in my formula, and did a "code check and profile", the difference between my original version and your changes was DRAMATIC!!! Your code required only about 4 to 5 % of the time as mine....and I believe it was attributable to your code being heavily array dependent, and mine had cumbersome loops. I will do my best to focus on that distinction in any future code. And sorry, did not understand your "tag usage" comment, but TrendSurfer explained, and I see the "Code Button" format icon in the dialog box for future reference. All noted, and thank you!!

Fxshrat:

Thank you for your generous contribution of help, and clarification on code tags. As a real neophyte on programming, I always thought if you posted code that "compiled" with no error in Amibroker, it would do same here.....Trend Surfer and Guiseppe sent me some notes on code tags, and Guiseppe noted how "asterisks" can disappear, for example. I sent Guiseppe a code snippet today using the code tag button (as Trend Surfer mentioned following on to your original observation) to try to ensure I get this right for the future, and make matters as easy as possible when you guys help with any future questions.....

thank you,
Mike

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