2022-02-21 02:52:04 +00:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-02-22 05:57:39 +00:00
|
|
|
"execution_count": 1,
|
2022-02-21 02:52:04 +00:00
|
|
|
"id": "3f7938c0-98e3-43b8-86e8-4f000cda7ce5",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"import datetime\n",
|
|
|
|
"import pandas as pd\n",
|
|
|
|
"\n",
|
|
|
|
"from fincal.fincal import TimeSeries\n",
|
|
|
|
"from fincal.core import Series"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-02-22 05:57:39 +00:00
|
|
|
"execution_count": 2,
|
2022-02-21 07:41:38 +00:00
|
|
|
"id": "4b8ccd5f-dfff-4202-82c4-f66a30c122b6",
|
2022-02-21 02:52:04 +00:00
|
|
|
"metadata": {},
|
2022-02-22 05:57:39 +00:00
|
|
|
"outputs": [
|
2022-02-26 07:16:42 +00:00
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"CPU times: total: 125 ms\n",
|
|
|
|
"Wall time: 99 ms\n"
|
|
|
|
]
|
|
|
|
},
|
2022-02-22 05:57:39 +00:00
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
|
|
|
"[(datetime.datetime(2022, 1, 31, 0, 0), 310.980011),\n",
|
2022-02-26 07:16:42 +00:00
|
|
|
" (datetime.datetime(2021, 5, 28, 0, 0), 249.67999300000002)]"
|
2022-02-22 05:57:39 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
"execution_count": 2,
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
2022-02-21 02:52:04 +00:00
|
|
|
"source": [
|
2022-02-26 07:16:42 +00:00
|
|
|
"%%time\n",
|
2022-02-22 05:57:39 +00:00
|
|
|
"dfd = pd.read_csv('test_files/msft.csv')\n",
|
|
|
|
"# dfd = dfd[dfd['amfi_code'] == 118825].reset_index(drop=True)\n",
|
|
|
|
"ts = TimeSeries([(i.date, i.nav) for i in dfd.itertuples()], frequency='D')\n",
|
|
|
|
"repr(ts)\n",
|
|
|
|
"ts[['2022-01-31', '2021-05-28']]"
|
2022-02-21 02:52:04 +00:00
|
|
|
]
|
|
|
|
},
|
2022-02-26 07:16:42 +00:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 12,
|
|
|
|
"id": "ffd9665d",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
|
|
|
"(datetime.datetime(2022, 1, 31, 0, 0), 310.980011)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"execution_count": 12,
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"ts['2022-01-31']"
|
|
|
|
]
|
|
|
|
},
|
2022-02-21 02:52:04 +00:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-02-22 05:57:39 +00:00
|
|
|
"execution_count": 3,
|
|
|
|
"id": "086d4377-d1b1-4e51-84c0-39dee28ef75e",
|
2022-02-21 02:52:04 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
2022-02-26 07:16:42 +00:00
|
|
|
"CPU times: total: 15.6 ms\n",
|
|
|
|
"Wall time: 16 ms\n"
|
2022-02-21 02:52:04 +00:00
|
|
|
]
|
2022-02-21 07:41:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
2022-02-22 05:57:39 +00:00
|
|
|
"TimeSeries([(datetime.datetime(2022, 1, 3, 0, 0), 334.75),\n",
|
|
|
|
"\t (datetime.datetime(2022, 1, 4, 0, 0), 329.01001),\n",
|
|
|
|
"\t (datetime.datetime(2022, 1, 5, 0, 0), 316.380005)\n",
|
2022-02-21 07:41:38 +00:00
|
|
|
"\t ...\n",
|
2022-02-22 05:57:39 +00:00
|
|
|
"\t (datetime.datetime(2022, 2, 16, 0, 0), 299.5),\n",
|
|
|
|
"\t (datetime.datetime(2022, 2, 17, 0, 0), 290.730011),\n",
|
|
|
|
"\t (datetime.datetime(2022, 2, 18, 0, 0), 287.929993)], frequency='D')"
|
2022-02-21 07:41:38 +00:00
|
|
|
]
|
|
|
|
},
|
2022-02-22 05:57:39 +00:00
|
|
|
"execution_count": 3,
|
2022-02-21 07:41:38 +00:00
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
2022-02-21 02:52:04 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
2022-02-22 05:57:39 +00:00
|
|
|
"%%time\n",
|
|
|
|
"s = ts.dates >= '2022-01-01'\n",
|
|
|
|
"ts[s]"
|
2022-02-21 02:52:04 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-02-22 05:57:39 +00:00
|
|
|
"execution_count": 4,
|
|
|
|
"id": "e815edc9-3746-4192-814e-bd27b2771a0c",
|
2022-02-21 02:52:04 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
2022-02-22 05:57:39 +00:00
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
2022-02-26 07:16:42 +00:00
|
|
|
"CPU times: total: 15.6 ms\n",
|
|
|
|
"Wall time: 4 ms\n"
|
2022-02-22 05:57:39 +00:00
|
|
|
]
|
|
|
|
},
|
2022-02-21 02:52:04 +00:00
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
2022-02-22 05:57:39 +00:00
|
|
|
"[(datetime.datetime(1992, 2, 19, 0, 0), 2.398438),\n",
|
|
|
|
" (datetime.datetime(1992, 2, 20, 0, 0), 2.447917),\n",
|
|
|
|
" (datetime.datetime(1992, 2, 21, 0, 0), 2.385417),\n",
|
2022-02-26 07:16:42 +00:00
|
|
|
" (datetime.datetime(1992, 2, 24, 0, 0), 2.3932290000000003),\n",
|
2022-02-22 05:57:39 +00:00
|
|
|
" (datetime.datetime(1992, 2, 25, 0, 0), 2.411458),\n",
|
|
|
|
" (datetime.datetime(1992, 2, 26, 0, 0), 2.541667),\n",
|
|
|
|
" (datetime.datetime(1992, 2, 27, 0, 0), 2.601563),\n",
|
|
|
|
" (datetime.datetime(1992, 2, 28, 0, 0), 2.572917),\n",
|
|
|
|
" (datetime.datetime(1992, 3, 2, 0, 0), 2.5625),\n",
|
|
|
|
" (datetime.datetime(1992, 3, 3, 0, 0), 2.567708)]"
|
2022-02-21 02:52:04 +00:00
|
|
|
]
|
|
|
|
},
|
2022-02-22 05:57:39 +00:00
|
|
|
"execution_count": 4,
|
2022-02-21 02:52:04 +00:00
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
2022-02-22 05:57:39 +00:00
|
|
|
"%%time\n",
|
|
|
|
"ts.iloc[:10]"
|
2022-02-21 02:52:04 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-02-26 07:16:42 +00:00
|
|
|
"execution_count": 11,
|
2022-02-22 05:57:39 +00:00
|
|
|
"id": "dc469722-c816-4b57-8d91-7a3b865f86be",
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
2022-02-21 17:38:13 +00:00
|
|
|
"outputs": [
|
|
|
|
{
|
2022-02-22 05:57:39 +00:00
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
2022-02-26 07:16:42 +00:00
|
|
|
"CPU times: total: 297 ms\n",
|
|
|
|
"Wall time: 290 ms\n"
|
2022-02-22 05:57:39 +00:00
|
|
|
]
|
2022-02-21 17:38:13 +00:00
|
|
|
}
|
|
|
|
],
|
2022-02-21 07:41:38 +00:00
|
|
|
"source": [
|
2022-02-22 05:57:39 +00:00
|
|
|
"%%time\n",
|
|
|
|
"from_date = datetime.date(1994, 1, 1)\n",
|
|
|
|
"to_date = datetime.date(2022, 1, 1)\n",
|
|
|
|
"# print(ts.calculate_returns(to_date, years=7))\n",
|
|
|
|
"rr = ts.calculate_rolling_returns(from_date, to_date)"
|
2022-02-21 07:41:38 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-02-22 05:57:39 +00:00
|
|
|
"execution_count": 6,
|
|
|
|
"id": "e5d357b4-4fe5-4a0a-8107-0ab6828d7c41",
|
2022-02-21 07:41:38 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
2022-02-22 05:57:39 +00:00
|
|
|
"TimeSeries([(datetime.datetime(1994, 1, 3, 0, 0), -0.06149359306648605),\n",
|
|
|
|
"\t (datetime.datetime(1994, 1, 4, 0, 0), -0.05433177603118022),\n",
|
|
|
|
"\t (datetime.datetime(1994, 1, 5, 0, 0), -0.04913276300578029)\n",
|
2022-02-21 17:18:24 +00:00
|
|
|
"\t ...\n",
|
2022-02-22 05:57:39 +00:00
|
|
|
"\t (datetime.datetime(2021, 12, 29, 0, 0), 0.5255410267822715),\n",
|
|
|
|
"\t (datetime.datetime(2021, 12, 30, 0, 0), 0.5306749265370103),\n",
|
|
|
|
"\t (datetime.datetime(2021, 12, 31, 0, 0), 0.5120942811985818)], frequency='D')"
|
2022-02-21 07:41:38 +00:00
|
|
|
]
|
|
|
|
},
|
2022-02-22 05:57:39 +00:00
|
|
|
"execution_count": 6,
|
2022-02-21 07:41:38 +00:00
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
2022-02-22 05:57:39 +00:00
|
|
|
"rr"
|
2022-02-21 07:41:38 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-02-22 05:57:39 +00:00
|
|
|
"execution_count": 7,
|
|
|
|
"id": "4bad2efa",
|
2022-02-21 17:38:13 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
2022-02-22 05:57:39 +00:00
|
|
|
"Series([1.0, 2.0, 3.0, 4.0, 5.0])"
|
2022-02-21 17:38:13 +00:00
|
|
|
]
|
|
|
|
},
|
2022-02-22 05:57:39 +00:00
|
|
|
"execution_count": 7,
|
2022-02-21 17:38:13 +00:00
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
2022-02-22 05:57:39 +00:00
|
|
|
"sr = Series([1, 2, 3, 4, 5], 'number')\n",
|
|
|
|
"sr"
|
2022-02-21 17:38:13 +00:00
|
|
|
]
|
2022-02-26 07:16:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 2,
|
|
|
|
"id": "adceda69",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"from fincal.fincal import TimeSeries\n",
|
|
|
|
"import datetime\n",
|
|
|
|
"ts = TimeSeries(data = [('2021-01-01', 220), ('2021-02-01', 230), ('2021-03-01', 240)], frequency='M')"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 5,
|
|
|
|
"id": "68cf9f8c",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
|
|
|
"(datetime.datetime(2021, 2, 1, 0, 0), 0.045454545454545414)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"execution_count": 5,
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"ts.calculate_returns('2021-02-05', interval_type='months', interval_value=1, compounding=False)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 6,
|
|
|
|
"id": "a583347f",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"D = {'a': 1, 'b': 2}"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 10,
|
|
|
|
"id": "f79ac787",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
|
|
|
"dict_keys(['a', 'b'])"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"execution_count": 10,
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"D.keys()"
|
|
|
|
]
|
2022-02-21 02:52:04 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
2022-02-21 17:18:24 +00:00
|
|
|
"display_name": "Python 3 (ipykernel)",
|
2022-02-21 02:52:04 +00:00
|
|
|
"language": "python",
|
|
|
|
"name": "python3"
|
|
|
|
},
|
|
|
|
"language_info": {
|
|
|
|
"codemirror_mode": {
|
|
|
|
"name": "ipython",
|
|
|
|
"version": 3
|
|
|
|
},
|
|
|
|
"file_extension": ".py",
|
|
|
|
"mimetype": "text/x-python",
|
|
|
|
"name": "python",
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
"pygments_lexer": "ipython3",
|
2022-02-26 07:16:42 +00:00
|
|
|
"version": "3.8.3"
|
2022-02-21 02:52:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 5
|
|
|
|
}
|