[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: some looping help needed .......



PureBytes Links

Trading Reference Links

The question simplifies to ... how do I calculate standard deviation 
at the current bar for all past values of some array without using a 
loop, thereby eliminating the innermost loop and leaving only the 
outer one.

When looking at most problems like this where the solution may not be 
immediately obvious, the simplest way is to break the problem down 
into its individual components and use EXPLORE to see that each 
calculation is doing what it's supposed to and from the perspective 
of speed it won't be any slower to do it this way, in some cases it 
may actually be faster i.e. here's the way most people write a 
stochastic calc ...

Sto = (C - LLV(C, Length)) / (HHV(C, Length) - LLV(C, Length));

The problem of course is that one has done the calc LLV(C, Length) 
twice ... Simpler and of course faster is ...

LLVX = LLV(C, Length)
Sto = (C - LLVX) / (HHV(C, Length) - LLVX); 

Back to your problem ... 

StDevX = Sqrt(Cum ((X - Average(X)) ^ 2) / n)

Let's assume we want to see how to get the calculations correct at 
BarIndex() == 10 ( The 11th Bar ) without using a loop and for the 
moment we won't care if the calc is correct at BI() = 9 or 11 because 
we know we can always write a loop to go around all of this if we 
need to ...

// Let's generate some simple dummy data "X" to  
// use where we can easily eyeball the results
// "X" can always be replaced by something real

X = BarIndex() + 100;

// The components

n = BarIndex() + 1;
CumX  = Cum(X);
MeanX = CumX / n;
XMean = X - MeanX;
Mean2 = XMean ^ 2;
CumM2 = Cum(Mean2);
nCumM = CumM2 / n;
StDvX = sqrt(nCumM);

Filter = BarIndex() <= 10;

AddColumn(X, "X", 1.0);
AddColumn(n, "n", 1.0);
AddColumn(CumX,  "CumX", 1.0);
AddColumn(MeanX, "MeanX", 1.2);
AddColumn(XMean, "X-MeanX", 1.2);
AddColumn(Mean2, "Mean2", 1.2);
AddColumn(CumM2, "CumM2", 1.2);
AddColumn(nCumM, "nCumX", 1.2);
AddColumn(StDvX, "StDevX", 1.2);

Try taking what's above and running it as an EXPLORE ... see the 
columns (below "hopefully") it shows i.e. one for each component 
including the data "X" ... It would appear that StDevX is correct not 
only for BI() == 10 but for ALL the other bars as well without ANY 
loops.

X	n	CumX	MeanX	X-MeanX	Mean2	CumM2	nCumX	StDevX
100	1	100	100.00	0.00	0.00	0.00	0.00	0.00
101	2	201	100.50	0.50	0.25	0.25	0.13	0.35
102	3	303	101.00	1.00	1.00	1.25	0.42	0.65
103	4	406	101.50	1.50	2.25	3.50	0.88	0.94
104	5	510	102.00	2.00	4.00	7.50	1.50	1.22
105	6	615	102.50	2.50	6.25	13.75	2.29	1.51
106	7	721	103.00	3.00	9.00	22.75	3.25	1.80
107	8	828	103.50	3.50	12.25	35.00	4.38	2.09
108	9	936	104.00	4.00	16.00	51.00	5.67	2.38
109	10	1045	104.50	4.50	20.25	71.25	7.13	2.67
110	11	1155	105.00	5.00	25.00	96.25	8.75	2.96

Since the rest of your AFL doesn't require any loops, one would 
conclude that your AFL really needs NO loops at all.






------------------------ Yahoo! Groups Sponsor --------------------~--> 
Get fast access to your favorite Yahoo! Groups. Make Yahoo! your home page
http://us.click.yahoo.com/dpRU5A/wUILAA/yQLSAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/