/* Exponential Smoothing */ options results if ~show('P','TCALC') then do address command 'run sys:turbocalc/turbocalc' address command 'waitforport TCALC' loadflag=1 end address 'TCALC' /* Add-in Rexx Math Library needed for some routines */ signal on syntax if ~show('l','rexxmathlib.library') then call addlib('rexxmathlib.library',0,-30) if ~show('l','rexxreqtools.library') then call addlib('rexxreqtools.library',0,-30) /* add to library list */ signal off syntax /* Start Main Routine */ if loadflag=1 then 'Load()' 'ActivateWindow()' ;'LiesCursorPos' range=rtgetstring(,"Enter Cell Range for Input","Input Request",,'rt_pubscrname="TurboCalc"') colon=pos(":",range) if colon=0 then do 'Message "Please select a range~before executing this script"' exit end /* Find cell references and cell, column numbers */ start_cell=substr(range,1,colon-1) end_cell=substr(range,colon+1) start_row=cellrow(start_cell) end_row=cellrow(end_cell) start_col=cellcol(start_cell) end_col=cellcol(end_cell) NRows=end_row-start_row+1 NCols=end_col-start_col+1 if NCols>1 then do 'Message "Only one column allowed"' exit end /* Get cell reference for output range */ out_cell=rtgetstring(,"Enter Cell Reference for Output","Input Request",,'rt_pubscrname="TurboCalc"') if out_cell="" then exit if length(out_cell)<2 | datatype(left(out_cell,1),'n') then do 'Message "Invalid cell reference"' exit end inval=rtgetlong("3","Enter number of intervals (default=3)","Input Request",,'rt_pubscrname="TurboCalc"') /* Suppress Screen Redraw to Speed Things Up */ 'Refresh 0' /* Get cell references for top cell in each column */ 'SelectCell' start_cell do col=start_col to end_col 'GetCursorPos' top_cell.col=result 'Column 1' end /* Get data from cell range */ col=start_col lav=0 tot=0 count.=0 total.=0 data.=0 do x=1 to NCols 'SelectCell' top_cell.col do y=1 to NRows 'GetValue' valtest=result if datatype(valtest)='NUM' then do 'GetValue' val=result val=strip(val) data.x.y=val tot=tot+val total.x=tot count.x=1+count.x end 'Down 1' end col=col+1 tot=0 lav=0 val=0 end /* Calculate Forecast Values */ z=1 x=1 dact.=0 summ.=0 Fval.=0 do y=inval to NRows z=y do i=z to z-inval+1 by -1 summ.y=(data.x.i)+summ.y end Fval.x.y=(summ.y)/inval end do y=inval to NRows dact.y=((data.x.y)-(Fval.x.y))**2 end do w=1 to inval+1 serr.w=0 end N=2 do w=inval+2 to NRows z=w do i=z to z-inval+1 by -1 summ.w=(summ.w)+(dact.i) end serr.w=sqrt((summ.w)/inval) end /* Output */ 'SelectCell' out_cell 'ColumnWidth 10' do y=1 to NRows 'Put' Fval.x.y 'Down 1' end 'Put' "Forecast" 'Down 2' 'Put " Interval="' 'SelectCell' out_cell 'Column 1' do y=1 to NRows 'Put' serr.y 'Down 1' end 'Put "St. Error"' 'Down 2' 'Put' inval 'Refresh 1' 'Refresh 2' 'Message' "Finished" exit /* Procedures */ cellrow: procedure do parse arg cell do charpos=2 to length(cell) if datatype(substr(cell,charpos,1),n) then return substr(cell,charpos) end return 0 end Return cellcol: procedure do parse arg cell labels="ABCDEFGHIJKLMNOPQRSTUVWXYZ" cell=upper(cell) len=length(cell) val=0 do charpos=1 to len if datatype(substr(cell,charpos,1),n) then do cell=reverse(substr(cell,1,charpos-1)) do x=1 to length(cell) val=(26**(x-1))*pos(substr(cell,x,1),labels)+val end return val end end return 0 end Return syntax: if arg(1)='FAIL' then do 'Message "Library is unavailable."' exit end