Exemplo n.º 1
0
def get_history_table():  # TODO: check app when markets are open
    request_date = get_db_date()

    with database.connect_db() as db:
        symbols = ["VIX", "VXST", "VXV", "VXMT", "VVIX", "SPX", "XIV", "VXX", "TLT"]
        query_dates = [database.encode_date(request_date - timedelta(days=30)), database.encode_date(request_date)]
        db.row_factory = stocks.quote_factory
        db.execute(
            "SELECT * FROM stocks WHERE symbol IN (%s) "
            "AND date BETWEEN ? and ? "
            "ORDER BY symbol, date;" % ",".join("?" * len(symbols)),
            symbols + query_dates,
        )
        quotes = db.fetchall()
        db.row_factory = None
        db.execute(
            "SELECT vol1d, vol2d, vol3d, vol4d, vol5d, vol6d, "
            "vol7d, vol8d, vol9d FROM volatility "
            "WHERE symbol=? AND date BETWEEN ? and ? "
            "ORDER BY date;",
            ["SPX"] + query_dates,
        )
        volatilities = np.array(db.fetchall()).T

    data = {}
    for symbol, quotes in groupby(quotes, key=itemgetter(0)):
        quotes = list(quotes)
        data[symbol] = ["%.2f" % quote.close for quote in quotes[-10:]]
        data["%s_change" % symbol] = [
            "%+.2f %%" % ((quote.close / prev_quote.close - 1) * 100)
            for quote, prev_quote in izip(quotes[-10:], quotes[-11:-1])
        ]
        if symbol in ["VIX", "VXST", "VXV", "VXMT"]:
            data["%s_color" % symbol] = [colorpicker.get_value_color(abs(quote.close), 5, 80) for quote in quotes[-10:]]
    data["Date"] = [quote.date.strftime("%d.%m.%y") for quote in quotes[-10:]]
    if (request.args.get("date") == "") and not dates.are_markets_open() and len(data["Date"]) > 0:
        data["Date"][-1] = "ETH"

    for period, row in enumerate(volatilities, 1):
        data["SPX vol. %d" % period] = ["%.2f" % item for item in row[-10:]]
        data["SPX vol. %d_color" % period] = [colorpicker.get_value_color(abs(item), 5, 80) for item in row[-10:]]
    return jsonify(data)
Exemplo n.º 2
0
 def test_are_markets_open_at_weekend(self):
     now = datetime(2015, 12, 7, 9, 29)
     self.assertFalse(dates.are_markets_open(now))
Exemplo n.º 3
0
 def test_are_markets_open_at_close(self):
     now = datetime(2015, 12, 8, 16, 0)
     self.assertFalse(dates.are_markets_open(now))
Exemplo n.º 4
0
 def test_are_markets_open_before_close(self):
     now = datetime(2015, 12, 8, 15, 59)
     self.assertTrue(dates.are_markets_open(now))
Exemplo n.º 5
0
 def test_are_markets_open_at_open(self):
     now = datetime(2015, 12, 8, 9, 30)
     self.assertTrue(dates.are_markets_open(now))
Exemplo n.º 6
0
 def test_are_markets_open_before_open(self):
     now = datetime(2015, 12, 8, 9, 29)
     self.assertFalse(dates.are_markets_open(now))