Ejemplo n.º 1
0
def addTarget_1_minus(dataset, futureDays=30):
    prices = dataset.price
    m = futureDays
    y_var = []
    for t in range(0, len(prices) - m + 1):
        p = prices.iloc[t:t + m]
        _, var = future_mean_var(p, True)
        y_var.append(var * 10000)
    for t in range(len(y_var), len(prices)):
        y_var.append(np.nan)
    dataset['y'] = y_var
    return dataset.dropna()
Ejemplo n.º 2
0
def addTarget_mean(dataset, futureDays=30):
    prices = dataset.price
    m = futureDays
    y_m = []
    for t in range(0, len(prices) - m + 1):
        p = prices.iloc[t:t + m]
        mean, _ = future_mean_var(p)
        y_m.append(mean * 100)
    for t in range(len(prices) - m + 1, len(prices)):
        y_m.append(np.nan)
    dataset['y'] = y_m
    print(max(y_m), min(y_m))
    return dataset.dropna()
Ejemplo n.º 3
0
def rpw_future(prices,
               initial_weights=None,
               risk_weights=None,
               risk_parity_method='ccd',
               maximum_iterations=100,
               tolerance=1E-8,
               min_assets_number=2,
               max_assets_number=6):
    r = prices.to_returns().dropna()
    covar = ledoit_wolf(r)[0]
    for i in range(len(r.columns)):
        _, var = future_mean_var(prices[prices.columns[i]].values)
        covar[i, i] = var * 100
    return covar
Ejemplo n.º 4
0
def addTarget(dataset, futureDays=30):
    prices = dataset.price
    m = futureDays
    y_var = []
    for t in range(0, len(prices)-m+1):
        p = prices.iloc[t:t+m]
        _, var = future_mean_var(p)
        y_var.append(var*10000)
    y_min = min(y_var)
    y_max = max(y_var)
    y_distance = y_max-y_min
    for t in range(len(y_var)):
        y_var[t] = (y_var[t]-y_min)/y_distance
    for t in range(len(prices)-m+1, len(prices)):
        y_var.append(np.nan)
    dataset['y'] = y_var
    return dataset.dropna()
Ejemplo n.º 5
0
def addTarget_2(dataset, futureDays=30, sm=5):
    prices = dataset.price
    m = futureDays
    y_var = []
    for t in range(0, len(prices) - m + 1):
        p = prices.iloc[t:t + m]
        _, var = future_mean_var(p)  #, True)
        y_var.append(var * 10000)

    ym_var = []
    for t in range(sm, len(y_var)):
        ym_var.append(sum(y_var[t - sm:t]) / sm)

    for t in range(len(ym_var), len(prices)):
        ym_var.append(np.nan)
    dataset['y'] = ym_var
    return dataset.dropna()
Ejemplo n.º 6
0
import pandas as pd
import numpy as np

# retrieve data from eod
etf_tickers = ['2800']
prices = ffn.get(tickers=etf_tickers,
                 market='HK',
                 provider=eod,
                 start='2005-01-01')
m = 30
mean = []
std = []

for t in range(0, len(prices) - m + 1):
    p = prices[t:t + m].values
    mean_t, var_t = future_mean_var(p, False)
    mean.append(mean_t)
    std.append(var_t**0.5)

p = prices['2800'][0:len(mean)].values
df = pd.DataFrame({
    '2800': p,
    'mean': mean,
    'std': std
},
                  index=prices.index[0:len(mean)])
df['mal'] = MA(df['2800'], 120)
df['mas'] = MA(df['2800'], 10)
df['diff'] = (df['mas'] / df['mal'] - 1) / 10
df['slope'] = df['diff'].pct_change()
df['mean_std'] = 2 * df['std'] - df['mean']