Exemplo n.º 1
0
    def GET(self, symbol, days):
        try:
            days = int(days)
        except Exception:
            days = 63
        from_date = TradeTime.get_latest_trade_date() - datetime.timedelta(days)
        equity_records = EquityDAO().get_all_equity_price_by_symbol(symbol, from_date)
        if datetime.date.today() > TradeTime.get_latest_trade_date():
            new_spy_price = YahooScraper.get_data_by_symbol(symbol)
            equity_records.append([datetime.date.today(), new_spy_price])
        dates = map(lambda x: x[0], equity_records)
        equity_prices = map(lambda x: x[1], equity_records)
        fig = Figure(figsize=[16, 4])
        ax = fig.add_axes([.1, .1, .8, .9])
        ax.plot(dates, equity_prices, label='price')
        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)

        # conver to canvas
        canvas = FigureCanvasAgg(fig)
        buf = cStringIO.StringIO()
        canvas.print_png(buf)
        data = buf.getvalue()
        return data
Exemplo n.º 2
0
 def get_following_vix(self, from_date=None, to_date=None):
     from_date = from_date or TradeTime.get_latest_trade_date(
     ) - datetime.timedelta(30)
     to_date = to_date or TradeTime.get_latest_trade_date()
     #self.logger.info('today=%s, from_date=%s, to_date=%s'%(datetime.datetime.today(), from_date, to_date))
     symbols = VIX.get_vix_symbol_list(from_date, to_date, 3)
     #records_index = self.get_vix_price_by_symbol('VIY00')
     symbol_dic = {}
     for symbol in symbols:
         symbol_dic[symbol] = list_to_hash(
             self.get_vix_price_by_symbol_and_date(symbol, from_date,
                                                   to_date))
     days = (to_date - from_date).days + 1
     records_f1 = []
     records_f2 = []
     records_f3 = []
     for i in range(days):
         date = from_date + datetime.timedelta(days=i)
         if TradeTime.is_trade_day(date):
             symbol_f1 = VIX.get_f1_by_date(date)
             symbol_f2 = VIX.get_f2_by_date(date)
             symbol_f3 = VIX.get_f3_by_date(date)
             records_f1.append(
                 [date, symbol_dic[symbol_f1].get(date), symbol_f1])
             records_f2.append(
                 [date, symbol_dic[symbol_f2].get(date), symbol_f2])
             records_f3.append(
                 [date, symbol_dic[symbol_f3].get(date), symbol_f3])
     # self.logger.info([records_f1[-1], records_f2[-1]], records_f3[-1]])
     return (records_f1, records_f2, records_f3)
Exemplo n.º 3
0
 def _generate_historical_daily(self, symbol, window, current_date):
     dates = TradeTime.generate_trade_dates_by_window(window + 1, current_date)
     start_date = dates[0]
     # end_date = current_date
     end_date = TradeTime.get_latest_trade_date()
     all_dates = TradeTime.generate_dates(start_date, TradeTime.get_latest_trade_date())
     prices = MontCarloSimulator.simulate_daily(symbol, start_date, end_date, len(all_dates), 1)
     rows = map(lambda x, y: [x, y], all_dates, prices[0])
     return rows
Exemplo n.º 4
0
 def get_daily_price(self, symbol):
     # return self.history(symbol, window=1)[0]
     date = self.specified_date_time.date()
     if symbol in self.provider.cache.keys() and date >= self.provider.cache[symbol][0]:
         dic = self.provider.cache[symbol][1]
         for i in range(15):
             if date not in dic:
                 date = date - datetime.timedelta(days=1)
             else:
                 return dic[date]
     else:
         window = len(TradeTime.generate_dates(date, TradeTime.get_latest_trade_date()))
         rows = self.provider.history(symbol, 'price', window, TradeTime.get_latest_trade_date())
         dic = list_to_hash(rows)
         self.provider.cache[symbol] = [rows[0][0], dic]
         return dic[date]
Exemplo n.º 5
0
 def get3vix(self, date_str=None):
     date_str = date_str or TradeTime.get_latest_trade_date().strftime(
         '%Y-%m-%d')
     following_symbols = list(VIX.get_following_symbols(date_str))
     symbols = ['VIY00']
     symbols.extend(following_symbols[0:3])
     return map(lambda x: self.get_vix_price_by_symbol(x), symbols)
