Plot draws only the last value

Hello, Plot only draws a line of the last value in the pane and not previous values. What have I done wrong? Thank you.

Plot

_SECTION_BEGIN("My Kelly ");

longonly = True;
kap = 1000;

//lookback
lookback = 54;

// Die Kelly-Zahl wird nur auf Basis der Lookback-Bars berechnet
if (lookback > 0)
{
	numwin = 0;
	numlose = 0;
	win = 0;
	lose = 0;
	sumwin = 0;
	sumlose = 0;
	avwin = 0;
	avlose = 0;
	riskpercent = 0;
	percentwin = 0;
	riskpercent = 0;
	sharestohold = 0;

// Berechnung der Gewinn- und Verlustwahrscheinlichkeit sowie des durchschn. Gewinns und Verlusts
	for (i = 0; i < lookback - 1; i++)
	{
		if ((C[i] - C[i + 1]) > 0) // Gewinn
		{
			numwin = numwin + 1;
			win = C[i] - C[i + 1];
			sumwin = sumwin + win;
			printf("A: %g, %g, %g\n", numwin, win, sumwin);
		}
		
		if (numwin != 0)
			avwin = sumwin / numwin;
		else avwin = 0;
	
	
		if ((C[i] - C[i + 1]) <= 0) // Verlust
		{
			numlose = numlose + 1;
			lose = -1 * (C[i] - C[i + 1]);
			sumlose = sumlose + lose;
			printf("B: %g, %g, %g\n", numlose, lose, sumlose);
		}
	
		if (numlose != 0)
			avlose = sumlose / numlose;
		else
			avlose = 0;
		printf("C: %g\n", avlose);
	}
	
	percentwin = numwin / lookback; // Prozentsatz steigender Bars
	printf("D: %g\n", percentwin);
	printf("avwin: %g\n", avwin);

	if ((avlose AND avwin) != 0)
		riskpercent = (percentwin - ((1 - percentwin) / (avwin / avlose)));
	else
		riskpercent = 0; // Kelly Formel
	printf("E: %g\n", riskpercent);
	

	// Berechnung der Aktienanzahl für das definierte Risikokapital
	if (avlose > 0)
		{
			sharestohold = ceil(kap * riskpercent / avlose);
			printf("K: %g\n", sharestohold);
		}
	
	ind1 = sharestohold;
	ind2 = 100*riskpercent;
	ind3 = avlose*numlose;
	ind4 = avwin*numwin;
		
	ChartType = ParamList("Chart Type", "SharesToHold|KellyPercent|Lose/Win"); //

	if (ChartType == "SharesToHold") // 
	{
		Plot(ind1, "SharesToHold", colorWhite, styleLine );
	}

	if (ChartType == "KellyPercent") // 
	{
		Plot(ind2, "KellyPercent", colorAqua, styleLine );
	}
	
	if (ChartType == "Lose/Win") // 
	{
		Plot(ind3, "%lose*avglose", colorRed, styleLine );
		Plot(ind4, "%win*avgwin", colorGreen, styleLine );
	}
}

I could not read your whole code,
but when i do

printf("\n %s %s %s %s", typeof(ind1), typeof(ind2), typeof(ind3), typeof(ind4));

the output is

number number number number

It is clear that somehow you are not doing Array processing and AB works on all bars

for (i = 0; i < lookback - 1; i++)

something wrong here too as lookback is only 54.

Hope an expert can guide you better.

Your output is not an array but number.
To get type of var you may use typeof operator:
printf( "Type of var: %s", typeof(your_var) );

Please read docs about difference of array and number.

In order to get resulting array in Barcount loop you have to use subscipt operator [ i ].
But your summing variables result in numbers e.g. see sumwin.
Also you did not iterate BarCount but lookback.

As aside at some cases you look forward see C[i + 1]
To look backward it is rather C[i - 1]
You seem to come from Easy Language platform.


Anyway you do not need loop!
Simply apply array processing.

lookback = 54;

diff = C-Ref(C,-1);
win = diff>0;
loss = NOT win;

sumwin = Sum(IIf(win, diff, 0), lookback);
numwin = Sum(IIf(win, 1, 0), lookback);
avgwin = SafeDivide(sumwin,numwin);

sumloss = Sum(IIf(loss, diff, 0), lookback);
numloss = lookback-numwin;
avgloss = SafeDivide(sumloss,numloss);

And FYI, before some guys intend to scream "ERROR!!". SafeDivide is new function of AB 6.35.

2 Likes

Thank you works now fine.

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