The ZX Stock Exchange

See all articles from SYNC v3 n5

You have just inherited $10,000 from a wealthy uncle who made it big on the stock market. He also gave you the modem number of his broker, a computer.

The ZX Stock Exchange carries stocks listed on the New York Stock Exchange as well as stocks not listed there. For the purpose of demonstrating how the market works, a random selection of stocks of five stocks has been made and entered as three letter abbreviations: AAA, BBB, CCC, DDD, EEE. When you enter the program, you may substitute the stocks that you believe have the greatest optential. However, you must use designations of three letters. You must also put in the full name of each in lines 890, 920, 950, 980, and 1010.

The stock prices are generated somewhat randomly, but they show short and long term trends just as on other stock exchanges. To get the “feel” of the market, the program should be played for at least 10 market days.

Your broker the computer charges you a modest one percent brokerage fee on all transactions made.

Getting Started

First, carefully type in the program in Listing 1. Save it by typing GOTO 2760. This will cause it to run automatically after LOADing.

When the program is running, the first thing the computer will ask is whether or not you want instructions. Regardless of whether you get instructions, press S to begin.

Figure 1 shows the initial printing of the stock: the name, initials, price per share. Your stock, cash, and total assets will then be briefly shown.

[Figure: Figure 1. Opening screen listing the five stocks, initials and $/share, with ZX Stock Exchange average and assets]

Next the computer will ask you for your transaction for each stock. To buy, type xxx, where xxx is the number of shares you want to buy. If you do not want to buy or sell, type 0.

After you have entered your initial transactions, the screen format will look like Figure 2. At the top will be displayed how many days you have been playing. Then a chart will show the stock initials, price per share, your holdings (the number of shares in your portfolio), the total value of the stocks, and the change from the previous day. The next lines will be any messages, the ZX Stock Exchange average, the net change, and the value of your stock, cash, and total assets. You will then be asked whether you want to continue. Type Y for yes and N for no.

[Figure: Figure 2. End of trading, day 1 screen]

[Figure: Figure 3. End of trading, day 2 screen asking for the next transaction]

Program Description

Lines 10-120 and 230-830 initialize the program and give instructions if requested.

The subroutine in lines 130-220 is frequently used for formatting. A cash value such as 23.6 is inputted as G. The subroutine will then manipulate G so that it comes out as a neater form stored in R$, 23.60 in the example. The length of R$ is stored in L for neatness by lining up the decimal points on the cash amounts.

Line 190 takes care of a peculiar happening on the ZX81 which is due to the nature of floating point arithmetic. E.g., type PRINT 1012.56-INT 1012.56 or PRINT 1012.56-1012. The computer should print .56, but instead it prints 0.55999994. This happens with a few other numbers also, and it must be accounted for in software.

Lines 840-1010 set up the initial display shown in Figure 1. Lines 1012-1180 calculate the stock exchange average (EA) and net change (NC). Lines 1190-1320 print your stock assets (SA), cash assets (C), and total assets (D).

Lines 1330-1470 ask if you want to continue. If the answer is Y (yes), the program then asks you to input your transactions.

Lines 1480-1750 make sure that you have not spent more than you have and that you do not try to sell more stock than you own. The brokerage fee (BF) is calculated at line 1640.

Lines 1760-2000 set up the screen display similar to Figure 2. Lines 1940-1990 randomly split stocks (i.e., doubles the number of shares you presently have) and declares dividends.

The subroutine that makes the stock prices is located in lines 2010-2500. D1 and D2 are random numbers of days which respectively determine when stock S will increase 4 points and when stock S2 will decrease 4 points. If D1 days have passed, it picks a stock (S), sets P, and determines a new D1. This is done similarly with D2, S2, and P2. If D1 or D2 days have not passed, the change is determined using the trend sign and slope (A), which is changed after TR days, and a random amount (R) less than $1.

The conclusion is contained in lines 2510-2750. Lines 2760-2780 save the program so that it RUNs automatically after LOADing.

For the benefit of those who are interested in programming techniques (and for those who want to “fix the game to their advantage,” a euphemmistic phrase for “cheating”), a list of the variables is found in Figure 4.

Line notes:

390: G (6), THE ZX STOCK EXCHANGE in inverse, G (5).
400: F (6), THE ZX STOCK EXCHANGE in normal letters, F (5).
760: WELCOME TO THE ZX STOCK EXCHANGE in inverse.
2700 and 2720: Inverse $ (32).

Listing 1. The ZX Stock Exchange.

  10 REM STOCK MARKET SIMULATION
