-
Notifications
You must be signed in to change notification settings - Fork 1
/
risk_analysis.py
78 lines (68 loc) · 2.67 KB
/
risk_analysis.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
67
68
69
70
71
72
73
74
75
76
77
78
import yfinance as yf
from datetime import date, timedelta
import sys
import math
import random
import json
def risk_analysis(h, d, t, p):
# Get stock data from Yahoo Finance
today = date.today()
timePast = today - timedelta(days=h)
data = yf.download("NVDA", start=timePast, end=today)
results = {"var95": [], "var99": [], "profit_loss": []}
# Convert the data to a list of lists
data_list = [list(row) for row in data.values]
daily_returns = [
(data_list[i][3] - data_list[i - 1][3]) / data_list[i - 1][3]
for i in range(1, len(data_list))
]
# Perform the risk analysis for each signal
for i in range(2, len(data_list)):
body = 0.01
# Three Soldiers
if (
(data_list[i][3] - data_list[i][0]) >= body
and data_list[i][3] > data_list[i - 1][3]
and (data_list[i - 1][3] - data_list[i - 1][0]) >= body
and data_list[i - 1][3] > data_list[i - 2][3]
and (data_list[i - 2][3] - data_list[i - 2][0]) >= body
):
signal = 1
# Three Crows
elif (
(data_list[i][0] - data_list[i][3]) >= body
and data_list[i][3] < data_list[i - 1][3]
and (data_list[i - 1][0] - data_list[i - 1][3]) >= body
and data_list[i - 1][3] < data_list[i - 2][3]
and (data_list[i - 2][0] - data_list[i - 2][3]) >= body
):
signal = -1
else:
signal = 0
if (signal == 1 and t == "buy") or (signal == -1 and t == "sell"):
# Generate d simulated returns
pct_change = [daily_returns[i - h : i]][0]
mean = sum(pct_change) / h
std = math.sqrt(sum([(x - mean) ** 2 for x in pct_change]) / h)
simulated = [random.gauss(mean, std) for _ in range(d)]
# Calculate the 95% and 99% VaR
simulated.sort(reverse=True)
var95 = simulated[int(len(simulated) * 0.95)]
var99 = simulated[int(len(simulated) * 0.99)]
results["var95"].append(var95)
results["var99"].append(var99)
# Calculate the profit or loss
if i + p < len(data_list):
if t == "buy":
profit_loss = data_list[i + p][3] - data_list[i][3]
elif t == "sell":
profit_loss = data_list[i][3] - data_list[i + p][3]
results["profit_loss"].append(profit_loss)
with open("/home/ec2-user/data.json", "w") as f:
json.dump(results, f)
if __name__ == "__main__":
h = int(sys.argv[1])
d = int(sys.argv[2])
t = sys.argv[3]
p = int(sys.argv[4])
risk_analysis(h, d, t, p)