Example #1
0
def returns(ts, **kwargs):
    '''
    Compute returns on the given period
    @param ts : time serie to process
    @param kwargs.type: gross or simple returns
    @param delta : period betweend two computed returns
    @param start : with end, will return the return betweend this elapsed time
    @param period : delta is the number of lines/periods provided
    @param end : so said
    @param cumulative: compute cumulative returns
    '''
    type = kwargs.get('type', 'net')
    cumulative = kwargs.get('cumulative', False)
    if type == 'net':
        relative = 0
    else:
        relative = 1  # gross
    start = kwargs.get('start', None)
    end = kwargs.get('end', dt.datetime.today())
    delta = kwargs.get('delta', None)
    period = kwargs.get('period', 1)
    if isinstance(start, dt.datetime):
        log.debug('{} / {} -1'.format(ts[end], ts[start]))
        return ts[end] / ts[start] - 1 + relative
    elif isinstance(delta, pd.DateOffset) or isinstance(delta, dt.timedelta):
        #FIXME timezone problem
        ts = reIndexDF(ts, delta=delta)
        period = 1
    rets_df = ts / ts.shift(period) - 1 + relative
    if cumulative:
        return rets_df.cumprod()
    return rets_df[1:]
Example #2
0
def returns(ts, **kwargs):
    '''
    Compute returns on the given period
    @param ts : time serie to process
    @param kwargs.type: gross or simple returns
    @param delta : period betweend two computed returns
    @param start : with end, will return the return betweend this elapsed time
    @param period : delta is the number of lines/periods provided
    @param end : so said
    @param cumulative: compute cumulative returns
    '''
    type = kwargs.get('type', 'net')
    cumulative = kwargs.get('cumulative', False)
    if type == 'net':
        relative = 0
    else:
        relative = 1  # gross
    start = kwargs.get('start', None)
    end = kwargs.get('end', dt.datetime.today())
    delta = kwargs.get('delta', None)
    period = kwargs.get('period', 1)
    if isinstance(start, dt.datetime):
        log.debug('{} / {} -1'.format(ts[end], ts[start]))
        return ts[end] / ts[start] - 1 + relative
    elif isinstance(delta, pd.DateOffset) or isinstance(delta, dt.timedelta):
        #FIXME timezone problem
        ts = reIndexDF(ts, delta=delta)
        period = 1
    rets_df = ts / ts.shift(period) - 1 + relative
    if cumulative:
        return rets_df.cumprod()
    return rets_df[1:]
Example #3
0
 def getHistoricalQuotes(self, symbol, index, market=None):
     assert (isinstance(index, pd.Index))
     source = 'yahoo'
     try:
         quotes = DataReader(symbol, source, index[0], index[-1])
     except:
         log.error('** Could not get {} quotes'.format(symbol))
         return pd.DataFrame()
     if index.freq != pd.datetools.BDay() or index.freq != pd.datetools.Day():
         #NOTE reIndexDF has a column arg but here not provided
         quotes = utils.reIndexDF(quotes, delta=index.freq, reset_hour=False)
     if not quotes.index.tzinfo:
         quotes.index = quotes.index.tz_localize(self.tz)
     quotes.columns = utils.Fields.QUOTES
     return quotes
Example #4
0
 def getHistoricalQuotes(self, symbol, index, market=None):
     assert (isinstance(index, pd.Index))
     source = 'yahoo'
     try:
         quotes = DataReader(symbol, source, index[0], index[-1])
     except:
         log.error('** Could not get {} quotes'.format(symbol))
         return pd.DataFrame()
     if index.freq != pd.datetools.BDay() or index.freq != pd.datetools.Day():
         #NOTE reIndexDF has a column arg but here not provided
         quotes = utils.reIndexDF(quotes, delta=index.freq, reset_hour=False)
     if not quotes.index.tzinfo:
         quotes.index = quotes.index.tz_localize(self.tz)
     quotes.columns = utils.Fields.QUOTES
     return quotes
Example #5
0
def average_returns(ts, **kwargs):
    ''' Compute geometric average returns from a returns time serie'''
    type = kwargs.get('type', 'net')
    if type == 'net':
        relative = 0
    else:
        relative = -1  # gross
    start = kwargs.get('start', ts.index[0])
    end = kwargs.get('end', ts.index[len(ts.index) - 1])
    delta = kwargs.get('delta', ts.index[1] - ts.index[0])
    period = kwargs.get('period', None)
    if isinstance(period, int):
        pass
    else:
        ts = reIndexDF(ts, start=start, end=end, delta=delta)
        period = 1
    avg_ret = 1
    for idx in range(len(ts.index)):
        if idx % period == 0:
            avg_ret *= (1 + ts[idx] + relative)
    return avg_ret - 1
Example #6
0
def average_returns(ts, **kwargs):
    ''' Compute geometric average returns from a returns time serie'''
    type = kwargs.get('type', 'net')
    if type == 'net':
        relative = 0
    else:
        relative = -1  # gross
    start = kwargs.get('start', ts.index[0])
    end = kwargs.get('end', ts.index[len(ts.index) - 1])
    delta = kwargs.get('delta', ts.index[1] - ts.index[0])
    period = kwargs.get('period', None)
    if isinstance(period, int):
        pass
    else:
        ts = reIndexDF(ts, start=start, end=end, delta=delta)
        period = 1
    avg_ret = 1
    for idx in range(len(ts.index)):
        if idx % period == 0:
            avg_ret *= (1 + ts[idx] + relative)
    return avg_ret - 1