コード例 #1
0
ファイル: test_utils.py プロジェクト: win2cs/zipline
def make_commodity_future_info(first_sid,
                               root_symbols,
                               years,
                               month_codes=None):
    """
    Make futures testing data that simulates the notice/expiration date
    behavior of physical commodities like oil.

    Parameters
    ----------
    first_sid : int
    root_symbols : list[str]
    years : list[int]
    month_codes : dict[str -> int]

    Expiration dates are on the 20th of the month prior to the month code.
    Notice dates are are on the 20th two months prior to the month code.
    Start dates are one year before the contract month.

    See Also
    --------
    make_future_info
    """
    nineteen_days = pd.Timedelta(days=19)
    one_year = pd.Timedelta(days=365)
    return make_future_info(
        first_sid=first_sid,
        root_symbols=root_symbols,
        years=years,
        notice_date_func=lambda dt: dt - MonthBegin(2) + nineteen_days,
        expiration_date_func=lambda dt: dt - MonthBegin(1) + nineteen_days,
        start_date_func=lambda dt: dt - one_year,
        month_codes=month_codes,
    )
コード例 #2
0
ファイル: base.py プロジェクト: JamesAltemus/Fully_Finance
def _reduce(price_data, period, term, region):
    calendar = build_trading_calendar(region)
    
    if term == 'start':
        if period == 'weekly':
            pmap = price_data.index.dayofweek
            price_data[pmap == 0]
        if period == 'monthly':
            calendar = CustomBusinessMonthBegin(calendar=calendar)
            
            st = price_data.index[0] + MonthBegin()
            end = price_data.index[-1] + MonthBegin()
            pmap = pd.date_range(st,end,freq = calendar)
            price_data['pmap'] = [1 if idx in pmap else 0 for idx in price_data.index]
            price_data = price_data[price_data['pmap'] == 1].drop('pmap', axis = 1)
    
    if term == 'end':
        if period == 'weekly':
            pmap = price_data.index.dayofweek
            price_data[pmap == 4]
        if period == 'monthly':
            calendar = CustomBusinessMonthEnd(calendar=calendar)
            
            st = price_data.index[0] + MonthEnd()
            end = price_data.index[-1] + MonthEnd()
            pmap = pd.date_range(st,end,freq = calendar)
            price_data['pmap'] = [1 if idx in pmap else 0 for idx in price_data.index]
            price_data = price_data[price_data['pmap'] == 1].drop('pmap', axis = 1)
    
    return price_data
コード例 #3
0
ファイル: utils.py プロジェクト: jingmouren/cnswd
def time_for_next_update(last_time, period='D', hour=9):
    """计算下次更新时间
    说明:
        'D':移动到下一天
        'W':移动到下周一
        'M':移动到下月第一天
        'Q':下一季度的第一天
        将小时调整到指定的hour
    """
    if pd.isnull(last_time):
        return MARKET_START
    period = period.upper()
    if period == 'D':
        d = BDay(normalize=True)
        return d.apply(last_time).replace(hour=hour)
    elif period == 'W':
        w = Week(normalize=True, weekday=0)
        return w.apply(last_time).replace(hour=hour)
    elif period == 'M':
        m = MonthBegin(normalize=True)
        return m.apply(last_time).replace(hour=hour)
    elif period == 'Q':
        q = QuarterBegin(normalize=True)
        return q.apply(last_time).replace(hour=hour)
    else:
        raise TypeError('不能识别的周期类型,仅接受{}'.format(('D', 'W', 'M', 'Q')))
