def parse_option_data_by_symbol(self, symbol): df = pd.read_hdf(self.option_data_file, symbol) if df is not None: print '{} data exists, parse it to object...'.format(symbol) equity = Equity() equity.symbol = symbol equity.tradeTime = self.trade_date self.equity_records.append(equity) for index, row in df.iterrows(): equity.lastPrice = row['Underlying_Price'] option = Option() option.symbol = symbol option.tradeTime = self.trade_date, option.expirationDate = row['Expiry'] option.optionType = row['Type'] option.strikePrice = row['Strike'] option.bidPrice = row['Bid'] option.lastPrice = row['Last'] option.priceChange = row['Change'] option.volatility = row['ImpliedVolatility'] option.theoretical = row['TheoreticalValue'] option.delta = row['Delta'] option.gamma = row['Gamma'] option.rho = row['Rho'] option.theta = row['Theta'] option.vega = row['Vega'] option.openInterest = row['Open Int'] option.volume = row['Volume'] self.option_records.append(option) else: print 'Missing data for {} ...'.format(symbol)
def combine_records(symbol, records): equity = Equity() equity.symbol = symbol equity.tradeTime = records[-1][0] equity.openPrice = records[0][1] equity.highPrice = max(map(lambda x:x[2], records)) equity.lowPrice = min(map(lambda x:x[3],records)) equity.lastPrice = records[-1][4] volume = sum(filter(lambda y: y is not None, map(lambda x: x[5], records))) if volume != 0: equity.volume = volume return equity
def load_equity_data_by_symbol(self, symbol): file_path = os.path.join(self.daily_path, 'equity_data', '{}.json'.format(symbol)) with open(file_path) as fs: json_data = json.load(fs) equity = Equity.loads(json_data['data'][0]) return equity
def parse_record(self, content, symbol): json_data = json.loads(content) dic_data = json_data['Time Series (1min)'] for key, value in dic_data.iteritems(): time = key open = value['1. open'] high = value['2. high'] low = value['3. low'] close = value['4. close'] # volume = value['5. volume'] equity = Equity(symbol, time, open, high, low, close, None, None) yield equity
def load_log(self, symbol, date): file_name = '%s%s.log' % (symbol, date.strftime('%Y%m%d')) path = PathMgr.get_data_path('quantopian_daily_min/%s' % file_name) content = read_file_to_string(path) lines = content.split('\n') filtered_lines = filter(lambda x: len(x) > 100, lines) lines = map(lambda x: string_fetch(x, 'PRINT ', ''), filtered_lines) close_list_str = ','.join(lines) prices_list = map(float, close_list_str.split(',')) datetimes = TradeTime.generate_datetimes(date, date) equities = map(lambda x, y: Equity(symbol, x, y, y, y, y), datetimes, prices_list) EquityMinDAO().insert(equities)
def parse_line(self, line, symbol): record = line.split(',') time = datetime.datetime.strptime('%s %s' % (record[0], record[1]), '%m/%d/%Y %H:%M') open_price = float(record[2]) high_price = float(record[3]) low_price = float(record[4]) close_price = float(record[5]) volume = float(record[6]) if 9.5 * 60 <= time.hour * 60 + time.minute <= 16 * 60: return Equity(symbol, time, open_price, high_price, low_price, close_price, close_price - open_price, volume) else: return None
def add_missing_data_in_real_time( 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) if TradeTime.is_trade_day(now.date()): default_start_time = datetime.datetime(now.year, now.month, now.day, 9, 30, 0) start_time = max( default_start_time, datetime.datetime(now.year, now.month, now.day, now.hour - 1, now.minute, 0)) if now > start_time: if TradeTime.is_half_trade_day(now.date()): default_end_time = datetime.datetime( now.year, now.month, now.day, 13, 0, 0) else: default_end_time = datetime.datetime( now.year, now.month, now.day, 16, 0, 0) end_time = min(now, default_end_time) if end_time > start_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_records = [] for i, time in enumerate(trade_minutes): if 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)
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)
def parse_line(self, line, code): time = datetime.datetime.strptime(line[0:19], '%Y-%m-%d %H:%M:%S') price = float(line[21:]) return Equity(code, time, price, price, price, price, price, None)