rem *_________________ section 5.0 ________________*
rem *            STOCK PRICE UPDATE ROUTINE        *
rem *            --------------------------	   *
rem *	UPDATED: February 5, 1983		   *
rem *	BY:	 Edward A. Valenzuela		   *
rem *	REVISION: 1.31				   *
rem *----------------------------------------------*

rem ----- requires subroutines -----
rem          stkfprnt.bas ... for print initialization
rem          stkhist.bas      to store prices
rem          stkprice.bas     for jump vectors
rem          qksort%.bas      to sort integers
rem          scrnin.bas   ... for screen handling      

5000 rem ... open files at beginning of section ...

CREATE "b:stkprice.$$$" RECL 30 as 1  

  if end #8 then 5010
5040 OPEN "b:stkprice.dat" RECL 30 as 8  
  if end #3 then 5020
5050 OPEN "b:stkprice.bak" RECL 30 as 3  
  if end #9 then 5030
5060 OPEN "b:stkfil01.dat" recl 80 as 9 

  IF END #8 THEN 5011 
rem ...... read in number of stocks ......
   GOSUB 320	: rem .. read header
   GOSUB 330	: rem .. read price file
   GOSUB 325	: rem .. read S&P 500 price

   GOSUB 315	: rem .. read stock data file

 for i%=1 to nstk%
  oldprice(i%) = newprice(i%)
 next i%
  oldprice(i%) = newprice(i%)

GOSUB 5200 : rem ...update each stock price
GOSUB 5410 : rem ...write out new file
 RETURN

rem ***************** SUBROUTINES ******************

5200 rem ... update the current price of each stock ...

print clear$; fn.line$(4)
 print " Enter date of current stock prices: mo/dy/yr"
     input newdate$
        nbegin%=1
	sum1 =0
	sum2 =0
	sum3 =0
print clear$
 GOSUB 5250

rem ... if TEMPRICE.DAT file exists, continue, else begin...
  if end #4 then 5210
  OPEN "B:TEMPRICE.DAT" RECL 30 as 4
  if end #4 then 5212
    read #4; ntemp%, newdate$, olddate$
    for i%=1 to nbegin%-1
      read #4; NEWprice(i%),OLDprice(i%)
    next i%
  5212 CLOSE 4

5210 for i%=nbegin% to nstk%
  print fn.line$(14)           
    for j%=1 to 4              
      print fn.clear$(2,line%) 
    next j%                    
  print fn.line$(16); \  
        "INSTRUCTIONS: ENTER stock price for "; namstk$(i%); BELL$
     input NEWprice(i%)
 GOSUB 5250 : rem ...print heading
 GOSUB 5255 : rem ...print record

rem ...to exit at any time, enter 0 for price ...
rem ... the prices entered will be saved ...
rem ... in the temporary price file ...

     if NEWprice(i%) = 0 then nstk%=i%-1: goto 5400
     next i%

 FOR I%=1 to nstk%          
     oldval = nshrs%(i%)*OLDprice(i%)
	sum2 = sum2 + oldval
     newval = nshrs%(i%)*NEWprice(i%)
	sum3 = sum3 + newval
     NEWvalue(i%) = newval
     incvalue(i%) = newval - oldval
        sum1 = sum1 + incvalue(i%)
 rem ... check for no oldvalue ...
 if oldval = 0 then pcntinc(i%) = 0 else \
    pcntinc(i%) = ( incvalue(i%)/oldval * 100. )
 NEXT I%

 if sum2 = 0 then sum9 = 0 else sum9 = sum1/sum2 * 100.
  
if end #8 then 5213
  read #8; OLDprice(i%),dummyprice
5213 namstk$(i%)="S&P 500"
  print fn.line$(15); fn.clear$(2,line%) : \
      print fn.clear$(2,line%) : \
      print fn.clear$(2,line%) 
  print fn.line$(16); \  
        "INSTRUCTIONS: ENTER stock price for "; namstk$(i%); BELL$
     input NEWprice(i%)
  incvalue(i%) = NEWprice(i%) - OLDprice(i%)
 if OLDprice(i%) = 0 then pcntinc(i%) = 0 else \
    pcntinc(i%)=( incvalue(i%)/OLDprice(i%) * 100. )