Exemplo n.º 6
0
    def __init__(self):
        self.circle = 20
        self.option_dao = OptionDAO()

        # get the equity records from 100 date ago.
        #from_date_str = (datetime.date.today() - datetime.timedelta(100)).strftime('%Y-%m-%d')
        from_date = (datetime.date.today() - datetime.timedelta(100))
        #self.spy_records = YahooEquityDAO().get_all_equity_price_by_symbol('SPY', from_date_str)
        self.spy_records = EquityDAO().get_all_equity_price_by_symbol(
            'SPY', from_date)
        self.hv_spy = OptionCalculater.get_year_history_volatility_list(
            self.spy_records, self.circle)
        self.spy_delta_records = self.get_delta_records(
            'SPY', self.spy_records)

        from_date = TradeTime.get_latest_trade_date() - datetime.timedelta(50)
        self.vix_index_records = VIXDAO().get_vix_price_by_symbol_and_date(
            'VIY00', from_date=from_date)
        (records_f1, records_f2,
         records_f3) = VIXDAO().get_following_vix(from_date)
        self.vixf1_records = records_f1
        self.vix_delta_records = map(lambda x, y: [x[0], y[1] - x[1]],
                                     self.vix_index_records,
                                     self.vixf1_records)
        self.hv_vix = list(self.calculate_f1_volatilities())
        #vxx_records = YahooEquityDAO().get_all_equity_price_by_symbol('VXX', from_date_str)
        vxx_records = EquityDAO().get_all_equity_price_by_symbol(
            'VXX', from_date)
        self.vxx_delta_records = self.get_delta_records('VXX', vxx_records)
Exemplo n.º 7
0
 def validate_integrity_for_real_time_data(self, symbol='SVXY', ):
     us_dt = datetime.datetime.now(tz=pytz.timezone('US/Eastern'))
     now = datetime.datetime(us_dt.year, us_dt.month, us_dt.day, us_dt.hour, us_dt.minute, us_dt.second)
     integrity_p = True
     latest_date = TradeTime.get_latest_trade_date()
     start_time = datetime.datetime(latest_date.year, latest_date.month, latest_date.day, 9, 30, 0)
     if TradeTime.is_half_trade_day(latest_date):
         default_end_time = datetime.datetime(latest_date.year, latest_date.month, latest_date.day, 13, 0, 0)
     else:
         default_end_time = datetime.datetime(latest_date.year, latest_date.month, latest_date.day, 16, 0, 0)
     end_time = min(now, default_end_time)
     minutes_count = range((end_time - start_time).seconds / 60 + 1)
     trade_minutes = map(lambda x: start_time + datetime.timedelta(minutes=x), minutes_count)
     # print trade_minutes
     rows = self.get_min_time_and_price(symbol, start_time, end_time)
     # print rows
     j = 0
     # self.logger.info('rows = %s, \n trade_minutes = %s' %(rows[-2:], trade_minutes[-2:]))
     for i, time in enumerate(trade_minutes):
         if j >= len(rows):
             break  # rows length may less than trade_minutes for 1 elements.
         if rows[j][0].minute > time.minute or rows[j][0].hour > time.hour:
             integrity_p = False
         else:
             j = j + 1
     return integrity_p, rows
Exemplo n.º 8
0
 def parse_for_option(bigcharts_option_symbol, underlying_symbol,
                      last_price):
     if last_price == '':
         return None
     content = bigcharts_option_symbol[len(underlying_symbol):]
     option_type, month = BigChartsScraper.find_type_and_month(content[0])
     date = int(content[1:3])
     year = int(content[3:5])
     expiration_date = datetime.date(year + 2000, month, date)
     strike_price = int(content[6:]) / pow(10, int(content[5]))
     option_symbol = '%s%s%s%08d' % (underlying_symbol,
                                     expiration_date.strftime('%y%m%d'),
                                     option_type[0], strike_price * 1000)
     option = Option()
     option.optionType = option_type
     option.tradeTime = TradeTime.get_latest_trade_date()
     option.symbol = option_symbol
     option.expirationDate = expiration_date
     if '^' + underlying_symbol in Symbols.indexes:
         option.underlingSymbol = '^' + underlying_symbol
     else:
         option.underlingSymbol = underlying_symbol
     option.strikePrice = strike_price
     option.daysToExpiration = (expiration_date - option.tradeTime).days
     option.lastPrice = float(last_price)
     return option
