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; |