コード例 #4
0
class Month:
    def __init__(self):
        last_month = datetime.date.today() - DateOffset(months=1)
        # 25号(不含)之后运行,月份为当月,1号至25号(含)运行,月份为上个月
        self.date = MonthBegin().rollforward(
            last_month) if last_month.day > 25 else MonthBegin().rollback(
                last_month)
        self.month = self.date.month
        self.year = self.date.year
        self.str = self.date.strftime("%Y%m%d")

    def before(self, i):
        """i个月之前"""
        m = Month()
        m.date = self.date - DateOffset(months=i)
        m.month = m.date.month
        m.year = m.date.year
        m.str = m.date.strftime("%Y%m%d")
        return m

    def str_format(self, format):
        """变更日期文字样式"""
        self.str = self.date.strftime(format)

    def __repr__(self):
        return f'<class Month: {self.str}>'
コード例 #5
0
 def __init__(self):
     last_month = datetime.date.today() - DateOffset(months=1)
     # 25号(不含)之后运行,月份为当月,1号至25号(含)运行,月份为上个月
     self.date = MonthBegin().rollforward(
         last_month) if last_month.day > 25 else MonthBegin().rollback(
             last_month)
     self.month = self.date.month
     self.year = self.date.year
コード例 #6
0
class TestMonthBegin(Base):
    _offset = MonthBegin

    offset_cases = []
    # NOTE: I'm not entirely happy with the logic here for Begin -ss
    # see thread 'offset conventions' on the ML
    offset_cases.append((
        MonthBegin(),
        {
            datetime(2008, 1, 31): datetime(2008, 2, 1),
            datetime(2008, 2, 1): datetime(2008, 3, 1),
            datetime(2006, 12, 31): datetime(2007, 1, 1),
            datetime(2006, 12, 1): datetime(2007, 1, 1),
            datetime(2007, 1, 31): datetime(2007, 2, 1),
        },
    ))

    offset_cases.append((
        MonthBegin(0),
        {
            datetime(2008, 1, 31): datetime(2008, 2, 1),
            datetime(2008, 1, 1): datetime(2008, 1, 1),
            datetime(2006, 12, 3): datetime(2007, 1, 1),
            datetime(2007, 1, 31): datetime(2007, 2, 1),
        },
    ))

    offset_cases.append((
        MonthBegin(2),
        {
            datetime(2008, 2, 29): datetime(2008, 4, 1),
            datetime(2008, 1, 31): datetime(2008, 3, 1),
            datetime(2006, 12, 31): datetime(2007, 2, 1),
            datetime(2007, 12, 28): datetime(2008, 2, 1),
            datetime(2007, 1, 1): datetime(2007, 3, 1),
            datetime(2006, 11, 1): datetime(2007, 1, 1),
        },
    ))

    offset_cases.append((
        MonthBegin(-1),
        {
            datetime(2007, 1, 1): datetime(2006, 12, 1),
            datetime(2008, 5, 31): datetime(2008, 5, 1),
            datetime(2008, 12, 31): datetime(2008, 12, 1),
            datetime(2006, 12, 29): datetime(2006, 12, 1),
            datetime(2006, 1, 2): datetime(2006, 1, 1),
        },
    ))

    @pytest.mark.parametrize("case", offset_cases)
    def test_offset(self, case):
        offset, cases = case
        for base, expected in cases.items():
            assert_offset_equal(offset, base, expected)
コード例 #7
0
ファイル: performance.py プロジェクト: fagan2888/Bigfish
 def max_drawdown(self):
     # TODO o(nk)算法,k较大时可以利用分治做成o(nlog(n)),月分析K最大为12
     columns = ['high', 'low', 'max_drawdown']
     calculator = partial(
         reduce, lambda u, v:
         [max(u[0], v[0]),
          min(u[1], v[1]),
          min(u[2], v[2], v[1] - u[0])])
     ts = (lambda x: x.groupby(MonthBegin().rollback).apply(
         lambda df: pd.Series(calculator(df.values), index=columns)))(
             (lambda x, y, z: pd.DataFrame(
                 {
                     'high': np.maximum(y, z),
                     'low': np.minimum(y, z),
                     'max_drawdown': x
                 },
                 columns=columns))(
                     *(lambda x, y: (x, y, y.shift(1).fillna(0)))(*(
                         lambda x: (x['rate'] * (x['rate'] < 0), x['rate'].
                                    cumsum()))(self._rate_of_return['D']))))
     result = DataFrameExtended([], index=ts.index.rename('time'))
     for key, value in self._column_names['M'].items():
         result[value[0]] = -(rolling_apply_2d(
             ts, key,
             calculator)['max_drawdown'].apply(_get_percent_from_log))
     result.total = -_get_percent_from_log(calculator(ts.values)[2])
     return _deal_float_error(result)