Exemplo n.º 9
0
 def get_all_unexpired_dates(self, equity_symbol, from_date=None, cursor = None):
     from_date = from_date or TradeTime.get_latest_trade_date()
     query_template = """select distinct(expirationDate) from  option_data 
                                 where underlingSymbol = '{}' and expirationDate > str_to_date('{}', '%Y-%m-%d')
                                 order by expirationDate"""
     query = query_template.format(equity_symbol, from_date.strftime('%Y-%m-%d'))
     rows = self.select(query, cursor)
     return map(lambda x: x[0], rows)
Exemplo n.º 10
0
 def get_following_expirationDate(self, equity_symbol, from_date=None):
     from_date = from_date or TradeTime.get_latest_trade_date()
     dates = self.get_all_unexpired_dates(equity_symbol, from_date)
     if all_number_p(equity_symbol):
         return dates[0]
     else:
         for d in dates:
             if d.weekday() == 4 and 14 < d.day < 22:
                 return d
Exemplo n.º 11
0
 def GET(self):
     records = YahooEquityDAO().get_start_end_date_by_symbols()
     last_trade_date = TradeTime.get_latest_trade_date()
     error = False
     for record in records:
         if record[2] < last_trade_date:
             error = True
     dic = Symbols.get_yahoo_mapping_dic()
     return render.start_end_date(records, last_trade_date, error, dic)
Exemplo n.º 12
0
 def get_missing_records_symbols(self):
     conn = self.get_connection()
     cursor = conn.cursor()
     missing_symbols = []
     last_trade_date = TradeTime.get_latest_trade_date()
     for symbol in Symbols.get_all_symbols():
         end_date = self.get_end_date_by_symbol(symbol, cursor)
         if end_date < last_trade_date:
             missing_symbols.append(symbol)
     conn.close()
     return missing_symbols
Exemplo n.º 13
0
 def __init__(self):
     from_date = (datetime.date.today() - datetime.timedelta(150))
     self.spy_records = EquityDAO().get_all_equity_price_by_symbol('SPY', from_date)
     self.vix_records = VIXDAO().get_vix_price_by_symbol_and_date('VIY00', from_date=from_date)
     if datetime.date.today() > TradeTime.get_latest_trade_date():
         new_spy_price = YahooScraper.get_data_by_symbol('SPY')
         new_vix_price = YahooScraper.get_data_by_symbol('VIX')
         self.spy_records.append([datetime.date.today(), new_spy_price])
         self.vix_records.append([datetime.date.today(), new_vix_price])
     self.dates = map(lambda x: x[0], self.spy_records)
     self.spy_values = map(lambda x: x[1], self.spy_records)
     self.vix_values = map(lambda x: x[1], self.vix_records)
Exemplo n.º 14
0
 def get_selected_expiration_date(self, unexpired_dates):
     selected_expiration_date = self.query_dic.get('expiration')
     if selected_expiration_date is None:
         filtered_dates = filter(lambda x: x > TradeTime.get_latest_trade_date(), unexpired_dates)
         for exp_date in filtered_dates:
             if exp_date.weekday() == 4 and 14 < exp_date.day < 22:
                 return exp_date.strftime('%Y-%m-%d')
         # if not find, return the first element of filtered dates,
         # e.g. ^VIX is a special option, the expiration date is not the third Friday likes other option...
         if len(filtered_dates) > 0:
             return filtered_dates[0].strftime('%Y-%m-%d')
     else:
         return selected_expiration_date
Exemplo n.º 15
0
 def get_date_values(self):
     expiration_date = self.get_expiration_date()
     latest_date = min(TradeTime.get_latest_trade_date(), expiration_date)
     days = (latest_date - self.start_date).days
     for i in range(days + 1):
         date = self.start_date + datetime.timedelta(days=i)
         if TradeTime.is_trade_day(date):
             value = DataProvider.get_price_by_date(self.option_symbol,
                                                    date)
             #print 'date=%s,value=%s'%(date,value)
             if value is not None:
                 if self.long_short.lower() == 'short':
                     value = -value
                 yield [date, self.quantity * value]
