-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
69 lines (45 loc) · 1.56 KB
/
main.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
import pandas as pd
from scipy.stats import norm
import seaborn as sns
import MySQLdb as mdb
import matplotlib.pyplot as plt
import time
scriptStart = time.time()
sym = ["S&P500"]
#connect to MySQL
def connect_DB():
db_host = '127.0.0.1'
db_user = 'root'
db_pass = '8h^=GP655@740u9'
db_name = 'lean'
con = mdb.connect(db_host, db_user, db_pass, db_name)
return con
#disconnect from database
def disconnect(con):
# disconnect from server
con.close()
#get data
def req_sql(sym, con):
# Select all of the historic close data
sql = """SELECT DATE_TIME, CLOSE FROM `""" + sym + """` WHERE PERIOD = 1440 ORDER BY DATE_TIME ASC;"""
#create a pandas dataframe
df = pd.read_sql_query(sql, con=con, index_col='DATE_TIME')
return df
#using THE MULTIVARIATE NORMAL VARIANCE–COVARIANCE APPROACH
def VaR(value, confidence_level, returns_mean, returns_volatility):
alpha = norm.ppf(1-confidence_level, mean, volatility)
return value - value*(alpha + 1)
if __name__ == "__main__":
con = connect_DB()
returns = req_sql(sym[0], con).pct_change()
investment_value = 1000
confidence_level = 0.99
mean = pd.rolling_mean(returns, window=252)
volatility = pd.rolling_std(returns, window=252)
var = VaR(investment_value, confidence_level, mean, volatility)
var_to_df = pd.DataFrame(var, index=returns.index, columns=["VaR"])
plt.plot(var_to_df)
plt.show()
disconnect(con)
timeused = (time.time()-scriptStart)/60
print("Done in ",timeused, " minutes")