Ejemplo n.º 1
0
def gen_data(symbols, dates, holding_days, window, Ybuy, Ysell):
    """
    generate normalized indicator values (dataX), 
    and the classification based on holding_days return (dataY)
    """
    # Get prices (adj close and tipical prices)
    prices = get_prices(symbols, dates)[0]  # adj close prices
    tp_prices = get_prices(symbols, dates)[2]  # tipical prices

    # get all normalized indicator values as dataX
    dataX = get_all_indicators(prices, tp_prices, window)[1]

    # ret: holding_days return (e.g. 21 days return)
    ret = prices.copy()
    ret[holding_days:] = (prices[holding_days:] /
                          prices[:-holding_days].values) - 1
    ret.ix[:holding_days] = 0

    ### Classification
    # Generate dataY to classification values LONG(1), SHORT(-1) or HOLD(0)
    # based on holding_days return.
    dataY = ret.copy()
    dataY.ix[:, :] = np.NaN
    dataY[(ret > Ybuy)] = 1  # if ret > Ybuy, Long
    dataY[(ret <= Ybuy) & (ret < Ysell)] = -1  # else if ret < Ysell, Short
    dataY.fillna(0, inplace=True)  # else hold, do nothing

    # print ret
    # print dataY

    # convert data from dataframe into np array (to fit for RTLearner I created)
    dataX_m = dataX.as_matrix()  # convert to np array
    dataY_m = dataY.as_matrix().T[0]  # convert to 1d np array

    return dataX_m, dataY_m
Ejemplo n.º 2
0
def build_order_list(dataY, symbols, dates, holding_days, output_filename):
    """
    construct order_list from dataY, also consider holding_days
    dataY: 1d np array, Y data (classification) for each day with Long, short or do nothing result
    """
    # get prices for index template
    prices = get_prices(symbols, dates)[0]

    dataY[0] = 1  # benchmark entry

    # build order_list
    order_list = []  # a list of both entry and exit orders

    count = holding_days
    for sym in symbols:
        for i in range(dataY.shape[0]):
            if count > 0 and count < holding_days:
                count -= 1
                continue
            if dataY[i] == 0:
                continue
            if dataY[i] != 0:
                count = holding_days - 1
                exit_i = i + holding_days
                # write into order_list
                if dataY[i] > 0:
                    order_list.append(
                        [prices.index[i].date(), sym, 'BUY', 200])
                    if exit_i < dataY.shape[0]:
                        order_list.append(
                            [prices.index[exit_i].date(), sym, 'SELL', 200])
                elif dataY[i] < 0:
                    order_list.append(
                        [prices.index[i].date(), sym, 'SELL', 200])
                    if exit_i < dataY.shape[0]:
                        order_list.append(
                            [prices.index[exit_i].date(), sym, 'BUY', 200])
    """
    # print order_list        
    for order in order_list:
        print "	".join(str(x) for x in order)
    """

    # write order_list to csv file
    with open(output_filename, "wb") as f:
        writer = csv.writer(f)
        writer.writerow(['Date', 'Symbol', 'Order', 'Shares'])
        writer.writerows(order_list)
    f.close()
Ejemplo n.º 3
0
def test_run():
    # Input data
    dates_in_sample = pd.date_range('2008-01-01', '2009-12-31')
    symbols = ['AAPL']
    prices = get_prices(symbols, dates_in_sample)[0]

    order_list = []
    sym = 'AAPL'

    for day, next_day in zip(prices.index[:-1], prices.index[1:]):
        if prices.ix[day, sym] < prices.ix[next_day, sym]:
            order_list.append([day.date(), sym, 'BUY', 200])
            order_list.append([next_day.date(), sym, 'SELL', 200])

        elif prices.ix[day, sym] > prices.ix[next_day, sym]:
            order_list.append([day.date(), sym, 'SELL', 200])
            order_list.append([next_day.date(), sym, 'BUY', 200])

    # write orders to csv file
    with open("orders-bestpossible.csv", "wb") as f:
        writer = csv.writer(f)
        writer.writerow(['Date', 'Symbol', 'Order', 'Shares'])
        writer.writerows(order_list)
    f.close()
def main(information):
    
    a_list_of_prices = indicators.get_prices(information)
    print(a_list_of_prices)
    strategies(a_list_of_prices)
