Select symbol,Date,Close ; cast(0 as I) as gain, cast(0 as I) as Loss ; From 'bhav_data' Order By symbol, Date Into Cursor s1 nofilter lcSymbol = '' scan if !( symbol == m.lcSymbol ) lcSymbol = symbol dimension aGainloss[1500,4] && depending on the acurracy of your mentioned max! copy fields s1.date, s1.gain, s1.Loss, s1.close to array aGainloss while s1.symbol == m.lcSymbol dimension aGainloss[_tally,4] skip -1 for ix = 1 to alen(aGainloss,1)-1 *-- probably a toss-up to use 1 less line but access the arrays more often m.diff = aGainloss[m.ix,4] - aGainloss[m.ix+1,4] if ( m.diff > 0 ) aGainloss[m.ix,2] = m.diff else aGainloss[m.ix,3] = -m.diff endif endfor #if .f. *-- directly calculating via the array should be faster, *-- if set relation into bhav_data cannot be used if ix>=14 *-- write in 1 continued statement for less work in the interpreter *-- since loss and gain are divided, averaging is not needed rs = ( aGainloss[m.ix-13,2] + .... + aGainloss[m.ix,2] ) / ; ( aGainloss[m.ix-13,3] + .... + aGainloss[m.ix,3] ) Update bhav_data Set RSI = (100-(100/(1+(m.rs)))) ; where symbol+Dtoc(Date,1) == m.lcSymbol + Dtoc(aGainloss[m.ix-14,1],1) *-- or a possibility to move only through the table via locate rest / found *-- and then replace endif #else *-- here should be the biggest beef, if gainloss:bhav_data is 1:1, *-- set relation and replace should work great! then use the old opprach <g> ... other setup code set relation on symbol+Dtoc(Date,1) into bhav_data ... calculate and replace RSI with (100-(100/(1+(m.rs)))) in bhav_data #endif endif endscan