コード例 #8
0
def _get_days_in_months(start_date, end_date, n_months, list_yr_mo):
    """
    Internaty function for computing the number of days in a list of months.
    Used with _expand_row().

    Parameters:
    ----------
    start_date: datetime object
        start date of a bill
    end_date: datetime object
        end date of a bill
    n_months: int
        number of months in list_yr_mo
    list_yr_mo: list
        list of yr_mo in the format of 'YYYY-mm'

    Return:
    -------
    numpy array
        array of number of days in each calendar month
    """
    if n_months == 1:
        days_in_months = np.array([(end_date - start_date).days])
    else:
        days_in_month_1 = ((start_date + MonthEnd()) - start_date).days
        days_in_month_n = (end_date - (end_date - MonthBegin())).days + 1
        days_in_months = [days_in_month_1]
        for month in list_yr_mo[1:-1]:
            Y, m = list(map(int, month.split("-")))
            days_in_months.append(calendar.monthrange(Y, m)[1])
        days_in_months.append(days_in_month_n)
    return np.array(days_in_months)
コード例 #9
0
ファイル: cache.py プロジェクト: zhangshoug/cswd
def next_update_time(last_updated, freq='D', hour=18, minute=0):
    """计算下次更新时间
    说明:
        'D':移动到下一天
        'W':移动到下周一
        'M':移动到下月第一天
        'Q':下一季度的第一天
        将时间调整到指定的hour和minute
    """
    if pd.isnull(last_updated):
        return MARKET_START
    freq = freq.upper()
    if freq == 'D':
        d = BDay(n=1, normalize=True)
        res = last_updated + d
        return res.replace(hour=hour, minute=minute)
    elif freq == 'W':
        w = Week(normalize=True, weekday=0)
        res = last_updated + w
        return res.replace(hour=hour, minute=minute)
    elif freq == 'M':
        m = MonthBegin(n=1, normalize=True)
        res = last_updated + m
        return res.replace(hour=hour, minute=minute)
    elif freq == 'Q':
        q = QuarterBegin(normalize=True, startingMonth=1)
        res = last_updated + q
        return res.replace(hour=hour, minute=minute)
    else:
        raise TypeError('不能识别的周期类型,仅接受{}'.format(('D', 'W', 'M', 'Q')))
コード例 #10
0
def get_month_lag(df, row):
    lag_date = row['contract_start_year_month'] - 1 * MonthBegin()
    mask = (lag_date == df.loc[:, 'contract_start_year_month']) & (
        row['product_type'] == df.loc[:, 'product_type']) & (
            row['rental_type'] == df.loc[:, 'rental_type'])
    lag_df = df.loc[mask, :]
    return lag_df
コード例 #11
0
def mktimerange(
        time_resolution: TimeResolution,
        date_from: Union[datetime, str],
        date_to: Union[datetime, str] = None) -> Tuple[Timestamp, Timestamp]:
    """
    Compute appropriate time ranges for monthly and annual time resolutions.
    This takes into account to properly floor/ceil the date_from/date_to
    values to respective "begin of month/year" and "end of month/year" values.

    Args:
        time_resolution: time resolution as enumeration
        date_from: datetime string or object
        date_to: datetime string or object

    Returns:
        Tuple of two Timestamps: "date_from" and "date_to"
    """

    if date_to is None:
        date_to = date_from

    if time_resolution == TimeResolution.ANNUAL:
        date_from = pd.to_datetime(date_from) - YearBegin(1)
        date_to = pd.to_datetime(date_to) + YearEnd(1)

    elif time_resolution == TimeResolution.MONTHLY:
        date_from = pd.to_datetime(date_from) - MonthBegin(1)
        date_to = pd.to_datetime(date_to) + MonthEnd(1)

    else:
        raise NotImplementedError(
            "mktimerange only implemented for annual and monthly time ranges")

    return date_from, date_to
