コード例 #1
0
def mapping(tickers, delta_s, tickers_pl, \
            init_cash, trans_dates, trans_prices, shares, residual_cashs, \
            freq='D'): 
    #freq: T-trading date, D-Daily, W-Weekly, M-Monthly
    # t=0
    d = delta_s.index[0]
    mapping_date = np.array(d)
    cash = init_cash
    mapping_cash = np.array(cash)    
    s = np.zeros(len(tickers))
    mapping_shares = np.array(s)    
    c = np.zeros(len(tickers))   
    pre_t_date = delta_s.index[0] + datetime.timedelta(days=-1)    
    for e in range(len(tickers)):
        while pre_t_date.date() not in tickers_pl[tickers[e]].index.date:
            pre_t_date = pre_t_date + datetime.timedelta(days=-1)
        p = read_price(pre_t_date, tickers[e], tickers_pl[tickers[e]])
        c[e] = p.Close    
    mapping_closes = np.array(c)
    w = np.zeros(len(tickers))  
    mapping_weights = np.array(w)
    v = 0.0
    mapping_value = np.array(v)
    # t>=1
    if freq == 'D':
        start_date = delta_s.index[0]
        end_date = tickers_pl[tickers[0]].index[-1]
        range_date = pd.date_range(start=start_date, end=end_date, freq='D')
    elif freq == 'T':
        range_date = trans_dates
    else:
        range_date = trans_dates
    t = 0
    for d in range_date:
        if datetime.datetime.weekday(d)<=4:
            if d == trans_dates[t]:
                s = shares[t]
                cash = residual_cashs[t]
                if t < len(trans_dates)-1:
                    t = t + 1
            mapping_date = np.vstack([mapping_date, d])
            mapping_cash = np.vstack([mapping_cash, cash])
            mapping_shares = np.vstack([mapping_shares, s])
            for e in range(len(tickers)):
                if d.date() in tickers_pl[tickers[e]].index.date:
                    p = read_price(d, tickers[e], tickers_pl[tickers[e]])
                    c[e] = p.Close
            mapping_closes = np.vstack([mapping_closes, c])
            v = sum(s*c)
            mapping_value = np.vstack([mapping_value, v])
            for e in range(len(tickers)):
                w[e] = s[e]*c[e]/v
            mapping_weights = np.vstack([mapping_weights,w])
    return mapping_date, mapping_shares, mapping_weights, mapping_closes, mapping_cash, mapping_value
コード例 #2
0
ファイル: placement.py プロジェクト: easy00000000/k_chart
def get_value(tickers, t_date, shares, tickers_pl=None):
    value = 0.0
    for e in range(len(tickers)):
        if tickers_pl is None:
            pl = None
        else:
            pl = tickers_pl[tickers[e]]
        p = read_price(t_date, tickers[e], pl)
        value = value + p.Close * shares[e]
    return value
コード例 #3
0
ファイル: placement_old.py プロジェクト: easy00000000/k_chart
def get_close_price(tickers, t_date, tickers_pl=None, weekend=False):
    prices = np.zeros(len(tickers))
    if weekend is not True:
        for e in range(len(tickers)):
            if tickers_pl is None:
                pl = None
            else:
                pl = tickers_pl[tickers[e]]
            p = read_price(t_date, tickers[e], pl)
            prices[e] = p.Close
    else:
        for e in range(len(tickers)):
            if tickers_pl is None:
                pl = None
            else:
                pl = tickers_pl[tickers[e]]
            p = read_price(t_date, tickers[e], pl, True)
            prices[e] = p.Close
    return prices
