-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdividends.jl
136 lines (109 loc) · 3.26 KB
/
dividends.jl
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
# SG 11 Nov 2020
# analyse stock fluctuation around dividend and evaluate if profitable
using Dates
using HTTP
using JSON
using Plots
using DataFrames
using CSV: write
using StatsPlots
tickers = [
"AGS.BR",
"BPOST.BR",
"VAN.BR",
"BEKB.BR",
"ABI.BR",
"VOW.DE",
"MMM",
"F",
"DAN",
"BMW.DE",
"IFX.DE",
];
# tickers=["AGS.BR"]#,"VOW.DE","F","DAN","BMW.DE","IFX.DE"];
df = DataFrame(
stock = string(),
prediv = Float64[],
postdiv = Float64[],
dividend = Float64[],
);
seconds_before = 3600 * 24 * 7;
seconds_after = 3600 * 24 * 7;
date_start = string(convert(UInt32, datetime2unix(DateTime(2018, 01, 01))), base = 10);
# date_end=string(convert(UInt32,datetime2unix(DateTime(2020,11,10))),base=10);
date_end = string(
convert(UInt32, datetime2unix(DateTime(year(today()), month(today()), day(today())))),
base = 10,
);
for j = 1:length(tickers)
ticker = tickers[j]
url = string(
"https://query1.finance.yahoo.com/v8/finance/chart/",
ticker,
"?period1=",
date_start,
"&period2=",
date_end,
"&interval=1d&events=div",
)
r = HTTP.request("GET", url)
data = String(r.body)
data = JSON.parse(data)
data = data["chart"]["result"][1]
data_dividends = data["events"]["dividends"]
indices = findall(data_dividends.slots .== 1)
for i = 1:length(indices)
dividend = data_dividends[data_dividends.keys[indices[i]]]
# plot stock value around dividend date
idx = findall(
dividend["date"] - seconds_before .<=
data["timestamp"] .<=
dividend["date"] + seconds_after,
)
push!(
df,
(
ticker,
data["indicators"]["quote"][1]["close"][idx][1],
data["indicators"]["quote"][1]["close"][idx][end],
dividend["amount"],
),
)
idx2 = findall(.!(isnothing.(data["indicators"]["quote"][1]["close"][idx])))
plot_div = plot(
Dates.unix2datetime.(data["timestamp"][idx][idx2]),
data["indicators"]["quote"][1]["close"][idx][idx2],
label = "without dividend",
)
# add dividend to stock value
idx = findall(
dividend["date"] .<= data["timestamp"] .<= dividend["date"] + seconds_after,
)
stockval = data["indicators"]["quote"][1]["close"][idx]
idx2 = findall(.!(isnothing.(stockval))) #filter isnothing
stockval = stockval[idx2] .+ convert(Float16, dividend["amount"])
plot!(
Dates.unix2datetime.(data["timestamp"][idx][idx2]),
stockval,
label = "with dividend",
legend = :outertopleft,
)
# savefig(plot_div,string("dividend",j,i))
# display(plot_div)
end
end
df.profit = df.dividend + df.postdiv - df.prediv;
df.profit_perc = 100 * (df.profit ./ df.prediv);
write("dataframe.csv", df)
@df df scatter(
:dividend,
:profit_perc,
group = :stock,
title = "Dividend profit",
ylabel = "profit percentage",
xlabel = "dividend (euro)",
m = (0.5, [:circle :rect :diamond :cross :hex :star7], 12),
bg = RGB(0.2, 0.2, 0.2),
legend = :outertopleft
)
savefig("dividends_profit")