def compute_portvalue(symbols, dates, orders_file, start_val=100000):
    # Read in order.csv data
    orders = pd.read_csv(orders_file,
                         index_col='Date',
                         parse_dates=True,
                         na_values=['nan'])
    # sort orders by date
    orders = orders.sort_index()

    # Get prices of a given symbols and date range
    prices = get_prices(symbols, dates)[0]

    # Step 1: 'prices' dataframe
    prices['CASH'] = 1.0  # add Cash -- all = 1.0
    # print prices.ix[:10,:]

    # Step 3: 'holdings' dataframe
    # initialize 'holdings' from 'prices', fill columns by 0
    # then add start value to the first colunm of cash
    holdings = pd.DataFrame(0, index=prices.index, columns=prices.columns)
    holdings.ix[0, -1] = start_val  # should be holdings.ix[:,-1] = start_val

    # read orders from the 'orders' dataframe
    # calculate leverage, update holdings, etc.
    # if one order void leverage, then cancel all trades on that day
    for row in orders.itertuples():
        # pass orders in non trading days
        if row.Index not in prices.index:
            continue

        # read in shares
        if row.Order == 'BUY':
            shares = row.Shares
        else:
            shares = -row.Shares

        # calculate leverage_before the order placed
        hld_before = holdings.ix[row.Index, :].copy()
        val_before = prices.ix[row.Index, :] * hld_before
        leverage_before = sum(abs(val_before[:-1])) / sum(val_before)

        # calculate leverage after the order placed
        hld = holdings.ix[row.Index, :].copy()
        hld[row.Symbol] += shares
        hld['CASH'] -= prices.ix[row.Index, row.Symbol] * shares
        val = prices.ix[row.Index, :] * hld
        leverage = sum(abs(val[:-1])) / sum(val)
        # print "lev-bef: ", leverage_before, "lev: ", leverage

        if True:  # leverage < 1.5 or leverage < leverage_before:
            # make order happen
            holdings.ix[row.Index, row.Symbol] += shares
            holdings.ix[row.Index,
                        'CASH'] -= prices.ix[row.Index, row.Symbol] * shares
            holdings.ix[row.Index:, :] = holdings.ix[row.Index, :].values

    # Step 4: 'values' dataframe
    values = prices * holdings

    # Step 5: 'portvals'
    portvals = values.sum(axis=1)

    return portvals
Ejemplo n.º 6
0
def build_orders(symbols, dates, holding_days, window, output_filename):
    # Get prices (adj close and tipical prices)
    prices = get_prices(symbols, dates)[0]
    tp_prices = get_prices(symbols, dates)[2]

    # get all indicator values (not normalized)
    indicators = get_all_indicators(prices, tp_prices, window)[0]
    # bollinger band precent
    bbp = indicators['BBP']
    # price/sma
    price_sma_ratio = indicators['PRICE_SMA']
    # momentum
    momentum = indicators['MOMENTUM']
    # cci
    cci = indicators['CCI']

    ### Use the four indicators to make rule based decision

    # Holdings starts as a NaN array of the same shape/index as price.
    orders = prices.copy()
    orders.ix[:, :] = np.NaN

    # Apply our entry order conditions all at once.  This represents our TARGET SHARES
    # at this moment in time, not actual orders.

    # orders didn't consider holding_days at this stage
    orders.ix[0, :] = 200  # benchmark entry
    orders[(momentum > 0)
           & ((price_sma_ratio < 0.95) | (bbp < 0) | (cci < -50))] = 200
    orders[(momentum < 0)
           & ((price_sma_ratio > 1.05) | (bbp > 1) | (cci > 150))] = -200
    orders.ffill(inplace=True)
    orders.fillna(0, inplace=True)

    # construct order_list from orders dataframe, also consider holding_days
    order_list = []  # a list of both entry and exit orders

    count = holding_days
    for sym in symbols:
        for i in range(orders.shape[0]):
            if count > 0 and count < holding_days:
                count -= 1
                continue
            if orders.ix[i, sym] == 0:
                continue
            if orders.ix[i, sym] != 0:
                count = holding_days - 1
                exit_i = i + holding_days
                # write into order_list
                if orders.ix[i, sym] > 0:
                    order_list.append(
                        [orders.index[i].date(), sym, 'BUY', 200])
                    if exit_i < orders.shape[0]:
                        order_list.append(
                            [orders.index[exit_i].date(), sym, 'SELL', 200])
                elif orders.ix[i, sym] < 0:
                    order_list.append(
                        [orders.index[i].date(), sym, 'SELL', 200])
                    if exit_i < orders.shape[0]:
                        order_list.append(
                            [orders.index[exit_i].date(), sym, 'BUY', 200])

    # print order_list
    for order in order_list:
        print "	".join(str(x) for x in order)

    # write order_list to csv file
    with open(output_filename, "wb") as f:
        writer = csv.writer(f)
        writer.writerow(['Date', 'Symbol', 'Order', 'Shares'])
        writer.writerows(order_list)
    f.close()
def main(information):

    a_list_of_prices = indicators.get_prices(information)
    print(a_list_of_prices)
    strategies(a_list_of_prices)