Exemplo n.º 16
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.º 17
0
 def save_to_csv(self, trade_date=None):
     if trade_date is None:
         trade_date = TradeTime.get_latest_trade_date()
     start_time = datetime.datetime(trade_date.year, trade_date.month, trade_date.day, 9, 30, 0)
     end_time = datetime.datetime(trade_date.year, trade_date.month, trade_date.day, 16, 0, 0)
     query = """select * from equity_realtime where tradeTime >= '{}' and tradeTime <= '{}'""".format(start_time, end_time)
     rows = self.select(query)
     if rows is not None and len(rows) > 0:
         records = map(lambda x: ','.join(map(str, x[1:])), rows)
         content = '\n'.join(records)
         raw_daily_path = PathMgr.get_raw_data_path(datetime.date.today().strftime('%Y-%m-%d'))
         realtime_dir = os.path.join(raw_daily_path, 'realtime')
         ensure_dir_exists(realtime_dir)
         file_path = os.path.join(realtime_dir, '%s.csv' % trade_date.strftime('%Y-%m-%d'))
         write_to_file(file_path, content)
Exemplo n.º 18
0
 def get_vix_price_by_symbol_and_date(self,
                                      symbol,
                                      from_date=datetime.datetime(
                                          1993, 1, 1),
                                      to_date=None,
                                      remove_invalid_date=True):
     to_date = to_date or TradeTime.get_latest_trade_date()
     query_template = """select dailyDate1dAgo, dailyLastPrice from vix where symbol = '{}' and dailyDate1dAgo >= str_to_date('{}', '%Y-%m-%d') and dailyDate1dAgo <= str_to_date('{}', '%Y-%m-%d') order by dailyDate1dAgo"""
     query = BaseDAO.mysql_format(query_template, symbol,
                                  from_date.strftime('%Y-%m-%d'),
                                  to_date.strftime('%Y-%m-%d'))
     rows = self.select(query)
     if remove_invalid_date:
         rows = filter(lambda x: TradeTime.is_trade_day(x[0]), rows)
     return rows
Exemplo n.º 19
0
def plot_vix_3in1():
    from_date = TradeTime.get_latest_trade_date() - datetime.timedelta(50)
    records_index = VIXDAO().get_vix_price_by_symbol_and_date(
        'VIY00', from_date=from_date)
    dates = map(lambda x: x[0], records_index)
    price_index = map(lambda x: x[1], records_index)
    (records_f1, records_f2,
     records_f3) = VIXDAO().get_following_vix(from_date)
    price_f1 = map(lambda x: x[1], records_f1)
    price_f2 = map(lambda x: x[1], records_f2)
    fig, ax = plt.subplots()
    ax.plot(dates, price_index, label='index')
    ax.plot(dates, price_f1, label='f1')
    ax.plot(dates, price_f2, label='f2')
    plt.grid()
    plt.legend(bbox_to_anchor=(1.05, 1), loc=8, borderaxespad=0.)
    plt.show()
Exemplo n.º 20
0
 def validate_integrity_for_min_data(self, symbol):
     print symbol
     us_dt = datetime.datetime.now(tz=pytz.timezone('US/Eastern'))
     now = datetime.datetime(us_dt.year, us_dt.month, us_dt.day, us_dt.hour,
                             us_dt.minute, us_dt.second)
     integrity_p = True
     latest_date = TradeTime.get_latest_trade_date()
     start_time = datetime.datetime(latest_date.year, latest_date.month,
                                    latest_date.day, 9, 30, 0)
     if TradeTime.is_half_trade_day(latest_date):
         default_end_time = datetime.datetime(latest_date.year,
                                              latest_date.month,
                                              latest_date.day, 13, 0, 0)
     else:
         default_end_time = datetime.datetime(latest_date.year,
                                              latest_date.month,
                                              latest_date.day, 16, 0, 0)
     end_time = min(now, default_end_time)
     minutes_count = range((end_time - start_time).seconds / 60 + 1)
     trade_minutes = map(
         lambda x: start_time + datetime.timedelta(minutes=x),
         minutes_count)
     # print trade_minutes
     rows = self.get_time_and_price(symbol, start_time, end_time)
     # print rows
     j = 0
     missing_time = []
     for i, time in enumerate(trade_minutes):
         if i >= len(rows):
             break
         if rows[j][0] > time:
             if j > 0:
                 price = rows[j - 1][1]
             else:
                 price = rows[0][1]
             if not (time.hour == 9 and time.minute == 30):
                 missing_time.append(time)
         else:
             j = j + 1
     if len(missing_time) > 0:
         integrity_p = False
     return integrity_p, missing_time
