OPTIONS X
C     PROGRAM PLOT.FOR
C
C  This program reads a stock data file and produces a candlestick chart
C  with High, Low, Close, and Volume.
C
C  INPUT.DAT Data line example:
C  | 9338     78.375    77.375   77.875  -0.500   5/14/92*|
C
      DIMENSION LINE(9),BUF(79),CLINE(14),FILE(3)
      REAL HIGH,LOW,CLSE,CHG,DATE,CHK
      INTEGER VOL,MO,DAY,TVOL,IYR
C
      IDY = 0
      IMO = 0
      THIGH = 0.0
      TLOW = 10000.0
      TVOL = 0
C
C  Open the input file
C
      CALL OPEN (2,'INPUT.DAT')
C
C  Initialize Charting Limits
C
      READ (2,1000,END=6) VOL,HIGH,LOW,CLSE,CHG,DATE,IYR
 1000 FORMAT (I5,3X,F8.3,2X,F8.3,1X,F8.3,1X,F7.3,2X,A5,1X,I2)
      DECODE (DATE,5,1001) MO,CHK,DAY
 1001 FORMAT (I2,A1,I2)
    5 READ (2,1000,END=6) VOL,HIGH,LOW,CLSE,CHG,DATE,IYR
      IF (HIGH .GT. THIGH) THIGH = HIGH
      IF (LOW .LT. TLOW) TLOW = LOW
      IF (VOL .GT. TVOL) TVOL = VOL
      GOTO 5
C
    6 REWIND 2
      FACT = (THIGH-TLOW)/55.0
      FVOL = FLOAT(TVOL)/18.0
C
C  Initialize Month and Day counters
C
      IDY = DAY
      IMO = MO-1
      CALL OPEN (3,'OUTPUT.CRT')
C
C  Main Loop Setup
C
   15 IDYS = 30
      IF ((IMO.EQ.1).OR.(IMO.EQ.3).OR.(IMO.EQ.5).OR.(IMO.EQ.7).OR.
     *(IMO.EQ.8).OR.(IMO.EQ.10).OR.(IMO.EQ.12)) IDYS = 31
      IF (IMO.EQ.2) IDYS = 28
C
C  Main Loop
C
      DO 10 I=IDY,IDYS
   14 READ (2,1000,END=30) VOL,HIGH,LOW,CLSE,CHG,DATE
      DECODE (DATE,5,1001) MO,CHK,DAY
      IF (CHK.NE.'/     ') GOTO 14
   16 IF (MO.NE.IMO) THEN
          CALL CMNTH(IMO,MO,DAY)
          GOTO 16
      ENDIF
   17 IF (DAY .GT. I) THEN
          DO 18 K=1,79
   18     BUF(K) = '      '
          ENCODE (CLINE,79,1002) BUF
 1002 FORMAT (79A1)
C
C  Print the line
C
          WRITE (3,1003) CLINE
 1003 FORMAT (13A6,A1)
          WRITE (1,1003) CLINE
          I=I+1
          GOTO 17
      ENDIF
      IDY = DAY+1
      IMO = MO
C
C  Compute Chart Volume Positions
C
      ITEST = 0
      IF (FVOL.GT.0) ITEST = VOL/FVOL
      DO 11 N=1,20
      BUF(N) = '      '
   11 IF (N .LE. ITEST) BUF(N) = '-     '
C
C  Compute Chart Price Positions
C
      ILOW = IFIX((LOW-TLOW)/FACT)
      IHIGH = IFIX((HIGH-TLOW)/FACT)
      ICLOS = IFIX((CLSE-TLOW)/FACT)
      DO 12 N=1,59
      BUF(N+20) = '      '
   12 IF ((N .GT. ILOW) .AND. (N .LT. IHIGH))  BUF(N+20) = '-     '
      BUF(ILOW+20) = '|     '
      BUF(IHIGH+20) = '|     '
      BUF(ICLOS+20) = '+     '
      ENCODE (CLINE,79,1002) BUF
C
C  Print the line
C
      WRITE (3,1003) CLINE
   10 WRITE (1,1003) CLINE
      GOTO 15
C
C  Normal Exit
C
   30 CONTINUE
      CALL CLOSE (2)
      CALL CLOSE (3)
      STOP 'Normal exit'
      END
      SUBROUTINE CMNTH(IMO,MO,DAY)
C
      INTEGER IMO,MO,DAY
C
C  Check for last month
C
   10 IMO = IMO+1
      IF (IMO.EQ.13) IMO = 1
      IF (IMO.EQ.MO) THEN
          RMO = 'DEC'
          IF (IMO.EQ.1) RMO = 'JAN'
          IF (IMO.EQ.2) RMO = 'FEB'
          IF (IMO.EQ.3) RMO = 'MAR'
          IF (IMO.EQ.4) RMO = 'APR'
          IF (IMO.EQ.5) RMO = 'MAY'
          IF (IMO.EQ.6) RMO = 'JUN'
          IF (IMO.EQ.7) RMO = 'JLY'
          IF (IMO.EQ.8) RMO = 'AUG'
          IF (IMO.EQ.9) RMO = 'SEP'
          IF (IMO.EQ.10) RMO = 'OCT'
          IF (IMO.EQ.11) RMO = 'NOV'
          WRITE (1,1001) RMO
 1001 FORMAT ('************************************* ',A3,
     *' *************************************')
          WRITE (3,1001) RMO
          RETURN
      ENDIF
      IDYS = 30
      IF ((IMO.EQ.1).OR.(IMO.EQ.3).OR.(IMO.EQ.5).OR.(IMO.EQ.7).OR.
     *(IMO.EQ.8).OR.(IMO.EQ.10).OR.(IMO.EQ.12)) IDYS = 31
      IF (IMO.EQ.2) IDYS = 28
      DO 20 M=1,IDYS
      WRITE (1,1000) IMO,M
   20 WRITE (3,1000) IMO,M
 1000 FORMAT ('          no data for ',I2,'/',I2)
      GOTO 10
      END