コード例 #12
0
    def processMonthColumn(self, rowList, row, index, columnName,
                           populateStartDate):
        monthStr = row[columnName]
        if (isinstance(monthStr, string_types)):
            monthStr = monthStr.strip(
            )  # Remove leading and trailing whitespaces

        monthBegin = ''
        monthEnd = ''
        if (isEmpty(monthStr)):
            logging.warning('%s of row %s is empty ', columnName, index)
        else:
            monthStr = re.sub('-M', '', monthStr)
            monthStr = re.sub(' \(.*\)', '', monthStr)
            monthEnd = pandas.to_datetime(monthStr, format="%Y%m") + MonthEnd(
                0)  # get the end date of the month
            monthEnd = re.sub(' 00:00:00', '',
                              str(monthEnd))  # remove time part from the date

            if (populateStartDate):
                monthBegin = pandas.to_datetime(
                    monthStr, format="%Y%m") + MonthBegin(
                        0)  # get the start date of the month
                monthBegin = re.sub(
                    ' 00:00:00', '',
                    str(monthBegin))  # remove time part from the date
                rowList.append(monthBegin)

            rowList.append(monthEnd)
コード例 #13
0
 def monthly_hit_ratio(self, returns):
     returns[returns > 0] = 1
     returns[returns < 0] = 0
     returns = pd.DataFrame(returns)
     group = returns.resample("M").sum()
     group['length'] = group.index.map(
         lambda x: len(pd.date_range(x - MonthBegin(), x, freq='B')))
     return group["Close"] / group["length"] * 100
コード例 #14
0
def process_and_calculate():
    global size, BM
    size = data1['size_tot'].unstack()
    book = (data2['tot_assets'] - data2['tot_liab']).unstack()
    book.index = book.index.astype(str).to_datetime('%Y%m%d')
    book.index.name = 'trddt'
    # data.to_pickle('./five_factor/book')
    book = book.resample('D').first().ffill()
    book.index = book.index + MonthBegin()
コード例 #15
0
ファイル: report.py プロジェクト: sjmf/reportgen
def get_month_range(df):
    from pandas.tseries.offsets import MonthEnd, MonthBegin

    return [
        (m + MonthBegin(), m + MonthEnd() + pd.Timedelta('23:59:59'))
        for m in list(
            df.groupby(pd.TimeGrouper(freq='M', closed='left',
                                      label='left')).groups)
    ]
コード例 #16
0
ファイル: test_offsets.py プロジェクト: ukarroum/pandas
def test_validate_n_error():
    with pytest.raises(TypeError, match="argument must be an integer"):
        DateOffset(n="Doh!")

    with pytest.raises(TypeError, match="argument must be an integer"):
        MonthBegin(n=timedelta(1))

    with pytest.raises(TypeError, match="argument must be an integer"):
        BDay(n=np.array([1, 2], dtype=np.int64))