Exemplo n.º 21
0
 def simulate(trade_nodes, start_date, end_date = None):
     if end_date is None:
         end_date = TradeTime.get_latest_trade_date()
     portfolio = Portfolio()
     dates = list(TradeSimulation.date_range(start_date, end_date))
     #print trade_nodes
     #print dates
     i = 0
     j = 0
     while i < len(dates) or j < len(trade_nodes):
         if j < len(trade_nodes) and dates[i] > trade_nodes[j].date:
             raise AssertionError('the trade date should be in dates ranges.')
         elif j < len(trade_nodes) and dates[i] == trade_nodes[j].date:
             portfolio.action(trade_nodes[j])
             j += 1
         else:
             #date_str = dates[i].strftime('%Y-%m-%d')
             #print (date_str, portfolio.get_returns(dates[i]))
             yield [dates[i], portfolio.get_returns(dates[i])]
             i += 1
Exemplo n.º 22
0
 def add_missing_data(self, symbol='SVXY', validate_date=None):
     if validate_date is None:
         validate_date = TradeTime.get_latest_trade_date()
     start_time = datetime.datetime.fromordinal(validate_date.toordinal())
     end_time = start_time + datetime.timedelta(days=1)
     rows = self.get_time_and_price(symbol, start_time, end_time)
     j = 0
     missing_records = []
     for i, time in enumerate(TradeTime.get_all_trade_min(validate_date)):
         if j >= len(rows) or rows[j][0] > time:
             if j > 0:
                 price = rows[j - 1][1]
             else:
                 price = rows[0][1]
             missing_records.append(
                 Equity(symbol, time, price, price, price, price, 0, 0))
         else:
             j = j + 1
     if len(missing_records) > 0:
         self.insert(missing_records)
     return len(missing_records)
Exemplo n.º 23
0
 def GET(self):
     from_date = TradeTime.get_latest_trade_date() - datetime.timedelta(30)
     vix_records = VIXDAO().get_vix_price_by_symbol_and_date('VIY00', from_date=from_date)
     dates = map(lambda x: x[0], vix_records)
     vix_prices = map(lambda x: x[1], vix_records)
     vxv_records = YahooEquityDAO().get_all_equity_price_by_symbol('^VXV', from_date_str=from_date.strftime('%Y-%m-%d'))
     vxmt_records = YahooEquityDAO().get_all_equity_price_by_symbol('^VXMT', from_date_str=from_date.strftime('%Y-%m-%d'))
     vxv_prices = map(lambda x: x[1], vxv_records)
     vxmt_prices = map(lambda x: x[1], vxmt_records)
     fig = Figure(figsize=[12, 8])
     ax = fig.add_axes([.1, .1, .8, .8])
     ax.plot(dates, vix_prices, label='vix')
     ax.plot(dates, vxv_prices, label='vxv')
     ax.plot(dates, vxmt_prices, label='vxmt')
     ax.legend(loc='upper left')
     ax.grid()
     canvas = FigureCanvasAgg(fig)
     buf = cStringIO.StringIO()
     canvas.print_png(buf)
     data = buf.getvalue()
     return data
