예제 #1
0
def share_table2fund(share_table):
    """
    @summary converts data frame of shares into fund values
    @param share_table: data frame containing shares on days transactions occured
    @return fund : time series containing fund value over time
    @return leverage : time series containing fund value over time
    """
    # Get the data from the data store
    dataobj = de.DataAccess('mysql')
    startday = share_table.index[0]
    endday = share_table.index[-1]

    symbols = list(share_table.columns)
    symbols.remove('_CASH')

    # print symbols

    # Get desired timestamps
    timeofday = dt.timedelta(hours=16)
    timestamps = du.getNYSEdays(startday - dt.timedelta(days=5),
                                endday + dt.timedelta(days=1), timeofday)
    historic = dataobj.get_data(timestamps, symbols, ["close"])[0]
    historic.fillna(method='ffill', inplace=True)
    historic["_CASH"] = 1
    closest = historic[historic.index <= share_table.index[0]].ix[:]
    ts_leverage = pandas.Series(0, index=[closest.index[-1]])

    # start shares/fund out as 100% cash
    first_val = closest.ix[-1] * share_table.ix[0]
    fund_ts = pandas.Series([first_val.sum(axis=1)], index=[closest.index[-1]])
    prev_row = share_table.ix[0]
    for row_index, row in share_table.iterrows():
        # print row_index
        trade_price = historic.ix[row_index:].ix[0:1]
        trade_date = trade_price.index[0]

        # print trade_date

        # get stock prices on all the days up until this trade
        to_calculate = historic[(historic.index <= trade_date)
                                & (historic.index > fund_ts.index[-1])]
        # multiply prices by our current shares
        values_by_stock = to_calculate * prev_row

        # for date, sym in values_by_stock.iteritems():
        #     print date,sym
        # print values_by_stock
        prev_row = row
        #update leverage
        ts_leverage = _calculate_leverage(values_by_stock, ts_leverage)

        # calculate total value and append to our fund history
        fund_ts = fund_ts.append([values_by_stock.sum(axis=1)])
    return [fund_ts, ts_leverage]
예제 #2
0
def calculate_efficiency(dt_start_date, dt_end_date, s_stock):
    """
    @summary calculates the exit-entry/high-low trade efficiency of a stock from historical data
    @param start_date: entry point for the trade
    @param end_date: exit point for the trade
    @param stock: stock to compute efficiency for
    @return: float representing efficiency
    """
    # Get the data from the data store
    dataobj = de.DataAccess('mysql')

    # Get desired timestamps
    timeofday = dt.timedelta(hours=16)
    timestamps = du.getNYSEdays(dt_start_date,
                                dt_end_date + dt.timedelta(days=1), timeofday)
    historic = dataobj.get_data(timestamps, [s_stock], ["close"])[0]
    # print "######"
    # print historic
    hi = numpy.max(historic.values)
    low = numpy.min(historic.values)
    entry = historic.values[0]
    exit_price = historic.values[-1]
    return (((exit_price - entry) / (hi - low))[0])
예제 #3
0
def get_data(ls_symbols, ls_keys):
    '''
    @summary: Gets a data chunk for backtesting
    @param dt_start: Start time
    @param dt_end: End time
    @param ls_symbols: symbols to use
    @note: More data will be pulled from before and after the limits to ensure
           valid data on the start/enddates which requires lookback/forward
    @return: data dictionry
    '''
    print("Getting Data from MySQL")
    # Modify dates to ensure enough data for all features
    dt_start = dt.datetime(2005, 1, 1)
    dt_end = dt.datetime(2012, 8, 31)
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt.timedelta(hours=16))

    c_da = de.DataAccess('mysql')

    ldf_data = c_da.get_data(ldt_timestamps, ls_symbols, ls_keys)

    d_data = dict(list(zip(ls_keys, ldf_data)))

    return d_data