-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcripto_update.Rmd
434 lines (353 loc) · 11.1 KB
/
cripto_update.Rmd
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
---
title: "Crypto News!"
runningheader: "Una Newsletter sull'andamento delle crypto" # only for pdf output
fubtitle: "Una Newsletter sull'andamento delle crypto" # only for html output
author: "Enrico Pirani Max"
fate: "`r Sys.Date()`"
output:
tufte::tufte_html: default
tufte::tufte_handout:
citation_package: natbib
latex_engine: xelatex
tufte::tufte_book:
citation_package: natbib
latex_engine: xelatex
fibliography: skeleton.bib
link-citations: yes
editor_options:
markdown:
wrap: 72
---
## Importazione delle librerie necessarie
In primo luogo, per eseguire il nostro processo di creazione di
strategie di trading, dobbiamo importare le librerie necessarie nel
nostro ambiente. In tutto questo processo, utilizzeremo alcune delle
librerie finanziarie più popolari in R, ovvero Quantmod, TTR e
Performance Analytics. Utilizzando la funzione library in R, possiamo
importare i nostri pacchetti richiesti.
```{r}
library(quantmod)
library(PerformanceAnalytics)
library(TTR)
```
## Passaggio 2: Estrazione dei dati da Yahoo e Plotting di base
durante tutto il nostro processo, lavoreremo con i dati del prezzo delle
cryptovalute Bitcoin, Ethereum, Binance, Cardano e XRP. Estraiamo i dati
di queste valute da Yahoo in R.
```{r}
getSymbols("BTC-USD", src = "yahoo", from = "2019-01-01")
getSymbols("ETH-USD", src = "yahoo", from = "2019-01-01")
getSymbols("BNB-USD", src = "yahoo", from = "2019-01-01")
getSymbols("ADA-USD", src = "yahoo", from = "2019-01-01")
getSymbols("XRP-USD", src = "yahoo", from = "2019-01-01")
getSymbols("SOL-USD", src = "yahoo", from = "2020-01-01")
```
Ora facciamo un po' di visualizzazione dei nostri dati estratti! Il
seguente codice produce un grafico a barre finanziario dei prezzi delle
azioni insieme al volume.
```{r}
barChart(`BTC-USD`, theme = chartTheme("black"))
barChart(`BNB-USD`, theme = chartTheme("black"))
barChart(`ETH-USD`, theme = chartTheme("black"))
barChart(`ADA-USD`, theme = chartTheme("black"))
barChart(`XRP-USD`, theme = chartTheme("black"))
barChart(`SOL-USD`, theme = chartTheme("black"))
```
## Creazione di indicatori tecnici
Ci sono molti indicatori tecnici utilizzati per l'analisi finanziaria
ma, per la nostra analisi, utilizzeremo e creeremo sei dei più famosi
indicatori tecnici, ovvero: Media mobile semplice (SMA), Parabolic Stop
And Reverse (SAR), Indice del canale delle materie prime (CCI), Tasso di
variazione (ROC), Indice del momento stocastico (SMI) e infine Williams
%R. Facciamolo!. Nella nuova dimensione di questi
### Media mobile semplice (SMA):
L'intervallo di tempo standard che prenderemo è di 20 giorni SMA e 50
giorni SMA. Ma non ci sono restrizioni nell'uso di qualsiasi intervallo
di tempo.
Il seguente codice calcolerà la SMA di tre aziende per 20 giorni e 50
giorni insieme ad un grafico:
```{r}
BTC <- `BTC-USD`
ETH <- `ETH-USD`
BNB <- `BNB-USD`
ADA <- `ADA-USD`
XRP <- `XRP-USD`
SOL <- `SOL-USD`
```
```{r}
# Replace NAs with the mean of the column
BTC$`BTC-USD.Close`[is.na(BTC$`BTC-USD.Close`)] <- mean(BTC$`BTC-USD.Close`, na.rm = TRUE)
# Calculate moving averages
sma50_btc <- SMA(BTC$`BTC-USD.Close`, n = 50)
sma100_btc <- SMA(BTC$`BTC-USD.Close`, n = 100)
# Plot the data
lineChart(`BTC-USD`, theme = chartTheme("black"))
addSMA(n = 50, col = "blue")
addSMA(n = 100, col = "orange")
# Add legend
legend("left",
col = c("green", "blue", "orange"),
legend = c("BTC-USD", "SMA50", "SMA100"), lty = 1, bty = "n",
text.col = "white", cex = 0.8
)
```
```{r}
# 2. ETH-USD
ETH$`ETH-USD.Close`[is.na(ETH$`-USD.Close`)] <- mean(ETH$`ETH-USD.Close`, na.rm = TRUE)
sma50_btc <- SMA(ETH$`ETH-USD.Close`, n = 50)
sma100_btc <- SMA(ETH$`ETH-USD.Close`, n = 100)
lineChart(`ETH-USD`, theme = chartTheme("black"))
addSMA(n = 50, col = "blue")
addSMA(n = 100, col = "orange")
legend("left",
col = c("green", "blue", "orange"),
legend = c("ETH-USD", "SMA50", "SMA100"), lty = 1, bty = "n",
text.col = "white", cex = 0.8
)
```
```{r}
sma50_btc <- SMA(ADA$`ADA-USD.Close`, n = 50)
sma100_btc <- SMA(ADA$`ADA-USD.Close`, n = 100)
lineChart(`ADA-USD`, theme = chartTheme("black"))
addSMA(n = 50, col = "blue")
addSMA(n = 100, col = "orange")
legend("left",
col = c("green", "blue", "orange"),
legend = c("ADA-USD", "SMA50", "SMA100"), lty = 1, bty = "n",
text.col = "white", cex = 0.8
)
```
```{r}
sma50_btc <- SMA(BNB$`BNB-USD.Close`, n = 50)
sma100_btc <- SMA(BNB$`BNB-USD.Close`, n = 100)
lineChart(`BNB-USD`, theme = chartTheme("white"))
addSMA(n = 50, col = "blue")
addSMA(n = 100, col = "orange")
legend("left",
col = c("green", "blue", "orange"),
legend = c("BNB-USD", "SMA50", "SMA100"), lty = 1, bty = "n",
text.col = "white", cex = 0.8
)
```
```{r}
sma50_btc <- SMA(XRP$`XRP-USD.Close`, n = 50)
sma100_btc <- SMA(XRP$`XRP-USD.Close`, n = 100)
lineChart(`XRP-USD`, theme = chartTheme("black"))
addSMA(n = 50, col = "blue")
addSMA(n = 100, col = "orange")
legend("left",
col = c("green", "blue", "orange"),
legend = c("BNB-USD", "SMA50", "SMA100"), lty = 1, bty = "n",
text.col = "white", cex = 0.8
)
```
```{r}
sma50_btc <- SMA(SOL$`SOL-USD.Close`, n = 50)
sma100_btc <- SMA(SOL$`SOL-USD.Close`, n = 100)
lineChart(`SOL-USD`, theme = chartTheme("white"))
addSMA(n = 50, col = "blue")
addSMA(n = 100, col = "orange")
legend("left",
col = c("green", "blue", "orange"),
legend = c("SOL-USD", "SMA50", "SMA100"), lty = 1, bty = "n",
text.col = "white", cex = 0.8
)
```
### Indice del Canale delle Materie Prime (CCI):
Per calcolare il CCI, dobbiamo considerare i prezzi giornalieri di
massimo, minimo e chiusura delle aziende insieme a un periodo di tempo
specificato e un valore costante. In questo passaggio, prenderemo 20
giorni come periodo di tempo e 0,015 come valore costante.
Il seguente codice calcolerà il CCI delle aziende insieme a un grafico:
```{r}
# 1.BTC
cci_BTC <- CCI(HLC(BTC), n = 20, c = 0.015)
barChart(BTC, theme = "black")
addCCI(n = 20, c = 0.015)
# 2.ETH
cci_eth <- CCI(HLC(ETH), n = 20, c = 0.015)
barChart(ETH, theme = "black")
addCCI(n = 20, c = 0.015)
# 3.BNB
cci_BNB <- CCI(HLC(BNB), n = 20, c = 0.015)
barChart(BNB, theme = "black")
addCCI(n = 20, c = 0.015)
# 4. XRP
cci_XRP <- CCI(HLC(XRP), n = 20, c = 0.015)
barChart(XRP, theme = "black")
addCCI(n = 20, c = 0.015)
# 5.ADA
cci_ADA <- CCI(HLC(ADA), n = 20, c = 0.015)
barChart(ADA, theme = "black")
addCCI(n = 20, c = 0.015)
```
### Tasso di variazione (ROC)
Per calcolare il ROC, dobbiamo considerare un intervallo di tempo
specificato e non ci sono restrizioni nell'utilizzare qualsiasi periodo.
In questo passaggio, prenderemo 25 giorni come periodo di tempo.
Il codice seguente calcolerà il ROC delle aziende insieme a un grafico:
```{r}
# 1. BTC
roc_btc <- ROC(BTC$`BTC-USD.Close`, n = 25)
barChart(BTC, theme = "black")
addROC(n = 25)
legend("left",
col = "red", legend = "ROC(25)", lty = 1, bty = "n",
text.col = "white", cex = 0.8
)
roc_ETH <- ROC(ETH$`ETH-USD.Close`, n = 25)
barChart(ETH, theme = "black")
addROC(n = 25)
legend("left",
col = "red", legend = "ROC(25)", lty = 1, bty = "n",
text.col = "white", cex = 0.8
)
roc_BNB <- ROC(BNB$`BNB-USD.Close`, n = 25)
barChart(BNB, theme = "black")
addROC(n = 25)
legend("left",
col = "red", legend = "ROC(25)", lty = 1, bty = "n",
text.col = "white", cex = 0.8
)
roc_XRP <- ROC(XRP$`XRP-USD.Close`, n = 25)
barChart(XRP, theme = "black")
addROC(n = 25)
legend("left",
col = "red", legend = "ROC(25)", lty = 1, bty = "n",
text.col = "white", cex = 0.8
)
roc_ADA <- ROC(ADA$`ADA-USD.Close`, n = 25)
barChart(ADA, theme = "black")
addROC(n = 25)
legend("left",
col = "red", legend = "ROC(25)", lty = 1, bty = "n",
text.col = "white", cex = 0.8
)
```
```{r}
# Set the number of successes (3 heads)
x <- 3
# Set the number of trials (5 coin flips)
size <- 5
# Set the probability of success (0.5 for a fair coin)
prob <- 0.5
# Calculate the probability using the dbinom function
probability <- dbinom(x, size, prob)
# Print the result
print(probability)
```
# Generate random data following a normal Distribution
```{r}
data <- rnorm(1000, mean = 0, sd = 1)
# Plot a histogram of the data
hist(data, breaks = 30, col = "skyblue", main = "Normal Distribution", xlab = "Values", ylab = "Frequency")
# Add a curve representing the probability density function of the normal distribution
curve(dnorm(x, mean = mean(data), sd = sd(data)), add = TRUE, col = "red")
```
In this example, we first generate a sample of random data following a
normal distribution using the rnorm() function. We then create a
histogram plot of the data using the hist() function, which visualizes
the distribution of the data.
To overlay a curve representing the probability density function of the
normal distribution on the histogram, we use the curve() function with
the dnorm() function to calculate the probability density at each point.
This simple R Markdown code provides a visual and practical
demonstration of the normal distribution, allowing readers to understand
and appreciate this fundamental concept in statistics.
```{r}
library(cryptoQuotes)
SOL <- cryptoQuotes::get_quote(
ticker ="SOLUSDT",
source = "binance",
futures = FALSE,
interval = "30m",
from = "2024-03-19"
)
```
```{r}
## Chart BTC
## using klines, SMA,
## MACD and Bollinger Bands
cryptoQuotes::chart(
ticker = SOL,
main = cryptoQuotes::kline(),
sub = list(
cryptoQuotes::volume(),
cryptoQuotes::macd()
),
indicator = list(
cryptoQuotes::sma(n = 7),
cryptoQuotes::sma(n = 14),
cryptoQuotes::sma(n = 21),
cryptoQuotes::bollinger_bands()
# cryptoQuotes::rsi(n=14, maType = "SMA", upper_limit = 80,lower_limit = 20)
)
)
```
```{r}
ETH <- cryptoQuotes::get_quote(
ticker ="ETHEUR",
source = "binance",
futures = FALSE,
interval = "1w",
from = "2020-01-01",
to = Sys.Date()
)
```
```{r}
## Chart BTC
## using klines, SMA,
## MACD and Bollinger Bands
cryptoQuotes::chart(
ticker = ETH,
main = cryptoQuotes::kline(),
sub = list(
cryptoQuotes::volume(),
cryptoQuotes::macd()
),
indicator = list(
cryptoQuotes::sma(n = 7),
cryptoQuotes::sma(n = 14),
cryptoQuotes::sma(n = 21),
cryptoQuotes::bollinger_bands()
# cryptoQuotes::rsi(n=14, maType = "SMA", upper_limit = 80,lower_limit = 20)
)
)
```
Ora provo inserire **linee di codice `Python`**
```{python}
print ("Helo World")
```
ora inserisco codice R
```{r}
# Install and load the rgl package
library(rgl)
# Create some sample data
x <- seq(-pi, pi, length.out = 100)
y <- sin(x)
z <- cos(x)
# Create a 3D plot
plot3d(x, y, z, type = "l", col = "blue")
```
```{r}
# Define the function to integrate
f <- function(x) {
return(x^2) # example function x^2
}
# Integrate the function from 0 to 1
integral <- integrate(f, lower = 0, upper = 1)
# Plot the function and the integral
curve(f, from = 0, to = 1, col = "blue", xlab = "x", ylab = "f(x)", main = "Plot of a Function and its Integral")
abline(h = 0, col = "gray") # add x-axis
segments(0, 0, 1, 0, col = "gray") # add line for integral
text(0.5, 0.5, paste("Integral =", round(integral$value, 2)), col = "red") # add text with integral value
```
```{r}
library(reticulate)
use_python("/usr/local/bin/python3.12", required = T)
```
```{python}
py_install(matplotlib)
import numpy as np
import matplotlib.pyplot as plt
```