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

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



PureBytes Links

Trading Reference Links

To use your example oscillator "X" of an MA with a randomly generated 
Length between 10 and 50 ...

LV = 10;
HV = 50;

Length = int(Random() * (HV - LV) + LV);

X     = MA(C, Length);

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

Plot(StDvX, "StDvX", colorWhite);

Filter = 1;

AddColumn(C,      "Close",  1.5); 
AddColumn(Length, "Length", 1.0); 
AddColumn(X,      "X",      1.5); 

AddColumn(n,      "n",      1.0); 
AddColumn(CumX,   "CumX",   1.5);  
AddColumn(MeanX,  "MeanX",  1.5);  
AddColumn(XMean,  "XMeanX", 1.5);  
AddColumn(Mean2,  "Mean2",  1.5);  
AddColumn(CumM2,  "CumM2",  1.5);  
AddColumn(nCumM,  "nCumX",  1.5);  
AddColumn(StDvX,  "StDevX", 1.5);  



--- In amibroker@xxxxxxxxxxxxxxx, "Fred" <ftonetti@xxxx> wrote:
> What you need to do for starters so that you understand is 
ELIMANATE 
> ALL THE FUNCTIONS ...
> 
> There's really no need for them ... This is straightline code.
> 
> You may think you are simplifying things but instead you are 
instead 
> needlessly doing calculations FOR EVERY BAR that only need be done 
> once ...
> 
> PS ... I'm on EDT as well
> 
> --- In amibroker@xxxxxxxxxxxxxxx, "treliff" <treliff@xxxx> wrote:
> > Fred, I don't doubt your expertise nor my own inexperience and 
this 
> > line 
> > 
> > "your oscillator regardless of what it is comprised of is no 
> > different."
> > 
> > sure makes sense. I just have to put this aside now and check 
back 
> > tomorrow (I'm ET). 
> > 
> > Meanwhile I simplified the code, replaced the mysterious 
Oscillator 
> > (nothing secret, it will just clutter the code further) with a 
> simple 
> > MA and used your StDvX definition (but turned it into a function 
to 
> > save space).
> > 
> > Now a lot may be wrong (could be improved) in this code, it may 
> > actually be total and complete Nonsense..... but one thing makes 
it 
> > unique (so far): it gives the correct result.
> > 
> > I enjoy chewing on your pointers but the bottom line of course is 
> to 
> > find a code without loop (or at least much faster) that gives me 
> > exactly the same result (plot) as this one.  
> > 
> > No need (yet) to post the end result, but are you sure you can do 
> > it?  
> > 
> > Sure do appreciate your time & patience so far!!
> > 
> > // code start
> > 
> > function Randomize(a,b)
> > { return Random(1)*(b-a)+a ; }
> > 
> > Cycle = int( Randomize(10,50) ) ;
> > 
> > n = BarIndex() + 1 ;
> > 
> > function StDvX(X) 
> > { return sqrt(Cum ((X - Cum(X)/n) ^ 2) / n) ; }
> > 
> > function Cycleconstant(number)
> > { return Cum(0) + Cycle[ number ] ; }
> > 
> > function X(i) 
> > { return MA(C,Cycleconstant(i)) ; } 
> > 
> > for (i = 0 ; i < BarCount ; i++ )
> > {
> > y = StDvX( X( i ) ) ; 
> > FinalArray[ i ] = y[ i ] ; 
> > }
> > 
> > Plot(FinalArray,"FinalArray",colorBlack);
> > 
> > // code end
> > 
> > 
> > --- In amibroker@xxxxxxxxxxxxxxx, "Fred" <ftonetti@xxxx> wrote:
> > > For example one of the things that StDev is a function of is 
how 
> > many 
> > > bars i.e. "n" ... right ?
> > > 
> > > But in our example "n" is constantly changing ... on bar 1 it's 
> > 1 ... 
> > > on bar 100 it's 100 ...
> > > 
> > > Which is why I wrote ... 
> > > 
> > > n = BarIndex() + 1;
> > > 
> > > n is used several ways ... 
> > > 
> > > It is used to get our Mean at each bar ...
> > > 
> > > Mean = Cum(X) / n ...
> > > 
> > > In the above calculation Cum(X) is an array ... so is Mean ... 
> AND 
> > SO 
> > > IS "n" ...
> > > 
> > > I don't think you are seeing this ... your oscillator 
regardless 
> of 
> > > what it is comprised of is no different.
> > > 
> > > --- In amibroker@xxxxxxxxxxxxxxx, "Fred" <ftonetti@xxxx> wrote:
> > > > Nonsense ... you still don't get it
> > > > 
> > > > --- In amibroker@xxxxxxxxxxxxxxx, "treliff" <treliff@xxxx> 
> wrote:
> > > > > Oscillator fits into a single dimension array, but it is a 
> > > FUNCTION 
> > > > > of, among others, the bar number i, or better, it is a 
> function 
> > > of 
> > > > > Cycle[i].
> > > > > 
> > > > > Because Cycle varies from 10 to 50 we in fact have 41 
> different 
> > > > > Oscillator arrays:
> > > > > 
> > > > > Oscillator(10)
> > > > > Oscillator(11)
> > > > > .
> > > > > .
> > > > > Oscillator(50)
> > > > > 
> > > > > Could just as well be:
> > > > > 
> > > > > MA(C,10)
> > > > > .
> > > > > .
> > > > > .
> > > > > MA(C,50)
> > > > > 
> > > > > (well, MA doesn't really oscillate around zero but that 
> doesn't 
> > > > > matter) 
> > > > > 
> > > > > Now we arrive at bar 300 with Cycle[300] is, say, 27.
> > > > > Then I want FinalArray[300] to contain 
> > > > > 
> > > > > StDvX( MA(C,27) ) [300]
> > > > > 
> > > > > (this is not good code but just indicates: the 300th array 
> > > element 
> > > > of 
> > > > > StDvX( MA(C,27) )
> > > > > 
> > > > > Next bar 301 has Cycle[301] which is 49.
> > > > > So FinalArray[301] should contain 
> > > > > 
> > > > > StDvX( MA(C,49) ) [301]
> > > > > 
> > > > > etc.
> > > > > 
> > > > > 
> > > > > --- In amibroker@xxxxxxxxxxxxxxx, "Fred" <ftonetti@xxxx> 
> wrote:
> > > > > > Wrong ... You can not have the equivalent of doubly 
> > dimensioned 
> > > > > > arrays i.e. tables in AFL ... one dimension is all you 
> > get ... 
> > > or 
> > > > > at 
> > > > > > least not without using something external i.e. Osaka or 
> > ABTool 
> > > > > > plugins ... 
> > > > > > 
> > > > > > If you can get your "oscillator" into an array "X" then 
the 
> > AFL 
> > > I 
> > > > > > wrote will give you the standard deviation at each bar 
> using 
> > > all 
> > > > > the 
> > > > > > prior elements of X ( your osciallator ) ... That is what 
> you 
> > > > were 
> > > > > > looking for, wasn't it ?  
> > > > > > 
> > > > > > Is there something about your osciallator that doesn't 
> allow 
> > it 
> > > > to 
> > > > > > fit into a single dimension array ?!  
> > > > > > 
> > > > > > --- In amibroker@xxxxxxxxxxxxxxx, "treliff" 
<treliff@xxxx> 
> > > wrote:
> > > > > > > More food for thought... I have to chew on all that but 
> one 
> > > > thing 
> > > > > > > right away:
> > > > > > > 
> > > > > > > "X(i) is an ELEMENT of the array X."
> > > > > > > 
> > > > > > > NO: each X(i) is a separate array (otherwise it would 
be X
> > [i] 
> > > > > > right? 
> > > > > > > Or wrong?)
> > > > > > > 
> > > > > > > In my code:
> > > > > > > 
> > > > > > > Cycle is an array
> > > > > > > bar i has Cycle[i] 
> > > > > > > Cycleconstant(i) = Cum(0)+Cycle[i] is an array 
> > (a "constant" 
> > > > > array)
> > > > > > > X(i) = Oscillator(Cycleconstant(i))is an **ARRAY** for 
> each 
> > i.
> > > > > > > 
> > > > > > > 
> > > > > > > --- In amibroker@xxxxxxxxxxxxxxx, "Fred" 
<ftonetti@xxxx> 
> > > wrote:
> > > > > > > > It appears you don't understand the array .vs. 
element 
> of 
> > > an 
> > > > > > array 
> > > > > > > > concept ...
> > > > > > > > 
> > > > > > > > Is the equation
> > > > > > > > 
> > > > > > > > X(i) = BarIndex() + i 
> > > > > > > > 
> > > > > > > > even meaningful ?  X(i) is an ELEMENT of the array 
X.  
> > > > BarIndex
> > > > > () 
> > > > > > > is 
> > > > > > > > an ARRAY.  How does one equate an ELEMENT of an array 
> > i.e. X
> > > > (i) 
> > > > > > to 
> > > > > > > > the entire contents of another array i.e. BarIndex() 
+ 
> a 
> > > > > modifier 
> > > > > > > i ?
> > > > > > > > 
> > > > > > > > Not doable, is it ?.  Further more why do you think 
you 
> > > need 
> > > > or 
> > > > > > > want 
> > > > > > > > to do this ?  With regards to ...
> > > > > > > > 
> > > > > > > > "Note however that in real life the X(i)'s are 
> > > independent.  
> > > > > > There 
> > > > > > > is 
> > > > > > > > no way to express X(i) in terms of X(i-1)"
> > > > > > > > 
> > > > > > > > Nor is there a need to ...
> > > > > > > > 
> > > > > > > > "Can the StDvX definition create the FinalArray 
without 
> a 
> > > > > loop ?"
> > > > > > > > 
> > > > > > > > Did you play with the code ? Look at the 
results ? ... 
> > > > Doesn't 
> > > > > it 
> > > > > > > do 
> > > > > > > > precisely that ?  It matters not what is in the array 
> of 
> > X 
> > > in 
> > > > > > terms 
> > > > > > > > of being able to calc the StDev of it's elements from 
> the 
> > > > first 
> > > > > > one 
> > > > > > > > to each bar along the way.  In fact that code with 
> minor 
> > > mods 
> > > > > > could 
> > > > > > > > be used to calc a variable length StDev based on a 
> > changing 
> > > > > value 
> > > > > > > of 
> > > > > > > > n where n was an array of elements based on whatever 
> calc 
> > > one 
> > > > > > > wanted.
> > > > > > > > 
> > > > > > > > --- In amibroker@xxxxxxxxxxxxxxx, "treliff" 
> > <treliff@xxxx> 
> > > > > wrote:
> > > > > > > > > So far so good, but now suppose that the array in 
> > > question, 
> > > > > the 
> > > > > > > one 
> > > > > > > > > we need to calculate the standard deviation over, 
> > changes 
> > > > > with 
> > > > > > > each 
> > > > > > > > > bar. In other words, there is not one array 
> > > > > > > > > 
> > > > > > > > > X = BarIndex() + 100;
> > > > > > > > > 
> > > > > > > > > but there are different arrays like for example
> > > > > > > > > 
> > > > > > > > > X(i) = BarIndex() + i;
> > > > > > > > > 
> > > > > > > > > (In my code this would be Oscillator(Cycleconstant
> (i)) 
> > > but 
> > > > > that 
> > > > > > > is 
> > > > > > > > > not of the essence. > 
> > > > > > > > > In my opinion this now is the remaining problem and 
> the 
> > > > real 
> > > > > > time-
> > > > > > > > > consumer: 
> > > > > > > > > 
> > > > > > > > > for (i = 0 ; i < BarCount ; i++ )
> > > > > > > > > { y = StDvX( X( i ) ) ) ; 
> > > > > > > > >   FinalArray[ i ] = y[ i ] ; } 
> > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > 
> > > > > > > > > --- In amibroker@xxxxxxxxxxxxxxx, "Fred" 
> > <ftonetti@xxxx> 
> > > > > wrote:
> > > > > > > > > > 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/