-
Notifications
You must be signed in to change notification settings - Fork 1
/
tradelog
196 lines (179 loc) · 7.55 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
#!/bin/sh
export POSIXLY_CORRECT=yes
export LC_NUMERIC=en_US.UTF-8
print_help()
{
echo "tradelog - log analyzer for stock market"
echo ""
echo "Usage: tradelog [-h|--help]"
echo " or: tradelog [FILTR] [COMMAND] [LOG [LOG2 [...]]"
echo ""
echo "Arguments:"
echo " -h | --h Print help"
echo ""
echo "COMMAND:"
echo " list-tick Listing of occurring stock exchange symbols \"tickers\""
echo " profit Listing profit from all closed trades"
echo " pos Listing of current hold position sorted in descending order by value"
echo " last-price Listing of 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 "FILTR:"
echo " -a DATETIME After: only records after this date are considered (without this date). DATETIME is in the format YYYY-MM-DD HH:MM:SS"
echo " -b DATETIME Before: only records BEFORE this date are considered (without 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 In the list of graphs, sets their width, in the length of the longest line to WIDTH. This, WIDTH must be a positive integer. Multiple occurrences of the switch is a faulty start."
}
COMMAND=""
DATETIME_BEFORE="9999-99-99 99:99:99"
DATETIME_AFTER="0000-00-00 00:00:00"
TICKER=""
WIDTH=""
LOG_FILES=""
GZ_LOG_FILES=""
while [ "$#" -gt 0 ]
do
case "$1" in
-h | --help)
print_help
exit 0
;;
list-tick | pos | profit | last-price | hist-ord | graph-pos)
COMMAND="$1"
shift
;;
-a)
if [ "$DATETIME_AFTER" = "0000-00-00 00:00:00" ]; then
DATETIME_AFTER="$2"
else
DATETIME_AFTER=$(awk -v FIRST="$DATETIME_AFTER" -v SECOND="$2" 'BEGIN{if(FIRST < SECOND){printf FIRST}else{printf SECOND}}')
fi
shift
shift
;;
-b)
if [ "$DATETIME_BEFORE" = "9999-99-99 99:99:99" ]; then
DATETIME_BEFORE="$2"
else
DATETIME_BEFORE=$(awk -v FIRST="$DATETIME_BEFORE" -v SECOND="$2" 'BEGIN{if(FIRST > SECOND){printf FIRST}else{printf SECOND}}')
fi
shift
shift
;;
-t)
if [ "$TICKER" = "" ]; then
TICKER="$2"
else
TICKER="$2;$TICKER"
fi
shift
shift
;;
-w)
WIDTH="$2"
shift
shift
;;
*.log)
if [ "$LOG_FILES" = "" ]; then
LOG_FILES="$1"
else
LOG_FILES="$1 $LOG_FILES"
fi
shift
;;
*.log.gz)
if [ "$GZ_LOG_FILES" = "" ]; then
GZ_LOG_FILES="$1"
else
GZ_LOG_FILES="$1 $GZ_LOG_FILES"
fi
shift
;;
*)
echo "Nevalidni parametr. tradelog -h pro vypis napovedy."
exit 0
esac
done
if [ "$GZ_LOG_FILES" = "" ] && [ "$LOG_FILES" = "" ]; then
READ_INPUT="$(cat)"
elif [ "$GZ_LOG_FILES" = "" ] && [ "$LOG_FILES" != "" ]; then
READ_INPUT=$(cat $LOG_FILES | sort)
elif [ "$GZ_LOG_FILES" != "" ]; then
READ_INPUT=$(gzip -d -c $GZ_LOG_FILES | cat $LOG_FILES - | sort)
fi
# print bez casu
PRINT="$(echo "$READ_INPUT" | awk -F ';' -v AFTER="$DATETIME_AFTER" -v BEFORE="$DATETIME_BEFORE" '{if ($1 > AFTER && $1 < BEFORE) {print $0}}')"
# print bez tickeru
if [ "$TICKER" != "" ]; then
PRINT="$(echo "$PRINT" | awk -F ';' -v TICKER="$TICKER" 'BEGIN{n = split(TICKER, tic)} {for(i = 1; i <= n; i++) if($2 == tic[i]) print}')"
fi
# print list ticky
if [ "$COMMAND" = "list-tick" ]; then
PRINT=$(echo "$PRINT" | awk -F ';' '{print $2}' | sort -u)
fi
# print profit
if [ "$COMMAND" = "profit" ]; then
PRINT=$(echo "$PRINT" | awk -F ';' '{if($3 == "buy"){BUY += $4 * $6} else{SELL += $4 * $6}} END{printf("%.2f\n", SELL-BUY)}')
fi
# print pos
if [ "$COMMAND" = "pos" ]; then
TMP_TICK=""
for TICK in $(echo "$PRINT" | awk -F ';' '{print $2}' | sort -u); do
if [ "$TMP_TICK" = "" ]; then
TMP_TICK="${TICK}"
else
TMP_TICK="$TMP_TICK;${TICK}"
fi
done
PRINT="$(echo "$PRINT" | awk -F ';' -v TICKERS="$TMP_TICK" 'BEGIN{n = split(TICKERS, tic)} {for(i = 1; i <= n; i++){if($2 == tic[i]){if($3 == "buy"){sum[i] += $6} else{sum[i] -= $6} last[i]=$4}}} END{max=0; for(i = 1; i <= n; i++){tmp = length(sprintf("%.2f", sum[i]*last[i])); if(max < tmp){max=tmp}}for(i = 1; i <= n; i++) printf("%-10s: %*.2f\n", tic[i], max, last[i]*sum[i])}' | sort -n -r -t ':' -k 2)"
fi
# print last-price
if [ "$COMMAND" = "last-price" ]; then
TMP_TICK=""
for TICK in $(echo "$PRINT" | awk -F ';' '{print $2}' | sort -u); do
if [ "$TMP_TICK" = "" ]; then
TMP_TICK="${TICK}"
else
TMP_TICK="$TMP_TICK;${TICK}"
fi
done
PRINT="$(echo "$PRINT" | awk -F ';' -v TICKERS="$TMP_TICK" 'BEGIN{n = split(TICKERS, tic)} {for(i = 1; i <= n; i++){if($2 == tic[i]){last[i]=$4}}} END{max=0; for(i = 1; i <= n; i++){tmp = length(last[i]); if(max < tmp){max=tmp}}for(i = 1; i <= n; i++)printf("%-10s: %*.2f\n", tic[i], max, last[i])}' )"
fi
#print hist-ord
if [ "$COMMAND" = "hist-ord" ]; then
TMP_TICK=""
for TICK in $(echo "$PRINT" | awk -F ';' '{print $2}' | sort -u); do
if [ "$TMP_TICK" = "" ]; then
TMP_TICK="${TICK}"
else
TMP_TICK="$TMP_TICK;${TICK}"
fi
done
if [ "$WIDTH" = "" ]; then
PRINT="$(echo "$PRINT" | awk -F ';' -v TICKERS="$TMP_TICK" 'BEGIN{n = split(TICKERS, tic)} {for(i = 1; i <= n; i++){if($2 == tic[i]){if($3 == "buy" || $3 == "sell"){sum[i] = sum[i] + 1}}}} END{for(i = 1; i <= n; i++){printf("%-10s: ", tic[i]);for(j = 1; j <= sum[i]; j++){printf "#"}printf "\n"}}')"
else
PRINT="$(echo "$PRINT" | awk -F ';' -v WIDTH="$WIDTH" -v TICKERS="$TMP_TICK" 'BEGIN{n = split(TICKERS, tic)} {for(i = 1; i <= n; i++){if($2 == tic[i]){if($3 == "buy" || $3 == "sell"){sum[i] = sum[i] + 1}}}} END{max=-1; for(i = 1; i <= n; i++){tmp = sum[i]; if(max < tmp){max=tmp}}for(i = 1; i <= n; i++){printf("%-10s: ", tic[i]);for(j = 1; j <= int(sum[i]/(max/WIDTH)); j++){printf "#"}printf "\n"}}')"
fi
fi
#print graph-pos
if [ "$COMMAND" = "graph-pos" ]; then
TMP_TICK=""
for TICK in $(echo "$PRINT" | awk -F ';' '{print $2}' | sort -u); do
if [ "$TMP_TICK" = "" ]; then
TMP_TICK="${TICK}"
else
TMP_TICK="$TMP_TICK;${TICK}"
fi
done
if [ "$WIDTH" = "" ]; then
PRINT="$(echo "$PRINT" | awk -F ';' -v WIDTH="$WIDTH" -v TICKERS="$TMP_TICK" 'function abs(x){if(x >=0){return x}else{return -x}} BEGIN{n = split(TICKERS, tic)} {for(i = 1; i <= n; i++){if($2 == tic[i]){if($3 == "buy"){sum[i] += $6} else{sum[i] -= $6}last[i] = $4}}} END{max=-1; for(i = 1; i <= n; i++){tmp = abs(sum[i]*last[i]); if(max < tmp){max=tmp}} for(i = 1; i <= n; i++){printf("%-10s:", tic[i]);if(abs(int(((sum[i]*last[i])/1000)))>= 1){printf " "}for(j = 1; j <= abs(int(((sum[i]*last[i])/1000))); j++){if(sum[i] > 0){printf "#"} else{printf "!"}}printf "\n"}}')"
else
PRINT="$(echo "$PRINT" | awk -F ';' -v WIDTH="$WIDTH" -v TICKERS="$TMP_TICK" 'function abs(x){if(x >=0){return x}else{return -x}} BEGIN{n = split(TICKERS, tic)} {for(i = 1; i <= n; i++){if($2 == tic[i]){if($3 == "buy"){sum[i] += $6} else{sum[i] -= $6}last[i] = $4}}} END{max=-1; for(i = 1; i <= n; i++){tmp = abs(sum[i]*last[i]); if(max < tmp){max=tmp}}for(i = 1; i <= n; i++){printf("%-10s:", tic[i]);if(abs(int(((sum[i]*last[i]))/(max/WIDTH)))>=1){printf " "} for(j = 1; j <= abs(int((sum[i]*last[i])/(max/WIDTH))); j++){if(sum[i] > 0){printf "#"} else{printf "!"}}printf "\n"}}')"
fi
fi
if [ "$PRINT" != "" ]; then
echo "$PRINT"
fi
exit 0