Compare commits

..

No commits in common. "e9bb795ecf052d6eba2cad2950b9e633f19bb48b" and "c713e3283b9af02c90cb20196baadf69c7a67f8a" have entirely different histories.

4 changed files with 20 additions and 52 deletions

View File

@ -140,7 +140,7 @@ class TimeSeries(TimeSeriesCore):
self, self,
data: List[Iterable] | Mapping, data: List[Iterable] | Mapping,
frequency: Literal["D", "W", "M", "Q", "H", "Y"] = None, frequency: Literal["D", "W", "M", "Q", "H", "Y"] = None,
validate_frequency: bool = True, validate_frequency: bool = False,
date_format: str = "%Y-%m-%d", date_format: str = "%Y-%m-%d",
): ):
"""Instantiate a TimeSeriesCore object""" """Instantiate a TimeSeriesCore object"""
@ -448,7 +448,7 @@ class TimeSeries(TimeSeriesCore):
) )
rolling_returns.append(returns) rolling_returns.append(returns)
rolling_returns.sort() rolling_returns.sort()
return self.__class__(rolling_returns, frequency.symbol) return self.__class__(rolling_returns, self.frequency.symbol)
@date_parser(1, 2) @date_parser(1, 2)
def volatility( def volatility(

View File

@ -1,5 +1,3 @@
from __future__ import annotations
import datetime import datetime
import statistics import statistics
from typing import Literal from typing import Literal

View File

@ -485,7 +485,7 @@
"source": [ "source": [
"import random\n", "import random\n",
"import math\n", "import math\n",
"import pyfacts as pft\n", "import fincal as fc\n",
"from typing import List\n", "from typing import List\n",
"import datetime\n", "import datetime\n",
"from dateutil.relativedelta import relativedelta" "from dateutil.relativedelta import relativedelta"
@ -536,7 +536,7 @@
"\n", "\n",
"\n", "\n",
"def sample_data_generator(\n", "def sample_data_generator(\n",
" frequency: pft.Frequency,\n", " frequency: fc.Frequency,\n",
" num: int = 1000,\n", " num: int = 1000,\n",
" skip_weekends: bool = False,\n", " skip_weekends: bool = False,\n",
" mu: float = 0.1,\n", " mu: float = 0.1,\n",
@ -571,11 +571,11 @@
" start_date = datetime.datetime(2017, 1, 1)\n", " start_date = datetime.datetime(2017, 1, 1)\n",
" timedelta_dict = {\n", " timedelta_dict = {\n",
" frequency.freq_type: int(\n", " frequency.freq_type: int(\n",
" frequency.value * num * (7 / 5 if frequency == pft.AllFrequencies.D and skip_weekends else 1)\n", " frequency.value * num * (7 / 5 if frequency == fc.AllFrequencies.D and skip_weekends else 1)\n",
" )\n", " )\n",
" }\n", " }\n",
" end_date = start_date + relativedelta(**timedelta_dict)\n", " end_date = start_date + relativedelta(**timedelta_dict)\n",
" dates = pft.create_date_series(start_date, end_date, frequency.symbol, skip_weekends=skip_weekends, eomonth=eomonth)\n", " dates = fc.create_date_series(start_date, end_date, frequency.symbol, skip_weekends=skip_weekends, eomonth=eomonth)\n",
" values = create_prices(1000, mu, sigma, num)\n", " values = create_prices(1000, mu, sigma, num)\n",
" ts = list(zip(dates, values))\n", " ts = list(zip(dates, values))\n",
" return ts\n" " return ts\n"
@ -583,36 +583,40 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 12,
"id": "c85b5dd9-9a88-4608-ac58-1a141295f63f", "id": "c85b5dd9-9a88-4608-ac58-1a141295f63f",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"market_data = sample_data_generator(num=3600, frequency=pft.AllFrequencies.D)\n", "data = sample_data_generator(num=261, frequency=fc.AllFrequencies.W)\n",
"mts = pft.TimeSeries(market_data, \"D\")\n", "ts = fc.TimeSeries(data, \"W\")"
"stock_data = sample_data_generator(num=3600, frequency=pft.AllFrequencies.D, mu=0.12, sigma=0.05)\n",
"sts = pft.TimeSeries(stock_data, 'D')"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 13,
"id": "0488a4d0-bca1-4341-9fae-1fd254adc0dc", "id": "0488a4d0-bca1-4341-9fae-1fd254adc0dc",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"1.020217253491451" "TimeSeries([(datetime.datetime(2017, 1, 1, 0, 0), 1003.03),\n",
"\t (datetime.datetime(2017, 1, 8, 0, 0), 1002.71),\n",
"\t (datetime.datetime(2017, 1, 15, 0, 0), 1008.77)\n",
"\t ...\n",
"\t (datetime.datetime(2021, 12, 12, 0, 0), 1107.21),\n",
"\t (datetime.datetime(2021, 12, 19, 0, 0), 1106.66),\n",
"\t (datetime.datetime(2021, 12, 26, 0, 0), 1104.32)], frequency='W')"
] ]
}, },
"execution_count": 8, "execution_count": 13,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"pft.beta(sts, mts)" "ts"
] ]
}, },
{ {
@ -704,7 +708,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.10.4" "version": "3.10.2"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -82,37 +82,3 @@ class TestSharpe:
return_period_value=12, return_period_value=12,
) )
assert round(sharpe_ratio, 4) == 0.3199 assert round(sharpe_ratio, 4) == 0.3199
class TestBeta:
def test_beta_daily_freq(self, create_test_data):
market_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D)
stock_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D, mu=0.12, sigma=0.08)
sts = pft.TimeSeries(stock_data, "D")
mts = pft.TimeSeries(market_data, "D")
beta = pft.beta(sts, mts, frequency="D", return_period_unit="days", return_period_value=1)
assert round(beta, 4) == 1.6001
def test_beta_daily_freq_daily_returns(self, create_test_data):
market_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D)
stock_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D, mu=0.12, sigma=0.08)
sts = pft.TimeSeries(stock_data, "D")
mts = pft.TimeSeries(market_data, "D")
beta = pft.beta(sts, mts)
assert round(beta, 4) == 1.6292
def test_beta_monthly_freq(self, create_test_data):
market_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D)
stock_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D, mu=0.12, sigma=0.08)
sts = pft.TimeSeries(stock_data, "D")
mts = pft.TimeSeries(market_data, "D")
beta = pft.beta(sts, mts, frequency="M")
assert round(beta, 4) == 1.629
def test_beta_monthly_freq_monthly_returns(self, create_test_data):
market_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D)
stock_data = create_test_data(num=3600, frequency=pft.AllFrequencies.D, mu=0.12, sigma=0.08)
sts = pft.TimeSeries(stock_data, "D")
mts = pft.TimeSeries(market_data, "D")
beta = pft.beta(sts, mts, frequency="M", return_period_unit="months", return_period_value=1)
assert round(beta, 4) == 1.6023