Exemplo n.º 1
0
 def history_min(self, symbol, window):
     # yahoo_symbol = Symbols.get_mapped_symbol(symbol, Symbols.YahooSymbolMapping)
     us_dt = datetime.datetime.now(tz=pytz.timezone('US/Eastern'))
     end_time = datetime.datetime(us_dt.year, us_dt.month, us_dt.day, us_dt.hour, us_dt.minute, us_dt.second)
     days_window = window/391 + 2
     from_date = TradeTime.get_from_date_by_window(days_window)
     start_time = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0)
     rows = YahooEquityDAO().get_min_time_and_price(symbol, start_time, end_time)
     return rows[-window:]
Exemplo n.º 2
0
 def history_min(self, symbol, window, current_date_time):
     # us_dt = datetime.datetime.now(tz=pytz.timezone('US/Eastern'))
     # end_time = datetime.datetime(us_dt.year, us_dt.month, us_dt.day, us_dt.hour, us_dt.minute, us_dt.second)
     end_time = current_date_time
     days_window = window/391 + 2
     from_date = TradeTime.get_from_date_by_window(days_window, current_date_time.date())
     start_time = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0)
     rows = YahooEquityDAO().get_min_time_and_price(symbol, start_time, end_time)
     return rows[-window:]
Exemplo n.º 3
0
 def history(self, symbol, field, window, current_date):
     fields_dic = {'open': 'openPrice', 'close': 'adjclosePrice', 'high': 'highPrice', 'low': 'lowPrice',
                   'price': 'adjclosePrice', 'unadj':'closePrice'}
     fields = fields_dic.keys()
     if field.lower() not in field:
         raise Exception('the field should be in %s...'%fields)
     price_field = fields_dic[field]
     yahoo_symbol = Symbols.get_mapped_symbol(symbol, Symbols.YahooSymbolMapping)
     from_date = TradeTime.get_from_date_by_window(window+1, current_date)  # window + 1: get one day more data.
     rows = YahooEquityDAO().get_equity_prices_by_start_end_date(yahoo_symbol, from_date, current_date, price_field)
     return rows[0:window]  # remove current date data.
Exemplo n.º 4
0
 def history(self, symbol, field, window):
     fields_dic = {'open': 'openPrice', 'close': 'adjclosePrice', 'high': 'highPrice', 'low': 'lowPrice',
                   'price': 'adjclosePrice', 'unadj':'closePrice'}
     fields = fields_dic.keys()
     if field.lower() not in field:
         raise Exception('the field should be in %s...'%fields)
     price_field = fields_dic[field]
     yahoo_symbol = Symbols.get_mapped_symbol(symbol, Symbols.YahooSymbolMapping)
     from_date = TradeTime.get_from_date_by_window(window)
     rows = YahooEquityDAO().get_all_equity_price_by_symbol(yahoo_symbol, from_date.strftime('%Y-%m-%d'), price_field)
     return rows
Exemplo n.º 5
0
 def filter_liquidity_symbols(self,
                              current_date=None,
                              window=30,
                              count=50,
                              ignore_symbols=['BIL', 'IEF', 'XIV']):
     if current_date is None:
         current_date = TradeTime.get_latest_trade_date()
     from_date = TradeTime.get_from_date_by_window(window, current_date)
     ignore_symbols_sql = ','.join(
         map(lambda x: '\'%s\'' % x, ignore_symbols))
     sql_template = """SELECT symbol, avg(adjClosePrice * volume) as liquidity FROM tradehero.yahoo_equity where tradeDate > '{}' and tradeDate <='{}'  and symbol not like '^%' and symbol not like '%.SS' and symbol not in ({}) group by symbol order by liquidity desc;"""
     sql = sql_template.format(from_date, current_date, ignore_symbols_sql)
     rows = self.select(sql)
     return map(lambda x: x[0], rows[:count])