コード例 #17
0
def make_commodity_future_info(first_sid,
                               root_symbols,
                               years,
                               month_codes=None,
                               multiplier=500):
    """
    Make futures testing data that simulates the notice/expiration date
    behavior of physical commodities like oil.

    Parameters
    ----------
    first_sid : int
        The first sid to use for assigning sids to the created contracts.
    root_symbols : list[str]
        A list of root symbols for which to create futures.
    years : list[int or str]
        Years (e.g. 2014), for which to produce individual contracts.
    month_codes : dict[str -> [1..12]], optional
        Dictionary of month codes for which to create contracts.  Entries
        should be strings mapped to values from 1 (January) to 12 (December).
        Default is zipline.futures.CMES_CODE_TO_MONTH
    multiplier : int
        The contract multiplier.

    Expiration dates are on the 20th of the month prior to the month code.
    Notice dates are are on the 20th two months prior to the month code.
    Start dates are one year before the contract month.

    See Also
    --------
    make_future_info
    """
    nineteen_days = pd.Timedelta(days=19)
    one_year = pd.Timedelta(days=365)
    return make_future_info(
        first_sid=first_sid,
        root_symbols=root_symbols,
        years=years,
        notice_date_func=lambda dt: dt - MonthBegin(2) + nineteen_days,
        expiration_date_func=lambda dt: dt - MonthBegin(1) + nineteen_days,
        start_date_func=lambda dt: dt - one_year,
        month_codes=month_codes,
        multiplier=multiplier,
    )
コード例 #18
0
 def ls_returns_sheet(self, cur_day=None):
     if cur_day is None:
         cur_day = pd.to_datetime(
             data_source.trade_calendar.get_latest_trade_days(
                 datetime.today().strftime("%Y%m%d")))
     else:
         cur_day = pd.to_datetime(cur_day)
     dates = [
         cur_day,
         cur_day.to_period('W').start_time, cur_day + MonthBegin(-1),
         cur_day + QuarterBegin(-1), cur_day + MonthBegin(-6),
         cur_day + YearBegin(-1), cur_day + YearBegin(-2)
     ]
     returns = list(map(lambda x: self.ls_range_pct(x, cur_day), dates)) + \
               [self.ls_annual_return, self.ls_total_return]
     return pd.DataFrame([returns],
                         columns=[
                             '日回报', '本周以来', '本月以来', '本季以来', '近6个月', '今年以来',
                             '近两年', '年化回报', '成立以来'
                         ])
コード例 #19
0
def cal_SMB_HML_FF(ret, EndDate, size=None, book=None, weights=None):
    # TODO return要和EndDate的频率保持一致
    percentile1 = [0.0, 0.5, 1.0]  # size
    percentile2 = [0.0, 0.3, 0.7, 1.0]  # value
    label_1 = [i + 1 for i in range(len(percentile1) - 1)]
    label_2 = [i + 1 for i in range(len(percentile2) - 1)]
    size, book = import_data(PV_vars=['size_tot'],
                             BS_vars=['tot_shrhldr_eqy_excl_min_int'])[:2]
    BE = book.drop(book.index[book.index.duplicated(keep='last')]).unstack()
    BE = BE[BE.index.month == 12]
    BE = BE[BE > 0]
    size = size.unstack()
    ME = size.copy()
    ME = ME.resample('M').last()
    ME6 = ME[ME.index.month == 6]
    ME12 = ME[ME.index.month == 12]
    ME12.loc[parse('20041231')] = size.loc['2005-01-04']
    ME12 = ME12.sort_index()
    BM = BE.reindex(index=ME12.index, columns=ME12.columns) / ME12
    mark_1 = DataFrame(
        [qcut(ME6.loc[i], q=percentile1, labels=label_1) for i in ME6.index])
    mark_1.index = mark_1.index + Day()
    mark_1 = mark_1.resample('D').ffill().reindex(index=EndDate)
    mark_2 = DataFrame(
        [qcut(BM.loc[i], q=percentile2, labels=label_2) for i in BM.index])
    mark_2.index = mark_2.index + MonthBegin(7)
    mark_2 = mark_2.resample('D').ffill().reindex(index=EndDate)

    if weights is None:
        df = DataFrame()
        df['ret'] = ret.stack()
        df['ref1'] = mark_1.stack()
        df['ref2'] = mark_2.stack()
        df = df.dropna()
        tmp = df.groupby(level=0).apply(
            lambda g: g.groupby(['ref1', 'ref2']).mean())['ret'].unstack()
    else:
        weights = size.resample('D').ffill().reindex(index=EndDate).shift(1)
        df = DataFrame()
        df['ret'] = (ret * weights).stack()
        df['ref1'] = mark_1.stack()
        df['ref2'] = mark_2.stack()
        df['w'] = weights.stack()
        df = df.dropna()
        tmp1 = df.groupby(
            level=0).apply(lambda g: g.groupby(['ref1', 'ref2']).sum())['ret']
        tmp2 = df.groupby(
            level=0).apply(lambda g: g.groupby(['ref1', 'ref2']).sum())['w']
        tmp = (tmp1 / tmp2).unstack()
    rHML = tmp.mean(axis=0, level=0)
    rSMB = tmp.mean(axis=1).unstack()
    return rSMB.iloc[:, -1] - rSMB.iloc[:, 0], rHML.iloc[:, -1] - rHML.iloc[:,
                                                                            0]