20 REM BY DONALD A. BURGIO/[address omitted]
30 DIM I$(5,3)
40 DIM O$(2)
50 LET A$="INT (RND*4.99)+1"
60 LET I$(1)="AAA"
70 LET I$(2)="BBB"
80 LET I$(3)="CCC"
90 LET I$(4)="DDD"
100 LET I$(5)="EEE"
110 RAND
120 GOTO 230
130 LET X=ABS G
140 LET B=INT X
150 LET E=X-B
160 LET Z$=STR$ B
170 IF E=0 THEN LET O$="00"
180 IF E<>0 THEN LET O$=STR$ (100*E)
190 IF O$(2)="." THEN LET O$=STR$ ((1000*E)+.01)
200 LET R$=Z$+"."+O$
210 LET L=LEN R$
220 RETURN
230 LET A=INT ((RND/10)*100+.5)/100
240 DIM S(5)
250 DIM P(5)
260 DIM T(5)
270 DIM C(5)
280 LET TT=0
290 LET F=0
300 LET D1=0
310 LET D2=0
320 LET P=0
330 LET P2=0
340 LET DY=0
350 LET EA=0
360 SLOW
370 CLS
380 FOR I=1 TO 20
390 PRINT AT 0,0;"[graphic G x6][inverse THE ZX STOCK EXCHANGE][graphic G x5]"
400 PRINT AT 0,0;"[graphic F x6]THE ZX STOCK EXCHANGE[graphic F x5]"
410 NEXT I
420 PRINT AT 3,0;"WHAT IS YOUR NAME?"
430 INPUT N$
440 PRINT AT 3,0;"DO YOU WANT INSTRUCTIONS? (Y/N)"
450 INPUT Z$
460 IF Z$="N" THEN PRINT AT 3,0;"[about 31 spaces]"
470 IF Z$="N" THEN GOTO 680
480 CLS
490 PRINT "WELCOME TO THE ZX STOCK EXCHANGE"
500 PRINT TAB ((33-LEN N$)/2);N$+"."
510 PRINT "YOUR ACCOUNT CURRENTLY CONTAINS"
520 PRINT "$10,000. YOU MAY BUY OR SELL"
530 PRINT "STOCKS. A TABLE OF AVAILABLE "
540 PRINT "STOCK, THEIR PRICES, AND THE"
550 PRINT "NUMBER OF SHARES IN YOUR PORT-"
560 PRINT "FOLIO WILL BE PRINTED. FOLLOW-"
570 PRINT "ING THIS THE INTITIALS OF EACH"
580 PRINT "STOCK WILL BE PRINTED. HERE YOU"
590 PRINT "INDICATE A TRANSACTION. TO BUY"
600 PRINT "A STOCK TYPE XXX, WHERE XXX IS "
610 PRINT "THE NUMBER OF SHARES YOU WISH TO"
620 PRINT "BUY. TO SELL TYPE -XXX, WHERE"
630 PRINT "-XXX IS THE NUMBER OF SHARES YOU"
640 PRINT "WISH TO SELL. A 1 PERCENT SRO-"
650 PRINT "KERAGE FEE WILL AUTOMATICALLY"
660 PRINT "BE CHARGED TO YOUR ACCOUNT."
670 PRINT TAB 11;"GOOD LUCK."
680 LET S(1)=130
690 LET S(2)=90
700 LET S(3)=120
710 LET S(4)=85
720 LET S(5)=115
730 LET TR=VAL A$
740 PRINT AT 21,7;"PRESS S TO START."
750 IF INKEY$="S" THEN GOTO 790
760 PRINT AT 0,0;"[inverse WELCOME TO THE ZX STOCK EXCHANGE]"
770 PRINT AT 0,0;"WELCOME TO THE ZX STOCK EXCHANGE"
780 GOTO 750
790 IF RND>.5 THEN GOTO 810
800 LET A=-A
810 CLS
820 GOSUB 2010
830 LET C=10000
840 PRINT
850 PRINT "STOCK INT. $/SHARE"
860 PRINT "--------------------------------"
870 LET G=S(1)
880 GOSUB 130
890 PRINT "A AND A ASSOCIATES AAA ";TAB (32-L);R$
900 LET G=S(2)
910 GOSUB 130
920 PRINT "B AND B BUYERS BBB ";TAB (32-L);R$
930 LET G=S(3)
940 GOSUB 130
950 PRINT "C AND C COAL CO. CCC ";TAB (32-L);R$
960 LET G=S(4)
970 GOSUB 130
980 PRINT "D AND D DEVELOPERS DDD ";TAB (32-L);R$
990 LET G=S(5)
1000 GOSUB 130
1010 PRINT "E AND E ENERGY EEE ";TAB (32-L);R$
1020 LET TA=EA
1030 LET EA=0
1040 LET SA=0
1050 FOR I=1 TO 5
1060 LET EA=EA+S(I)
1070 LET SA=SA+S(I)*P(I)
1080 NEXT I
1090 LET EA=INT (100*(EA/5)+.5)/100
1100 LET NC=INT ((EA-TA)*100+.5)/100
1110 LET D=SA+C
1120 IF F THEN GOTO 1160
1130 PRINT
1140 PRINT "ZX STOCK EXCHANGE AVER.:";EA
1150 GOTO 1190
1160 PRINT
1170 PRINT "ZX STOCK EXCHANGE AVER.:";EA
1180 PRINT "NET CHANGE:";NC
1190 PRINT
1200 PRINT N$;":"
1210 LET SA=INT (100*SA+.5)/100
1220 LET G=SA
1230 GOSUB 130
1240 PRINT "STOCK ASSETS=$";TAB (24-L);R$
1250 LET C=INT (100*C+.5)/100
1260 LET G=C
1270 GOSUB 130
1280 PRINT "CASH ASSETS= $";TAB (24-L);R$
1290 LET D=INT (100*D+.5)/100
1300 LET G=D
1310 GOSUB 130
1320 PRINT "TOTAL ASSETS=$";TAB (24-L);R$
1330 PRINT
1340 IF NOT F THEN PAUSE 225
1350 IF NOT F THEN POKE 16437,255
1360 IF NOT F THEN GOTO 1400
1370 PRINT "DO YOU WISH TO CONTINUE? (Y/N)"
1380 INPUT C$
1390 IF C$="N" THEN GOTO 2510
1400 FOR I=10 TO 21
1410 PRINT AT I,0;"[spaces]"
1420 NEXT I
1430 PRINT AT 11,0;"WHAT IS YOUR TRANSACTION IN:"
1440 FOR I=1 TO 5
1450 PRINT AT 12,0;I$(I);"?"
1460 INPUT T(I)
1470 NEXT I
1480 PRINT AT 20,0;"PLEASE WAIT..."
1490 LET DP=0
1500 LET DS=0
1510 FOR I=1 TO 5
1520 LET T(I)=INT (T(I)+.5)
1530 IF T(I)<=0 THEN GOTO 1560
1540 LET DP=DP+T(I)*S(I)
1550 GOTO 1620
1560 LET DS=DS-T(I)*S(I)
1570 IF -T(I)<=P(I) THEN GOTO 1620
1580 PRINT AT 20,0;"YOU HAVE OVERSOLD A STOCK; TRY AGAIN. "
1590 PAUSE 300
1600 POKE 16437,255
1610 GOTO 1400
1620 NEXT I
1630 LET TT=DP+DS
1640 LET BF=INT (.01*TT*100+.5)/100
1650 LET CT=C-DP-BF+DS
1660 IF CT>=0 THEN GOTO 1720
1670 PRINT AT 19,0;"YOU HAVE TRIED TO SPEND "
1680 PRINT "$";-CT;" MORE THAN YOU HAVE."
1690 PAUSE 300
1700 POKE 16437,255
1710 GOTO 1400
1720 LET C=CT
1730 FOR I=1 TO 5
1740 LET P(I)=P(I)+T(I)
1750 NEXT I
1760 CLS
1770 GOSUB 2010
1780 LET DY=DY+1
1790 PRINT "*** END OF TRADING: DAY ";DY;" ***"
1800 PRINT
1810 PRINT "STK. $/SHR. HDS. $ VALUE CHANGE"
1820 PRINT "................................"
1830 FOR I=1 TO 5
1840 LET G=S(I)
1850 GOSUB 130
1860 PRINT AT 3+I,0;I$(I);AT 3+I,(11-L);R$;AT 3+I,13;P(I);
1870 LET G=S(I)*P(I)
1880 GOSUB 130
1890 PRINT AT 3+I,(24-L);R$;AT 3+I,26;C(I)
1900 NEXT I
1910 LET F=1
1920 PRINT
1930 LET R=VAL ((STR$ RND)(1 TO 4))
1940 LET I=INT (RND*7)
1945 IF I>5 THEN GOTO 1940
1950 IF I=0 THEN GOTO 1940
1960 IF R<.15 THEN PRINT I$(I);" DECLARES DIVIDENTS OF $";(R*4+.5);"/SHARE."
1970 IF R<.15 THEN LET C=C+P(I)*(R*4+.5)
1980 IF R>.93 THEN PRINT I$(I);" SPLITS STOCK."
1990 IF R>.93 THEN LET P(I)=P(I)*2
2000 GOTO 1020
2010 FAST
2020 IF D1>0 THEN GOTO 2060
2030 LET S=VAL A$
2040 LET D1=VAL A$
2050 LET P=1
2060 IF D2>0 THEN GOTO 2100
2070 LET S2=VAL A$
2080 LET D2=VAL A$
2090 LET P2=1
2100 LET D1=D1-1
2110 LET D2=D2-1
2120 FOR I=1 TO 5
2130 LET R=RND
2140 IF R>.25 THEN GOTO 2170
2150 LET R=.25
2160 GOTO 2240
2170 IF R>.5 THEN GOTO 2200
2180 LET R=.5
2190 GOTO 2240
2200 IF R>.75 THEN GOTO 2230
2210 LET R=.75
2220 GOTO 2240
2230 LET R=0
2240 LET BC=0
2250 IF P<1 THEN GOTO 2290
2260 IF INT (S+.5)<>INT (I+.5) THEN GOTO 2290
2270 LET BC=4
2280 LET P=0
2290 IF P2<1 THEN GOTO 2330
2300 IF INT (S2+.5)<>INT (I+.5) THEN GOTO 2330
2310 LET BC=-4
2320 LET P2=0
2330 LET C(I)=INT (A*S(I))+R+INT (3-6*RND+.5)+BC
2340 LET C(I)=INT (100*C(I)+.5)/100
2350 LET S(I)=S(I)+C(I)
2360 IF S(I)>0 THEN GOTO 2400
2370 LET C(I)=0
2380 LET S(I)=0
2390 GOTO 2410
2400 LET S(I)=INT (100*S(I)+.5)/100
2410 NEXT I
2420 LET TR=TR-1
2430 IF TR<1 THEN GOSUB 2460
2440 SLOW
2450 RETURN
2460 LET TR=VAL A$
2470 LET A=INT ((RND/10)*100+.5)/100
2480 IF RND<=.5 THEN GOTO 2500
2490 LET A=-A
2500 RETURN
2510 CLS
2520 PRINT
2530 PRINT
2540 PRINT "AT THE END OF ";DY;" DAYS TRADING:"
2550 IF D>=10000 THEN GOTO 2610
2560 PRINT "YOU HAVE LOST $";
2570 LET G=10000-D
2580 GOSUB 130
2590 PRINT R$
2600 GOTO 2650
2610 PRINT "YOU HAVE MADE $";
2620 LET G=D-10000
2630 GOSUB 130
2640 PRINT R$
2650 PRINT "ON THE ZX STOCK EXCHANGE."
2660 PRINT
2670 PRINT "HOPE YOU HAD FUN, ";N$;"."
2680 PRINT "COME BACK AGAIN."
2690 PRINT AT 20,2;"PRESS S TO RETURN TO BASIC."
2700 PRINT AT 0,0;"[inverse $ x32]"
2710 PRINT AT 0,0;"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
2720 PRINT AT 21,0;"[inverse $ x32]"
2730 IF INKEY$="S" THEN STOP
2740 PRINT AT 21,0;"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
2750 GOTO 2700
2760 CLEAR
2770 SAVE "STOCK"
2780 RUN

Figure 4. List of Variables.

Variable   Function
A$         the function INT (RND*4.99)+1
A          trend slope + sign
B          formatting:  INT X (dollar amount)
BC         big change constant (-4,0,4)
BF         brokerage fee
C          cash assets
CT         cash total (temporary)
C(I)*      change in stock value
D          total assets
DY         current simulation day
DP         total day's purchases
DS         total day's sales
D1         SPS**
D2         SPS**
E          formatting:  Cents amount
EA         exchange average
F          0 = first simulation day
G          formatting:  initial cash amount
I$(I)*     Stock initials
L          length of R$ (for lining up decimal points)
N$         user's name
NC         net change
O$         formatting:  the final cents amount
P          SPS**
P2         SPS**
P(I)*      number of shares in user's portfolio
R$         final formatted cash amount
R          random value
S          SPS**
SA         stock assets
S2         SPS**
S(I)*      stock values
TA         total assets
TT         total transaction amount
TR         number of days in the trend
T(I)*      transaction amount
X          formatting:  ABS G
Z$         formatting:  STR$ B, general input

*I         *2 = BBB   *4 = DDD
*1 = AAA   *3 = CCC   *5 = EEE   ** Stock price subroutine; see text.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.