コード例 #1
0
ファイル: stockdata.py プロジェクト: Greyvend/tradersite
def history_run(stocks, index, time_period, start_date, end_date=date.today()):
    """
    Run PCA signal on desired history period with particular stocks and
    benchmark index
    :param stocks: list of stock names to run strategy on
    :param index: name of benchmark index to compare risks with
    :param time_period: size of time period, for which prices will be given
    to signal function
    :param start_date: beginning date to run from.
    Important note: Some time from the beginning of this period is required for
    the statistic gathering, so signal will be run with some lag from
    start_date
    :param end_date: final date to run signal on
    :return: trinity of: dates - list of dates on which signal was run,
    prices - list of lists of corresponding prices for all the stocks,
    signals - list of signal results. Every of those is a list too.
    """

    def backward_chunks(l, amount, size, pos):
        result = []
        for i in range((pos - size * amount), pos, size):
            result.append(l[i:i+size])
        return result

    #gather and split price data in equal chunks to feed signal
    dates, index_prices = _get_prices(index, start_date, end_date)

    #split stock data in the same way
    all_prices = []

    # define position to be a starting history point. It should be shifted,
    # because k preceding index price time periods are required for the
    # first stock price time period
    start_position = (pca.k - 1) * time_period
    for stock in stocks:
        history = _get_prices(stock, start_date, end_date)[1][start_position:]
        all_prices.append(history)

    # make successive calls to signal function with continuous time history
    signals = []
    stop_pos = len(all_prices[0]) + 1
    for t in range(time_period, stop_pos):
        # select prices of certain time period
        prices = []
        for stock_price_list in all_prices:
            prices.append(stock_price_list[t - time_period:t])

        # divide index list to chunks of length k, according to algorithm
        # requirements
        split_index = backward_chunks(index_prices, pca.k, time_period,
                                      t + start_position)

        # call algorithm function
        try:
            signals.append(pca.signal(prices, split_index))
        except pca.WrongPricesError, pca.WrongParameterException:
            raise
コード例 #2
0
ファイル: tests.py プロジェクト: Greyvend/tradersite
 def simple_test_random_values(self):
     #parameters
     pca.k = 2
     pca.H = 2
     prices = [[1.0, 1.1, 1.2, 1.3], [2.0, 2.1, 2.2, 2.3], [3.0, 3.1, 3.2,
                                                            3.3]]
     index = [[randrange(1, 20) for j in range(4)] for i in range(pca.k)]
     results = pca.signal(prices, index)
     self.assertEqual(results, ['sell', 'sell', 'sell'])