-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHurst.py
66 lines (53 loc) · 2.03 KB
/
Hurst.py
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
import yfinance as yf
import matplotlib.pyplot as plt
from hurst import compute_Hc
import statistics
asset = yf.download("^BVSP", start = "1995-01-01", interval = "1d", rounding = True)
assetClosing = asset["Adj Close"]
assetOpening = asset["Open"]
print("Asset return: ", str(round((assetClosing[len(assetClosing) - 1]/assetOpening[0] - 1) * 100, 2)) + "%")
print("Asset length: ", len(assetClosing))
for x in range(50, 100, 5):
periodAnalysis = 50
count = 0
high = 0
low = 0
roi = 1
buy = False
sell = False
start = periodAnalysis
end = len(assetClosing)
#Começa o loop a partir da janela móvel que se quer analisar
for i in range(start, end, 1):
auxAsset = assetClosing[i - periodAnalysis : i]
lastMean = statistics.mean(auxAsset)
currentMean = statistics.mean(assetClosing[i - periodAnalysis + 1 : i + 1])
if (i != periodAnalysis) and (i != len(assetClosing) - 1):
if buy:
roi = roi * (assetOpening[i + 1]/assetOpening[i])
buy = False
if sell:
roi = roi * ((1 - assetOpening[i + 1]/assetOpening[i]) + 1)
sell = False
H, c, data = compute_Hc(auxAsset, kind = 'random_walk', min_window = 5, max_window = periodAnalysis)
if H > (x / 100):
count += 1
if(currentMean - lastMean) > 0:
high += 1
buy = True
if(currentMean - lastMean) < 0:
low += 1
#sell = True
if (high == 0) and (low == 0):
break
print("\nH = " + str(x / 100))
print("H > " + str(x / 100) + ": " + str(count))
print("Percentage: " + str(round(count / len(assetClosing) * 100, 2)) + "%")
print("High: ", high)
print("Low: ", low)
print("Strategy return: " + str(round((roi - 1) * 100, 2)) + "%")
'''
Possibilidades a fazer:
- Considerar a cotação do ativo para verificar quantidades que podem ser compradas com o capital disponível
- Considerar custos operacionais
'''