예제 #1
0
def get_melted_balance(i=None):
    if not i:
        i = rcall('dim(bt$strat.balance)[1]')[0]
    # Same as
    # mat <- balance[i, , , ...]
    rcall('''
    require(magrittr)
    dims <- dim(bt$strat.balance)
    margin.idx <- bt$strat.balance %%>%% slice.index(MARGIN=1)
    mat <- bt$strat.balance[which(margin.idx == %d)] %%>%%
        array(dims[-1])
    ''' % i)
    return rcall('reshape2::melt(mat)')
예제 #2
0
def get_returns():
    returns = rcall('bt$strat.returns')
    shape = rcall('dim(bt$strat.returns)')
    return np.array(returns).reshape(shape)
예제 #3
0
def load_rda(fname=None, from_dir='.'):
    if not fname:
        fname = choose_file(from_dir=from_dir)
    print('[load_rda] loading "%s"' % fname)
    rcall('load("%s")' % fname)
예제 #4
0
def get_balance():
    balance = rcall('bt$strat.balance')
    shape = rcall('dim(bt$strat.balance)')
    return np.array(balance).reshape(shape)
예제 #5
0
from matplotlib.lines import Line2D

import load_r
load_r.load_rda(from_dir='../results/')
balance = load_r.get_balance()
balance[np.where(np.isnan(balance))] = 1
returns = load_r.get_returns()
returns[np.where(np.isnan(returns))] = 0
#returns_annualized_days = (1+returns)**365-1 #annualized returns
zlim, zrange = load_r.find_zlim(balance, center=1)
last_b_annual = balance[-1]**(365 / balance.shape[0]) - 1
print('done...')

from datetime import datetime
dates = np.array(rcall('as.character(bt$dates)'))
dates = np.array([datetime.strptime(d, '%Y-%m-%d') for d in dates])


def get_group_indexes(dates, format='%Y-%m'):
    formatted = np.array([datetime.strftime(d, format) for d in dates])
    clusters = []
    for cl in np.unique(formatted):
        idx = np.where(formatted == cl)[0]
        clusters.append(idx)
    return clusters


groups_idx_list = get_group_indexes(dates, '%Y-%m')