-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtradelog
executable file
·275 lines (263 loc) · 8.08 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
# ios-proj-1 tradelog
# author: Tomas Mauts
export LC_ALL=en_US.UTF-8
export POSIXLY_CORRECT=yes
print_help() {
echo "Usage: tradelog [-h|--help] [FILTER] [COMMAND] [LOG [LOG2 [...]]"
echo ""
echo "COMMANDS"
echo " list-tick Prints a list of occurring stock symbols"
echo " profit Overall profit"
echo " pos Value of held stock symbol"
echo " last-price Last known price of each ticker"
echo " hist-ord Transactions history of ticker"
echo " graph-pos Graph of values of held positions according to ticker"
echo ""
echo "FILTERS"
echo " -a DATETIME after - uses tickers after set time"
echo " -b DATETIME before - uses tickers before set time"
echo " -t TICKER uses only selected tickers"
echo " -w WIDTH Width of the longest line in graph"
echo " -h --help help"
}
BEFORE="9999-99-99"
cmdCount=0
# parsing parameters
while [ "$#" -gt 0 ]; do
case "$1" in
-h | --help)
print_help
exit 0
;;
list-tick)
COMMAND="list-tick"
cmdCount=$((cmdCount=cmdCount+1))
shift
;;
profit)
COMMAND="profit"
cmdCount=$((cmdCount=cmdCount+1))
shift
;;
pos)
COMMAND="pos"
cmdCount=$((cmdCount=cmdCount+1))
shift
;;
last-price)
COMMAND="last-price"
cmdCount=$((cmdCount=cmdCount+1))
shift
;;
hist-ord)
COMMAND="hist-ord"
cmdCount=$((cmdCount=cmdCount+1))
shift
;;
graph-pos)
COMMAND="graph-pos"
cmdCount=$((cmdCount=cmdCount+1))
shift
;;
-t)
TICKERS="$2\|$TICKERS"
shift; shift
;;
-b)
BEFORE="$2"
shift; shift
;;
-a)
AFTER="$2"
shift; shift
;;
-w)
if [ -n "$WIDTH" ]; then
1>&2 echo "Only one WIDTH parameter allowed"
exit 1
fi
WIDTH="$2"
if [ "$WIDTH" -le 0 ]; then
1>&2 echo "Invalid WIDTH parameter"
exit 1
fi
shift; shift
;;
*)
if [ ! -f "$1" ]; then
1>&2 echo "$1 is not a file or parameter"
exit 1
fi
if [ "$1" = *".gz" ]; then
LOG_GZ="$LOG_GZ $1"
else
LOG="$LOG $1"
fi
shift
;;
esac
done
if [ $cmdCount -gt 1 ]; then
1>&2 echo "Too many commands"
exit 1
fi
# Parsing inputs
# Only raw text or stdin
READ_INPUT="cat $LOG"
# gz and raw text
if [ "$LOG_GZ" ] && [ "$LOG" ]; then
READ_INPUT="gzip -d -c $LOG_GZ | $READ_INPUT -"
# only gz
elif [ "$LOG_GZ" ] && [ ! "$LOG" ]; then
READ_INPUT="gzip -d -c $LOG_GZ"
fi
READ_INPUT="$READ_INPUT | sort"
FILTER="eval $READ_INPUT"
# FILTERS
if [ "$TICKERS" ]; then
FILTER="$FILTER | grep -w \$TICKERS"
fi
if [ "$AFTER" ]; then
FILTER="$FILTER | awk -F ';' '\$1 > \"$AFTER\" {print \$0}'"
fi
if [ "$BEFORE" != "9999-99-99" ]; then
FILTER="$FILTER | awk -F ';' '\$1 < \"$BEFORE\" {print \$0}'"
fi
# :)
# Filter for evaluating pos / graph-pos
posFilt="eval awk -F ';' '{
if (curTick == \"\") {
curTick = \$2;
sum = 0;
}
else if (curTick != \$2) {
printf(\"%s:%.2f\n\", curTick, sum*lastPrice);
curTick = \$2;
sum = 0;
}
if (\$3 == \"buy\")
sum+=\$6;
else
sum-=\$6;
lastPrice = \$4
} END {printf(\"%s:%.2f\n\", curTick, sum*lastPrice)}'"
#Sorts output by length
sorter="eval awk -F ':' '{ print length(\$2), \$0 }' | sort -n -r | cut -d\" \" -f2- |
awk -F ':' '{ if(len == \"\")len = length(\$2); printf(\"%-10s: %*s\n\", \$1, len, \$2)}'"
# Execute
case "$COMMAND" in
list-tick)
$FILTER | awk -F ';' '{print $2}' | sort -u
;;
profit)
$FILTER | awk -F ';' '{if ($3 == "buy") {sum-=$4*$6}
else {sum+=$4*$6}} END {printf "%.2f\n", sum}'
;;
pos)
# Sort chronologically and by TICKERS
FILTER="$FILTER | sort -s -t ';' -k 2,2"
$FILTER | $posFilt | $sorter | sort -t ':' -k 2,2 -n -r
;;
last-price)
FILTER="$FILTER | sort -s -t ';' -k 2,2"
$FILTER | awk -F ';' '{
if (curTick == "") {
curTick = $2
}
# new ticker
if (curTick != $2) {
printf("%s:%.2f\n", curTick, lastPrice)
curTick = $2
}
lastPrice = $4
} END {printf("%s:%.2f\n", curTick, lastPrice)}' | $sorter | sort -t ':' -k 1,1
;;
hist-ord)
FILTER="$FILTER | sort -s -t ';' -k 2,2"
if [ -z "$WIDTH" ]; then
$FILTER | awk -F ';' '{
# first line
if (curTick == "") {
curTick = $2
transact = 0
}
# new ticker
else if (curTick != $2) {
printf("%-10s: ", curTick)
for (i=0; i<transact; i++){printf("#")}
printf("\n")
curTick = $2
transact = 0
}
# sum transactions
transact+=1
} END {printf("%-10s: ", curTick);for(i=0; i<transact; i++){printf("#")};printf("\n")}'
else
FILTER="$FILTER | sort -s -t ';' -k 2,2"
$FILTER | awk -F ';' '{
# first line
if (curTick == "") {
curTick = $2
count = 0
}
# new ticker
else if (curTick != $2) {
printf("%d;%s\n", count, curTick)
curTick = $2
count = 0
}
count+=1
} END{printf("%d;%s\n", count, curTick)}' | sort -n -r | awk -F ';' -v width="$WIDTH" '{if(max==""){max = $1}
printf("%-10s: ", $2);for(i=0; i<int($1/max*width); i++){printf("#")};printf("\n")}' | sort -t ':' -k 1,1
fi
;;
graph-pos)
if [ -z "$WIDTH" ]; then
WIDTH=1000
FILTER="$FILTER | sort -s -t ';' -k 2,2"
$FILTER | $posFilt | $sorter | sort -t ':' -k 2,2 -n -r | awk -F ':' -v width="$WIDTH" '{
if(int($2/width) == 0){
printf("%-10s:\n", $1);
next;
}
printf("%-10s: ", $1);
if($2 < 0) {
for(i=0;i<int(-$2/width);i++){
printf("!");
}
} else {
for(i=0;i<int($2/width);i++){
printf("#");
}
}
printf("\n")}' | sort -t ':' -k 1,1
else
FILTER="$FILTER | sort -s -t ';' -k 2,2"
# sed stuff - puts minus at the end of number | numerical sort | put - to the front of number
# basically abs value of max num
$FILTER | $posFilt | $sorter | sed -r 's/-([0-9]+.[0-9]+)/\1-/g;' | sort -t ':' -k 2,2 -n -r | sed -r 's/([0-9]+.[0-9]+)-/-\1/g;' | awk -F ':' -v width="$WIDTH" '{
if(max==""){
($2) > 0 ? max=$2 : max=-$2;
}
if(int($2/max*width) == 0){
printf("%-10s:\n", $1);
next;
}
printf("%-10s: ", $1);
if($2 < 0) {
for(i=0;i<int(-$2/max*width);i++){
printf("!");
}
} else {
for(i=0;i<int($2/max*width);i++){
printf("#");
}
}
printf("\n")}' | sort -t ':' -k 1,1
fi
;;
*)
$FILTER
esac
exit 0