コード例 #4
0
ファイル: placement_old.py プロジェクト: easy00000000/k_chart
def est_transacton_prices(tickers, delta_s, tickers_pl):
    trans_prices = pd.DataFrame(columns=delta_s.columns)
    shares = pd.DataFrame(columns=delta_s.columns)
    #t=0
    t_date = delta_s.index[0]
    # find 1st close price
    prices = np.zeros(len(tickers))
    pre_t_date = t_date + datetime.timedelta(days=-1)
    for e in range(len(tickers)):
        while pre_t_date.date() not in tickers_pl[tickers[e]].index.date:
            pre_t_date = pre_t_date + datetime.timedelta(days=-1)
        c = read_price(pre_t_date, tickers[e], tickers_pl[tickers[e]])
        prices[e] = c.Close
    #t>=1
    for t in range(len(delta_s.index)):
        if t == 0:
            # initialize dateframe
            trans_prices.loc[len(trans_prices)] = np.zeros(len(tickers))
            trans_prices['Date'] = delta_s.index[0]
            shares.loc[len(shares)] = np.zeros(len(tickers))
            shares['Date'] = delta_s.index[0]
        else:
            # add one more row
            trans_prices.loc[len(trans_prices)] = trans_prices.loc[
                len(trans_prices) - 1]
            shares.loc[len(shares)] = shares.loc[len(shares) - 1]
        pre_prices = prices
        t_date = delta_s.index[t]
        t_date = t_date + datetime.timedelta(days=1)
        s = delta_s.iloc[t].copy()
        for e in range(len(tickers)):
            t_date_e = t_date
            while t_date_e.date() not in tickers_pl[tickers[e]].index.date:
                t_date_e = t_date_e + datetime.timedelta(days=1)
            if s[e] > 0.0:  # buy
                transaction = 'BUY'
            elif s[e] < 0.0:
                transaction = 'SELL'
            else:
                transaction = 'No Trade'
            place_price = get_transaction_price(t_date_e, tickers[e],
                                                transaction, pre_prices[e],
                                                tickers_pl[tickers[e]])
            trans_prices[tickers[e]].iloc[t] = place_price
            shares[tickers[e]].iloc[t] = s[e] + shares[tickers[e]].iloc[t]
        trans_prices['Date'].iloc[t] = t_date_e
        shares['Date'].iloc[t] = t_date_e
        # find t period close price
        prices = get_close_price(tickers, t_date, tickers_pl,
                                 True)  # True : weekend close
    # set date as index
    trans_prices.set_index("Date", inplace=True)
    shares.set_index("Date", inplace=True)
    return trans_prices, shares
コード例 #5
0
ファイル: placement.py プロジェクト: easy00000000/k_chart
def get_close_price(tickers,
                    t_date,
                    tickers_pl=None,
                    freq='Date'):  # 'Daily, Weekly, Monthly
    prices = np.zeros(len(tickers))
    if freq == 'Daily':
        for e in range(len(tickers)):
            if tickers_pl is None:
                pl = None
            else:
                pl = tickers_pl[tickers[e]]
            p = read_price(t_date, tickers[e], pl)
            prices[e] = p.Close
    elif freq == 'Weekly':
        for e in range(len(tickers)):
            if tickers_pl is None:
                pl = None
            else:
                pl = tickers_pl[tickers[e]]
            p = read_price(t_date, tickers[e], pl, 'Weekly')
            prices[e] = p.Close
    return prices
コード例 #6
0
ファイル: placement.py プロジェクト: easy00000000/k_chart
def get_transaction_price(t_date,
                          ticker,
                          transaction,
                          pre_Close,
                          pl=None,
                          worst=False):
    p = read_price(t_date, ticker, pl)
    if transaction == 'BUY':
        price = _get_long_price(p, pre_Close)
    elif transaction == 'SELL':
        price = _get_short_price(p, pre_Close)
    else:
        price = p.Close
    return price
