SI
SI
discoversearch

We've detected that you're using an ad content blocking browser plug-in or feature. Ads provide a critical source of revenue to the continued operation of Silicon Investor.  We ask that you disable ad blocking while on Silicon Investor in the best interests of our community.  If you are not using an ad blocker but are still receiving this message, make sure your browser's tracking protection is set to the 'standard' level.
Strategies & Market Trends : TA-Quotes Plus -- Ignore unavailable to you. Want to Upgrade?


To: Craig DeHaan who wrote (7374)10/25/1998 1:16:00 PM
From: Bob Jagow  Respond to of 11149
 
I've printed out your post, Craig, and will get back to you on your
questions.
Meanwhile, I realized that it makes sense to move the starting LR
calc inside the for bar = first to last loop
Code with this change follows. -Bob
-------
input = "80k+vol.lst";
output = "swLR.lst"; //processms ="E:\Meta\swLR",msdata;
integer i, N1, N2, Sx, Sx1, lrflag, bar, first, last;
float pa1, a1, pb1, b1, pLR1, LR1; // p denotes previous val
float pa2, a2, pb2, b2, pLR2, LR2;
float div, Sxx, Sxy, Sy, Sxx1, Sxy1, Sy1;
string lrstr;
first := -10; // Back test
last := 0; // ending bar
N1 := 5; // Set len of short LR here ******************
N2 := 50; // Set len of long LR here ******************
lrflag := 0; // Set to 0 for LRI; 1 for TSF *******
if lrflag != 0 then lrfalg:= 1; endif; // a precaution
if lrflag = 0 then lrstr := "LRI: "; else lrstr := "TSF: "; endif;
Daystoload = 100 + N2; DaysRequired = 100 + N2;
for bar = first to last do // back testing loop
// Nest all overall conditions using builtin indicators before LR, i.e.,
// if HasOptions = True then
// if close(bar) > 5 and close(bar) < 60 then
// if macd(day) > macdsignal(day) then
// if macd(day-1) <=macdsignal(day-1) then
if bar = first then // just the first time!!
Sx := 0; Sxx := 0; Sxy := 0; Sy := 0;
for i = first -1 to first -N1 step -1 do // N1 bars
Sx := Sx + i; Sy := Sy + close(i);
Sxx := Sxx + i*i; Sxy := Sxy + i*close(i);
next i;
Sx1 := Sx; Sy1 := Sy; Sxx1 := Sxx; Sxy1 := Sxy; // save for N1
div := N1*Sxx - Sx*Sx;
pa1 := (Sxx*Sy -Sx*Sxy)/div;
pb1 := (N1*Sxy - Sx*Sy)/div;
pLR1 := pa1 + lrflag*pb1; // 1*pb if TSF
for i = first -N1 -1 to first -N2 step -1 do // N2 total bars
Sx := Sx + i; Sy := Sy + close(i); // keep at it
Sxx := Sxx + i*i; Sxy := Sxy + i*close(i);
next i;
div := N2*Sxx - Sx*Sx;
pa2 := (Sxx*Sy -Sx*Sxy)/div;
pb2 := (N2*Sxy - Sx*Sy)/div;
pLR2 := pa2 + lrflag*pb2; // 1*pb if TSF
endif; // the first time!!

// Stepwise calculation of LR1 and LR2
// Calc LR2
i := bar -N2; // Replace N2 bar with current bar
Sx := Sx +bar -i; Sy := Sy +close(bar) -close(i);
Sxx := Sxx +bar*bar -i*i; Sxy := Sxy +bar*close(bar) -i*close(i);
div := N2*Sxx - Sx*Sx;
a2 := (Sxx*Sy -Sx*Sxy)/div;
b2 := (N2*Sxy - Sx*Sy)/div;
LR2 := a2 + lrflag*b2; // 1*bcalc
// now calc LR1 using the saved values
i := bar -N1; // Replace N1 with current bar
Sx1 := Sx1 +bar -i;
Sy1 := Sy1 +close(bar) -close(i);
Sxx1 := Sxx1 +bar*bar -i*i;
Sxy1 := Sxy1 +bar*close(bar) -i*close(i);
div := N1*Sxx1 - Sx1*Sx1;
a1 := (Sxx1*Sy1 -Sx1*Sxy1)/div;
b1 := (N1*Sxy1 - Sx1*Sy1)/div;
LR1 := a1 + lrflag*b1; // 1*bcalc
if LR1 > LR2 and pLR1 < pLR2 then // only an example
print symbol,",",date(bar),",",a1:7:3,",",pa1:7:3,",",
b1:7:3,",",pb1:7:3,",",lrstr,",",LR1:7:3,",",pLR1:7:3;
println ",",a2:7:3,",",pa2:7:3,",",b2:7:3,",",
pb2:7:3,",",lrstr,",",LR2:7:3,",",pLR2:7:3;
endif;
//endif; endif; endif; endif;
pa1:= a1; pb1:= b1; pLR1:= LR1; pa2:= a2; pb2:= b2; pLR2:= LR2;
next bar;



To: Craig DeHaan who wrote (7374)10/25/1998 2:46:00 PM
From: Bob Jagow  Read Replies (1) | Respond to of 11149
 
Craig,
A to Z gives a formula for a, but mine is better ;)
199.234.225.19
You get a and b by solving
Eq1: a*N + b*Sx = Sy
Eq2: a*Sx + b*Sxx = Sxy
Recall that the divisor is the determinant of the left side and the numerators are the determinants with the right col replacing the var col.
Hence div = N*Sxx - Sx^2, div*a = Sy*Sxx - Sxy*Sx, and div*b = N*Sxy -Sx*Sy.

A to Z calculates a by backsubst in the 1st equation, bu this propagates b's error into a.

Thanks for catching if lrflag != 0 then lrfalg:= 1; -- never, in my arrogance, bothered to check TSF :-(

Re "intuitively it seems the i:= bar -N*; should be just i:=-N* as all the previous loop sums started at 'first' originally.", you can verify that bar is needed by a calc of 10/23/98 with first = -10.

Don't understand "And is it possible to reduce them back to first with just one differential added to the previous sums without incrementally stepping them backward?"

Re "I haven't incorporated integral backtesting in many scans and that's where the logic is fuzziest.", start doing it ;)
Just have to insert a little code after the normal println.

Don't think we should try to get Gary to shoehorn GET into r2.01 ;) -Bob
---------------------
print symbol:-5,",",date(bar),",",close(bar):7:3;
if first != 0 then //measure performance
BestCl:= max(0, bar+1, cl); //from next bar to bar 0
BestPct:= 100*(BestCl -close(bar))/close(bar);
for i = bar+1 to 0 do //find first max then exit the loop
if close(i) >= BestCl then
BestDay:= i;
i:= 0;
endif;
next i;
print",",date(BestDay),",",close(BestDay):7:3,",",BestPct:5:3;
endif; // print back test results
println;
---------------------