Ejemplo n.º 1
0
 def test_get_returns_and_pl_average_return(self):
     """
     Run on simple 2 period time schema and verify that average return is
     being processed correctly. No positions are closed.
     """
     all_prices = [[1., 2.], [3., 4.], [5., 6.]]
     signals = [['buy', 'buy', 'sell'],
                [None, None, None]]
     returns, p_and_l = stockdata.get_returns_and_pl(all_prices, signals)
     self.assertEqual(returns, [0, -0.38077249551779302])
     self.assertEqual(p_and_l, [0, 0])
Ejemplo n.º 2
0
 def test_get_returns_and_pl_closing_trades(self):
     """
     Run on simple 2 period time schema and verify that trades are closed
     and P&L is counted correctly.
     """
     all_prices = [[1., 2.], [3., 4.], [5., 6.]]
     signals = [['buy', 'buy', 'sell'],
                ['sell', 'sell', 'buy']]
     returns, p_and_l = stockdata.get_returns_and_pl(all_prices, signals)
     self.assertEqual(returns, [0, -0.38077249551779302])
     self.assertEqual(p_and_l, [0, 1.0])
Ejemplo n.º 3
0
 def test_get_returns_and_pl_3_days_range(self):
     """
     Run on 2 period time schema and verify that double 'buy' doesn't
     produce any changes. After closing long position another 'sell' is
     made on day 3.
     """
     all_prices = [[1., 2., 2.6, 2.9], [3., 4., 4.4, 4.9], [5., 6., 7.13,
                                                         7.18]]
     signals = [['buy', 'buy', 'sell'],
                ['sell', 'buy', 'buy'],
                ['sell', None, 'sell'],
                ['buy', None, 'buy']]
     returns, p_and_l = stockdata.get_returns_and_pl(all_prices, signals)
     self.assertEqual(returns, [0, -0.38077249551779302,
                                -0.76376474777389891, -0.95015550861867115])
     self.assertEqual(p_and_l, [0, 0.0, 0.0, -0.34999999999999964])
Ejemplo n.º 4
0
def run(request):
    def graph_img(x, y, name):
        def y_precise():
            result = ["chd=t:"]
            for elem in y:
                result.append(str(elem))
                result.append(',')
            result[-1] = '&'
            return ''.join(result)

        G = Line(y, encoding='text')
        G.axes.type('xy')
        min_y = min(y)
        max_y = max(y)
        G.axes.range(1, min_y, max_y)
        G.scale(min_y, max_y)
        G.axes.label(0, x[0], x[-1])
        G.axes.label(1, min_y, max_y)
        G.title(name)
        image_code = G.img()
        norm_img = re.sub(r'chd=t.*?\&', y_precise(), image_code)
        if len(norm_img) > 2000:
            if len(image_code) > 2000:
                raise LongPeriodException()
            else:
                return image_code
        return norm_img
    try:
        # getting parameters
        start_date = datetime.strptime(request.POST['start_date'],
                                       '%Y-%m-%d').date()
        end_date = datetime.strptime(request.POST['end_date'],
                                     '%Y-%m-%d').date()
        stock_amount = int(request.POST['stock_amount'])
        time_period = int(request.POST['time_period'])
        k = int(request.POST['k'])
        H = int(request.POST['H'])
        regime_switcher = request.POST['regime_switcher']
    except KeyError:
        raise
    # starting our job
    pca.k = k
    pca.H = H
    if regime_switcher == 'off':
        pca.regime_switcher = False
    else:
        pca.regime_switcher = True
    index = '^GSPC'
    source = os.path.join(settings.BASE_DIR,
                          'strategy_runner/stock_names/'
                          'NASDAQ.csv')
    stocks = stockdata.get_stock_names(source, stock_amount)
    try:
        dates, prices, signals = stockdata.history_run(stocks, index,
                                                       time_period, start_date,
                                                       end_date)
    except (pca.WrongParameterException, pca.WrongPricesError) as e:
        return render(request, 'strategy_runner/base_history.html',
                      {'error_message': str(e)})
    date_strings = [date.strftime('%Y-%m-%d') for date in dates]
    returns, pl = stockdata.get_returns_and_pl(prices, signals)

    #prepare img tags with graphs to pass to the template
    graph_imgs = []
    try:
        for i, price_list in enumerate(prices):
            graph_imgs.append(graph_img(date_strings, price_list, stocks[i]))
        graph_imgs.append(graph_img(date_strings, returns,
                                    'Cumulative Log Returns'))
        graph_imgs.append(graph_img(date_strings, pl, r'Profit/Loss'))
    except LongPeriodException as e:
        return render(request, 'strategy_runner/base_history.html',
                      {'error_message': str(e)})

    # NamedPrices = namedtuple('NamedPrices', ['stock', 'prices'])
    # named_prices = []
    # for i, stock in enumerate(stocks):
    #     named_prices.append(NamedPrices(stock, prices[i]))
    return render(request,
                  'strategy_runner/base_history.html',
                  {'graph_imgs': graph_imgs})