Exemplo n.º 6
0
 def history_min(self, symbol, window):
     url_template = 'http://ondemand.websol.barchart.com/getHistory.json?apikey={}&symbol={}.BZ&type=formTMinutes&startDate={}00'
     days_window = window / 391 + 2
     from_date = TradeTime.get_from_date_by_window(days_window)
     start_time = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0)
     url = url_template.format(self.api_key, symbol, start_time.strftime('%Y%m%d%M%S'))
     content = HttpHelper.http_get(url)
     data = json.loads(content)
     if data['status']['code'] != 200:
         raise Exception('http response unexcepted, the the content is: %s' % content)
     else:
         rows = map(lambda x: [datetime.datetime.strptime(x['timestamp'][:-6], '%Y-%m-%dT%H:%M:%S'), x['close']], data['results'])
         rows = filter(lambda x: TradeTime.is_valid_trade_time(x[0]), rows)
         rows.sort(key=lambda x: x[0])
         return rows[-window:]
Exemplo n.º 7
0
 def history(self, symbol, field, window):
     from_date = TradeTime.get_from_date_by_window(window)
     url_template = 'http://ondemand.websol.barchart.com/getHistory.json?apikey={}&symbol={}.BZ&type=daily&startDate={}000000'
     url = url_template.format(self.api_key, symbol, from_date.strftime('%Y%m%d'))
     fields_dic = {'open': 'open', 'close': 'close', 'high': 'high', 'low': 'low',
                   'price': 'close', 'unadj': 'close'}
     fields = fields_dic.keys()
     if field.lower() not in field:
         raise Exception('the field should be in %s...'%fields)
     price_field = fields_dic[field]
     content = HttpHelper.http_get(url)
     data = json.loads(content)
     if data['status']['code'] != 200:
         raise Exception('http response unexcepted, the the content is: %s'%content)
     else:
         rows = map(lambda x: [datetime.datetime.strptime(x['tradingDay'], '%Y-%m-%d'), x[price_field]], data['results'])
         return rows
Exemplo n.º 8
0
    def GET(self, from_date_str=None):
        default_from_date = TradeTime.get_latest_trade_date() - datetime.timedelta(60)
        if from_date_str is None or from_date_str == '':
            from_date = default_from_date
        else:
            try:
                input_from_date = datetime.datetime.strptime(from_date_str, '%Y-%m-%d').date()
                from_date = TradeTime.get_from_date_by_window(22, input_from_date)
            except Exception:
                from_date = default_from_date
        records_svxy = YahooEquityDAO().get_all_equity_price_by_symbol('SVXY', from_date.strftime('%Y-%m-%d'), 'Closeprice')
        dates = map(lambda x: x[0], records_svxy)
        price_svxy = map(lambda x: x[1], records_svxy)

        # shift
        append_dates = TradeTime.generate_dates(dates[-1], dates[-1] + datetime.timedelta(days=50))
        dates = dates[21:] + append_dates[1:22]
        price_svxy = price_svxy[21:] + [price_svxy[-1]] * 21

        if from_date < default_from_date:
            dates = dates[:42]
            price_svxy = price_svxy[:42]

        fig = Figure(figsize=[24, 4])
        ax = fig.add_axes([.1, .1, .8, .8])
        ax.plot(dates, price_svxy, label='SVXY')
        ax.legend(loc='upper left')
        ax.grid()
        ax.xaxis.set_major_formatter(DateFormatter('%y%m%d'))
        ax.set_xticks(dates)
        for tick in ax.get_xticklabels():
            tick.set_rotation(45)
        canvas = FigureCanvasAgg(fig)
        buf = cStringIO.StringIO()
        canvas.print_png(buf)
        data = buf.getvalue()
        return data