Exemplo n.º 24
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.º 25
0
 def add_missing_data(self, symbol='SVXY', validate_date=None):
     if validate_date is None:
         validate_date = TradeTime.get_latest_trade_date()
     start_time = datetime.datetime.fromordinal(validate_date.toordinal())
     end_time = start_time + datetime.timedelta(days=1)
     rows = self.get_min_time_and_price(symbol, start_time, end_time)
     j = 0
     missing_records = []
     for i, time in enumerate(TradeTime.get_all_trade_min(validate_date)):
         # if i == 390:
         #     print time
         if j >= len(rows) or rows[j][0].minute > time.minute or rows[j][0].hour > time.hour:
             if j > 0:
                 # price = rows[j-1][1]
                 price = self.get_nearest_price(time, symbol)
             else:
                 price = rows[0][1]
             missing_records.append((symbol, time, price))
         else:
             j = j+1
     if len(missing_records) > 0:
         for record in missing_records:
             self.insert(*record)
     return len(missing_records)
Exemplo n.º 26
0
                break
            if rows[j][0] > time:
                if j > 0:
                    price = rows[j - 1][1]
                else:
                    price = rows[0][1]
                if not (time.hour == 9 and time.minute == 30):
                    missing_time.append(time)
            else:
                j = j + 1
        if len(missing_time) > 0:
            integrity_p = False
        return integrity_p, missing_time

    def remove_market_open_records(self):
        sql = """delete from equity_min where tradetime like '%9:30:00'"""
        self.execute_query(sql)


if __name__ == '__main__':
    # EquityMinDAO().add_missing_data()
    # EquityMinDAO().add_missing_data(validate_date=datetime.date(2018, 1, 19))
    # EquityMinDAO().add_missing_data_in_real_time('XIV')
    # print EquityMinDAO().save_to_csv()
    # print EquityMinDAO().validate_integrity_for_min_data('UBT')
    # for trade_date in TradeTime.generate_dates(datetime.date(2009, 1, 30), TradeTime.get_latest_trade_date()):
    #     print 'Add missing data for %s' % trade_date
    #     EquityMinDAO().add_missing_data('VXX', trade_date)
    for trade_date in TradeTime.generate_dates(
            datetime.date(2018, 5, 30), TradeTime.get_latest_trade_date()):
        EquityMinDAO().add_missing_data('SPY', trade_date)
Exemplo n.º 27
0
                sub_records.append(record)
                combined_record = AGG30Min.combine_records(symbol, sub_records)
                combined_records.append(combined_record)
                sub_records = []
        Equity30MinDAO().insert(combined_records)

    @staticmethod
    def agg1mtodaily(symbol, start_time=datetime.datetime(1971, 1, 1, 0, 0, 0), end_time=datetime.datetime(9999, 1, 1, 0, 0, 0)):
        records = EquityMinDAO().get_records(symbol, start_time, end_time)
        sub_records = []
        combined_records = []
        for record in records:
            sub_records.append(record)
            if record[0].hour == 15:
                sub_records.append(record)
                combined_record = AGG30Min.combine_records(symbol, sub_records)
                combined_records.append(combined_record)
                sub_records = []
        YahooEquityDAO().save_from_equities(combined_records)

if __name__ == '__main__':
    # AGG30Min.agg1to30('510050')
    # AGG30Min.agg1mtodaily('510050')
    from common.tradetime import TradeTime
    for trade_date in TradeTime.generate_dates(datetime.date(1999, 3, 10), TradeTime.get_latest_trade_date()):
        from_datetime = datetime.datetime(trade_date.year, trade_date.month, trade_date.day, 0, 0, 0)
        to_datetime = from_datetime + datetime.timedelta(days=1)
        print 'Generate 30 min data for %s' % trade_date
        AGG30Min.agg1to30('QQQ', from_datetime, to_datetime)

Exemplo n.º 28
0
 def get_vix_symbol_list(from_date, to_date=None, fx=1):
     to_date = to_date or TradeTime.get_latest_trade_date()
     for (year, index) in VIX.get_year_index_list(from_date, to_date, fx):
         yield VIX.get_symbol_by_year_index(year, index)
Exemplo n.º 29
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
Exemplo n.º 30
0
 def get_unexpired_dates(self, selected_symbol, delta_days = 60):
     unexpired_dates = OptionDAO().get_all_unexpired_dates(selected_symbol, from_date=TradeTime.get_latest_trade_date() - datetime.timedelta(days=delta_days))
     return unexpired_dates