コード例 #20
0
def clearPositionByDeliveryDay(positions,
                               nextTradeDate,
                               context,
                               moveOrderLog=None):
    # latestTradeDate 表示最近已经完成的交易日。比如20191107 11:00,那么此时20191107交易日没有完成,已经完成的最近的是20191106
    # moveOrderLog  这玩意 本来是为了将所有的移仓的动作都写文本,然后等待开盘时间到了后,从文本读取品种直接下单。但是后来发现
    # 总不是从内存写到本地,然后到时候又要从本地读到内存,那我干脆直接存到内存算了

    #本函数一定是在交易时间内运行,即在判为,查下下个交易日是不是要到交割月了。是的话,就平仓了。

    # 先查持仓

    if len(positions) <= 0:
        return

    from pandas.tseries.offsets import MonthBegin
    symbolList = []
    dt = context.now.strftime('%Y-%m-%d %H:%M:%S')
    for aposition in positions:
        aSymbol = aposition.symbol
        symbolList.append(aSymbol)

    instuInfo = gm3HelpBylw.getInstumInfo(symbolList)

    for aposition in positions:
        aSymbol = aposition.symbol
        delistDate = instuInfo.loc[instuInfo['symbol'] == aSymbol,
                                   'delisted_date'].iloc[0]
        deliveryMothDate = pd.Timestamp(delistDate) + MonthBegin(-1)
        deliveryMothDate = deliveryMothDate.strftime('%Y-%m-%d')

        if nextTradeDate < deliveryMothDate:

            vol_ = aposition['volume']
            side_ = aposition['side']

            if side_ == PositionSide_Long:

                if moveOrderLog is not None:

                    moveOrderLog.info("%s,%s,%s", aSymbol, vol_, '卖平')
                    gm3HelpBylw.gmOrder.clearLong(aSymbol, vol_, 'Delivery-cLong', dt,\
                                                                      orderLog=context.orderLog)

            if side_ == PositionSide_Short:

                if moveOrderLog is not None:
                    # 平仓
                    moveOrderLog.info("%s,%s,%s", aSymbol, vol_, '买平')
                    gm3HelpBylw.gmOrder.clearShort(aSymbol, vol_, 'Delivery-cShort', dt, \
                                                  orderLog=context.orderLog)
コード例 #21
0
 def to_offset(self) -> DateOffset:
     if self.value == "H":
         return Hour(1)
     elif self.value == "D":
         return Day(1)
     elif self.value == "W-MON":
         return Week(1, weekday=0)
     elif self.value == "MS":
         return MonthBegin(1)
     elif self.value == "QS-DEC":
         return QuarterBegin(startingMonth=10)
     elif self.value == "AS":
         return YearBegin(1)
     raise NotImplementedError(self.value)
コード例 #22
0
def get_month_lag(df, row):
    '''
    Summary: Used in the create_month_lag function to grab the prior month data for each record

    Input: dataframe to grab prior month data

    Output: new lag dataframe that contains only data for the prior month for a single record
    '''
    lag_date = row['contract_start_year_month'] - 1 * MonthBegin()
    mask = (lag_date == df.loc[:, 'contract_start_year_month']) & (
        row['product_type'] == df.loc[:, 'product_type']) & (
            row['rental_type'] == df.loc[:, 'rental_type'])
    lag_df = df.loc[mask, :]
    return lag_df
