Skip to content

Time manip

time_manip

time_manip = TimeManip()

A TimeManip object instance Usage:

from tradingtoolbox.utils.time_manip import time_manip

days = time_manip.days_ago(3)
print(days)

>>> datetime.datetime(2024, 9, 25, 23, 54, 58, 71997)

TimeManip

convert_datetime_to_ms

convert_datetime_to_ms(df: DataFrame)

Parameters:

Name Type Description Default
df DataFrame

The dataframe to operate onw

required
Source code in src/tradingtoolbox/utils/time_manip.py
def convert_datetime_to_ms(self, df: pd.DataFrame):
    """
    Parameters:
        df: The dataframe to operate onw
    """
    return pd.to_datetime(df).astype(np.int64) // 10**6

convert_datetime_to_s

convert_datetime_to_s(df: DataFrame)

Parameters:

Name Type Description Default
df DataFrame

The dataframe to operate onw

required
Source code in src/tradingtoolbox/utils/time_manip.py
def convert_datetime_to_s(self, df: pd.DataFrame):
    """
    Parameters:
        df: The dataframe to operate onw
    """
    return pd.to_datetime(df).astype(np.int64) // 10**9

convert_duration_to_timestamp

convert_duration_to_timestamp(df: DataFrame, unit='ms')

Parameters:

Name Type Description Default
df DataFrame

The dataframe to operate onw

required
Source code in src/tradingtoolbox/utils/time_manip.py
def convert_duration_to_timestamp(self, df: pd.DataFrame, unit="ms"):
    """
    Parameters:
        df: The dataframe to operate onw
    """
    return pd.to_timedelta(df, unit=unit)

convert_ms_to_datetime

convert_ms_to_datetime(df: DataFrame)

Parameters:

Name Type Description Default
df DataFrame

The dataframe to operate onw

required
Source code in src/tradingtoolbox/utils/time_manip.py
def convert_ms_to_datetime(self, df: pd.DataFrame):
    """
    Parameters:
        df: The dataframe to operate onw
    """
    return pd.to_datetime(df, unit="ms")

convert_s_to_datetime

convert_s_to_datetime(df: DataFrame)

Parameters:

Name Type Description Default
df DataFrame

The dataframe to operate onw

required
Source code in src/tradingtoolbox/utils/time_manip.py
def convert_s_to_datetime(self, df: pd.DataFrame):
    """
    Parameters:
        df: The dataframe to operate onw
    """
    return pd.to_datetime(df, unit="s")

days_ago

days_ago(days=0, to_timestamp=False)

Return X days ago

Parameters:

Name Type Description Default
days

The number of days ago you want to return

0
to_timestamp

weather to return a timestamp or not

False
Source code in src/tradingtoolbox/utils/time_manip.py
def days_ago(self, days=0, to_timestamp=False):
    """
    Return X days ago

    Parameters:
        days: The number of days ago you want to return
        to_timestamp: weather to return a timestamp or not
    """
    today = datetime.now()
    date = today - timedelta(days=days)
    if to_timestamp:
        return date.timestamp()
    return date

hours_ago

hours_ago(hours=0, to_timestamp=False)

Return X hours ago

Parameters:

Name Type Description Default
hours

The number of hours ago you want to return

0
to_timestamp

weather to return a timestamp or not

False
Source code in src/tradingtoolbox/utils/time_manip.py
def hours_ago(self, hours=0, to_timestamp=False):
    """
    Return X hours ago

    Parameters:
        hours: The number of hours ago you want to return
        to_timestamp: weather to return a timestamp or not
    """
    today = datetime.now(UTC)
    date = today - timedelta(hours=hours)
    if to_timestamp:
        return date.timestamp()
    return date

minutes_ago

minutes_ago(minutes=0, to_timestamp=False)

Return X hours ago

Parameters:

Name Type Description Default
hours

The number of hours ago you want to return

required
to_timestamp

weather to return a timestamp or not

False
Source code in src/tradingtoolbox/utils/time_manip.py
def minutes_ago(self, minutes=0, to_timestamp=False):
    """
    Return X hours ago

    Parameters:
        hours: The number of hours ago you want to return
        to_timestamp: weather to return a timestamp or not
    """
    today = datetime.now(UTC)
    date = today - timedelta(minutes=minutes)
    if to_timestamp:
        return date.timestamp()
    return date

months_ago

months_ago(months=0, to_timestamp=False)

Return X months ago

Parameters:

Name Type Description Default
months

The number of months ago you want to return

0
to_timestamp

weather to return a timestamp or not

False
Source code in src/tradingtoolbox/utils/time_manip.py
def months_ago(self, months=0, to_timestamp=False):
    """
    Return X months ago

    Parameters:
        months: The number of months ago you want to return
        to_timestamp: weather to return a timestamp or not
    """
    today = datetime.now()
    date = today - relativedelta(months=months)
    if to_timestamp:
        return date.timestamp()
    return date

pd_days_ago

pd_days_ago(df: DataFrame, days=0)

Parameters:

Name Type Description Default
df DataFrame

The dataframe to operate onw

required
days

The number of days ago

0
Source code in src/tradingtoolbox/utils/time_manip.py
def pd_days_ago(self, df: pd.DataFrame, days=0):
    """
    Parameters:
        df: The dataframe to operate onw
        days: The number of days ago
    """
    today = df.index.values[-1]
    months_ago = today - pd.DateOffset(days=days)

    return df[df.index >= months_ago]

pd_hours_ago

pd_hours_ago(df: DataFrame, hours=0)

Parameters:

Name Type Description Default
df DataFrame

The dataframe to operate onw

required
hours

The number of hours ago

0
Source code in src/tradingtoolbox/utils/time_manip.py
def pd_hours_ago(self, df: pd.DataFrame, hours=0):
    """
    Parameters:
        df: The dataframe to operate onw
        hours: The number of hours ago
    """
    today = df.index.values[-1]
    hours_ago = today - pd.DateOffset(hours=hours)

    return df[df.index >= hours_ago]

pd_months_ago

pd_months_ago(df: DataFrame, months=0)

Parameters:

Name Type Description Default
df DataFrame

The dataframe to operate onw

required
months

The number of months ago

0
Source code in src/tradingtoolbox/utils/time_manip.py
def pd_months_ago(self, df: pd.DataFrame, months=0):
    """
    Parameters:
        df: The dataframe to operate onw
        months: The number of months ago
    """
    today = df.index.values[-1]
    months_ago = today - pd.DateOffset(months=months)

    return df[df.index >= months_ago]