Skip to content

Commit

Permalink
Fix sorting for finance module #290 #286
Browse files Browse the repository at this point in the history
  • Loading branch information
mdancho84 committed Jul 16, 2024
1 parent e6506f3 commit 8ed9711
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/pytimetk/finance/atr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from pytimetk.utils.checks import check_dataframe_or_groupby, check_date_column, check_value_column
from pytimetk.utils.memory_helpers import reduce_memory_usage
from pytimetk.utils.pandas_helpers import sort_dataframe



Expand Down Expand Up @@ -146,6 +147,8 @@ def augment_atr(
check_dataframe_or_groupby(data)
check_value_column(data, close_column)
check_date_column(data, date_column)

data, idx_unsorted = sort_dataframe(data, date_column, keep_grouped_df = True)

if isinstance(periods, int):
periods = [periods]
Expand All @@ -168,6 +171,9 @@ def augment_atr(

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret

Expand Down
6 changes: 6 additions & 0 deletions src/pytimetk/finance/bbands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from pytimetk.utils.checks import check_dataframe_or_groupby, check_date_column, check_value_column
from pytimetk.utils.memory_helpers import reduce_memory_usage
from pytimetk.utils.pandas_helpers import sort_dataframe



Expand Down Expand Up @@ -139,6 +140,8 @@ def augment_bbands(
check_dataframe_or_groupby(data)
check_value_column(data, close_column)
check_date_column(data, date_column)

data, idx_unsorted = sort_dataframe(data, date_column, keep_grouped_df = True)

if isinstance(periods, int):
periods = [periods]
Expand Down Expand Up @@ -170,6 +173,9 @@ def augment_bbands(

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret

Expand Down
6 changes: 6 additions & 0 deletions src/pytimetk/finance/cmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from pytimetk.utils.checks import check_dataframe_or_groupby, check_date_column, check_value_column
from pytimetk.utils.memory_helpers import reduce_memory_usage
from pytimetk.utils.pandas_helpers import sort_dataframe


@pf.register_dataframe_method
Expand Down Expand Up @@ -154,6 +155,8 @@ def augment_cmo(
check_dataframe_or_groupby(data)
check_value_column(data, close_column)
check_date_column(data, date_column)

data, idx_unsorted = sort_dataframe(data, date_column, keep_grouped_df = True)

if isinstance(periods, int):
periods = [periods] # Convert to a list with a single value
Expand All @@ -174,6 +177,9 @@ def augment_cmo(

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret

Expand Down
6 changes: 6 additions & 0 deletions src/pytimetk/finance/macd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pytimetk.utils.checks import check_dataframe_or_groupby, check_date_column, check_value_column
from pytimetk.utils.memory_helpers import reduce_memory_usage
from pytimetk.utils.pandas_helpers import sort_dataframe


@pf.register_dataframe_method
Expand Down Expand Up @@ -128,6 +129,8 @@ def augment_macd(

if reduce_memory:
data = reduce_memory_usage(data)

data, idx_unsorted = sort_dataframe(data, date_column, keep_grouped_df = True)

if engine == 'pandas':
ret = _augment_macd_pandas(data, date_column, close_column, fast_period, slow_period, signal_period)
Expand All @@ -138,6 +141,9 @@ def augment_macd(

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret

Expand Down
6 changes: 6 additions & 0 deletions src/pytimetk/finance/ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pytimetk.utils.checks import check_dataframe_or_groupby, check_date_column, check_value_column
from pytimetk.utils.memory_helpers import reduce_memory_usage
from pytimetk.utils.pandas_helpers import sort_dataframe


@pf.register_dataframe_method
Expand Down Expand Up @@ -118,6 +119,8 @@ def augment_ppo(

if reduce_memory:
data = reduce_memory_usage(data)

data, idx_unsorted = sort_dataframe(data, date_column, keep_grouped_df = True)

if engine == 'pandas':
ret = _augment_ppo_pandas(data, date_column, close_column, fast_period, slow_period)
Expand All @@ -128,6 +131,9 @@ def augment_ppo(

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret

Expand Down
7 changes: 6 additions & 1 deletion src/pytimetk/finance/qsmomentum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from pytimetk.utils.checks import check_dataframe_or_groupby, check_date_column, check_value_column
from pytimetk.utils.memory_helpers import reduce_memory_usage

from pytimetk.utils.pandas_helpers import sort_dataframe


@pf.register_dataframe_method
Expand Down Expand Up @@ -125,6 +125,8 @@ def augment_qsmomentum(
check_dataframe_or_groupby(data)
check_value_column(data, close_column)
check_date_column(data, date_column)

data, idx_unsorted = sort_dataframe(data, date_column, keep_grouped_df = True)

# Check if roc_fast_period lists, tuples or integers
if isinstance(roc_fast_period, int):
Expand Down Expand Up @@ -184,6 +186,9 @@ def f(close):

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret

Expand Down
7 changes: 6 additions & 1 deletion src/pytimetk/finance/roc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pytimetk.utils.checks import check_dataframe_or_groupby, check_date_column, check_value_column
from pytimetk.utils.memory_helpers import reduce_memory_usage

from pytimetk.utils.pandas_helpers import sort_dataframe


@pf.register_dataframe_method
Expand Down Expand Up @@ -125,6 +125,8 @@ def augment_roc(
check_value_column(data, close_column)
check_date_column(data, date_column)

data, idx_unsorted = sort_dataframe(data, date_column, keep_grouped_df = True)

# Check start_index > periods
if start_index >= min(periods):
raise ValueError("start_index must be less than the minimum value in periods.")
Expand Down Expand Up @@ -154,6 +156,9 @@ def augment_roc(

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret

Expand Down
6 changes: 6 additions & 0 deletions src/pytimetk/finance/rsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from pytimetk.utils.checks import check_dataframe_or_groupby, check_date_column, check_value_column
from pytimetk.utils.memory_helpers import reduce_memory_usage
from pytimetk.utils.pandas_helpers import sort_dataframe


@pf.register_dataframe_method
Expand Down Expand Up @@ -137,6 +138,8 @@ def augment_rsi(
check_value_column(data, close_column)
check_date_column(data, date_column)

data, idx_unsorted = sort_dataframe(data, date_column, keep_grouped_df = True)

if isinstance(close_column, str):
close_column = [close_column]

Expand All @@ -159,6 +162,9 @@ def augment_rsi(

if reduce_memory:
ret = reduce_memory_usage(ret)

ret.index = idx_unsorted
ret = ret.sort_index()

return ret

Expand Down

0 comments on commit 8ed9711

Please sign in to comment.