File: lt-stockfilter

package info (click to toggle)
linuxtrade 3.65-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,880 kB
  • ctags: 1,969
  • sloc: ansic: 30,091; sh: 2,278; perl: 566; makefile: 126
file content (110 lines) | stat: -rwxr-xr-x 2,007 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/sh

PROGNAME="$0"
PATH=$PATH:$HOME/bin:/usr/share/linuxtrade/bin

usage() {
	cat <<EOF
Usage:
	`basename $PROGNAME` [options] {symbol] ...

	Given a list of stock symbols on the command line or read from
	stdin (one per line), look up the profile for the stock and
	filter out symbols that do not meet the desired criteria.

	When reading symbols from stdin, an extra comment may be
	appended to the input lines.  These comments will be passed
	along to the output.

	NOTE: Uses lt-stockprofile to get the stock profiles.

Options:
	-p price	Minimum Price [$PRICEmin]
	-P price	Maximum Price [$PRICEmax]
	-v volume	Minimum Volume [$VOLmin]
	-V volume	Maximum Volume [$VOLmax]
	-D lvl		Debug level
EOF

	exit 1
}

#
#       Report an error and exit
#
error() {
	echo "`basename $PROGNAME`: $1" >&2
	exit 1
}

#
#       Process the options
#
DEBUG=0
VOLmin=300000
VOLmax=2000000000
PRICEmin=5
PRICEmax=50
unset OPTIND
while getopts "p:P:v:V:D:h?" opt
do
	case $opt in
	p)	PRICEmin="$OPTARG";;
	P)	PRICEmax="$OPTARG";;
	v)	VOLmin="$OPTARG";;
	V)	VOLmax="$OPTARG";;
	D)	DEBUG="$OPTARG";;
	h|\?)	usage;;
	esac
done
shift `expr $OPTIND - 1`

#
#	Main Program
#
check_one()
{
    lt-stockprofile $1 \
    | awk -v "SYM=$1" -v "XTRA=$2" \
	    -v "VOLmin=$VOLmin" -v "VOLmax=$VOLmax" \
	    -v "PRICEmin=$PRICEmin" -v "PRICEmax=$PRICEmax" \
    '
    /Daily Volume \(/ {
	    volume=$5
	    gsub(",", "", volume)
	    if (match(volume, "K")) {
		gsub("K", "", volume)
		volume *= 1000.0
	    }
	    if (match(volume, "M")) {
		gsub("M", "", volume)
		volume *= 1000000.0
	    }
    }
    /Recent Price/ {
	    price=$3
	    gsub("[$]", "", price)
    }
    END {
	if (volume < VOLmin) exit
	if (volume > VOLmax) exit
	if (price < PRICEmin) exit
	if (price > PRICEmax) exit
	if (XTRA != "")
	    printf("%s	%s\n", SYM, XTRA)
	else
	    print SYM
    }
    '
}

if [ $# = 0 ]; then
    while read symbol xtra
    do
	check_one "$symbol" "$xtra"
    done
else
    for i in $*; do
	check_one "$i" ""
    done
fi