修订版 | ef67ffd09338425732a8a3ac7ba472ecf6379ce4 (tree) |
---|---|
时间 | 2024-01-18 04:34:42 |
作者 | Lorenzo Isella <lorenzo.isella@gmai...> |
Commiter | Lorenzo Isella |
These are new codes to process the transactions.
@@ -0,0 +1,38 @@ | ||
1 | +rm(list=ls()) | |
2 | + | |
3 | +library(tidyverse) | |
4 | +library(binancer) | |
5 | +library(janitor) | |
6 | +library(unnest) | |
7 | +library(openxlsx) | |
8 | + | |
9 | + | |
10 | +source("/home/lorenzo/myprojects-hg/R-codes/stat_lib.R") | |
11 | + | |
12 | + | |
13 | +coin_prices_usdt <- binance_coins_prices(unit = "USDT")|> | |
14 | + as_tibble()|> | |
15 | + arrange(desc(usd)) |> | |
16 | + rename("price_usdt"="usd") |> | |
17 | + add_row(symbol="USDT", price_usdt=1.) | |
18 | + | |
19 | +my_initial_prices <- read_excel("./list-transactions/portfolio_to_fix.xlsx")|> | |
20 | + rename("initial_price_usdt"="average_price_usdt") | |
21 | + | |
22 | + | |
23 | + | |
24 | +portfolio_gains_losses <- my_initial_prices |> | |
25 | + left_join(y=coin_prices_usdt, by=c("coin"="symbol")) |> | |
26 | + mutate(variation=(price_usdt-initial_price_usdt)/initial_price_usdt) | |
27 | + | |
28 | +portfolio_output <- portfolio_gains_losses |> | |
29 | + select(coin, initial_price_usdt, price_usdt, variation) |> | |
30 | + mutate(percentage_variation=paste(round(variation*100, 1), "%", sep="")) |> | |
31 | + arrange(desc(variation)) | |
32 | + | |
33 | + | |
34 | +save_excel(portfolio_output, "portfolio_gain_losses.xlsx") | |
35 | + | |
36 | + | |
37 | + | |
38 | +print("So far so good") |
@@ -0,0 +1,456 @@ | ||
1 | +rm(list=ls()) | |
2 | + | |
3 | +library(tidyverse) | |
4 | +library(janitor) | |
5 | +library(openxlsx) | |
6 | +library(stringr) | |
7 | +library(stringi) | |
8 | +library(openxlsx) | |
9 | +library(cryptoQuotes) | |
10 | +library(broom) | |
11 | + | |
12 | +source("/home/lorenzo/myprojects-hg/R-codes/stat_lib.R") | |
13 | + | |
14 | + | |
15 | + | |
16 | +df_ini <- read_all_csv() | |
17 | + | |
18 | +df <- df_ini |> | |
19 | + clean_data() | |
20 | + | |
21 | +save_excel(df, "transaction_history.xlsx") | |
22 | + | |
23 | + | |
24 | +total_deposit <- df|>filter(operation=="Fiat Deposit")|>pull(change)|>sum() | |
25 | + | |
26 | +##two way to achieve the same | |
27 | + | |
28 | +## beginnings <- df |> | |
29 | +## arrange(coin, utc_time) |> | |
30 | +## group_by(coin) |> | |
31 | +## slice(1) |> | |
32 | +## ungroup() | |
33 | + | |
34 | + | |
35 | +beginnings <- df |> | |
36 | + arrange(coin, utc_time) |> | |
37 | + group_by(coin) |> | |
38 | + filter(row_number()==1) |> | |
39 | + ungroup() | |
40 | + | |
41 | +save_excel(beginnings, "first_transaction_per_coin.xlsx") | |
42 | + | |
43 | + | |
44 | +## see what happened with ADA | |
45 | + | |
46 | +test <- df |> | |
47 | + find_text_filter("ADA") |> | |
48 | + arrange(desc(change)) | |
49 | + | |
50 | +ada_all <- df |> | |
51 | + find_text_filter("ADA") | |
52 | + | |
53 | +ada <- test |> | |
54 | + slice(1:2) | |
55 | + | |
56 | +test <- df |> | |
57 | + find_text_filter("TIA") |> | |
58 | + arrange(desc(change)) | |
59 | + | |
60 | +tia <- test |> | |
61 | + slice(1:2) | |
62 | + | |
63 | +test <- df |> | |
64 | + find_text_filter("DOT") |> | |
65 | + arrange(desc(change)) | |
66 | + | |
67 | +dot <- test |> | |
68 | + slice(1:3) | |
69 | + | |
70 | +## test <- df |> | |
71 | +## find_text_filter("ADA") |> | |
72 | +## arrange(desc(change)) | |
73 | + | |
74 | +## ada <- test |> | |
75 | +## slice(1:2) | |
76 | + | |
77 | +test <- df |> | |
78 | + find_text_filter("BTC") |> | |
79 | + arrange(desc(change)) | |
80 | + | |
81 | +btc <- test |> | |
82 | + slice(c(7,8,9)) ## to fix | |
83 | + | |
84 | + | |
85 | +test <- df |> | |
86 | + find_text_filter("KAVA") |> | |
87 | + arrange(desc(change)) | |
88 | + | |
89 | + | |
90 | +kava <- test |> | |
91 | + slice(1:25) ## to fix | |
92 | + | |
93 | + | |
94 | +test <- df |> | |
95 | + find_text_filter("MINA") |> | |
96 | + arrange(desc(change)) | |
97 | + | |
98 | +mina <- test |> | |
99 | + slice(1) | |
100 | + | |
101 | + | |
102 | +test <- df |> | |
103 | + find_text_filter("BSW") |> | |
104 | + arrange(desc(change)) | |
105 | + | |
106 | +bsw <- test |> | |
107 | + slice(1:3) | |
108 | + | |
109 | +test <- df |> | |
110 | + find_text_filter("ETH") |> | |
111 | + arrange(desc(change)) | |
112 | + | |
113 | +eth <- test |> | |
114 | + slice(1) | |
115 | + | |
116 | + | |
117 | +test <- df |> | |
118 | + find_text_filter("CTSI") |> | |
119 | + arrange(desc(change)) | |
120 | + | |
121 | +ctsi <- test |> | |
122 | + slice(1:2) | |
123 | + | |
124 | + | |
125 | + | |
126 | +test <- df |> | |
127 | + find_text_filter("ACH") |> | |
128 | + arrange(desc(change)) | |
129 | + | |
130 | +ach_all <- df |> | |
131 | + find_text_filter("ACH") | |
132 | + | |
133 | +ach_agg <- ach_all |> | |
134 | + group_by(operation, account) |> | |
135 | + summarise(total_change=sum(change), | |
136 | + number_transactions=n()) |> | |
137 | + ungroup() | |
138 | + | |
139 | +ach <- ach_all |> | |
140 | + filter(operation=="Simple Earn Locked Subscription") |> | |
141 | + mutate(change=abs(change)) | |
142 | + | |
143 | +## ach <- test |> | |
144 | +## slice(c(1,2,4,5,7)) | |
145 | + | |
146 | + | |
147 | + | |
148 | + | |
149 | +test <- df |> | |
150 | + find_text_filter("MATIC") |> | |
151 | + arrange(desc(change)) | |
152 | + | |
153 | + | |
154 | +matic <- test |> | |
155 | + slice(1:2) | |
156 | + | |
157 | + | |
158 | + | |
159 | + | |
160 | +test <- df |> | |
161 | + find_text_filter("ARB") |> | |
162 | + arrange(desc(change)) | |
163 | + | |
164 | +arb <- test |> | |
165 | + slice(1) | |
166 | + | |
167 | +test <- df |> | |
168 | + find_text_filter("AVAX") |> | |
169 | + arrange(desc(change)) | |
170 | + | |
171 | +avax <- test |> | |
172 | + slice(1) | |
173 | + | |
174 | + | |
175 | + | |
176 | +test <- df |> | |
177 | + find_text_filter("AAVE") |> | |
178 | + arrange(desc(change)) | |
179 | + | |
180 | + | |
181 | +aave <- test |> | |
182 | + slice(1) | |
183 | + | |
184 | + | |
185 | + | |
186 | +test <- df |> | |
187 | + find_text_filter("AXS") |> | |
188 | + arrange(desc(change)) | |
189 | + | |
190 | +axs <- test |> | |
191 | + slice(1:3) | |
192 | + | |
193 | + | |
194 | + | |
195 | +test <- df |> | |
196 | + find_text_filter("NEAR") |> | |
197 | + arrange(desc(change)) | |
198 | + | |
199 | + | |
200 | +near <- test |> | |
201 | + slice(c(1,2,5)) | |
202 | + | |
203 | + | |
204 | +test <- df |> | |
205 | + find_text_filter("ATOM") |> | |
206 | + arrange(desc(change)) | |
207 | + | |
208 | +atom <- test |> | |
209 | + slice(1) | |
210 | + | |
211 | +test <- df |> | |
212 | + find_text_filter("SKL") |> | |
213 | + arrange(desc(change)) | |
214 | + | |
215 | +skl <-test |> | |
216 | + slice(5) | |
217 | + | |
218 | +test <- df |> | |
219 | + find_text_filter("LDO") |> | |
220 | + arrange(desc(change)) | |
221 | + | |
222 | +ldo <- test |> | |
223 | + slice(1) | |
224 | + | |
225 | +test <- df |> | |
226 | + find_text_filter("OM") |> | |
227 | + arrange(desc(change)) | |
228 | + | |
229 | +om <- test |> | |
230 | + slice(1) | |
231 | + | |
232 | +om$change <- om$change-test$change[2] | |
233 | + | |
234 | + | |
235 | +test <- df |> | |
236 | + find_text_filter("ALGO") |> | |
237 | + arrange(desc(change)) | |
238 | + | |
239 | +algo <- test |> | |
240 | + filter(change== -200) |> | |
241 | + mutate(change=-change) | |
242 | + | |
243 | +test <- df |> | |
244 | + find_text_filter("FLOKI") |> | |
245 | + arrange(desc(change)) | |
246 | + | |
247 | + | |
248 | +floki_all <- df |> | |
249 | + find_text_filter("FLOKI") | |
250 | + | |
251 | +save_excel(floki_all, "floki_transactions.xlsx") | |
252 | + | |
253 | +floki <- test |> | |
254 | + slice(1:6) | |
255 | + | |
256 | + | |
257 | +test <- df |> | |
258 | + find_text_filter("CAKE") |> | |
259 | + filter(change>=.4) |> | |
260 | + distinct(change, .keep_all=T) |> | |
261 | + arrange(utc_time) | |
262 | + | |
263 | + | |
264 | + | |
265 | + | |
266 | +cake <- test |> | |
267 | + slice(1:8) | |
268 | + | |
269 | + | |
270 | +test <- df |> | |
271 | + find_text_filter("LINK") |> | |
272 | + arrange(desc(change)) | |
273 | + | |
274 | +link <- test |> | |
275 | + slice(1) | |
276 | + | |
277 | +test <- df |> | |
278 | + find_text_filter("APE") |> | |
279 | + filter(change>=0.4) | |
280 | + | |
281 | +ape <- test |> | |
282 | + slice(1:5) | |
283 | + | |
284 | + | |
285 | +sol_all <- df |> | |
286 | + find_text_filter("SOL") |> | |
287 | + arrange(desc(change)) | |
288 | + | |
289 | +sol_agg <- sol_all |> | |
290 | + group_by(operation, account) |> | |
291 | + summarise(total_change=sum(change), | |
292 | + number_transactions=n()) |> | |
293 | + ungroup() | |
294 | + | |
295 | +sol <- sol_all |> | |
296 | + filter(operation %in% c("Simple Earn Flexible Redemption", | |
297 | + "Simple Earn Locked Rewards", | |
298 | + "Staking Rewards")) | |
299 | + | |
300 | +test <- df |> | |
301 | + find_text_filter("ONE") |> | |
302 | + arrange(desc(change)) | |
303 | + | |
304 | +one_all <- df |> | |
305 | + find_text_filter("ONE") | |
306 | + | |
307 | + | |
308 | +one <- test |> | |
309 | + slice(1) | |
310 | + | |
311 | + | |
312 | +test <- df |> | |
313 | + find_text_filter("XRP") |> | |
314 | + arrange(desc(change)) | |
315 | + | |
316 | +xrp <- test |> | |
317 | + slice(1:2) | |
318 | + | |
319 | + | |
320 | +test <- df |> | |
321 | + find_text_filter("DODO") |> | |
322 | + arrange(desc(change)) | |
323 | + | |
324 | +dodo <- test |> | |
325 | + slice(1) | |
326 | + | |
327 | + | |
328 | + | |
329 | +test <- df |> | |
330 | + find_text_filter("GLMR") |> | |
331 | + arrange(desc(change)) | |
332 | + | |
333 | +glmr <- test |> | |
334 | + slice(1) | |
335 | + | |
336 | + | |
337 | +test <- df |> | |
338 | + find_text_filter("SOL") |> | |
339 | + arrange(desc(abs(change))) | |
340 | + | |
341 | + | |
342 | +df.out <- bind_rows(ada, tia, dot, btc, kava, mina, | |
343 | + bsw, eth, ctsi, sol, | |
344 | + matic, | |
345 | + arb, avax, aave, axs, near, atom, skl, | |
346 | + ldo, om, floki, link, one, xrp, dodo, glmr, | |
347 | + ach | |
348 | + ) |> | |
349 | + mutate(coin_date=as.character(utc_time)) |> | |
350 | + mutate(coin_date=substrLeft(coin_date, 10)) ## |> | |
351 | + ## mutate(from=date, | |
352 | + ## to=date) |> | |
353 | + ## mutate(pair=paste(coin, "USDT", sep="")) | |
354 | + | |
355 | +save_excel(df.out,"ini_portfolio.xlsx") | |
356 | + | |
357 | + | |
358 | +df_sel <- df.out |> | |
359 | + group_by(coin, coin_date) |> | |
360 | + summarise(total_change=sum(change)) |> | |
361 | + ungroup() |> | |
362 | + mutate(pair=paste(coin, "USDT", sep="")) | |
363 | + | |
364 | +coin_list <- df_sel |> | |
365 | + group_by(coin, coin_date) |> | |
366 | + group_split() | |
367 | + | |
368 | + ## select(coin, from, to) |> | |
369 | + ## group_by(coin, from, to) |> | |
370 | + ## group_split() | |
371 | + | |
372 | + | |
373 | +############################################################### | |
374 | + | |
375 | + | |
376 | +## coins <- df.out |> | |
377 | +## pull(coin) |> | |
378 | +## su() | |
379 | + | |
380 | +## coin_pairs <- paste(coins,"USDT", sep="") | |
381 | + | |
382 | +price_fun <- function(x){ | |
383 | + | |
384 | + res <- cryptoQuotes::getQuote( | |
385 | + ticker = x$pair, | |
386 | + source = "binance", | |
387 | + futures = FALSE, | |
388 | + interval = "1d", | |
389 | + from = x$coin_date, | |
390 | + to = x$coin_date) |> | |
391 | + tidy() |> | |
392 | + rename("date"="index") |> | |
393 | + filter(series=="Close") |> | |
394 | + mutate(coin=x$coin, | |
395 | + pair=x$pair, | |
396 | + amount=x$total_change) |> | |
397 | + select(coin, pair,date, amount, series,value) |> | |
398 | + rename("exchange_rate_to_usdt"="value") | |
399 | + | |
400 | + return(res) | |
401 | + | |
402 | +} | |
403 | + | |
404 | + | |
405 | + | |
406 | +prices <- map(coin_list, price_fun) | |
407 | + | |
408 | +df_prices <- prices |> | |
409 | + list_to_df() |> | |
410 | + mutate(total_value_in_usdt=amount*exchange_rate_to_usdt) | |
411 | + | |
412 | +df_prices_agg <- df_prices |> | |
413 | + group_by(coin, pair) |> | |
414 | + summarise(value_usdt_when_purchased=sum(total_value_in_usdt), | |
415 | + total_amount=sum(amount)) |> | |
416 | + ungroup() |> | |
417 | + mutate(average_price_usdt=value_usdt_when_purchased/total_amount) | |
418 | + | |
419 | +save_excel(df_prices_agg, "portfolio_to_fix.xlsx") | |
420 | + | |
421 | +############################################################################# | |
422 | +############################################################################# | |
423 | +############################################################################# | |
424 | +############################################################################# | |
425 | + | |
426 | +coin_set <- sol_all | |
427 | + | |
428 | + | |
429 | +coin_deposit <- coin_set |> | |
430 | + filter(operation=="Deposit") | |
431 | + | |
432 | +coin_conversion <- coin_set |> | |
433 | + filter(operation== "Binance Convert" ) | |
434 | + | |
435 | +coin_staking_rewards <- coin_set |> | |
436 | + filter(operation == "Staking Rewards") | |
437 | + | |
438 | +coin_to_bnb <- coin_set |> | |
439 | + filter(operation == "Small Assets Exchange BNB" ) |> | |
440 | + mutate(change=-abs(change)) | |
441 | + | |
442 | +coin_simple_rewards <- coin_set |> | |
443 | + filter(operation == "Simple Earn Locked Rewards") | |
444 | + | |
445 | +coin_locked_redemption <- coin_set |> | |
446 | + filter(operation == "Simple Earn Locked Redemption") | |
447 | + | |
448 | + | |
449 | + | |
450 | + | |
451 | + | |
452 | +my_coin_hist <- bind_rows(coin_deposit, coin_conversion, coin_staking_rewards, | |
453 | + coin_to_bnb, coin_simple_rewards) | |
454 | + | |
455 | + | |
456 | +print("So far so good") |