Example #1
0
def cumulative_state(transactions):
    dt = pd.DataFrame(transactions)
    dates = get_dates(transactions)
    timestamps = get_timestamps(dates)
    tickers = rt.all_tickers(transactions)
    cs = prepare_cs(dates, timestamps)
    for ticker in tickers:
        lq = list()
        lp = list()
        lv = list()
        for ldate in dates:
            quantity = dt[(dt.ticker == ticker) & (dt.dt_date <= ldate)]['quantity'].sum()
            price = cv.close_price_from(ldate, ticker)
            lq.append(quantity)
            lp.append(price)
            lv.append(quantity * price)
        cs[ticker + '_q'] = lq
        cs[ticker + '_p'] = lp
        cs[ticker + '_v'] = lv

    tv_list = list()
    for ldate in dates:
        total_value = 0.0
        for ticker in tickers:
            total_value = total_value + float(cs[cs.dt_date == ldate][ticker + '_v'])
        tv_list.append(total_value)
    cs['total_value'] = tv_list
    return cs
def test_all_tickers():
    t = rt.read_file('transactions')
    l = rt.all_tickers(t)
    assert l.count('tsla') == 1
    assert l.count('ibm') == 1
    assert l.count('scty') == 1