@ -3,36 +3,23 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "3f7938c0-98e3-43b8-86e8-4f000cda7ce 5",
"id": "e1ecfa5 5",
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import pandas as pd\n",
"\n",
"from fincal.fincal import TimeSeries\n",
"from fincal.core import Series"
"import fincal as fc"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4b8ccd5f-dfff-4202-82c4-f66a30c122b 6",
"id": "ccac389 6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: total: 125 ms\n",
"Wall time: 99 ms\n"
]
},
{
"data": {
"text/plain": [
"[(datetime.datetime(2022, 1, 31, 0, 0), 310.980011),\n",
" (datetime.datetime(2021, 5, 28, 0, 0), 249.67999300000002)]"
"fincal.fincal.TimeSeries"
]
},
"execution_count": 2,
@ -41,247 +28,341 @@
}
],
"source": [
"%%time\n",
"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']]"
"fc.TimeSeries"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "ffd9665d",
"execution_count": 3,
"id": "a54bfbdf",
"metadata": {},
"outputs": [],
"source": [
"data = [\n",
" (\"2022-01-01\", 10),\n",
" (\"2022-01-02\", 12),\n",
" (\"2022-01-03\", 14)\n",
" # (\"2022-01-04\", 16),\n",
" # (\"2022-01-06\", 18),\n",
" # (\"2022-01-07\", 20),\n",
" # (\"2022-01-09\", 22),\n",
" # (\"2022-01-10\", 24),\n",
" # (\"2022-01-11\", 26),\n",
" # (\"2022-01-12\", 28),\n",
" # (\"2023-01-01\", 30),\n",
" # (\"2023-01-02\", 32),\n",
" # (\"2023-01-03\", 34),\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "fcc5f8f1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(datetime.datetime(2022, 1, 31, 0, 0), 310.980011)"
"TimeSeries([(datetime.datetime(2022, 1, 1, 0, 0), 10),\n",
"\t(datetime.datetime(2022, 1, 2, 0, 0), 12),\n",
"\t(datetime.datetime(2022, 1, 3, 0, 0), 14)], frequency='M')"
]
},
"execution_count": 12,
"execution_count": 5 ,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ts['2022-01-31']"
"ts = fc.TimeSeries(data, 'M')\n",
"ts"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "086d4377-d1b1-4e51-84c0-39dee28ef75e",
"execution_count": 7 ,
"id": "c9e9cb1b ",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: total: 15.6 ms\n",
"Wall time: 16 ms\n"
]
},
"data": {
"text/plain": [
"TimeSeries([(datetime.datetime(2022, 1, 1, 0, 0), 10),\n",
"\t(datetime.datetime(2022, 1, 2, 0, 0), 12),\n",
"\t(datetime.datetime(2022, 1, 3, 0, 0), 14),\n",
"\t(datetime.datetime(2022, 1, 4, 0, 0), 15),\n",
"\t(datetime.datetime(2022, 1, 5, 0, 0), 16)], frequency='M')"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ts['2022-01-04'] = 15\n",
"ts"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8e812756",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"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",
"TimeSeries([(datetime.datetime(2022, 1, 1, 0, 0), 10 ),\n",
"\t (datetime.datetime(2022, 1, 8, 0, 0), 20 ),\n",
"\t (datetime.datetime(2022, 1, 15, 0, 0), 28 )\n",
"\t ...\n",
"\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')"
"\t (datetime.datetime(2022, 12, 17, 0, 0), 28 ),\n",
"\t (datetime.datetime(2022, 12, 24, 0, 0), 28 ),\n",
"\t (datetime.datetime(2022, 12, 31, 0, 0), 28)], frequency='W ')"
]
},
"execution_count": 3,
"execution_count": 8 ,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"s = ts.dates >= '2022-01-01'\n",
"ts[s]"
"ts.expand('W', 'ffill')"
]
},
{
"cell_type": "code",
"execution_count": 4 ,
"id": "e815edc9-3746-4192-814e-bd27b2771a0c ",
"execution_count": 12 ,
"id": "55918da9-2df6-4773-9ca0-e19b52c3ece2 ",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: total: 15.6 ms\n",
"Wall time: 4 ms\n"
]
},
{
"data": {
"text/plain": [
"[(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",
" (datetime.datetime(1992, 2, 24, 0, 0), 2.3932290000000003),\n",
" (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)]"
"TimeSeries([(datetime.datetime(2022, 1, 1, 0, 0), 10),\n",
"\t(datetime.datetime(2022, 4, 1, 0, 0), 28),\n",
"\t(datetime.datetime(2022, 7, 1, 0, 0), 28),\n",
"\t(datetime.datetime(2022, 10, 1, 0, 0), 28),\n",
"\t(datetime.datetime(2023, 1, 1, 0, 0), 30)], frequency='Q')"
]
},
"execution_count": 4 ,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"ts.iloc[:10]"
"ts.shrink('Q', 'ffill')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "dc469722-c816-4b57-8d91-7a3b865f86be",
"metadata": {
"tags": []
},
"execution_count": 13,
"id": "36eefec7-7dbf-4a28-ac50-2e502d9d6864",
"metadata": {},
"outputs": [],
"source": [
"weekly_data = [('2017-01-01', 67),\n",
"('2017-01-08', 79),\n",
"('2017-01-15', 73),\n",
"('2017-01-22', 63),\n",
"('2017-01-29', 85),\n",
"('2017-02-05', 66),\n",
"('2017-02-12', 78),\n",
"('2017-02-19', 75),\n",
"('2017-02-26', 76),\n",
"('2017-03-05', 82),\n",
"('2017-03-12', 85),\n",
"('2017-03-19', 63),\n",
"('2017-03-26', 78),\n",
"('2017-04-02', 65),\n",
"('2017-04-09', 85),\n",
"('2017-04-16', 86),\n",
"('2017-04-23', 67),\n",
"('2017-04-30', 65),\n",
"('2017-05-07', 82),\n",
"('2017-05-14', 73),\n",
"('2017-05-21', 78),\n",
"('2017-05-28', 74),\n",
"('2017-06-04', 62),\n",
"('2017-06-11', 84),\n",
"('2017-06-18', 83)]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "39bd8598-ab0f-4c81-8428-ad8248e686d3",
"metadata": {},
"outputs": [],
"source": [
"week_ts = fc.TimeSeries(weekly_data, 'W')"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "d64dd3c6-4295-4301-90e4-5c74ea23c4af",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: total: 297 ms\n",
"Wall time: 290 ms\n"
"(datetime.datetime(2017, 1, 1, 0, 0), 67)\n",
"(datetime.datetime(2017, 2, 1, 0, 0), 85)\n",
"(datetime.datetime(2017, 3, 1, 0, 0), 76)\n",
"(datetime.datetime(2017, 4, 1, 0, 0), 78)\n",
"(datetime.datetime(2017, 5, 1, 0, 0), 65)\n",
"(datetime.datetime(2017, 6, 1, 0, 0), 74)\n"
]
}
],
"source": [
"%%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)"
"for i in week_ts.shrink('M', 'ffill', skip_weekends=True):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e5d357b4-4fe5-4a0a-8107-0ab6828d7c41",
"execution_count": 23 ,
"id": "a549c5c0-c89a-4cc3-b396-c4afa77a9879 ",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"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",
"\t ...\n",
"\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')"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
"ename": "OverflowError",
"evalue": "date value out of range",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"File \u001b[0;32m~/Documents/projects/fincal/fincal/core.py:405\u001b[0m, in \u001b[0;36mTimeSeriesCore.get\u001b[0;34m(self, date, default, closest)\u001b[0m\n\u001b[1;32m 404\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 405\u001b[0m item \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_item_from_date\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdate\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 406\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m item\n",
"File \u001b[0;32m~/Documents/projects/fincal/fincal/core.py:69\u001b[0m, in \u001b[0;36mdate_parser.<locals>.parse_dates.<locals>.wrapper_func\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 68\u001b[0m args[j] \u001b[38;5;241m=\u001b[39m parsed_date\n\u001b[0;32m---> 69\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/Documents/projects/fincal/fincal/core.py:328\u001b[0m, in \u001b[0;36mTimeSeriesCore._get_item_from_date\u001b[0;34m(self, date)\u001b[0m\n\u001b[1;32m 326\u001b[0m \u001b[38;5;129m@date_parser\u001b[39m(\u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m 327\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_get_item_from_date\u001b[39m(\u001b[38;5;28mself\u001b[39m, date: \u001b[38;5;28mstr\u001b[39m \u001b[38;5;241m|\u001b[39m datetime\u001b[38;5;241m.\u001b[39mdatetime):\n\u001b[0;32m--> 328\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m date, \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m[\u001b[49m\u001b[43mdate\u001b[49m\u001b[43m]\u001b[49m\n",
"\u001b[0;31mKeyError\u001b[0m: datetime.datetime(1, 1, 1, 0, 0)",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[0;31mOverflowError\u001b[0m Traceback (most recent call last)",
"Input \u001b[0;32mIn [23]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mweek_ts\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msync\u001b[49m\u001b[43m(\u001b[49m\u001b[43mts\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/Documents/projects/fincal/fincal/fincal.py:733\u001b[0m, in \u001b[0;36mTimeSeries.sync\u001b[0;34m(self, other, fill_method)\u001b[0m\n\u001b[1;32m 731\u001b[0m new_other[dt] \u001b[38;5;241m=\u001b[39m other[dt][\u001b[38;5;241m1\u001b[39m]\n\u001b[1;32m 732\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 733\u001b[0m new_other[dt] \u001b[38;5;241m=\u001b[39m other\u001b[38;5;241m.\u001b[39mget(dt, closest\u001b[38;5;241m=\u001b[39mclosest)[\u001b[38;5;241m1\u001b[39m]\n\u001b[1;32m 735\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m(new_other, frequency\u001b[38;5;241m=\u001b[39mother\u001b[38;5;241m.\u001b[39mfrequency\u001b[38;5;241m.\u001b[39msymbol)\n",
"File \u001b[0;32m~/Documents/projects/fincal/fincal/core.py:69\u001b[0m, in \u001b[0;36mdate_parser.<locals>.parse_dates.<locals>.wrapper_func\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 67\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 68\u001b[0m args[j] \u001b[38;5;241m=\u001b[39m parsed_date\n\u001b[0;32m---> 69\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/Documents/projects/fincal/fincal/core.py:408\u001b[0m, in \u001b[0;36mTimeSeriesCore.get\u001b[0;34m(self, date, default, closest)\u001b[0m\n\u001b[1;32m 406\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m item\n\u001b[1;32m 407\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m:\n\u001b[0;32m--> 408\u001b[0m date \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m delta\n",
"\u001b[0;31mOverflowError\u001b[0m: date value out of range"
]
}
],
"source": [
"rr"
"week_ts.sync(ts) "
]
},
{
"cell_type": "code",
"execution_count": 7 ,
"id": "4bad2efa ",
"execution_count": 1 ,
"id": "4755aea3-3655-4651-91d2-8e54c24303bc ",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Series([1.0, 2.0, 3.0, 4.0, 5.0])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"sr = Series([1, 2, 3, 4, 5], 'number')\n",
"sr"
"import fincal as fc"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "adceda69 ",
"id": "bd9887b3-d98a-4c80-8f95-ef7b7f19ded4",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['date', 'nav']\n",
"CPU times: user 56.9 ms, sys: 3.3 ms, total: 60.2 ms\n",
"Wall time: 60.2 ms\n"
]
}
],
"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')"
"%%time\n",
"ts = fc.read_csv('test_files/msft.csv', frequency='D')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "68cf9f8c",
"execution_count": 3 ,
"id": "b7c17 6d4-d 89f-4bda-9d67-75463eb9046 8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(datetime.datetime(2021, 2, 1, 0, 0), 0.045454545454545414)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"(datetime.datetime(2022, 2, 9, 0, 0), 311.209991)\n",
"(datetime.datetime(2022, 2, 10, 0, 0), 302.380005)\n",
"(datetime.datetime(2022, 2, 11, 0, 0), 295.040009)\n",
"(datetime.datetime(2022, 2, 14, 0, 0), 295.0)\n",
"(datetime.datetime(2022, 2, 15, 0, 0), 300.470001)\n",
"(datetime.datetime(2022, 2, 16, 0, 0), 299.5)\n",
"(datetime.datetime(2022, 2, 17, 0, 0), 290.730011)\n",
"(datetime.datetime(2022, 2, 18, 0, 0), 287.929993)\n"
]
}
],
"source": [
"ts.calculate_returns('2021-02-05', interval_type='months', interval_value=1, compounding=False)"
"for i in ts.tail(8):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 6 ,
"id": "a583347f ",
"execution_count": 4 ,
"id": "69c57754-a6fb-4881-9359-ba17c7fb8be5 ",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1.85 ms, sys: 143 µs, total: 1.99 ms\n",
"Wall time: 2 ms\n"
]
}
],
"source": [
"D = {'a': 1, 'b': 2}"
"%%time\n",
"ts['2022-02-12'] = 295"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "f79ac787",
"execution_count": 5 ,
"id": "7aa02023-406e-4700-801c-c06390ddf914 ",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 3.7 ms, sys: 121 µs, total: 3.82 ms\n",
"Wall time: 3.84 ms\n"
]
},
{
"data": {
"text/plain": [
"dict_keys(['a', 'b'])"
"{'start_date': datetime.datetime(1999, 12, 27, 0, 0),\n",
" 'end_date': datetime.datetime(2009, 3, 9, 0, 0),\n",
" 'drawdown': -0.7456453305351521}"
]
},
"execution_count": 10,
"execution_count": 5 ,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"D.keys()"
"%%time\n",
"ts.max_drawdown()"
]
}
],
@ -301,7 +382,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3 "
"version": "3.10.2 "
}
},
"nbformat": 4,