-
Notifications
You must be signed in to change notification settings - Fork 0
/
tradelog
275 lines (244 loc) · 9.02 KB
/
tradelog
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/sh
export POSIXLY_CORRECT=yes
export LC_NUMERIC=en_US.UTF-8
export LC_ALL=C
#napoveda
print_help(){
echo "NAME: tradelog - stock market log analyzer"
echo "USAGE: tradelog [-h | --help]"
echo " tradelog [FILTER...] [COMMAND] [LOG...]"
echo ""
echo "COMMAND: "
echo " list-tick - print a list of stock symbols, so-called \"Tickers\""
echo " profit - print a total profit from closed positions"
echo " pos - print a list of values of currently held positions sorted in descending order by value"
echo " last-price - print the last known price for every ticker"
echo " hist-ord - print a histogram of the number of transactions according to the ticker"
echo " graph-pos - print a graph of values of held positions according to the ticker"
echo ""
echo "FILTERS:"
echo " -a DATETIME - after: only logs after this date are processed (excluding this date). DATETIME format is YYYY-MM-DD HH:MM:SS"
echo " -b DATETIME - before: only logs before this date are processed (excluding this date)"
echo " -t TICKER - only entries corresponding to a given ticker are considered.
With multiple occurrences of the switch, the set of all listed ticker is taken"
echo " -w WIDTH - for the graph printing set its width, that means adjust the length of the longest line to WIDTH.
WIDTH has to be a positive integer. Multiple occurrences of the switch is an invalid execution"
echo " -h | --help - print this help"
echo ""
}
#Error vystup
err(){
echo "E: $*" >>/dev/stderr
exit 1
}
#V pripade pracovanie s prazdnym logom je vystup rovno prazdny a ukoncuje sa
empty_log(){
EMPTY=$(eval "$*")
if [ -z "$EMPTY" ]; then
exit 0
fi
}
#premenne pre vstup
COMMAND=""
LOG_FILES=""
GZ_LOG_FILES=""
B_TIME="9999-99-99"
A_TIME=""
TICKERS=""
WIDTH="0"
READ_INPUT=""
TIME_FORMAT="[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-6][0-9]:[0-6][0-9]"
#zpracovat argumenty
while [ "$#" -gt 0 ]; do
case "$1" in
list-tick | pos | profit | last-price | hist-ord | graph-pos)
if [ -n "$COMMAND" ]; then
err "There can be used only one COMMAND at a time"
fi
COMMAND="$1"
shift
;;
-h)
print_help
exit 0
;;
-w)
if [ "$2" -le 0 ] || [ $WIDTH != "0" ]; then
err "WIDTH has to be a positive integer without multiple occurrences"
elif [ -z "$2" ]; then
err "Missing argument"
fi
WIDTH="$2"
shift
shift #zpracovali sa 2 parametry subezne
;;
-t)
CHECK=$( echo "$2" | grep ".*;.*" ) #nemoze obsahovat ';'
CHECK_2=$( echo "$2" | grep ".*[[:space:]].*" ) #nemoze obsahovat ' '
if [ -n "$CHECK" ] || [ -n "$CHECK_2" ]; then
err "Invalid ticker, cannot use ';' or ' '"
elif [ -z "$2" ]; then
err "Missing argument"
fi
if [ -z "$TICKERS" ]; then
TICKERS="$2"
else
TICKERS="$2\|$TICKERS"
#pipujeme tickery, '\' kvoli pouziti v uvozovkach eval\awk
fi
shift
shift
;;
-b)
CHECK=$( echo "$2" | grep "$TIME_FORMAT")
if [ -z "$CHECK" ]; then
err "Invalid format of time, use YYYY-MM-DD HH:MM:SS"
elif [ -z "$2" ]; then
err "Missing argument"
fi
B_TIME="$2"
shift
shift
;;
-a)
CHECK=$( echo "$2" | grep "$TIME_FORMAT")
if [ -z "$CHECK" ]; then
err "Invalid format of time, use YYYY-MM-DD HH:MM:SS"
elif [ -z "$2" ]; then
err "Missing argument"
fi
A_TIME="$2"
shift
shift
;;
*.gz)
if ! [ -r "$1" ]; then
err "Cannot read from file"
fi
GZ_LOG_FILES="$GZ_LOG_FILES $1"
shift
;;
*)
if ! [ -r "$1" ]; then
err "Cannot read from file"
fi
LOG_FILES="$LOG_FILES $1"
shift
;;
esac
done
#nacitanie logov
if [ -n "$GZ_LOG_FILES" ] && [ -n "$LOG_FILES" ]; then
READ_INPUT="gzip -d -c $GZ_LOG_FILES | cat $LOG_FILES -"
#logy a zipy
elif [ -n "$GZ_LOG_FILES" ]; then
READ_INPUT="gzip -d -c $GZ_LOG_FILES"
#zipy
elif [ -n "$LOG_FILES" ]; then
READ_INPUT="cat $LOG_FILES"
#logy
else
READ_INPUT="cat"
#stdin
fi
if [ -z "$TICKERS" ]; then
TICK_FILTER="cat"
#bez tikerov
else
TICK_FILTER="grep '^.*;\($TICKERS\);'"
fi
#filtrace(bud pomocou casu alebo tickerov, medzerou oddelene premenne)
READ_FILTERED="$READ_INPUT | awk -F ';' '{ if ( \$1 > \"$A_TIME\" ) {print \$0} }'"
READ_FILTERED="$READ_FILTERED | awk -F ';' '{ if ( \$1 < \"$B_TIME\" ) {print \$0} }'"
READ_FILTERED="$READ_FILTERED | $TICK_FILTER"
#vypisanie dlzky retazca v druhom stlpci a zoradenie podla danej dlzky
LENGTH="awk -F ' ' '{ printf \"%s %d\n\", \$0, length(\$2) }' | sort -r -n -t ' ' -k 3,3"
#formatovanie vystupu vo funkciach pos a last-price, nastavenie : zarovnanie dolava a ' ' pred nejdlhsi retazec
SPACES="awk -F ' ' 'BEGIN { start = 1; max_len = 0 }
{ if (start) { start = 0; max_len = \$3 };
printf \"%-10s: %*s\n\",\$1, max_len, \$2 }' "
#vypocet hodnot aktuálně držených pozic, vystup: TICKER HODNOTA(zaokruhlena na 2 des. miesta)
POS="awk -F ';' 'BEGIN { amount = 0; tick = 0; value = 0 }
{ if ( tick != \$2 && tick )
{ printf \"%s %.2f\n\", tick, amount * value;
amount = 0 };
if ( \$3 == \"sell\" ) { amount = amount - \$6 }
else { amount = amount + \$6 }
tick = \$2;
value = \$4 }
END { printf \"%s %.2f\n\",tick,amount * value }' "
#vykreslenie grafu pre hist-ord a graph-pos, prijima: TICKER POCET_ZNAKOV
GRAPH="awk -F ' ' '{ if (\$2 > 0) { sign = \"#\";
repeat = \$2 }
else { sign = \"!\"
repeat = -\$2 }
printf \"%-10s:\", \$1;
if ( repeat != 0 ) {printf \" \"}
for ( i = 0; i < repeat; i++) { printf\"%s\", sign}
printf \"\n\" }'"
if [ "$COMMAND" = "list-tick" ]; then
eval "$READ_FILTERED | awk -F ';' '{ print \$2 }' | sort -u"
elif [ "$COMMAND" = "profit" ]; then
PROFIT="awk -F ';' 'BEGIN { profit = 0 }
{ value = \$4 * \$6;
if ( \$3 == \"sell\" ) { profit = profit + value }
else { profit = profit - value } }
END { printf \"%.2f\n\", profit } '"
eval "$READ_FILTERED | $PROFIT"
elif [ "$COMMAND" = "pos" ]; then
if [ "$READ_INPUT" != "cat" ]; then
empty_log "$READ_FILTERED"
fi
eval "$READ_FILTERED | sort | sort -s -t ';' -k 2,2 | $POS | $LENGTH | $SPACES | sort -r -n -t ':' -k 2,2"
elif [ "$COMMAND" = "last-price" ]; then
if [ "$READ_INPUT" != "cat" ]; then
empty_log "$READ_FILTERED"
fi
LAST_PRICE="awk -F ';' 'BEGIN { tick = 0; value = 0 }
{ if ( tick != \$2 && tick ) { printf \"%s %.2f\n\", tick, value};
tick = \$2;
value = \$4 }
END { printf \"%s %.2f\n\", tick, value }' "
eval "$READ_FILTERED | sort | sort -s -t ';' -k 2,2 | $LAST_PRICE | $LENGTH | $SPACES | sort"
elif [ "$COMMAND" = "hist-ord" ]; then
if [ "$READ_INPUT" != "cat" ]; then
empty_log "$READ_FILTERED"
fi
HIST="awk -F ';' 'BEGIN { tick = 0; number = 0 }
{ if ( tick != \$2 && tick ) { print tick, number; number = 0};
tick = \$2;
number = number + 1 }
END { print tick, number }' "
if [ "$WIDTH" != 0 ]; then
RATIO="sort -n -r -t ' ' -k 2,2 |
awk -F ' ' 'BEGIN { start = 1; ratio = 0 }
{ if (start) { start = 0; ratio = $WIDTH / \$2 };
printf \"%s %d\n\", \$1, \$2*ratio }' |
sort"
else
RATIO="cat"
fi
eval "$READ_FILTERED | sort -t ';' -k 2,2 | $HIST | $RATIO | $GRAPH"
elif [ "$COMMAND" = "graph-pos" ]; then
if [ "$READ_INPUT" != "cat" ]; then
empty_log "$READ_FILTERED"
fi
if [ "$WIDTH" != 0 ]; then
RATIO=" awk -F ' ' '{ if (\$2 < 0) { abs = -\$2 }
else { abs = \$2 }
printf \"%s %.2f\n\", \$0, abs}' |
sort -n -r -t ' ' -k 3,3 |
awk -F ' ' 'BEGIN { start = 1; ratio = 0 }
{ if (start) { start = 0;
ratio = $WIDTH / \$3};
printf \"%s %d\n\", \$1, \$2 * ratio }' |
sort"
else
#bez zadanej sirky je implicitne nastaveny 1 znak = 1000 + zaokruhlenie dole
WIDTH=1000;
RATIO="awk -F ' ' '{ printf \"%s %d\n\",\$1, \$2 / $WIDTH }' "
fi
eval "$READ_FILTERED | sort | sort -s -t ';' -k 2,2 | $POS | $RATIO | $GRAPH "
else
eval "$READ_FILTERED"
fi