コード例 #23
0
def count_new_contributors(commits_data: List[Dict[str, Any]]) -> pd.DataFrame:
    """
    Processes API results in a Pandas DataFrame to compute the monthly
    count of new contributors per repository.

    :param commits_data: list of dicts containing commits information
    :return: Pandas DataFrame containing aggregated data
    """

    df = (pd.DataFrame.from_records(commits_data).groupby(
        ["repo", "author"])["date"].min().reset_index())
    df["datemonth"] = (pd.to_datetime(df["date"]) - MonthBegin(1)).dt.date
    df = df.groupby(["repo", "datemonth"]).size().reset_index()
    return df
コード例 #24
0
ファイル: econ.py プロジェクト: superqyl/TVIX
def attach_PMI_index(df):
    df["month_start"] = df["date"] + Day(1) - MonthBegin()
    df["first_B_day_of_month"] = df["month_start"] - BDay() + BDay()
    res = pd.merge(
        df, create_PMI_index(),
        left_on='first_B_day_of_month',
        right_on='date',
        how='left'
    )
    res = res.fillna(method='ffill')
    import pdb; pdb.set_trace()
    res = res.drop(["month_start", "first_B_day_of_month", "date_y"], axis=1)
    res = res.rename(columns = {"date_x": "date"})
    res.index = res["date"]
    return res
コード例 #25
0
    def _trigger_dts_calc(self, start_dt, end_dt):
        # bump the dates to ensure we capture month start and end
        start_dt_use = start_dt - MonthBegin()
        end_dt_use = end_dt + MonthEnd()
        sessions = self._trading_calendar.sessions(start_dt_use, end_dt_use)
        mkt_open = self._trading_calendar.open_time
        mkt_close = self._trading_calendar.close_time
        dt_dates = self._dt_func(sessions)
        # we make sure the date ranges does not fall outside the start
        # and end dates.
        dt_dates = [dt for dt in dt_dates if dt >= start_dt.value and\
                    dt <= end_dt.value]
        # we cannot do the same for times, for e.g. for 24 hour calendar
        # it becomes meaningless. open may be > close.
        dt_times = self._time_func(mkt_open, mkt_close)

        return [dt1 + dt2 for dt1 in dt_dates for dt2 in dt_times]
コード例 #26
0
ファイル: account.py プロジェクト: HugoDelatte/finances
 def _summarize(self):
     """
     Compute stats from the transactions
     """
     self.currency = self.transaction_df.iloc[0]['currency']
     self.categories = set(self.transaction_df['category'])
     self.last_transaction_date = self.transaction_df['date'].max()
     self.balance = self.transaction_df['amount'].sum()
     self.actual_month_end = self.total_df.index[-1]
     self.actual_month_begin = self.actual_month_end + MonthBegin(-1)
     self.actual_month_pnl = self.total_df.loc[self.actual_month_end,
                                               'amount']
     self.previous_month_end = self.total_df.index[-2]
     self.previous_month_pnl = self.total_df.loc[self.previous_month_end,
                                                 'amount']
     self.avg_monthly_pnl = self.total_df[:-1]['amount'].sum() / len(
         self.total_df[:-1])
