home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a114 / 1.img / SAMPAPP / SAMPAPP.ZIP / DOFEED.SC < prev    next >
Encoding:
Text File  |  1990-08-25  |  9.9 KB  |  222 lines

  1. ; Script:     DOFEED.SC
  2. ; Version:    3.5
  3. ; Date:       23 March 1990
  4. ;
  5. ; The DOFEED script contains the procedures used to generate random stock
  6. ; buying and selling activity. When DOFEED first begins, it attempts to
  7. ; initialize the PRICE table--if the PRICE table has already been
  8. ; initialized, it is assumed that another copy of FEED is running elsewhere
  9. ; on the network, and the information in the fields manipulated by Rnd is not
  10. ; zeroed out.  If the PRICE table is not yet initialized, then information
  11. ; about the latest stock trading is zeroed out (High and Low set to the
  12. ; Price, Last Activity zeroed, and so on).
  13.  
  14. ; The SingleDoFeed procedure creates random stock trading information in a
  15. ; single user environment (necessary to create a real-time graph).
  16.  
  17. PROC CLOSED SingleDoFeed()                  ; simulates Dofeed.sc for single user
  18.   IF NETTYPE() = "SingleUser"               ; should work on single user only
  19.     THEN 
  20.       EDIT "Price"                          ; put the Price table on workspace
  21.       Zip = INT(RAND() * NRECORDS(TABLE())) ; generate random record # in table
  22.       MOVETO RECORD Zip + 1                 ; and move to that record
  23.       ChangePrice()                         ; make random changes to Price
  24.       DO_IT!                                ; save all new info in Price table
  25.   ENDIF
  26. ENDPROC  ; SingleDoFeed
  27.  
  28. ; The DoFeed procedure creates random stock trading information on a network
  29. ; and displays it as it is generated.
  30.  
  31. PROC CLOSED DoFeed()
  32.   USEVARS CurLine, FirstStockrow, LastStockrow, LeftMargin, IsColorMonitor
  33.   @ 0, 0
  34.   ?? FORMAT("W80,AC",
  35.             "Simulated price feed is running. Press any key to escape.")
  36.   R = FirstStockRow - 1                  ; display trades on starting line
  37.   COEDIT "Price"                         ; get the PRICE table on workspace
  38.   WHILE NOT CHARWAITING()                ; for as long as user doesn't press key
  39.     Zip = INT(RAND()*NRECORDS(TABLE()))  ; generate random record # in table
  40.     MOVETO RECORD Zip + 1                ; and move to that record
  41.     Retry = FALSE                        ; don't want to retry just yet
  42.     LOCKRECORD                           ; Can we get the record?
  43.     WHILE NOT Retval                     ; No, so we must
  44.       Retry = TRUE                       ;   keep reminder that we need to retry,
  45.       SLEEP 200                          ;   pause a moment before retrying,
  46.       LOCKRECORD                         ;   try to get the record again, and
  47.       PutMessage("*")                    ;   display an asterisk to show we're retrying.
  48.     ENDWHILE
  49.  
  50. ; If we get here, we locked record successfully.
  51.  
  52.     IF (Retry)                           ; if we had to retry, erase asterisk
  53.       THEN ClearMessage()
  54.     ENDIF
  55.     ChangePrice()                        ; make random changes to Price Table
  56.  
  57. ; Time to display what we've done
  58.  
  59.     Lastr = R                            ; Lastr is previous row we displayed trade on
  60.     R = R + 1                            ; increment our row counter to get new trade row
  61.     IF R > LastStockRow                  ; wrap to start line when we get to bottom
  62.       THEN R = FirstStockRow
  63.     ENDIF
  64.     @ Lastr, LeftMargin
  65.     ?? "            "                    ; clear Last trade: marker from previous trade
  66.     ?? CurLine                           ; and display previous trade
  67.     IF (IsColorMonitor)
  68.       THEN STYLE ATTRIBUTE 116           ; red on light grey for color monitors
  69.       ELSE STYLE REVERSE                 ; everyone else gets inverse
  70.     ENDIF
  71.          
  72.     @ R, LeftMargin
  73.     ?? "Last trade: "                    ; and update the trade information
  74.     CurLine = FORMAT("W6", [TICKER]) +
  75.               "  " + FORMAT("W9.3", [PRICE]) +
  76.               " " + FORMAT("W6", Volume) + " Shares " +
  77.               " at " + [LAST] + "  "
  78.     SWITCH
  79.       CASE Change > .001 :
  80.          CurLine = CurLine + "+" + FORMAT("W8.3", Change)
  81.       CASE Change < -.001 :
  82.          CurLine = CurLine + "-" + FORMAT("W8.3", -Change)
  83.       OTHERWISE:
  84.          CurLine = CurLine + "Unchanged"
  85.     ENDSWITCH
  86.     ?? CurLine                          ; display the trade information
  87.     STYLE ATTRIBUTE 31                  ; back to high intensity or white on blue
  88.     UNLOCKRECORD
  89.   ENDWHILE
  90.  
  91. ; If we drop out of the preceding loop, user must have pressed a key.
  92.  
  93.   x = GETCHAR()                         ; gobble up the keystroke
  94.   DO_IT!                                ; update the table
  95.   CLEARALL                              ; clear the workspace
  96.   DoneFeed()                            ; update Semaphor to tell world we're done
  97. ENDPROC  ; DoFeed
  98.  
  99. PROC ChangePrice()                      ; procedure makes random changes to Price table
  100.   WHILE (TRUE)
  101.     ChangeDecimal = .125 * INT(RAND()*8)            ; convert changes to 1/8 of dollar
  102.     ChangeInt = INT(ROUND((RAND()-.5)*[Price],3))   ; generate a random price
  103.     IF ChangeInt < 0
  104.       THEN Change = ChangeInt - ChangeDecimal
  105.       ELSE Change = ChangeInt + ChangeDecimal
  106.     ENDIF
  107.     Newprice = [Price] + Change         ; calculate new stock price
  108.     If Newprice > 200                   ; maximum of new price should be less than $200
  109.       THEN LOOP
  110.       ELSE QUITLOOP
  111.     ENDIF
  112.   ENDWHILE
  113.   Volume = INT(RAND() * 5000) + 5       ; calculate new random value for Volume
  114.   [High] = MAX([High], Newprice)        ; calculate new session High price
  115.   [Low]  = MIN([Low], Newprice)         ; calculate new session Low price
  116.   [Volume] = [Volume] + Volume          ; update the stock trading volume
  117.   [Chg] = Newprice - ([Price] - [Chg])  ; insert the change in price
  118.   [Price] = NewPrice                    ; and update the Price field
  119.   [LAST] = TIME()                       ; finally, insert the time of the trade
  120. ENDPROC  ; ChangePrice
  121.  
  122.  
  123. ; TryToInitialize attempts to reinitialize fields in the Price table.
  124. ; If we are the first ones there, we perform the initialization by
  125. ; zeroing out the fields that will be generated by RND. If we aren't the
  126. ; first ones there, then someone else is already generating stock activity
  127. ; and we shouldn't reinitialize the table.
  128.  
  129. PROC TryToInitialize()
  130.   TestInitialize()                      ; Can we perform an initialization?
  131.   IF (NOT Retval)                       ; No, so
  132.     THEN TestWaitForInitialize()        ;   Do we need to wait for someone else to
  133.          WHILE (Retval)                 ;     complete an initialization?
  134.            PutMessage(                  ; Yes, so
  135.            "Session Initialization being performed by another user.  Please wait...")
  136.            SLEEP 500                    ;   after a short pause,
  137.            TestWaitForInitialize()      ;   keep checking.
  138.          ENDWHILE
  139.          ClearMessage()                 ; erase the message, since we can go on
  140.          RETURN                         ; move on
  141.   ENDIF
  142.  
  143. ; If we get here, we must have to initialize the market
  144.  
  145.   PutMessage("Initializing Stock Transaction Session...")
  146.   WHILE (TRUE)                          ; wait for any query to finish,
  147.     LOCK "Price" PWL                    ;  since queries do a WL on table
  148.     IF (Retval)
  149.        THEN QUITLOOP
  150.     ENDIF
  151.     SLEEP 200
  152.   ENDWHILE
  153.   COEDIT "Price"                        ; get the table on workspace
  154.   SCAN                                  ; and for each record,
  155.     [Volume] = 0                        ;   reset the daily volume
  156.     [High]   = [Price]                  ;   today's high price is equal to previous close
  157.     [Low]    = [Price]                  ;   today's low price is equal to previous close
  158.     [Chg]    = 0                        ;   no change in price yet
  159.   ENDSCAN
  160.   DO_IT!                                ; and save the new table
  161.   UNLOCK "Price" PWL
  162.   DoneInitialize()                      ; release the initialization semaphore
  163.   ClearMessage()                        ; erase the init message
  164. ENDPROC ; TryToInitialize
  165.  
  166. PROC ClearBackground(Attr, FirstLine, LastLine)
  167.   PRIVATE FirstLine, LastLine, Attr
  168.   PAINTCANVAS FILL " " ATTRIBUTE Attr FirstLine, 0, LastLine, 79
  169. ENDPROC
  170.  
  171. PROC PutMessage(Msg)                    ; define a message window like Paradox's
  172.   IF (IsColorMonitor)                   ; are we on a color monitor?
  173.     THEN STYLE ATTRIBUTE 116            ; yes, so use red on light grey
  174.     ELSE STYLE REVERSE                  ; no, everyone else gets inverse
  175.   ENDIF
  176.   @ 24, 80 - LEN(Msg) ?? Msg             ; displays in message window
  177.   STYLE ATTRIBUTE 31                    ; return style to normal
  178. ENDPROC
  179.  
  180. PROC ClearMessage()                     ; clear the message window we defined
  181.   ClearBackground(31, 24, 24)
  182. ENDPROC
  183.  
  184. PROC StartUp()                          ; get the feed started
  185.   RESET                                 ; start with a known system state
  186.   CurLine = ""
  187.   FirstStockRow = 8                     ; Where will the stock info display start?
  188.   LastStockRow = 20                     ; Where will the stock info display end?
  189.   LeftMargin = 6
  190.   CURSOR OFF                            ; don't need the cursor to display info
  191.   IsColorMonitor = (Monitor()="Color")  ; find out what kind of monitor we're on
  192.   ClearBackground(31, 0, 24)            ; and initialize the screen to blank
  193.   TryToInitialize()                     ; see if we need to initialize Price table
  194.   @ FirstStockRow - 2, LeftMargin       ; finally, display headings for stock listings
  195.   ?? "           " +
  196.      FORMAT("W6,AL", "Symbol") +
  197.      FORMAT("W11,AR", "Price") +
  198.      FORMAT("W20,AC", "Shares Traded") +
  199.      FORMAT("W8,AC", "Time") +
  200.      FORMAT("W9,AR", "Change")
  201.   CLEARALL                              ; clear all tables on workspace
  202. ENDPROC  ; StartUp
  203.  
  204. PROC TestInitialize()
  205.   LOCK "Semaphor" WL, "Semaphor" PWL
  206.   RETURN Retval
  207. ENDPROC
  208.  
  209. PROC TestWaitForInitialize()
  210.   LOCK "Semaphor" WL
  211.   RETURN NOT Retval
  212. ENDPROC
  213.  
  214. PROC DoneInitialize()
  215.   UNLOCK "Semaphor" PWL
  216. ENDPROC
  217.  
  218. PROC DoneFeed()
  219.   UNLOCK "Semaphor" WL
  220. ENDPROC
  221.  
  222.