-
Notifications
You must be signed in to change notification settings - Fork 4
/
examples.py
63 lines (54 loc) · 1.94 KB
/
examples.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
from datetime import datetime
from finml.data_reader import GetInitData, StockMarket
from finml.portfolio_selection.single_factor import lowVol
from finml.portfolio_optimization import SimpleMeanVariance
# >>> Korean market
# Get initial data (For the first time, this will takes about an hour)
kor_market = GetInitData(source='krx', data_path = 'data')
kor_market.get_tickers() # Get a list of stock tickers
kor_market.get_prices(start=datetime(2010,1,1), # Get price information (for all stocks)
end=datetime.now())
kor_market.get_fs() # Get financial statements (for all stocks)
kor_market.fs_cleansing(standard='005930')
kor_market.calculate_indicators() # Calculate investment indicators
# Portfolio selection
stock_list = lowVol(
market=kor_market,
last_nyears=1,
num_pf=30,
interval='d')
start, end = kor_market.convert_to_date(last_nyears=1)
mean, cov = kor_market.get_mean_cov(
interval='d',
start=start,
end=end,
subset=stock_list)
# Portfolio optimization
SMV = SimpleMeanVariance(mean, cov)
SMV.plot(show=True, save_path='simple_mean_variance.jpeg')
# CAPM
from finml.asset_pricing import CAPM
returns, betas, alphas = CAPM(
market = kor_market,
stock_tickers = stock_list,
index_ticker = 'KPI200',
risk_free = 0,
start = start,
end = end)
# Fama French 3 factor model
from finml.asset_pricing import FamaFrench3
FamaFrench3(
market=kor_market,
ticker='005930',
tools='statsmodels',
plot_return=True)
# >>> American market
'''
# get market data
market = StockMarket(start_date=datetime(2018,1,1), end_date=datetime.now())
market.get_stock_price(['AAPL', 'TSLA', 'MSFT', 'NVDA'])
market_mean, market_covariance, _ = market.get_stock_statistics()
# simple mean-variance plot
SMV = SimpleMeanVariance(market_mean, market_covariance)
SMV.plot(show=True, save_path='log/simple_mean_variance.jpeg')
'''