コード例 #27
0
def next_update_time(last_updated, freq='D', hour=18, minute=0, second=0):
    """计算下次更新时间
    说明:
        'S':移动到下一秒
        'm':移动到下一分钟
        'H':移动到下一小时
        'D':移动到下一天
        'W':移动到下周一
        'M':移动到下月第一天
        'Q':下一季度的第一天
        将时间调整到指定的hour和minute
    """
    if pd.isnull(last_updated):
        return MARKET_START
    if freq == 'S':
        off = Second()
        return last_updated + off
    elif freq == 'm':
        off = Minute()
        return last_updated + off
    elif freq == 'H':
        off = Hour()
        return last_updated + off
    elif freq == 'D':
        d = BDay(n=1, normalize=True)
        res = last_updated + d
        return res.replace(hour=hour, minute=minute, second=second)
    elif freq == 'W':
        w = Week(normalize=True, weekday=0)
        res = last_updated + w
        return res.replace(hour=hour, minute=minute, second=second)
    elif freq == 'M':
        m = MonthBegin(n=1, normalize=True)
        res = last_updated + m
        return res.replace(hour=hour, minute=minute, second=second)
    elif freq == 'Q':
        q = QuarterBegin(normalize=True, startingMonth=1)
        res = last_updated + q
        return res.replace(hour=hour, minute=minute, second=second)
    else:
        raise TypeError('不能识别的周期类型,仅接受{}'.format(
            ('S', 'm', 'H', 'D', 'W', 'M', 'Q')))
コード例 #28
0
ファイル: Lab2.py プロジェクト: ronmicha/PPL
def get_from_start_of_week_or_month(row, n, incomes=True):
    if n == 7:
        if row[WEEKDAY] != 0:
            start_date = row[DATE] - pd.DateOffset(days=int(row[WEEKDAY]))
            end_date = row[DATE] - pd.DateOffset(days=1)
        else:  # Look at previous week
            start_date = row[DATE] - pd.DateOffset(days=7)
            end_date = row[DATE] - pd.DateOffset(days=1)
    else:  # n == 30
        if row[DAY] != 1:
            start_date = row[DATE] - MonthBegin()
            end_date = row[DATE] - pd.DateOffset(days=1)
        else:  # Look at previous month
            start_date = row[DATE] - pd.DateOffset(months=1)
            end_date = row[DATE] - pd.DateOffset(days=1)
    last_n_days = transactions_df[(transactions_df[USER_ID] == row[USER_ID]) &
                                  (transactions_df[INCOME] == incomes) &
                                  (transactions_df[DATE] >= start_date) &
                                  (transactions_df[DATE] <= end_date)]
    return last_n_days
コード例 #29
0
def timestamp_rollforward_rollback():
    """ How to role the date forward (end of time) or backward (beg of time) """
    now = datetime(2014, 4, 15)
    print "Current time is:", now
    now = now + 3 * Day()
    print "Adding 3 days to now:", now

    offset = MonthEnd()
    now = offset.rollforward(now)
    print "Rolling foward to last day of the month", now

    offset = MonthBegin()
    now = offset.rollback(now)
    print "Rolling foward to first day of the month", now

    ts = pd.Series(np.random.randn(20),
                   index=pd.date_range('1/1/2000', periods=20, freq='4d'))
    print "Original Time Series is:\n", ts

    offset = YearBegin()
    ts = ts.groupby(offset.rollforward).mean()
    print "Time Series after rolling forward\n", ts
コード例 #30
0
                                    BYearBegin, BYearEnd, QuarterBegin,
                                    QuarterEnd, BQuarterBegin, BQuarterEnd)

_offset_map = {
    'D'     : Day(),
    'B'     : BDay(),
    'H'     : Hour(),
    'T'     : Minute(),
    'S'     : Second(),
    'L'     : Milli(),
    'U'     : Micro(),
    None    : None,

    # Monthly - Calendar
    'M'      : MonthEnd(),
    'MS'     : MonthBegin(),

    # Monthly - Business
    'BM'     : BMonthEnd(),
    'BMS'    : BMonthBegin(),

    # Annual - Calendar
    'A-JAN' : YearEnd(month=1),
    'A-FEB' : YearEnd(month=2),
    'A-MAR' : YearEnd(month=3),
    'A-APR' : YearEnd(month=4),
    'A-MAY' : YearEnd(month=5),
    'A-JUN' : YearEnd(month=6),
    'A-JUL' : YearEnd(month=7),
    'A-AUG' : YearEnd(month=8),
    'A-SEP' : YearEnd(month=9),