Ejemplo n.º 1
0
 def get_portfolio_info(self, include_option=False):
     output = self.run_cmd('account')
     if output == '':
         raise Exception(
             'Failed to get account info, please check the IB gateway, config, network and ibpy2 packages, etc...'
         )
     str_available_funds = string_fetch(output, 'AvailableFunds, value=',
                                        ',')
     # get_logger().info("available_funds string value: %s"%str_available_funds)
     available_funds = float(str_available_funds)
     str_net_liquidation = string_fetch(output, 'NetLiquidation, value=',
                                        ',')
     # get_logger().info("net_liquidation string value: %s" % str_net_liquidation)
     net_liquidation = float(str_net_liquidation)
     items = output.split('<updatePortfolio')
     if len(items) > 0:
         contract_list = map(API.parse_contract, items[1:])
         if include_option:
             contract_dict = list_to_hash(contract_list)
         else:
             contract_dict = list_to_hash(
                 filter(lambda x: len(x[0]) <= 15, contract_list))
     else:
         contract_dict = {}
     return Portfolio(available_funds, net_liquidation, contract_dict)
Ejemplo n.º 2
0
 def update_delta(risk_free_interest_rate=0.005):
     date_price_records = VIXDAO().get_vix_price_by_symbol('VIY00')
     date_hv_lst = OptionCalculater.get_year_history_volatility_list(
         date_price_records)
     date_price_dic = list_to_hash(date_price_records)
     date_hv_dic = list_to_hash(date_hv_lst)
     option_dao = OptionDAO()
     vix_option_records = option_dao.get_vix_options()
     conn = BaseDAO.get_connection()
     cursor = conn.cursor()
     count = 0
     for (symbol, trade_date, left_days, strike_price,
          option_type) in vix_option_records:
         underlying_price = date_price_dic.get(trade_date)
         sigma = date_hv_dic.get(trade_date)
         if underlying_price is not None and sigma is not None:
             delta = OptionCalculater.get_delta(underlying_price,
                                                strike_price, left_days,
                                                risk_free_interest_rate,
                                                sigma,
                                                option_type[0:1].lower())
             option_dao.update_delta_for_vix_options(
                 symbol, trade_date, delta, cursor)
             count += 1
             if count == 1000:
                 conn.commit()
                 count = 0
     conn.commit()
     conn.close()
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
 def calculate_f1_volatilities(self):
     symbols = list(set(map(lambda x: x[2], self.vixf1_records)))
     symbol_dic = {}
     for symbol in symbols:
         symbol_records = VIXDAO().get_vix_price_by_symbol_and_date(
             symbol, self.vixf1_records[0][0])
         hv_dic = list_to_hash(
             OptionCalculater.get_year_history_volatility_list(
                 symbol_records, self.circle))
         symbol_dic[symbol] = hv_dic
     for vix_f1_record in self.vixf1_records[20:]:
         date = vix_f1_record[0]
         symbol = vix_f1_record[2]
         hv = symbol_dic[symbol].get(date)
         yield [date, hv]
Ejemplo n.º 5
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]
Ejemplo n.º 6
0
 def _generate_index_dic(self, rows):
     indexes = range(len(rows))
     time_indexes = map(lambda x, y: [x[0], y], rows, indexes)
     return list_to_hash(time_indexes)
Ejemplo n.º 7
0
 def get_reversed_yahoo_symbol_mapping():
     lst = hash_to_list(Symbols.YahooSymbolMapping)
     reversed_lst = map(lambda x: [x[1], x[0]], lst)
     dic = list_to_hash(reversed_lst)
     return dic
Ejemplo n.º 8
0
 def __init__(self, processes, update_process_fn):
     self.processes = processes
     self.processes_dict = list_to_hash(
         map(lambda x: [x, [False, None, None]], processes))
     self.update_process_fn = update_process_fn