Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 3
0
def view_stock(request, exchange, ticker):
    try:
        symbol = Symbol.objects.get(ticker=ticker,
                                    exchange__abbreviation=exchange)
    except Symbol.DoesNotExist:
        return json_response({'error': 'Symbol not found'})

    price = get_price(symbol)
    result = serialize_symbol(symbol, price)
    return json_response(result)
Exemplo n.º 4
0
def view_stock(request, exchange, ticker):
    try:
        symbol = Symbol.objects.get(ticker=ticker,
                                    exchange__abbreviation=exchange)
    except Symbol.DoesNotExist:
        return json_response({'error': 'Symbol not found'})

    price = get_price(symbol)
    result = serialize_symbol(symbol, price)
    return json_response(result)
Exemplo n.º 5
0
def view_index(request, ticker):
    try:
        index = Symbol.objects.get(ticker=ticker)
    except Symbol.DoesNotExist:
        return json_response({'error': 'Index not found'})

    price = get_price(index)
    components = get_components(index)

    result = serialize_symbol(index, price)
    prices = get_prices(components)
    result['components'] = [serialize_symbol(p.symbol, p) for p in prices]

    return json_response(result)
Exemplo n.º 6
0
def view_index(request, ticker):
    try:
        index = Symbol.objects.get(ticker=ticker)
    except Symbol.DoesNotExist:
        return json_response({'error': 'Index not found'})

    price = get_price(index)
    components = get_components(index)

    result = serialize_symbol(index, price)
    prices = get_prices(components)
    result['components'] = [serialize_symbol(p.symbol, p) for p in prices]

    return json_response(result)
Exemplo n.º 7
0
def view_indexes(request, number):
    indexes = Symbol.objects.filter(type=Symbol.INDEX)
    prices = [get_price(s) for s in indexes]
    return json_response([serialize_symbol(p.symbol, p) for p in prices])
Exemplo n.º 8
0
import datetime
import sys

from data.cache import get_price, get_quotes
from data.models import get_symbol, get_exchange


try:
    lse = get_exchange('LSE')
    ftse = get_symbol(lse, 'FTSE')
except:
    print 'Please run the setup script first!'
    sys.exit(1)


start_date = datetime.date(year=2012, month=2, day=2)
end_date = datetime.date(year=2012, month=8, day=2)

quotes = get_quotes(ftse, start_date, end_date)

for quote in quotes:
    print quote


for symbol in ftse.components.all():
    price = get_price(symbol)
    print price
Exemplo n.º 9
0
def view_indexes(request, number):
    indexes = Symbol.objects.filter(type=Symbol.INDEX)
    prices = [get_price(s) for s in indexes]
    return json_response([serialize_symbol(p.symbol, p) for p in prices])