在Python中实现自定义计数系统

时间:2016-07-13 06:57:56

标签: python numpy pandas

在金融方面,期货合约通常以其到期年和月来表示。例如,201212将是2012年 - 12月。

有些合约,例如Corn,只交易了几个月[3,5,7,9,12],而有时,可能只想交易[12]合约(尽管它在其他几个月交易)也是如此,所以你要交易201212201312等。

我目前正在使用int格式在我的系统中表示这些合约,并将其用作Pandas索引。

棘手的是,根据合同,我经常需要获得下一份合同(在较小程度上,前一份合同)。

我写了一个生成这样的东西的生成器表达式:

def contract_generator(self, recent=True, year=None, month=None, traded_only=False):
    if year is None:
        year = datetime.datetime.now().year - 1 if recent == True else self.first_contract

    if traded_only is True:
        months = self.trade_only
    else:
        months = self.months_traded

    months = deque(months)

    if month is not None:
        months.rotate(months.index(month)-1)
    while True:
        for month in months:
            yield {
                'year': year,
                'month': month,
                'contract': str(year)+str("%02d" % (month,)),
                'formatted_contract': self.contract_format(year, month),
                'expiration_date': self.expiration_date(year, month)
                }
        year+=1

def next_contract(self, contract):
    c = contract_to_tuple(contract)
    j = self.contract_generator(year = c[0], month = c[1], traded_only=False)
    return next(j)['contract']

def contract_to_tuple(contract):
    contract = str(contract)
    return (int(contract[0:4]),int(contract[4:6]))

(其中months_traded& trade_only是我在第2段中提到的列表。

至关重要的是,它的马车以及上述方法不能正常工作。我可以解决它,但说实话,我真的不喜欢这种方法。必须有更好的方法。

思路:

  • 以某种方式使用datetime实现
  • 创建某种自定义对象,实现算术运算符,这样我就可以201212 + 1来获得下一个合约(但是对于pandas来说这真的很容易吗?)
  • 我不认为python已经融入其中,但也许我可以定义一个自定义数字库,以某种方式映射到这种行为。

是否有一种简单/优雅的方法可以做到这一点已经存在?或者我真的需要从头开始制作吗?

修改

我的最终结果:

def nc(self, contract, months=None):
    d = pd.to_datetime(str(contract), format='%Y%m')
    months_traded = deque(self.months_traded)
    months_traded.rotate(-1)
    output_month = months_traded[self.months_traded.index(d.month)]
    output_year = d.year + 1 * d.month==12
    return str(output_year)+str("%02d" % (output_month,))

1 个答案:

答案 0 :(得分:1)

这应该这样做:

def next_contract(contract, last=False):
    d = pd.to_datetime(str(contract), format='%Y%m')
    d += pd.offsets.MonthBegin(12 * (last * -2 + 1))
    return int(d.strftime('%Y%m'))

演示

next_contract(201212)

201312

next_contract(201212, last=True)

201112

解释

def next_contract(contract, last=False):
    # convert contract to datetime with assumed format of yyyymm
    d = pd.to_datetime(str(contract), format='%Y%m')
    # use pandas offsets.  I don't care that it's month begin
    # because I'm ditching the day anyway.
    # (last * -2 + 1) equals -1 when last is True and 1 when last is False
    d += pd.offsets.MonthBegin(12 * (last * -2 + 1))
    return int(d.strftime('%Y%m'))

对于它的价值,这里是一个类的存根。老实说,编写其他月份的所有处理应该留给你练习。

class Corn(object):
    def __init__(self, contract):
        self.contract = contract

    def __add__(self, i):
        d = pd.to_datetime(str(self.contract), format='%Y%m')
        d += pd.offsets.MonthBegin(12 * i)
        self.contract = int(d.strftime('%Y%m'))
        return self

    def __sub__(self, i):
        return self.__add__(-i)

    def get_next(self):
        return self + 1

    def get_last(self):
        return self - 1

    def __repr__(self):
        return str(self.contract)
corn = Corn(201212)
print(corn + 1)
print(corn.get_next())

201312
201412