Example #1
0
def comprehensive_analysis(records, **conf):
    rates = DataFrame()
    for code, values in records:
        rates[code] = change_rate(
            values, conf['col'], conf['slide'], conf['days'])

    # print '==== plot rates ===='
    # plot_items(rates, 2, 400)

    # print '==== highest correlations ===='
    # smoothed = rates.applymap(lambda x: round(x / 5.0))
    # sims = smoothed.corr()
    # sims = sims[sims != 1]
    # print sims.head().stack().nlargest(5)

    print '==== highest liquidities(variances) ===='
    var_highest = rates.abs().sum().nlargest(5)
    print var_highest
Example #2
0
df1+df2
df1.add(df2, fill_value=0)

# Operations between DataFrame and Series
frame = DataFrame(np.arange(12.).reshape((4, 3)), columns=list('bde'),
                  index=['Utah', 'Ohio', 'Texas', 'Oregon'])
series = frame.ix[0]
frame - series #boardcast on each row
series2 = frame['d']
frame.sub(series2, axis=0)  #boardcast on each column

#Function application and mapping------------------------
# numpy的ufunc会被应用到元素级
frame = DataFrame(np.random.randn(4, 3), columns=list('bde'),index=['Utah', 'Ohio', 'Texas', 'Oregon'])
np.abs(frame)
frame.abs()
# DataFrame的apply默认将函数应用在各列
f = lambda x: x.max() - x.min() #x is an array?
frame.apply(f)
frame.apply(f,axis=1)   #应用于各行

def f(x):
    return Series([x.min(), x.max()], index = ['min', 'max'])
frame.apply(f)
#元素级的python函数应该用applymap,Series用map
format = lambda x: '%.2f' % x
frame.applymap(format)
frame['e'].map(format)

#Hierarchical indexing=====================================
data = Series(np.random.randn(10),index=[['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd', 'd'],[1, 2, 3, 1, 2, 3, 1, 2, 2, 3]])
Example #3
0
  'honey ham': 'pig',
  'nova lox': 'salmon'
}

data['animal'] = data['food'].map(str.lower).map(meat_to_animal)
data

data['food'].map(lambda x: meat_to_animal[x.lower()])

# 数据标准化
datafile = 'd:/data/normalization_data.xls' #参数初始化
data = pd.read_excel(datafile, header = None) #读取数据

(data - data.min())/(data.max() - data.min()) #最小-最大规范化
(data - data.mean())/data.std() #零-均值规范化
data/10**np.ceil(np.log10(data.abs().max())) #小数定标规范化


###替换值
data = Series([1., -999., 2., -999., -1000., 3.])
data

data.replace(-999, np.nan)

data.replace([-999, -1000], np.nan)

data.replace([-999, -1000], [np.nan, 0])

data.replace({-999: np.nan, -1000: 0})

Example #4
0
class Portfolio(object):

    """This class represents portfolio and its events."""

    def __repr__(self):
        return '<Portfolio {}>'.format(self.prices.shape)

    def __init__(self, ohlcs, starting_capital=100000, price_type='cprices',
                 transaction_fee_bps=15., transaction_fee_min=7):
        self.price_type = price_type
        self.transaction_fee_bps = transaction_fee_bps
        self.transaction_fee_min = transaction_fee_min
        self.prices = self.from_ohlcs(ohlcs, price_type)
        self.volumes = self.from_ohlcs(ohlcs, 'volumes')
        self.trades = DataFrame(zeros(self.prices.shape), self.prices.index,
                                self.prices.columns)
        self.fees = DataFrame(zeros(self.prices.shape), self.prices.index,
                              self.prices.columns)
        self.starting_capital = starting_capital
        self.capital = []
        self.quantities = []
        self.values = []
        self.refresh()

    def from_ohlcs(self, ohlcs, price_type):
        """Set prices using a list of ohlc classes."""
        dfs = []
        for ohlc in ohlcs:
            df = DataFrame(getattr(ohlc, price_type),
                           posix_as_dt(ohlc.timestamps))
            dfs.append(df)
        prices = concat(dfs, join='outer', axis=1)
        prices.columns = [ohlc.symbol for ohlc in ohlcs]
        return prices.fillna(method='pad')

    def refresh(self):
        """Calculates positions, values, free capital and costs from trades.
        Fees of short positions (if any) are same as cost for long. This is not
        realistic, but the class is intended to represent long only portfolios.
        """
        self.fees = self.transaction_fee_bps * self.trades.abs() * \
            self.prices / 10000
        small = self.fees < self.transaction_fee_min
        nonzero = self.trades.abs() > 0
        self.fees[small * nonzero] = self.transaction_fee_min
        self.quantities = self.trades.cumsum()
        self.values = self.quantities * self.prices
        self.capital = self.starting_capital + self.total_trade_values - \
            self.total_fees

    def trade(self, timestring, symbol, quantity):
        """Convenience function to enter trades and refresh."""
        self.trades[symbol][timestring] = quantity
        self.refresh()

    def trade_max(self):
        """Trade all capital on first day, equal sized positions."""
        first_day = dt_as_str(self.prices.index[0])
        trade_sizes = zeros(len(self.prices.columns))
        trade_sizes[:] = self.starting_capital / float(len(trade_sizes))
        trade_sizes = [trade_sizes[ind] / self.prices.iloc[0].values[ind] for
                       ind in arange(len(trade_sizes))]
        trade_sizes = trunc(trade_sizes)
        for ind in arange(len(self.prices.columns)):
            self.trade(first_day, self.prices.columns[ind], trade_sizes[ind])
        self.refresh()

    @property
    def market_value(self):
        """Value of equity positions at each time."""
        return self.values.sum(axis=1)

    @property
    def total_value(self):
        """Total value of portfolio at each time."""
        return self.market_value + self.capital

    @property
    def trade_values(self):
        """Trade values for each trade."""
        tvals = -self.trades * self.prices
        to_0 = (tvals == 0) + (isnull(tvals))
        tvals[to_0] = 0
        return tvals

    @property
    def total_trade_values(self):
        """Cumulative sum of all trades."""
        return self.trade_values.sum(axis=1).cumsum()

    @property
    def total_fees(self):
        """Cumulative sum of fees."""
        return self.fees.sum(axis=1).cumsum()