|
25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more... |
Home Projects Up Sitemap Search Blog Forum+Chat About Us Privacy Terms of Use Feedback FAQ Images Services Payments Humor Music |
The purpose of this program is to calculate the moving average, the arithmetic mean of the last N items, in a list of numbers that you input. You select the "N" number of items.
The program will ask if you want the output printed on a line printer. It will then ask, "After how many items do you want the moving average to start." For example, if you input "3", the following will happen:
For ease of data entry, the program runs continuously until you enter a Ctrl+C sequence from your keyboard. That is, press and hold down the control ('CONTROL' OR 'CTRL' OR 'CTL') key on your keyboard, and simultaneously press the capital letter 'C'.
Simple in concept, moving averages are used extensively in stock trading systems. For example, a buy or sell signal might be generated when two different moving averages cross over each other, such as the 30-day moving average crossing the 200-day. The type of signal (buy or sell) would be determined by the trends of the two lines (up or down) and whether the shorter-term average is crossing to the upside or to the downside.
10 REM PROGRAM NAME 'MOVAVG' 20 REM COPYRIGHT (C) 1982 BY STEVEN WHITNEY. 21 REM Initially published by http://25yearsofprogramming.com. 22 REM Published under GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY. 30 PRINT CHR$(27)"E";STRING$(75,"+"):PRINT:PRINT,"*** MOVING AVERAGE CALCULATIONS ***" 40 PRINT 50 PRINT "THIS PROGRAM ACCEPTS A LIST OF NUMBERS FROM YOU. AFTER EACH NUMBER" 60 PRINT"YOU INPUT, IT WILL RETURN THE AVERAGE OF THE NUMBERS SO FAR." 70 PRINT"AFTER THE N-TH ITEM (YOU SELECT N), IT WILL RETURN THE N-ITEM MOVING AVERAGE." 80 PRINT STRING$(75,"-") 90 PRINT"FOR DATA ENTRY CONVENIENCE, THE PROGRAM WILL RUN UNTIL YOU STOP IT BY" 100 PRINT"HOLDING DOWN THE <CTRL> KEY AND SIMULTANEOUSLY PRESSING THE LETTER C." 110 PRINT STRING$(75,"-") 120 PRINT"IF NOT ALREADY DONE, PLEASE DEPRESS THE 'CAPS LOCK' KEY." 130 PRINT"ALL YOUR INPUT MUST BE CAPITAL LETTERS.":PRINT:PRINT STRING$(75,"-") 140 PRINT"DO YOU WANT THE OUTPUT THROUGH A PRINTER? (Y/N)";:Z$=INPUT$(1):PRINT Z$ 150 IF Z$<>"Y" AND Z$<>"N" THEN 140 160 IF Z$="Y" THEN Z=1 170 PRINT 180 INPUT "AFTER HOW MANY ITEMS DO YOU WANT THE MOVING AVERAGE TO START";N 190 IF Z=1 THEN LPRINT STRING$(36,"="):LPRINT"MOVING AVERAGE STARTS AFTER";N;"ITEMS.":LPRINT STRING$(36,"="):LPRINT "DATA","AVERAGE":LPRINT "-------","--------" 200 PRINT STRING$(75,"-"):PRINT "BEGIN INPUT":PRINT 210 DIM A(N) 220 S=0 230 I=I+1 240 IF I>N THEN 300 250 INPUT "NEXT #";A(I) 260 FOR J=1 TO I : S=S+A(J) : NEXT J 270 PRINT,"AVERAGE =";S/I 280 IF Z=1 THEN LPRINT A(I),S/I 290 PRINT : GOTO 220 300 PRINT:PRINT,"MOVING AVERAGE STARTS WITH NEXT ITEM":PRINT 310 IF Z=1 THEN LPRINT STRING$(36,"=") 320 IF Z=1 THEN LPRINT "DATA",STR$(N)"-ITEM MOV. AVG.":LPRINT "------","---------------" 330 S=0 340 FOR J=1 TO N-1 : A(J)=A(J+1) : NEXT J 350 INPUT "NEXT #";A(N) 360 FOR J=1 TO N : S=S+A(J) : NEXT J 370 PRINT:PRINT,STR$(N);"-ITEM MOVING AVERAGE=";S/N 380 IF Z=1 THEN LPRINT A(N),S/N 390 PRINT : GOTO 330 400 REM COPYRIGHT (C)1982 STEVEN WHITNEY.
|
|
|
|
|
Copyright ©2010 Steven Whitney. Last modified Thu 10/21/2010 02:08:03 -0700. |
||