rem ... ASK IF ANY MISTAKES ...
PRINT
print "You now have a chance to correct input errors"

5308 input "any price changes ? (y/n)?"; yes$
if yes$ = "n" or yes$ = "N" then goto 5310 
input "enter number of stock for price change"; num%
     previous.value = nshrs%(num%)*newprice(num%)
print "price for "; namstk$(num%); " is "; newprice(num%)
input "enter correct price "; newprice(num%)

     oldval = nshrs%(num%)*oldprice(num%)
     newval = nshrs%(num%)*NEWprice(num%)
	sum3 = sum3 + newval - previous.value
     NEWvalue(num%) = newval
     incvalue(num%) = newval - oldval
        sum1 = sum1 + incvalue(num%) - (previous.value-oldval)
rem ... check for no oldvalue ...
 if oldval = 0 then pcntinc(num%) = 0 else \
    pcntinc(num%) = ( incvalue(num%)/oldval * 100. )

 if sum2 = 0 then sum9 = 0 else sum9 = sum1/sum2 * 100.

GOTO 5308

5310 rem ----- sort the percentage increases -----

  N%=NSTK%
  FOR J%=1 TO N%
    X%(J%) = INT%(PCNTINC(J%)*10.)
    KODE%(J%)=J%
  NEXT J%

  GOSUB 1500 : rem ENTRY to qksort SUBROUTINE...

5315 rem ...... print the performance summary for the week ......
  print clear$; tab(10); "DO YOU WANT TO PRINT TO :"
  print tab(14); " CONSOLE = 1 "
  print tab(14); " PRINTER = 2 "
  print tab(14); " BOTH    = 3 " 
  
print fn.line$(10); bell$
 input "INSTRUCTIONS: enter selection and hit return"; selection%
 if SELECTION% < 2 then goto 5316

title$ = "OPTION 5 - PRICES UPDATED FOR THE CURRENT WEEK - NEW VALUATION"
TITLE2$ = "sorted by: percent increase for week"

  gosub 6600 : rem ENTRY to STKFPRNT.BAS SUBROUTINE...
  gosub 5350
  CONSOLE

5316 if selection% = 2 then RETURN else GOSUB 5317
 RETURN

5250 rem ... home cursor, print header ...
 print home$
   for j%=1 to 4
   print fn.clear$(2,line%)
   next j%
 print fn.line$(16); fn.clear$(37,line%)
 print fn.clear$(2,line%)
 print home$
 print tab(6); " ***** STOCK PRICE UPDATE *****"
 print "                                           price"	
 print "number  STOCK NAME  OLD PRICE  NEW PRICE   change" 
 print "----------------------------------------   ------"
 print
RETURN
                  
5255 rem ... display next record ...
  if i% < 6 then print fn.line$( 4 + fn.mod%(i%,5) ) else \
     print fn.line$(24) : print fn.line$(9)
  pq = newprice(i%) - oldprice(i%)
  print using \
   " ###  / stock  name /  ##.###   ##.###    ###.###" ; \
   i%,namstk$(i%),oldprice(i%),newprice(i%),pq
RETURN


5350 rem ----- SUBROUTINE PRINT -----

for2$="    ## /................../ #####   ####.#   ###.###   ###.###  ##.###   ####     #####.##"
print "                              $        %     current  previous  price   number   current"
print "           stock name     increase increase   price    price  increase of shares  value" 
print

     for i%=1 to nstk%
       j%=kode%(i%)
       pq = NEWprice(j%) - OLDprice(j%)
       print using for2$;i%,namstk$(j%),incvalue(j%),pcntinc(j%),NEWprice(j%),\
                         OLDprice(j%),pq,nshrs%(j%),NEWvalue(j%)
       if fn.mod%(i%,5) eq 5 then print
     next i%
  print
  print using for2$;i%,namstk$(i%),incvalue(i%),pcntinc(i%),NEWprice(i%),OLDprice(i%)
for3$="       TOTALS :            ######.## ###.#                                        ######.##"
  print
  print using for3$; sum1,sum9,sum3

RETURN

