finsim 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- finsim/__init__.py +2 -0
- finsim/data/__init__.py +4 -0
- finsim/data/finnhub.py +37 -0
- finsim/data/preader.py +329 -0
- finsim/data/quandl.py +20 -0
- finsim/estimate/__init__.py +0 -0
- finsim/estimate/constants.py +7 -0
- finsim/estimate/fit.py +104 -0
- finsim/estimate/native/__init__.py +0 -0
- finsim/estimate/native/pyfit.py +36 -0
- finsim/estimate/native/pyrisk.py +36 -0
- finsim/estimate/risk.py +77 -0
- finsim/portfolio/__init__.py +6 -0
- finsim/portfolio/create.py +135 -0
- finsim/portfolio/dynamic.py +292 -0
- finsim/portfolio/helper.py +32 -0
- finsim/portfolio/optimize/__init__.py +0 -0
- finsim/portfolio/optimize/metrics.py +58 -0
- finsim/portfolio/optimize/native/__init__.py +1 -0
- finsim/portfolio/optimize/native/pymetrics.py +51 -0
- finsim/portfolio/optimize/numerics.py +282 -0
- finsim/portfolio/optimize/policy.py +300 -0
- finsim/portfolio/portfolio.py +244 -0
- finsim/retrieve_stock_symbols_cli/__init__.py +2 -0
- finsim/retrieve_stock_symbols_cli/cli.py +60 -0
- finsim/simulation/__init__.py +0 -0
- finsim/simulation/stock.py +122 -0
- finsim/tech/__init__.py +0 -0
- finsim/tech/ma.py +38 -0
- finsim-1.1.0.dist-info/METADATA +53 -0
- finsim-1.1.0.dist-info/RECORD +35 -0
- finsim-1.1.0.dist-info/WHEEL +5 -0
- finsim-1.1.0.dist-info/entry_points.txt +2 -0
- finsim-1.1.0.dist-info/licenses/LICENSE +503 -0
- finsim-1.1.0.dist-info/top_level.txt +1 -0
finsim/__init__.py
ADDED
finsim/data/__init__.py
ADDED
finsim/data/finnhub.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
import requests
|
|
6
|
+
import pandas as pd
|
|
7
|
+
from pandas import DataFrame
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class FinnHubStockReader:
|
|
11
|
+
def __init__(self, token: str):
|
|
12
|
+
self._token = token
|
|
13
|
+
|
|
14
|
+
def get_all_US_symbols(self) -> list[dict[str, Any]]:
|
|
15
|
+
request_url = f"https://finnhub.io/api/v1/stock/symbol?exchange=US&token={self._token}"
|
|
16
|
+
r = requests.get(request_url)
|
|
17
|
+
response = r.json()
|
|
18
|
+
return response
|
|
19
|
+
|
|
20
|
+
def get_stock_candlestick(self, symbol: str, startdate: str, enddate: str) -> DataFrame:
|
|
21
|
+
starttimestamp = int(datetime.strptime(startdate, '%Y-%m-%d').timestamp())
|
|
22
|
+
endtimestamp = int(datetime.strptime(enddate, '%Y-%m-%d').timestamp())
|
|
23
|
+
request_url = 'https://finnhub.io/api/v1/stock/candle?symbol={}&resolution=1&from={}&to={}&token={}'.format(symbol, starttimestamp, endtimestamp, self._token)
|
|
24
|
+
r = requests.get(request_url)
|
|
25
|
+
response = r.json()
|
|
26
|
+
if response['s'] == 'ok':
|
|
27
|
+
df = pd.DataFrame({
|
|
28
|
+
'TimeStamp': [pd.Timestamp(ts, unit='s') for ts in response['t']],
|
|
29
|
+
'High': response['h'],
|
|
30
|
+
'Low': response['l'],
|
|
31
|
+
'Open': response['o'],
|
|
32
|
+
'Close': response['c'],
|
|
33
|
+
'Volume': response['v']
|
|
34
|
+
})
|
|
35
|
+
return df
|
|
36
|
+
else:
|
|
37
|
+
raise Exception('Error: {}'.format(response['s']))
|
finsim/data/preader.py
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
|
|
2
|
+
from datetime import datetime, timedelta
|
|
3
|
+
from typing import Union
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from os import PathLike
|
|
6
|
+
import logging
|
|
7
|
+
from time import sleep
|
|
8
|
+
from functools import lru_cache
|
|
9
|
+
import threading
|
|
10
|
+
import traceback
|
|
11
|
+
|
|
12
|
+
import pandas as pd
|
|
13
|
+
import tables
|
|
14
|
+
import yfinance as yf
|
|
15
|
+
import tables as tb
|
|
16
|
+
from tqdm import tqdm
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def extract_online_yahoofinance_data(symbol: str, startdate: str, enddate: str) -> pd.DataFrame:
|
|
20
|
+
try:
|
|
21
|
+
df = yf.download(
|
|
22
|
+
symbol,
|
|
23
|
+
start=datetime.strptime(startdate, '%Y-%m-%d'),
|
|
24
|
+
end=datetime.strptime(enddate, '%Y-%m-%d') + timedelta(days=1),
|
|
25
|
+
auto_adjust=False
|
|
26
|
+
)
|
|
27
|
+
except:
|
|
28
|
+
logging.warning(f'Symbol {symbol} does not exist between {startdate} and {enddate}.')
|
|
29
|
+
return pd.DataFrame({
|
|
30
|
+
'TimeStamp': [],
|
|
31
|
+
'High': [],
|
|
32
|
+
'Low': [],
|
|
33
|
+
'Open': [],
|
|
34
|
+
'Close': [],
|
|
35
|
+
'Adj Close': [],
|
|
36
|
+
'Volume': [],
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
oricols = df.columns
|
|
40
|
+
if isinstance(oricols, pd.core.indexes.multi.MultiIndex):
|
|
41
|
+
df.columns = [col[0] for col in oricols]
|
|
42
|
+
oricols = df.columns
|
|
43
|
+
df['TimeStamp'] = df.index
|
|
44
|
+
df = df.loc[:, ['TimeStamp'] + list(oricols)].reset_index()
|
|
45
|
+
|
|
46
|
+
return df
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def extract_batch_online_yahoofinance_data(
|
|
50
|
+
symbols: list[str],
|
|
51
|
+
startdate: str,
|
|
52
|
+
enddate: str,
|
|
53
|
+
threads: bool=True
|
|
54
|
+
) -> pd.DataFrame:
|
|
55
|
+
combined_df = yf.download(
|
|
56
|
+
' '.join(symbols),
|
|
57
|
+
start=datetime.strptime(startdate, '%Y-%m-%d'),
|
|
58
|
+
end=datetime.strptime(enddate, '%Y-%m-%d') + timedelta(days=1),
|
|
59
|
+
group_by='ticker',
|
|
60
|
+
threads=threads
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
dataframes = {}
|
|
64
|
+
for symbol in symbols:
|
|
65
|
+
try:
|
|
66
|
+
df = combined_df[symbol].copy()
|
|
67
|
+
oricols = df.columns
|
|
68
|
+
df['TimeStamp'] = df.index
|
|
69
|
+
df = df.loc[:, ['TimeStamp'] + list(oricols)]
|
|
70
|
+
dataframes[symbol] = df
|
|
71
|
+
except:
|
|
72
|
+
dataframes[symbol] = pd.DataFrame({
|
|
73
|
+
'TimeStamp': [],
|
|
74
|
+
'High': [],
|
|
75
|
+
'Low': [],
|
|
76
|
+
'Open': [],
|
|
77
|
+
'Close': [],
|
|
78
|
+
'Adj Close': [],
|
|
79
|
+
'Volume': [],
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
return dataframes
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# yahoo reader with local cache
|
|
86
|
+
METATABLE_FILENAME = 'SYMBOL_CACHE.h5'
|
|
87
|
+
METATABLE_ROWDES = {
|
|
88
|
+
'symbol': tb.StringCol(shape=(), itemsize=10, dflt='', pos=0),
|
|
89
|
+
'query_startdate': tb.StringCol(shape=(), itemsize=10, dflt='0000-00-00', pos=1),
|
|
90
|
+
'query_enddate': tb.StringCol(shape=(), itemsize=10, dflt='0000-00-00', pos=2),
|
|
91
|
+
'data_startdate': tb.StringCol(shape=(), itemsize=10, dflt='0000-00-00', pos=3),
|
|
92
|
+
'data_enddate': tb.StringCol(shape=(), itemsize=10, dflt='0000-00-00', pos=4)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def get_yahoofinance_data(
|
|
97
|
+
symbol: str,
|
|
98
|
+
startdate: str,
|
|
99
|
+
enddate: str,
|
|
100
|
+
cacheddir: Union[str, PathLike]=None
|
|
101
|
+
) -> pd.DataFrame:
|
|
102
|
+
if cacheddir is None:
|
|
103
|
+
return extract_online_yahoofinance_data(symbol, startdate, enddate)
|
|
104
|
+
|
|
105
|
+
if isinstance(cacheddir, str):
|
|
106
|
+
cacheddir = Path(cacheddir)
|
|
107
|
+
|
|
108
|
+
if not cacheddir.is_dir():
|
|
109
|
+
logging.info(f'Creating directory: {cacheddir.as_posix()}')
|
|
110
|
+
cacheddir.mkdir()
|
|
111
|
+
|
|
112
|
+
cached_metafile_path = cacheddir / METATABLE_FILENAME
|
|
113
|
+
if not cached_metafile_path.exists():
|
|
114
|
+
logging.info(f'Creating file: {cached_metafile_path.as_posix()}')
|
|
115
|
+
metatable_h5file = tb.open_file(cached_metafile_path.as_posix(), 'w')
|
|
116
|
+
table = metatable_h5file.create_table('/', 'metatable', METATABLE_ROWDES, title='metatable')
|
|
117
|
+
else:
|
|
118
|
+
metatable_h5file = tb.open_file(cached_metafile_path.as_posix(), 'r+')
|
|
119
|
+
table = metatable_h5file.root.metatable
|
|
120
|
+
|
|
121
|
+
preexist = False
|
|
122
|
+
for row in table.where('symbol=="{}"'.format(symbol)):
|
|
123
|
+
preexist = True
|
|
124
|
+
if row['query_startdate'].decode('utf-8') <= startdate and row['query_enddate'].decode('utf-8') >= enddate:
|
|
125
|
+
df = pd.read_hdf((cacheddir / f"{symbol}.h5").as_posix(), key='yahoodata')
|
|
126
|
+
if len(df) > 0:
|
|
127
|
+
df = df[(df['TimeStamp'] >= startdate) & (df['TimeStamp'] <= enddate)]
|
|
128
|
+
metatable_h5file.close()
|
|
129
|
+
df = df[~df['Close'].isna()]
|
|
130
|
+
return df
|
|
131
|
+
|
|
132
|
+
df = extract_online_yahoofinance_data(symbol, startdate, enddate)
|
|
133
|
+
logging.debug(f'Caching data for {symbol} from {startdate} to {enddate}')
|
|
134
|
+
df.to_hdf(cacheddir / f"{symbol}.h5", key='yahoodata')
|
|
135
|
+
|
|
136
|
+
if preexist:
|
|
137
|
+
logging.debug(f'Updating symbol {symbol} in metatable.')
|
|
138
|
+
for row in table.where('symbol=="{}"'.format(symbol)):
|
|
139
|
+
row['query_startdate'] = startdate
|
|
140
|
+
row['query_enddate'] = enddate
|
|
141
|
+
if len(df) > 0:
|
|
142
|
+
row['data_startdate'] = datetime.strftime(df['TimeStamp'].to_list()[0].date(), '%Y-%m-%d')
|
|
143
|
+
row['data_enddate'] = datetime.strftime(df['TimeStamp'].to_list()[-1].date(), '%Y-%m-%d')
|
|
144
|
+
else:
|
|
145
|
+
row['data_startdate'] = '0000-00-00'
|
|
146
|
+
row['data_enddate'] = ' 0000-00-00'
|
|
147
|
+
row.update()
|
|
148
|
+
else:
|
|
149
|
+
logging.debug(f'Creating symbol {symbol} in metatable')
|
|
150
|
+
newrow = table.row
|
|
151
|
+
newrow['symbol'] = symbol
|
|
152
|
+
newrow['query_startdate'] = startdate
|
|
153
|
+
newrow['query_enddate'] = enddate
|
|
154
|
+
if len(df) > 0:
|
|
155
|
+
newrow['data_startdate'] = datetime.strftime(df['TimeStamp'].to_list()[0].date(), '%Y-%m-%d')
|
|
156
|
+
newrow['data_enddate'] = datetime.strftime(df['TimeStamp'].to_list()[-1].date(), '%Y-%m-%d')
|
|
157
|
+
else:
|
|
158
|
+
newrow['data_startdate'] = '0000-00-00'
|
|
159
|
+
newrow['data_enddate'] = '0000-00-00'
|
|
160
|
+
newrow.append()
|
|
161
|
+
|
|
162
|
+
table.flush()
|
|
163
|
+
metatable_h5file.close()
|
|
164
|
+
|
|
165
|
+
df = df[~df['Close'].isna()]
|
|
166
|
+
|
|
167
|
+
return df
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
@lru_cache(maxsize=256)
|
|
171
|
+
def get_symbol_closing_price(
|
|
172
|
+
symbol: str,
|
|
173
|
+
datestr: str,
|
|
174
|
+
epsilon: float=1e-10,
|
|
175
|
+
cacheddir: Union[PathLike, str]=None,
|
|
176
|
+
backtrack: bool=False
|
|
177
|
+
) -> float:
|
|
178
|
+
df = get_yahoofinance_data(symbol, datestr, datestr, cacheddir=cacheddir)
|
|
179
|
+
if len(df) == 0:
|
|
180
|
+
if backtrack:
|
|
181
|
+
prevdatestr = datetime.strftime(datetime.strptime(datestr, '%Y-%m-%d') - timedelta(days=1), '%Y-%m-%d')
|
|
182
|
+
return get_symbol_closing_price(symbol, prevdatestr, epsilon=epsilon, cacheddir=cacheddir, backtrack=True)
|
|
183
|
+
else:
|
|
184
|
+
raise IndexError('Price not found!')
|
|
185
|
+
else:
|
|
186
|
+
return df['Close'][0]
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def finding_missing_symbols_in_cache(
|
|
190
|
+
symbols: list[str],
|
|
191
|
+
startdate: str,
|
|
192
|
+
enddate: str,
|
|
193
|
+
cacheddir: Union[str, Path]
|
|
194
|
+
) -> list[str]:
|
|
195
|
+
if isinstance(cacheddir, str):
|
|
196
|
+
cacheddir = Path(cacheddir)
|
|
197
|
+
|
|
198
|
+
cached_metafile_path = cacheddir / METATABLE_FILENAME
|
|
199
|
+
if not cached_metafile_path.exists():
|
|
200
|
+
return symbols
|
|
201
|
+
|
|
202
|
+
# in table
|
|
203
|
+
metatable = pd.read_hdf(cached_metafile_path, 'metatable')
|
|
204
|
+
existing_within_range_symbols = list(
|
|
205
|
+
metatable['symbol'][
|
|
206
|
+
(metatable['query_startdate'] <= startdate) & (metatable['query_enddate'] >= enddate)
|
|
207
|
+
]
|
|
208
|
+
)
|
|
209
|
+
if logging.root.level >= logging.DEBUG:
|
|
210
|
+
logging.debug('exisiting within range symbols')
|
|
211
|
+
for symbol in existing_within_range_symbols:
|
|
212
|
+
logging.debug('\t{}'.format(symbol))
|
|
213
|
+
|
|
214
|
+
# check what are in the cached directories
|
|
215
|
+
existing_symbols = [
|
|
216
|
+
symbolfilepath.stem
|
|
217
|
+
for symbolfilepath in cacheddir.glob("*.h5")
|
|
218
|
+
if symbolfilepath is not cached_metafile_path
|
|
219
|
+
]
|
|
220
|
+
if logging.root.level >= logging.DEBUG:
|
|
221
|
+
logging.debug('exisiting symbols')
|
|
222
|
+
for symbol in existing_symbols:
|
|
223
|
+
logging.debug('\t{}'.format(symbol))
|
|
224
|
+
|
|
225
|
+
existing_valid_symbols = set(existing_within_range_symbols) & set(existing_symbols)
|
|
226
|
+
|
|
227
|
+
return sorted(list(set(symbols) - set(existing_valid_symbols)))
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def dataframe_to_hdf(df: pd.DataFrame, filepath: Union[PathLike, str], key: str) -> None:
|
|
231
|
+
df.to_hdf(filepath, key=key)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def generating_cached_yahoofinance_data(
|
|
235
|
+
symbols: list[str],
|
|
236
|
+
startdate: str,
|
|
237
|
+
enddate: str,
|
|
238
|
+
cacheddir: Union[PathLike, str],
|
|
239
|
+
slicebatch: int=50,
|
|
240
|
+
waittime: int=1,
|
|
241
|
+
yfinance_multithreads: bool=False,
|
|
242
|
+
io_multithreads: bool=False
|
|
243
|
+
) -> None:
|
|
244
|
+
if isinstance(cacheddir, str):
|
|
245
|
+
cacheddir = Path(cacheddir)
|
|
246
|
+
cached_metafile_path = cacheddir / METATABLE_FILENAME
|
|
247
|
+
tocache_symbols = finding_missing_symbols_in_cache(symbols, startdate, enddate, cacheddir)
|
|
248
|
+
|
|
249
|
+
logging.info(f'Total number of symbols: {len(symbols)}')
|
|
250
|
+
logging.info(f'Total number of symbols needed to cache: {len(tocache_symbols)}')
|
|
251
|
+
if not cacheddir.is_dir():
|
|
252
|
+
logging.info(f'Creating directory: {cacheddir.as_posix()}')
|
|
253
|
+
cacheddir.mkdir()
|
|
254
|
+
if not cached_metafile_path.exists():
|
|
255
|
+
logging.info(f'Creating file: {cached_metafile_path.as_posix()}')
|
|
256
|
+
metatable_h5file = tb.open_file(cached_metafile_path.as_posix(), 'w')
|
|
257
|
+
table = metatable_h5file.create_table('/', 'metatable', METATABLE_ROWDES, title='metatable')
|
|
258
|
+
else:
|
|
259
|
+
metatable_h5file = tb.open_file(cached_metafile_path.as_posix(), 'r+')
|
|
260
|
+
table = metatable_h5file.root.metatable
|
|
261
|
+
|
|
262
|
+
nbsymbols = len(tocache_symbols)
|
|
263
|
+
for startidx in tqdm(range(0, nbsymbols, slicebatch)):
|
|
264
|
+
success = False
|
|
265
|
+
while not success:
|
|
266
|
+
try:
|
|
267
|
+
dataframes = extract_batch_online_yahoofinance_data(
|
|
268
|
+
tocache_symbols[startidx:min(startidx + slicebatch, nbsymbols)],
|
|
269
|
+
startdate,
|
|
270
|
+
enddate,
|
|
271
|
+
threads=yfinance_multithreads
|
|
272
|
+
)
|
|
273
|
+
success = True
|
|
274
|
+
except:
|
|
275
|
+
sleep(waittime)
|
|
276
|
+
|
|
277
|
+
writing_threads = []
|
|
278
|
+
for symbol in dataframes:
|
|
279
|
+
df = dataframes[symbol]
|
|
280
|
+
df = df[~df['Close'].isna()]
|
|
281
|
+
if len(df) > 0:
|
|
282
|
+
thissymbol_startdate = datetime.strftime(df['TimeStamp'].to_list()[0].date(), '%Y-%m-%d')
|
|
283
|
+
thissymbol_enddate = datetime.strftime(df['TimeStamp'].to_list()[-1].date(), '%Y-%m-%d')
|
|
284
|
+
else:
|
|
285
|
+
thissymbol_startdate = '0000-00-00'
|
|
286
|
+
thissymbol_enddate = '0000-00-00'
|
|
287
|
+
|
|
288
|
+
logging.debug(f'Caching data for {symbol} from {startdate} to {enddate}')
|
|
289
|
+
if not io_multithreads:
|
|
290
|
+
dataframe_to_hdf(df, cacheddir / f'{symbol}.h5', key='yahoodata')
|
|
291
|
+
else:
|
|
292
|
+
thread = threading.Thread(
|
|
293
|
+
target=dataframe_to_hdf,
|
|
294
|
+
args=(df, cacheddir / f'{symbol}.h5', 'yahoodata')
|
|
295
|
+
)
|
|
296
|
+
thread.start()
|
|
297
|
+
writing_threads.append(thread)
|
|
298
|
+
|
|
299
|
+
try:
|
|
300
|
+
logging.debug(f'Creating symbol {symbol} in metatable')
|
|
301
|
+
newrow = table.row
|
|
302
|
+
newrow['symbol'] = symbol
|
|
303
|
+
newrow['query_startdate'] = startdate
|
|
304
|
+
newrow['query_enddate'] = enddate
|
|
305
|
+
newrow['data_startdate'] = thissymbol_startdate
|
|
306
|
+
newrow['data_enddate'] = thissymbol_enddate
|
|
307
|
+
newrow.append()
|
|
308
|
+
|
|
309
|
+
except tables.HDF5ExtError as e:
|
|
310
|
+
logging.error(f'Cannot append record for symbol {symbol}')
|
|
311
|
+
traceback.print_exc()
|
|
312
|
+
continue
|
|
313
|
+
|
|
314
|
+
table.flush()
|
|
315
|
+
|
|
316
|
+
if io_multithreads:
|
|
317
|
+
for thread in writing_threads:
|
|
318
|
+
thread.join()
|
|
319
|
+
|
|
320
|
+
metatable_h5file.close()
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
@lru_cache(maxsize=30)
|
|
324
|
+
def get_dividends_df(symbol: str) -> pd.DataFrame:
|
|
325
|
+
ticker = yf.Ticker(symbol)
|
|
326
|
+
df = pd.DataFrame(ticker.dividends)
|
|
327
|
+
df['TimeStamp'] = df.index.map(lambda item: datetime.strftime(item, '%Y-%m-%d'))
|
|
328
|
+
df = df.loc[:, ['TimeStamp', 'Dividends']]
|
|
329
|
+
return df
|
finsim/data/quandl.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
|
|
2
|
+
import pandas as pd
|
|
3
|
+
import quandl
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class QuandlReader:
|
|
7
|
+
def __init__(self, token: str):
|
|
8
|
+
self.token = token
|
|
9
|
+
|
|
10
|
+
def get_treasury_real_longterm_rates(self) -> pd.DataFrame:
|
|
11
|
+
return quandl.get("USTREASURY/REALLONGTERM", authtoken=self.token)
|
|
12
|
+
|
|
13
|
+
def get_treasury_real_yield_curve_rates(self) -> pd.DataFrame:
|
|
14
|
+
return quandl.get("USTREASURY/REALYIELD", authtoken=self.token)
|
|
15
|
+
|
|
16
|
+
def get_treasury_yield_curve_rates(self) -> pd.DataFrame:
|
|
17
|
+
return quandl.get("USTREASURY/YIELD", authtoken=self.token)
|
|
18
|
+
|
|
19
|
+
def get_treasury_longterm_rates(self) -> pd.DataFrame:
|
|
20
|
+
return quandl.get("USTREASURY/LONGTERMRATES", authtoken=self.token)
|
|
File without changes
|
finsim/estimate/fit.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
|
|
2
|
+
from typing import Literal, Tuple
|
|
3
|
+
from itertools import product
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from nptyping import NDArray, Shape, Float, Datetime64
|
|
7
|
+
|
|
8
|
+
from .constants import dividing_factors_dict
|
|
9
|
+
from .native.pyfit import python_fit_BlackScholesMerton_model, python_fit_multivariate_BlackScholesMerton_model
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# Note: always round-off to seconds first, but flexible about the unit to be used.
|
|
13
|
+
|
|
14
|
+
def fit_BlackScholesMerton_model(
|
|
15
|
+
timestamps: NDArray[Shape["*"], Datetime64],
|
|
16
|
+
prices: NDArray[Shape["*"], Float],
|
|
17
|
+
unit: Literal['second', 'minute', 'hour', 'day', 'year']='year',
|
|
18
|
+
lowlevellang: Literal['C', 'P']='P'
|
|
19
|
+
) -> Tuple[float, float]:
|
|
20
|
+
dividing_factor = dividing_factors_dict[unit]
|
|
21
|
+
|
|
22
|
+
ts = np.array(timestamps, dtype='datetime64[s]')
|
|
23
|
+
ts = np.array(ts, dtype=np.float64) / dividing_factor
|
|
24
|
+
|
|
25
|
+
if lowlevellang == 'C':
|
|
26
|
+
raise ValueError("Cython fitting is no longer supported!")
|
|
27
|
+
elif lowlevellang == 'P':
|
|
28
|
+
return python_fit_BlackScholesMerton_model(ts, prices)
|
|
29
|
+
else:
|
|
30
|
+
raise ValueError(
|
|
31
|
+
'Unknown low-level language: {}. (Should be "P" (Python), or "C" (Cython).)'.format(
|
|
32
|
+
lowlevellang))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def fit_multivariate_BlackScholesMerton_model(
|
|
36
|
+
timestamps: NDArray[Shape["*"], Datetime64],
|
|
37
|
+
multiprices: NDArray[Shape["*, *"], Float],
|
|
38
|
+
unit: Literal['second', 'minute', 'hour', 'day', 'year']='year',
|
|
39
|
+
lowlevellang: Literal['C', 'P']='P'
|
|
40
|
+
) -> Tuple[NDArray[Shape["*"], Float], NDArray[Shape["*, *"], Float]]:
|
|
41
|
+
dividing_factor = dividing_factors_dict[unit]
|
|
42
|
+
|
|
43
|
+
ts = np.array(timestamps, dtype='datetime64[s]')
|
|
44
|
+
ts = np.array(ts, dtype=np.float64) / dividing_factor
|
|
45
|
+
|
|
46
|
+
if lowlevellang == 'C':
|
|
47
|
+
raise ValueError("Cython fitting is no longer supported!")
|
|
48
|
+
elif lowlevellang == 'P':
|
|
49
|
+
return python_fit_multivariate_BlackScholesMerton_model(ts, multiprices)
|
|
50
|
+
else:
|
|
51
|
+
raise ValueError(
|
|
52
|
+
'Unknown low-level language: {}. (Should be "C" (Cython).)'.format(
|
|
53
|
+
lowlevellang))
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
######## routines below are for time-weighted portfolio building
|
|
57
|
+
|
|
58
|
+
def fit_timeweighted_BlackScholesMerton_model(
|
|
59
|
+
timestamps: NDArray[Shape["*"], Datetime64],
|
|
60
|
+
prices: NDArray[Shape["*"], Float],
|
|
61
|
+
weights: NDArray[Shape["*"], Float],
|
|
62
|
+
unit: Literal['second', 'minute', 'hour', 'day', 'year']='year'
|
|
63
|
+
) -> Tuple[float, float]:
|
|
64
|
+
dividing_factor = dividing_factors_dict[unit]
|
|
65
|
+
|
|
66
|
+
ts = np.array(timestamps, dtype='datetime64[s]')
|
|
67
|
+
ts = np.array(ts, dtype=np.float64) / dividing_factor
|
|
68
|
+
|
|
69
|
+
dlogS = np.log(prices[1:] / prices[:-1])
|
|
70
|
+
dt = ts[1:] - ts[:-1]
|
|
71
|
+
|
|
72
|
+
r = np.average(dlogS / dt, weights=weights[1:])
|
|
73
|
+
sigma = np.sqrt(np.average(np.square(dlogS / np.sqrt(dt)), weights=weights[1:]) - np.square(
|
|
74
|
+
np.average(dlogS / np.sqrt(dt), weights=weights[1:])))
|
|
75
|
+
|
|
76
|
+
return r, sigma
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def fit_timeweighted_multivariate_BlackScholesMerton_model(
|
|
80
|
+
timestamps: NDArray[Shape["*"], Datetime64],
|
|
81
|
+
multiprices: NDArray[Shape["*, *"], Float],
|
|
82
|
+
weights: NDArray[Shape["*"], Float],
|
|
83
|
+
unit: Literal['second', 'minute', 'hour', 'day', 'year']='year'
|
|
84
|
+
) -> Tuple[NDArray[Shape["*"], Float], NDArray[Shape["*, *"], Float]]:
|
|
85
|
+
dividing_factor = dividing_factors_dict[unit]
|
|
86
|
+
|
|
87
|
+
ts = np.array(timestamps, dtype='datetime64[s]')
|
|
88
|
+
ts = np.array(ts, dtype=np.float64) / dividing_factor
|
|
89
|
+
|
|
90
|
+
# possibly hit this part
|
|
91
|
+
dlogS = np.log(multiprices[:, 1:] / multiprices[:, :-1])
|
|
92
|
+
dt = ts[1:] - ts[:-1]
|
|
93
|
+
|
|
94
|
+
# estimation
|
|
95
|
+
r = np.zeros(multiprices.shape[0])
|
|
96
|
+
for i in range(multiprices.shape[0]):
|
|
97
|
+
r[i] = np.average((dlogS[i, :] / dt), weights=weights[1:])
|
|
98
|
+
cov = np.zeros((multiprices.shape[0], multiprices.shape[0]))
|
|
99
|
+
for i, j in product(range(multiprices.shape[0]), range(multiprices.shape[0])):
|
|
100
|
+
avg_i = np.average(dlogS[i, :] / np.sqrt(dt), weights=weights[1:])
|
|
101
|
+
avg_j = np.average(dlogS[j, :] / np.sqrt(dt), weights=weights[1:])
|
|
102
|
+
cov[i, j] = np.average(dlogS[i, :] * dlogS[j, :] / dt, weights=np.square(weights[1:])) - avg_i * avg_j
|
|
103
|
+
|
|
104
|
+
return r, cov
|
|
File without changes
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
|
|
2
|
+
from typing import Tuple
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
import numba as nb
|
|
6
|
+
from nptyping import NDArray, Shape, Float
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@nb.njit
|
|
10
|
+
def python_fit_BlackScholesMerton_model(
|
|
11
|
+
ts: NDArray[Shape["*"], Float],
|
|
12
|
+
prices: NDArray[Shape["*"], Float]
|
|
13
|
+
) -> Tuple[float, float]:
|
|
14
|
+
dlogS = np.log(prices[1:] / prices[:-1])
|
|
15
|
+
dt = ts[1:] - ts[:-1]
|
|
16
|
+
|
|
17
|
+
r = np.mean(dlogS / dt)
|
|
18
|
+
sigma = np.std(dlogS / np.sqrt(dt))
|
|
19
|
+
|
|
20
|
+
return r, sigma
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@nb.njit
|
|
24
|
+
def python_fit_multivariate_BlackScholesMerton_model(
|
|
25
|
+
ts: NDArray[Shape["*"], Float],
|
|
26
|
+
multiprices: NDArray[Shape["*, *"], Float]
|
|
27
|
+
) -> Tuple[NDArray[Shape["*"], Float], NDArray[Shape["*, *"], Float]]:
|
|
28
|
+
dlogS = np.log(multiprices[:, 1:] / multiprices[:, :-1])
|
|
29
|
+
dt = ts[1:] - ts[:-1]
|
|
30
|
+
|
|
31
|
+
r = np.zeros(multiprices.shape[0])
|
|
32
|
+
for i in range(multiprices.shape[0]):
|
|
33
|
+
r[i] = np.mean((dlogS[i, :] / dt))
|
|
34
|
+
cov = np.cov(dlogS / np.sqrt(dt), bias=True)
|
|
35
|
+
|
|
36
|
+
return r, cov
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
|
|
2
|
+
import numpy as np
|
|
3
|
+
import numba as nb
|
|
4
|
+
from nptyping import NDArray, Shape, Float
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@nb.njit
|
|
8
|
+
def python_estimate_downside_risk(
|
|
9
|
+
ts: NDArray[Shape["*"], Float],
|
|
10
|
+
prices: NDArray[Shape["*"], Float],
|
|
11
|
+
target_return: float
|
|
12
|
+
) -> float:
|
|
13
|
+
dlogS = np.log(prices[1:] / prices[:-1])
|
|
14
|
+
dt = ts[1:] - ts[:-1]
|
|
15
|
+
|
|
16
|
+
rms_rarray = dlogS / np.sqrt(dt)
|
|
17
|
+
less_return_array = target_return - rms_rarray
|
|
18
|
+
less_return_array[less_return_array < 0] = 0.
|
|
19
|
+
downside_risk = np.sqrt(np.mean(np.square(less_return_array)))
|
|
20
|
+
return downside_risk
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@nb.njit
|
|
24
|
+
def python_estimate_upside_risk(
|
|
25
|
+
ts: NDArray[Shape["*"], Float],
|
|
26
|
+
prices: NDArray[Shape["*"], Float],
|
|
27
|
+
target_return: float
|
|
28
|
+
) -> float:
|
|
29
|
+
dlogS = np.log(prices[1:] / prices[:-1])
|
|
30
|
+
dt = ts[1:] - ts[:-1]
|
|
31
|
+
|
|
32
|
+
rms_rarray = dlogS / np.sqrt(dt)
|
|
33
|
+
more_return_array = rms_rarray - target_return
|
|
34
|
+
more_return_array[more_return_array < 0] = 0.
|
|
35
|
+
upside_risk = np.sqrt(np.mean(np.square(more_return_array)))
|
|
36
|
+
return upside_risk
|
finsim/estimate/risk.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
|
|
2
|
+
from typing import Literal
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
from scipy import stats
|
|
6
|
+
from nptyping import NDArray, Shape, Float, Datetime64
|
|
7
|
+
|
|
8
|
+
from .native.pyrisk import python_estimate_downside_risk, python_estimate_upside_risk
|
|
9
|
+
from .constants import dividing_factors_dict
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def estimate_downside_risk(
|
|
13
|
+
timestamps: NDArray[Shape["*"], Datetime64],
|
|
14
|
+
prices: NDArray[Shape["*"], Float],
|
|
15
|
+
target_return: float,
|
|
16
|
+
unit: Literal['second', 'minute', 'hour', 'day', 'year']='year',
|
|
17
|
+
lowlevellang: Literal['C', 'P']='P'
|
|
18
|
+
) -> float:
|
|
19
|
+
dividing_factor = dividing_factors_dict[unit]
|
|
20
|
+
|
|
21
|
+
ts = np.array(timestamps, dtype='datetime64[s]')
|
|
22
|
+
ts = np.array(ts, dtype=np.float64) / dividing_factor
|
|
23
|
+
|
|
24
|
+
if lowlevellang == 'C':
|
|
25
|
+
raise ValueError("Cython fitting is no longer supported!")
|
|
26
|
+
elif lowlevellang == 'P':
|
|
27
|
+
return python_estimate_downside_risk(ts, prices, target_return)
|
|
28
|
+
else:
|
|
29
|
+
raise ValueError(
|
|
30
|
+
'Unknown low-level language: {}. (Should be "P" (Python), or "C" (Cython))'.format(
|
|
31
|
+
lowlevellang))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def estimate_upside_risk(
|
|
35
|
+
timestamps: NDArray[Shape["*"], Datetime64],
|
|
36
|
+
prices: NDArray[Shape["*"], Float],
|
|
37
|
+
target_return: float,
|
|
38
|
+
unit: Literal['second', 'minute', 'hour', 'day', 'year'] = 'year',
|
|
39
|
+
lowlevellang: Literal['C', 'P'] = 'P'
|
|
40
|
+
) -> float:
|
|
41
|
+
dividing_factor = dividing_factors_dict[unit]
|
|
42
|
+
|
|
43
|
+
ts = np.array(timestamps, dtype='datetime64[s]')
|
|
44
|
+
ts = np.array(ts, dtype=np.float64) / dividing_factor
|
|
45
|
+
|
|
46
|
+
if lowlevellang == 'C':
|
|
47
|
+
raise ValueError("Cython fitting is no longer supported!")
|
|
48
|
+
elif lowlevellang == 'P':
|
|
49
|
+
return python_estimate_upside_risk(ts, prices, target_return)
|
|
50
|
+
else:
|
|
51
|
+
raise ValueError(
|
|
52
|
+
'Unknown low-level language: {}. (Should be "P" (Python), or "C" (Cython).)'.format(
|
|
53
|
+
lowlevellang))
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def estimate_beta(
|
|
57
|
+
timestamps: NDArray[Shape["*"], Datetime64],
|
|
58
|
+
prices: NDArray[Shape["*"], Float],
|
|
59
|
+
market_prices: NDArray[Shape["*"], Float],
|
|
60
|
+
unit: Literal['second', 'minute', 'hour', 'day', 'year']='year'
|
|
61
|
+
) -> float:
|
|
62
|
+
dividing_factor = dividing_factors_dict[unit]
|
|
63
|
+
|
|
64
|
+
ts = np.array(timestamps, dtype='datetime64[s]')
|
|
65
|
+
ts = np.array(ts, dtype=np.float64) / dividing_factor
|
|
66
|
+
dt = ts[1:] - ts[:-1]
|
|
67
|
+
|
|
68
|
+
dlogprices = np.log(prices[1:] / prices[:-1])
|
|
69
|
+
dlogmarketprices = np.log(market_prices[1:] / market_prices[:-1])
|
|
70
|
+
assert len(dt) == len(dlogprices)
|
|
71
|
+
assert len(dt) == len(dlogmarketprices)
|
|
72
|
+
|
|
73
|
+
stockyields = dlogprices / dt
|
|
74
|
+
marketyields = dlogmarketprices / dt
|
|
75
|
+
|
|
76
|
+
reg = stats.linregress(x=marketyields, y=stockyields)
|
|
77
|
+
return reg.slope
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
|
|
2
|
+
from .portfolio import Portfolio, OptimizedPortfolio
|
|
3
|
+
from .dynamic import DynamicPortfolio, DynamicPortfolioWithDividends
|
|
4
|
+
from .create import get_optimized_portfolio_on_sharpe_ratio, get_optimized_portfolio_on_mpt_costfunction, \
|
|
5
|
+
get_optimized_portfolio_on_mpt_entropy_costfunction
|
|
6
|
+
from .optimize.numerics import get_BlackScholesMerton_stocks_estimation
|