Exemplo n.º 9
0
    def GET(self, from_date_str=None):
        default_from_date = TradeTime.get_latest_trade_date() - datetime.timedelta(60)
        if from_date_str is None or from_date_str == '':
            from_date = default_from_date
        else:
            try:
                input_from_date = datetime.datetime.strptime(from_date_str, '%Y-%m-%d').date()
                from_date = TradeTime.get_from_date_by_window(22, input_from_date)
            except Exception:
                from_date = default_from_date
        # records_index = VIXDAO().get_vix_price_by_symbol_and_date('VIY00', from_date=from_date)
        records_index = YahooEquityDAO().get_all_equity_price_by_symbol('^VIX', from_date.strftime('%Y-%m-%d'),'Closeprice')
        (records_f1, records_f2, records_f3) = VIXDAO().get_following_vix(from_date)
        new_spy_price = None
        if datetime.date.today() > TradeTime.get_latest_trade_date():
            try:
                new_spy_price = YahooScraper.get_data_by_symbol('SPY')
            except Exception as e:
                new_spy_price = CNBCScraper.get_data_by_symbol('SPY')
            try:
                new_vix_price = YahooScraper.get_data_by_symbol('VIX')
            except Exception as e:
                new_vix_price = CNBCScraper.get_data_by_symbol('VIX')
            new_vix_features = CBOEScraper.get_vix_future()
            [new_f1, new_f2, new_f3] = [new_vix_features[0][2], new_vix_features[1][2], new_vix_features[2][2]]
            records_index.append([datetime.date.today(), new_vix_price])
            records_f1.append([datetime.date.today(), new_f1])
            records_f2.append([datetime.date.today(), new_f2])
            records_f3.append([datetime.date.today(), new_f3])
        dates = map(lambda x: x[0], records_index)
        price_index = map(lambda x: x[1], records_index)
        price_f1 = map(lambda x: x[1], records_f1)
        price_f2 = map(lambda x: x[1], records_f2)
        price_f3 = map(lambda x: x[1], records_f3)
        hv_records = self.get_historical_volatility(new_spy_price, from_date)[-len(dates):]
        hv_prices = map(lambda x: x[1]*100, hv_records)
        spy_prices = self.get_equity_prices('SPY', from_date, new_spy_price)
        [spy_low, spy_high] = self.get_low_and_upper(spy_prices, price_index)

        # shift
        append_dates = TradeTime.generate_dates(dates[-1], dates[-1] + datetime.timedelta(days=50))

        dates = dates[21:] + (append_dates[0:21])
        hv_prices = self.shift_values(hv_prices)
        spy_prices = self.shift_values(spy_prices)
        price_index = self.shift_values(price_index)
        price_f1 = self.shift_values(price_f1)
        price_f2 = self.shift_values(price_f2)
        price_f3 = self.shift_values(price_f3)

        if from_date < default_from_date:
            dates = dates[:42]
            hv_prices = hv_prices[:42]
            spy_prices = spy_prices[:42]
            spy_low = spy_low[:42]
            price_index = price_index[:42]
            price_f1 = price_f1[:42]
            price_f2 = price_f2[:42]
            price_f3 = price_f3[:42]


        fig = Figure(figsize=[24, 8])
        ax1 = fig.add_axes([.1, .1, .8, .8])
        ax2 = ax1.twinx()
        ax1.plot(dates, hv_prices, label='historical volatility', color='black')
        ax1.plot(dates, price_index, label='vix index', color='blue')
        ax1.plot(dates, price_f1, label='vix first month', color='deepskyblue')
        ax1.plot(dates, price_f2, label='vix second month', color='lightskyblue')
        ax1.plot(dates, price_f3, label='vix third month', color='lightblue')
        ax1.legend(loc='upper left')

        ax2.plot(dates, spy_prices, 'red', label='SPY')
        ax2.plot(dates, spy_low, 'orange', label='spy_low')
        # ax2.plot(dates, spy_high, 'yellow', label='spy_high')
        ax2.legend(loc='upper right')
        ax1.grid()
        ax1.xaxis.set_major_formatter(DateFormatter('%y%m%d'))
        ax1.set_xticks(dates)
        for tick in ax1.get_xticklabels():
            tick.set_rotation(45)
        canvas = FigureCanvasAgg(fig)
        buf = cStringIO.StringIO()
        canvas.print_png(buf)
        data = buf.getvalue()
        return data