5317 rem ..... subroutine DISPLAY .....
 print clear$;
 print tab(4); " ***** STOCK PRICE CHANGE ANALYSIS *****"
 print
for5$ ="## /........../ ###.###  ###.##  ###.##    ####.#"
 print " # STOCK NAME   CURRENT  PRICE      %        $" 
 print "                 PRICE   change    inc      inc"
 print "-------------------------------------------------"
 print
    for k%=1 to nstk% step 10
     print fn.line$(7); fn.clear$(2,line%); : 
       for i%=1 to 11   :
        print fn.clear$(2,line%) :
       next i% :
     print fn.line$(6)
       end% = k% + 9
       if nstk% < end% then end% = nstk%
       
     for i%=k% to end%
       j%=kode%(i%)
       pq = NEWprice(j%) - OLDprice(j%)
       print using for5$;i%,namstk$(j%),NEWPRICE(j%),pq,pcntinc(j%), \
           incvalue(j%)
       if fn.mod%(i%,5) eq 5 then print
     next i%
for l%=1 to 2000: next l%: rem ... DELAY ...
    next k%
    pq = newprice(i%) - oldprice(i%)
  print
  print using for5$;i%,namstk$(i%),newprice(i%),pq,pcntinc(i%), \
      incvalue(i%)
  print
for6$ ="     PORTFOLIO :                 ###.##   #####.##"
  print using for6$; sum9,sum1
  for l%=1 to 2000: next l%: rem ... DELAY ...

RETURN

5400 rem ----- write out the new prices, update the price file -----
rem ...... write new records into temporary file ......

CREATE "B:TEMPRICE.DAT" RECL 30 as 4
print # 4 ; nstk%,newdate$,olddate$
 for i%=1 to nstk%
   print #4; NEWprice(i%),OLDprice(i%)
 next i%
CLOSE 4

5402 rem ...... close all open files ......
  CLOSE 1,3,8,9
RETURN

5410 rem ----- SUBROUTINE WRITE TO FILE -----
 print # 1 ; nstk%,newdate$,olddate$
 for i%=1 to nstk%
   print #1; NEWprice(i%),OLDprice(i%)
 next i%
   print #1; NEWprice(i%),OLDprice(i%) : rem S&P 500

rem ...... close all open files except 3 ......
  CLOSE 1,8,9

rem ......delete the backup file ......automatically closes 3 .....
  DELETE 3     \ delete the old backup file

rem ...... move the old data file into the backup spot
  if rename("B:stkprice.bak", "B:stkprice.dat") = 0 then goto 5490
    
rem ...... move the temporary file into the new data file
  if rename("B:stkprice.dat", "B:stkprice.$$$") = 0 then goto 5492

REM ... NORMAL EXIT FROM SUBROUTINE ...
REM ... GO TO THE HISTORICAL FILE UPDATE ROUTINE ...
 print
 print tab(10);"UPDATING HISTORICAL PRICE FILE"
 GOSUB 5500 : rem ... ENTRY to STKHIST.BAS subroutine ...
    
5480 RETURN

5490 print "unable to rename old data file to backup"
     go to 5480
5492 print "unable to rename temporary file to new data file"
     go to 5480

5999 RETURN

5010 print "FILE B: STKPRICE.DAT NOT FOUND"
     print "check diskette loaded in drive B"
     print "does a stock price file exist?"
     input "do you want to continue the routine? y or n?"; y$
     if y$ = "y" then close 8: initialize: goto 5040
 RETURN

5011 print
     print "reached END-OF-FILE on B:STKPRICE.DAT"
     print " not enough stock prices in data file "
     input "to continue, enter yes"; y$
     goto 5402

5020 print "FILE B: STKPRICE.BAK NOT FOUND"
     print "does a backup of the stock price file exist?"
     input "do you want to continue the routine? y or n?"; y$
     if y$ = "y" then goto 5050
 RETURN

5030 print "FILE B:STKFIL01.DAT NOT FOUND"
     print "check diskette loaded in drive B"
     print "or perhaps you have not run the CREATE STOCK FILE yet!"
     input "do you want to continue the routine? y or n?"; y$
     if y$ = "y" then goto 5060
 RETURN

rem *------------ end of weekly update ------------*