예제 #1
0
def view_recommendations(request, number):
    number = int(number)

    c_date_size = 48
    b_date_size = 6
    a_date_size = 18

    today = datetime.now(utc)
    b_start = today - timedelta(b_date_size)
    a_start = b_start - timedelta(a_date_size)
    c_start = a_start - timedelta(c_date_size)

    statement = '''
SELECT * FROM (
WITH tc AS (SELECT symbol_id, Avg(close) FROM data_quote WHERE date >= '{0:%Y-%m-%d}' AND date < '{1:%Y-%m-%d}' GROUP BY symbol_id),
     ta AS (SELECT symbol_id, Avg(close) FROM data_quote WHERE date >= '{1:%Y-%m-%d}' AND date < '{2:%Y-%m-%d}' GROUP BY symbol_id),
     tb AS (SELECT symbol_id, Avg(close) FROM data_quote WHERE date >= '{2:%Y-%m-%d}' AND date <= '{3:%Y-%m-%d}' GROUP BY symbol_id)
       SELECT tc.symbol_id as symbol_id, tc.avg as c, tb.avg as b, ta.avg as a FROM tc
         INNER JOIN ta on tc.symbol_id = ta.symbol_id
         INNER JOIN tb on ta.symbol_id = tb.symbol_id
           WHERE tc.avg > tb.avg AND tb.avg > ta.avg
) t INNER JOIN data_symbol ON id = symbol_id
      ORDER BY ((c - a) * (b - a) * (c - b)) DESC LIMIT {4};
'''.format(c_start, a_start, b_start, today, number)

    symbols = Symbol.objects.raw(statement)

    result = []
    for symbol in symbols:
        price = get_price(symbol)
        result.append(serialize_symbol(symbol, price))

    return json_response(result)
예제 #2
0
def view_recommendations(request, number):
    number = int(number)

    c_date_size = 48
    b_date_size = 6
    a_date_size = 18

    today = datetime.now(utc)
    b_start = today - timedelta(b_date_size)
    a_start = b_start - timedelta(a_date_size)
    c_start = a_start - timedelta(c_date_size)

    statement = '''
SELECT * FROM (
WITH tc AS (SELECT symbol_id, Avg(close) FROM data_quote WHERE date >= '{0:%Y-%m-%d}' AND date < '{1:%Y-%m-%d}' GROUP BY symbol_id),
     ta AS (SELECT symbol_id, Avg(close) FROM data_quote WHERE date >= '{1:%Y-%m-%d}' AND date < '{2:%Y-%m-%d}' GROUP BY symbol_id),
     tb AS (SELECT symbol_id, Avg(close) FROM data_quote WHERE date >= '{2:%Y-%m-%d}' AND date <= '{3:%Y-%m-%d}' GROUP BY symbol_id)
       SELECT tc.symbol_id as symbol_id, tc.avg as c, tb.avg as b, ta.avg as a FROM tc
         INNER JOIN ta on tc.symbol_id = ta.symbol_id
         INNER JOIN tb on ta.symbol_id = tb.symbol_id
           WHERE tc.avg > tb.avg AND tb.avg > ta.avg
) t INNER JOIN data_symbol ON id = symbol_id
      ORDER BY ((c - a) * (b - a) * (c - b)) DESC LIMIT {4};
'''.format(c_start, a_start, b_start, today, number)

    symbols = Symbol.objects.raw(statement)

    result = []
    for symbol in symbols:
        price = get_price(symbol)
        result.append(serialize_symbol(symbol, price))

    return json_response(result)
예제 #3
0
def list_favourites(request):
    if not request.user.is_authenticated():
        return HttpResponseForbidden()

    symbols = request.user.investor.favourites.all()
    return json_response([{
        'name': s.name,
        'ticker': s.ticker,
        'exchange': s.exchange.abbreviation
    } for s in symbols])
예제 #4
0
def _favourite_symbol(request, add, symbol):
    if not request.user.is_authenticated():
        return HttpResponseForbidden()

    investor = request.user.investor
    if add:
        investor.favourites.add(symbol)
    else:
        investor.favourites.remove(symbol)
    investor.save()
    return json_response({'success': 'that\'s what i\'m talking about'})
예제 #5
0
def _favourite_symbol(request, add, symbol):
    if not request.user.is_authenticated():
        return HttpResponseForbidden()

    investor = request.user.investor
    if add:
        investor.favourites.add(symbol)
    else:
        investor.favourites.remove(symbol)
    investor.save()
    return json_response({'success': 'that\'s what i\'m talking about'})
예제 #6
0
def list_favourites(request):
    if not request.user.is_authenticated():
        return HttpResponseForbidden()

    symbols = request.user.investor.favourites.all()
    return json_response([{'name': s.name, 'ticker': s.ticker, 'exchange': s.exchange.abbreviation} for s in symbols])