-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtradelog
348 lines (336 loc) · 8.46 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/bin/sh
export POSIXLY_CORRECT=yes
export LC_NUMERIC=en_US.UTF-8
#=======================================================================================================================
# Help
#=======================================================================================================================
help_message()
{
echo "tradelog - stock market log analyzer"
echo "usage: tradelog [-h|--help]"
echo " tradelog [FILTER] [COMMAND] [LOG [LOG2 [...]]]"
echo ""
echo "COMMAND - can be one of:"
echo ""
echo "list-trick - listing of occurring stock exchange symbols, so-called \"tickers\""
echo "profit - statement of total profit from closed positions"
echo "pos - list of values of currently held positions sorted in descending order by value"
echo "last-price - list of the last known price for each ticker"
echo "hist-ord - histogram report of the number of transactions according to the ticker"
echo "graph-pos - statement of the graph of values of held positions according to the ticker"
echo ""
echo "FILTER - can be a combination of the following"
echo ""
echo "-a DATETIME - after: only records AFTER this date are considered (without this date). DATETIME format:"
echo " YYYY-MM-DD HH:MM:SS"
echo "-b DATETIME - before: only records BEFORE this date (without this date) are considered"
echo "-t TICKER - only entries corresponding to a given ticker are considered. With multiple occurrences of the"
echo " switch, the set of all listed ticker is taken."
echo "-w WIDTH - in the list of graphs, sets their width, ie the length of the longest line to WIDTH. Thus, WIDTH"
echo " must be a positive integer. Multiple occurrences of the switch is a faulty start"
echo ""
echo "HELP"
echo "-h OR --help - print help with a brief description of each command and switch"
}
#=======================================================================================================================
# Parameter check
#=======================================================================================================================
COMMAND=""
FILES=""
FILES_GZIP=""
DATETIME_A=""
DATETIME_B=""
TICKER=""
WIDTH=""
COMMAND=""
while [ "$#" -gt 0 ]
do
case "$1" in
list-tick | profit | pos | last-price | hist-ord | graph-pos)
COMMAND="$1"
shift
;;
-h | --help)
help_message
exit 0
;;
-a)
if [ "$DATETIME_A" = "" ]; then
DATETIME_A=$2
else
DATETIME_A="$DATETIME_A\n$2"
fi
shift
shift
;;
-b)
if [ "$DATETIME_B" = "" ]; then
DATETIME_B=$2
else
DATETIME_B="$2\n$DATETIME_B"
fi
shift
shift
;;
-t)
TICKER="$TICKER;$2"
shift
shift
;;
-w)
WIDTH="$2"
shift
shift
;;
*.log)
FILES="$FILES $1"
shift
;;
*.log.gz)
FILES_GZIP="$FILES_GZIP $1"
shift
;;
*)
echo "non valid parameter"
echo "with -h | --help you can print help message"
exit 1
;;
esac
done
if [ "$FILES_GZIP" = "" ]; then
INPUT_LOG=$(cat $FILES)
elif [ "$FILES" = "" ]; then
INPUT_LOG=$(gzip -d -c $FILES_GZIP | sort)
else
INPUT_LOG=$(gzip -d -c $FILES_GZIP | cat $FILES - | sort)
fi
#=======================================================================================================================
# Filter Values
#=======================================================================================================================
if [ "$DATETIME_A" != "" ]; then
DATETIME_A=$(echo "$DATETIME_A" | awk ' NR==1{ out=$0 }
{if( $0 > out)
{
out=$0
}
}
END{print out}
' )
fi
if [ "$DATETIME_B" != "" ]; then
DATETIME_B=$(echo "$DATETIME_B" | awk ' NR==1{ out=$0 }
{
if ( $0 < out )
{
out=$0
}
}
END{print out}
' )
fi
INPUT_LOG_FILTERED=$(echo "$INPUT_LOG" | awk -F ';' -v after="$DATETIME_A" -v before="$DATETIME_B" '{ if (($1 > after) && (before == "" || $1 < before)) {print $0} }' )
if [ "$TICKER" != "" ]; then
INPUT_LOG_FILTERED=$(echo "$INPUT_LOG_FILTERED" | awk -F ";" -v ticker="$TICKER" ' BEGIN{ n=split(ticker,t,";")} { for (tic = n; tic >= 0; tic--) { if($2 == t[tic]) print $0} }')
fi
#=======================================================================================================================
# Commands
#=======================================================================================================================
if [ "$COMMAND" = "list-tick" ]; then
FIN_PRINT=$(echo "$INPUT_LOG_FILTERED" | awk -F ";" '{print $2}' | sort -u)
elif [ "$COMMAND" = "profit" ]; then
FIN_PRINT=$(echo "$INPUT_LOG_FILTERED" | awk -F ";" 'BEGIN{ prof=0 } { if ( $3 =="buy" ) prof=prof-($4*$6); else prof=prof+($4*$6);} END{ printf "%0.2f", prof }')
elif [ "$COMMAND" = "pos" ]; then
INPUT_LOG_FILTERED=$(echo "$INPUT_LOG_FILTERED" | sort -t ";" -k "2,2")
FIN_PRINT=$(echo "$INPUT_LOG_FILTERED" | awk -F ";" 'BEGIN{ ticker=""; value=0; lastprice=0 }
{ if ( ticker == $2 )
{
{
if ( $3 =="buy" )
value=value+$6;
else
value=value-$6;
}
lastprice=$4;
}
else
{
if ( ticker != "") {
{ printf("%-10s: %0.2f\n", ticker, value*lastprice) }
}
ticker=$2;
{
if ( $3 =="buy" )
value=+$6;
else
value=-$6;
}
lastprice=$4;
}
} END{ printf("%s:%f", ticker, value*lastprice)}')
ALLI=$(echo "$FIN_PRINT" | awk -F ":" ' BEGIN{ max=0 }
{
tmp=sprintf("%.2f", $2)
len=length(tmp);
if ( len > max ) max=len; } END{ print max }')
FIN_PRINT=$(echo "$FIN_PRINT" | sort -r -n -t ":" -k 2 | awk -F ":" -v alli=$ALLI '{ printf("%-10s: %*.2f\n", $1, alli, $2) }')
elif [ "$COMMAND" = "last-price" ]; then
INPUT_LOG_FILTERED=$(echo "$INPUT_LOG_FILTERED" | sort -t ";" -k "2,2")
FIN_PRINT=$(echo "$INPUT_LOG_FILTERED" | awk -F ";" 'BEGIN{ lastprice=0 }
{ if ( ticker == $2 )
{
lastprice=$4;
}
else
{
if ( ticker != "") {
{ printf("%s: %0.2f\n", ticker, lastprice) }
}
ticker=$2;
lastprice=$4;
}
} END{ printf("%s: %0.2f", ticker, lastprice)}')
ALLI=$(echo "$FIN_PRINT" | awk -F ":" ' BEGIN{ max=0 }
{
tmp=sprintf("%.2f", $2)
len=length(tmp);
if ( len > max ) max=len; } END{ print max }')
FIN_PRINT=$(echo "$FIN_PRINT" | awk -F ":" -v alli=$ALLI '{ printf("%-10s: %*.2f\n", $1, alli, $2) }')
elif [ "$COMMAND" = "hist-ord" ]; then
INPUT_LOG_FILTERED=$(echo "$INPUT_LOG_FILTERED" | sort -t ";" -k "2,2")
FIN_PRINT=$(echo "$INPUT_LOG_FILTERED" | awk -F ";" 'BEGIN{ hist="" }
{ if ( ticker == $2 )
{
hist=hist+1;
}
else
{
if ( ticker != "") {
{ printf("%-10s: %s\n", ticker, hist) }
}
ticker=$2;
hist=1;
}
} END{ printf("%-10s: %s\n", ticker, hist)}')
FIN_PRINT=$(echo "$FIN_PRINT" | sort -r -n -t ":" -k "2" | awk -F ":" -v width="$WIDTH" 'NR<2{ max=$2 }
{
out="";
n=0;
if(width != ""){
if(max == $2){
n=width;
}else
{
n=$2/(max/width);
}
}else{
n=$2;
}
for(i=1 ; i <= n; i++)
{
out=out "#";
}
{ printf("%-10s: %s\n", $1, out) }
}
')
FIN_PRINT=$(echo "$FIN_PRINT" | sort -t ":")
elif [ "$COMMAND" = "graph-pos" ]; then
INPUT_LOG_FILTERED=$(echo "$INPUT_LOG_FILTERED" | sort -t ";" -k "2,2")
FIN_PRINT=$(echo "$INPUT_LOG_FILTERED" | awk -F ";" 'BEGIN{ ticker=""; value=0; lastprice=0 }
{ if ( ticker == $2 )
{
{
if ( $3 =="buy" )
value=value+$6;
else
value=value-$6;
}
lastprice=$4;
}
else
{
if ( ticker != "") {
{ printf("%-10s: %0.2f\n", ticker, value*lastprice) }
}
ticker=$2;
{
if ( $3 =="buy" )
value=+$6;
else
value=-$6;
}
lastprice=$4;
}
} END{ printf("%-10s: %0.2f\n", ticker, value*lastprice)}')
FIN_PRINT=$(echo "$FIN_PRINT" | sort -r -n -t ":" -k 2)
MAX=$(echo "$FIN_PRINT" | awk -F ":" '
BEGIN{ first = "" }
{
if (first == "")
{
first = $2;
}
}
END{
last=$2*(-1);
if(first > last)
{
out=first;
}else
{
out=last;
}
{print out}
}
')
FIN_PRINT=$(echo "$FIN_PRINT" | sort -t ":" -k 1)
FIN_PRINT=$(echo "$FIN_PRINT" | awk -F ":" -v max="$MAX" -v width="$WIDTH" '
{ out= "";
if ( width == "" )
{
n = $2 / 1000;
if ( n >= 0 )
{
for ( i = 1; i <= n ; i++ )
{
out=out "#";
}
}else
{
for ( i = -1; i >= n; i-- )
{
out=out "!";
}
}
}else
{
n=$2/(max/width);
if ( n >= 0 )
{
for ( i = 1; i <= n ; i++ )
{
out=out "#";
}
}else
{
for ( i = -0.99; i > n; i-- )
{
out=out "!";
}
}
}
{ if( out == "" )
{
printf "%-10s:\n", $1;
}else
{
printf "%-10s: %s\n", $1, out;
}
}
}
' )
fi
if [ "$FIN_PRINT" = "" ]; then
echo "$INPUT_LOG_FILTERED"
else
echo "$FIN_PRINT"
fi