def test_transaction(): engine = Engine(best_ip=True, thread_num=1) with engine.connect(): df = engine.get_k_data('000001', '20170601', '20171231', '1m') df = engine.get_security_bars(['000001', '000521'], '1d', start=pd.to_datetime('20180102'))
def transactions(): eg = Engine(best_ip=True) eg.connect() m1 = eg.get_security_bars('000001', '1m') df = eg.time_and_price('000001') ohlcv = df.price.resample('1 Min', label='right', closed='left').ohlc() ohlcv['volume'] = df.vol.resample('1 Min', label='right', closed='left').sum()
def engine_func(best_ip, thread_num): engine = Engine(best_ip=best_ip, thread_num=thread_num) with engine.connect(): assert engine.best_ip is not None assert engine.gbbq is not None assert engine.security_list is not None assert engine.stock_quotes() is not None assert engine.customer_block is not None assert engine.quotes('000001') is not None assert engine.get_security_bars('000001', '1m') is not None assert engine.get_security_bars('000001', '1d') is not None assert engine.get_security_bars('000300', '1m', index=True) is not None assert engine.get_security_bars('000300', '1d', index=True) is not None assert engine.concept is not None assert engine.fengge is not None assert engine.index is not None assert engine.stock_list is not None
def ensure_benchmark_data(symbol, first_date, last_date, now, trading_day): """ Ensure we have benchmark data for `symbol` from `first_date` to `last_date` Parameters ---------- symbol : str The symbol for the benchmark to load. first_date : pd.Timestamp First required date for the cache. last_date : pd.Timestamp Last required date for the cache. now : pd.Timestamp The current time. This is used to prevent repeated attempts to re-download data that isn't available due to scheduling quirks or other failures. trading_day : pd.CustomBusinessDay A trading day delta. Used to find the day before first_date so we can get the close of the day prior to first_date. We attempt to download data unless we already have data stored at the data cache for `symbol` whose first entry is before or on `first_date` and whose last entry is on or after `last_date`. If we perform a download and the cache criteria are not satisfied, we wait at least one hour before attempting a redownload. This is determined by comparing the current time to the result of os.path.getmtime on the cache path. """ path = get_data_filepath(get_benchmark_filename(symbol)) # If the path does not exist, it means the first download has not happened # yet, so don't try to read from 'path'. if os.path.exists(path): try: data = pd.Series.from_csv(path).tz_localize('UTC') if has_data_for_dates(data, first_date, last_date): return data # Don't re-download if we've successfully downloaded and written a # file in the last hour. last_download_time = last_modified_time(path) if (now - last_download_time) <= ONE_HOUR: logger.warn( "Refusing to download new benchmark data because a " "download succeeded at %s." % last_download_time) return data except (OSError, IOError, ValueError) as e: # These can all be raised by various versions of pandas on various # classes of malformed input. Treat them all as cache misses. logger.info( "Loading data for {path} failed with error [{error}].".format( path=path, error=e, )) logger.info( "Cache at {path} does not have data from {start} to {end}.\n" "Downloading benchmark data for '{symbol}'.", start=first_date, end=last_date, symbol=symbol, path=path, ) engine = Engine(auto_retry=True, multithread=True, thread_num=8) engine.connect() data = engine.get_security_bars(symbol, '1d', index=True) data = data['close'].sort_index().tz_localize('UTC').pct_change(1).iloc[1:] data.index = data.index.shift(-15, '1H') # change datetime at 15:00 to midnight data.to_csv(path) return data
print(grouped.sort_values('up_limit', ascending=False)) def minute_time_data(): stock_list = engine.stock_list.index.tolist() now = datetime.datetime.now() for stock in stock_list: fs = engine.api.to_df( engine.api.get_minute_time_data(stock[0], stock[1])) # print(fs) print((datetime.datetime.now() - now).total_seconds()) def quotes(): start_dt = datetime.datetime.now() quote = engine.stock_quotes() print(datetime.datetime.now() - start_dt).total_seconds() process_quotes(quote) if __name__ == '__main__': engine = Engine(best_ip=True) with engine.connect(): print( engine.get_security_bars('002920', '1d', pd.to_datetime('20170701')))
def main(): engine = Engine(best_ip=True, thread_num=1) with engine.connect(): engine.get_k_data('000001', '20161201', '20171231', '1m') def test_transaction(): engine = Engine(best_ip=True, thread_num=1) with engine.connect(): df = engine.get_k_data('000001', '20170601', '20171231', '1m') df = engine.get_security_bars(['000001', '000521'], '1d', start=pd.to_datetime('20180102')) if __name__ == '__main__': engine = Engine(best_ip=True, thread_num=1) with engine.connect(): print(engine.api.get_security_count(0)) print(engine.api.get_security_count(1)) lists = engine.stock_list print( engine.get_security_bars('300737', '1d', pd.to_datetime('20161201'), pd.to_datetime('20171231'))) print(engine.get_k_data('300737', '20161201', '20171231', '1d')) print(timeit.timeit(test_transaction, number=1)) print(timeit.timeit(main, number=1))
def ensure_benchmark_data(symbol, first_date, last_date, now, trading_day): """ Ensure we have benchmark data for `symbol` from `first_date` to `last_date` Parameters ---------- symbol : str The symbol for the benchmark to load. first_date : pd.Timestamp First required date for the cache. last_date : pd.Timestamp Last required date for the cache. now : pd.Timestamp The current time. This is used to prevent repeated attempts to re-download data that isn't available due to scheduling quirks or other failures. trading_day : pd.CustomBusinessDay A trading day delta. Used to find the day before first_date so we can get the close of the day prior to first_date. We attempt to download data unless we already have data stored at the data cache for `symbol` whose first entry is before or on `first_date` and whose last entry is on or after `last_date`. If we perform a download and the cache criteria are not satisfied, we wait at least one hour before attempting a redownload. This is determined by comparing the current time to the result of os.path.getmtime on the cache path. """ path = get_data_filepath(get_benchmark_filename(symbol)) # If the path does not exist, it means the first download has not happened # yet, so don't try to read from 'path'. if os.path.exists(path): try: data = pd.Series.from_csv(path).tz_localize('UTC') if has_data_for_dates(data, first_date, last_date): return data # Don't re-download if we've successfully downloaded and written a # file in the last hour. last_download_time = last_modified_time(path) if (now - last_download_time) <= ONE_HOUR: logger.warn( "Refusing to download new benchmark data because a " "download succeeded at %s." % last_download_time ) return data except (OSError, IOError, ValueError) as e: # These can all be raised by various versions of pandas on various # classes of malformed input. Treat them all as cache misses. logger.info( "Loading data for {path} failed with error [{error}].".format( path=path, error=e, ) ) logger.info( "Cache at {path} does not have data from {start} to {end}.\n" "Downloading benchmark data for '{symbol}'.", start=first_date, end=last_date, symbol=symbol, path=path, ) engine = Engine(auto_retry=True, multithread=True, thread_num=8, best_ip=True) engine.connect() data = engine.get_security_bars(symbol, '1d',index=True) data = data['close'].sort_index().tz_localize('UTC').pct_change(1).iloc[1:] data.index = data.index.normalize() # change datetime at 15:00 to midnight data.to_csv(path) return data