lParameter tDateStart or if not coded as a procedure tDateStart = blabla... create cursor gainloss(date D,gain f(10,2),loss f(10,2)) INDEX ON Date TAG Date1 select symbol from bhav_data group by 1 into cursor distsymbol nofilter local lnLastClose, lnCounter, lcTotalCount lnCounter = 0 lcTotalCount = TRANSFORM(RECCOUNT("distsymbol"),"999,999,999") WAIT WINDOW TRANSFORM(lnCounter,"999,999,999") + " of " + lcTotalCount + " records" scan lnCounter = lnCounter + 1 * Slowest thing you can do is write to screen so let's do it every 50 records instead of every record IF MOD(lnCounter,50) = 0 WAIT WINDOW TRANSFORM(lnCounter,"999,999,999") + " of " + lcTotalCount + " records" ENDIF SELECT GainLoss ZAP *-- get ONLY the records NOT CALCULATED before / needed to get the values for calculation! select date,close from bhav_data ; where PADR(symbol,20) = distsymbol.symbol ; and date>=m.tDateStart into cursor s1 *-- since you work consecutively, pre-fill lnLastClose and work alway of the current record! lnLastClose = s1.close SKIP SCAN Rest While ! EOF() lnDiff = lnLastClose - s1.close INSERT INTO GainLoss (Date, Gain, Loss) ; VALUES (S1.Date,IIF(m.lndiff>0,m.lndiff,0),IIF(m.lnDiff<=0,-m.lnDiff,0)) lnLastClose = s1.close endscan *-- DON'T close the tables * use in s1 *-- update bhav_data one of the ways you already do or written by others: *-- here experimentation is your best bet to show the fastest way. *-- Probably another (compound) index like PADR(symbol,20)+dtoc(date) *-- will be your best bet IF that doesn't slow down other insert operations *-- done to the table. Keep the big picture in mind. *-- But DON'T close the temp tables <g> * use in gainloss endscan * The following WAIT just makes the numbers equal at the end of the processing * if there aren't a number of recs divisable by 50 WAIT WINDOW TRANSFORM(lnCounter,"999,999,999") + " of " + lcTotalCount + " records"If you CANNOT keep the calculations done one earlier dates and always have to recalculate ALL,
*-- I assume symbol is c(20) CREATE CURSOR ac_gainloss (symbol C(20), date D, gain f(10,2), loss f(10,2) INDEX ON ON symbol + DTOS(date) TAG symbol1 SELECT bhav_data SET ORDER TO symbol1 && symbol + DTOS(date) lc_symbol = SPACE(0) SCAN *-- Set key value IF .NOT. lc_symbol == bhav_data->symbol lc_symbol = bhav_data->symbol WAIT WINDOW lc_symbol NOWAIT ENDIF *-- Prefill lnLastclose = bhav_data->close *-- Move to next day SKIP *-- Loop though all the data for this symbol SCAN WHILE bhav_data->symbol = lc_symbol *-- calculate with Chuck' approach or with something similar to your old one or Jim's *-- Prefill lnLastclose = bhav_data->close ENDSCAN *-- If using Jim's or your old approach, update from temp cursor and clean temp cursor here ENDSCAN *-- If Chuck's approach is better for your data structure, update hereAgain, I am not sure which way of summarizing is the best for your scenario, but the gathering of data is optimized.