コード例 #7
0
ファイル: placement.py プロジェクト: easy00000000/k_chart
def est_transacton_prices(tickers, delta_s, tickers_pl, init_cash):
    trans_dates = []
    residual_cash = []
    trans_prices = np.zeros([len(delta_s), len(tickers)])
    shares = np.zeros([len(delta_s), len(tickers)])
    #t=0
    t_date = delta_s.index[0]
    cash = init_cash
    # pre_close_price
    prices = np.zeros(len(tickers))
    pre_t_date = t_date + datetime.timedelta(days=-1)
    for e in range(len(tickers)):
        while pre_t_date.date() not in tickers_pl[tickers[e]].index.date:
            pre_t_date = pre_t_date + datetime.timedelta(days=-1)
        c = read_price(pre_t_date, tickers[e], tickers_pl[tickers[e]])
        prices[e] = c.Close
    #t>=1
    for t in range(len(delta_s.index)):
        pre_prices = prices
        t_date = delta_s.index[t]
        t_date = t_date + datetime.timedelta(days=1)
        s = delta_s.iloc[t].copy()
        for e in range(len(tickers)):
            t_date_e = t_date
            while t_date_e.date() not in tickers_pl[tickers[e]].index.date:
                t_date_e = t_date_e + datetime.timedelta(days=1)
            if s[e] > 0.0:  # buy
                transaction = 'BUY'
            elif s[e] < 0.0:
                transaction = 'SELL'
            else:
                transaction = 'No Trade'
            place_price = get_transaction_price(t_date_e, tickers[e],
                                                transaction, pre_prices[e],
                                                tickers_pl[tickers[e]])
            trans_prices[t, e] = place_price
            fee = _calc_fee(place_price, s[e], transaction)
            delta_cash = -s[e] * place_price - fee
            cash = cash + delta_cash
        if t == 0:
            shares[t] = s
        else:
            shares[t] = shares[t - 1] + s
        trans_dates.append(t_date_e)
        residual_cash.append(cash)
        prices = get_close_price(tickers, t_date_e, tickers_pl, 'Weekly')
    return trans_dates, trans_prices, shares, residual_cash
コード例 #8
0
ファイル: placement_old.py プロジェクト: easy00000000/k_chart
def est_transacton_prices_1(tickers, delta_s, tickers_pl):
    trans_prices = delta_s.copy()
    place_date = delta_s.index.date
    #t=0
    t_date = delta_s.index[0]
    prices = np.zeros(len(tickers))
    pre_t_date = t_date + datetime.timedelta(days=-1)
    for e in range(len(tickers)):
        while pre_t_date.date() not in tickers_pl[tickers[e]].index.date:
            pre_t_date = pre_t_date + datetime.timedelta(days=-1)
        c = read_price(pre_t_date, tickers[e], tickers_pl[tickers[e]])
        prices[e] = c.Close
    #t>=1
    for t in range(len(delta_s.index)):
        pre_prices = prices
        t_date = delta_s.index[t]
        t_date = t_date + datetime.timedelta(days=1)
        s = delta_s.iloc[t].copy()
        for e in range(len(tickers)):
            t_date_e = t_date
            while t_date_e.date() not in tickers_pl[tickers[e]].index.date:
                t_date_e = t_date_e + datetime.timedelta(days=1)
            if s[e] > 0.0:  # buy
                transaction = 'BUY'
            elif s[e] < 0.0:
                transaction = 'SELL'
            else:
                transaction = 'No Trade'
            place_price = get_transaction_price(t_date_e, tickers[e],
                                                transaction, pre_prices[e],
                                                tickers_pl[tickers[e]])
            trans_prices[tickers[e]].iloc[t] = place_price
        place_date[t] = t_date_e
        prices = get_close_price(tickers, t_date, tickers_pl, True)
    trans_prices.reset_index(level=0, inplace=True)
    trans_prices.Date = place_date
    trans_prices.set_index("Date", inplace=True)
    return trans_prices
コード例 #9
0
ファイル: reporting_1.py プロジェクト: easy00000000/k_chart
df = pd.read_excel(open('s.xlsx', 'rb'), sheet_name=0)
s = df.copy()
s.set_index("Date", inplace=True)

# portfolio assets
tickers = s.columns

# t=0
shares = np.zeros(len(tickers))
cash = 500000
# pre_price at t=0
t_date = s.index[0]
pre_t_date = get_working_date(t_date, True)
prices = shares.copy()
for e in range(len(tickers)):
    c = read_price(pre_t_date, tickers[e])
    prices[e] = c.Close
value = 0.0
print(pre_t_date.strftime("%Y-%m-%d"), shares, "{0:42.2f}".format(cash),
      "{0:.2f}".format(value))

# t>=1
for t in range(len(s.index)):
    pre_shares = shares
    pre_cash = cash
    pre_prices = prices
    t_date = s.index[t]
    t_date = get_working_date(t_date)
    delta_s = np.zeros(len(tickers))
    for e in range(len(tickers)):
        delta_s[e] = s[s.columns[e]][t]