def test_data_api(): address = data_config.get("remote.data.address", None) username = data_config.get("remote.data.username", None) password = data_config.get("remote.data.password", None) if address is None or username is None or password is None: raise ValueError("no data service config available!") api = DataApi(address, use_jrpc=False) login_msg = api.login(username=username, password=password) print login_msg daily, msg = api.daily(symbol="600030.SH,000002.SZ", start_date=20170103, end_date=20170708, fields="open,high,low,close,volume,last,trade_date,settle") daily2, msg2 = api.daily(symbol="600030.SH", start_date=20170103, end_date=20170708, fields="open,high,low,close,volume,last,trade_date,settle") # err_code, err_msg = msg.split(',') assert msg == '0,' assert msg2 == '0,' assert daily.shape == (248, 9) assert daily2.shape == (124, 9) df, msg = api.bar(symbol="600030.SH", trade_date=20170904, freq=common.QUOTE_TYPE.MIN, start_time=90000, end_time=150000) print df.columns assert df.shape == (240, 15) print "test passed"
def loginJaqsApp(self): """ # 登录jaqs的api。需要账号和token,可在官网上注册。 """ print(u'正在登陆JaqsAPI.') self.api = DataApi(addr=self.setting['ADDR']) self.api.login(self.setting['PHONE'], self.setting['TOKEN'])
def initApi(): try: global api api = DataApi(addr=addr) api.login(phone, token) except Exception as e: print('Init Api Failed')
def get_data(symbol=None): if not os.path.exists('./data/prepared/datacsv/'): os.makedirs('./data/prepared/datacsv/') if not symbol: api = DataApi(addr="tcp://data.quantos.org:8910") result, msg = api.login( "18652420434", "eyJhbGciOiJIUzI1NiJ9.eyJjcmVhdGVfdGltZSI6IjE1MTcwNjAxMDgyOTMiLCJpc3MiOiJhdXRoMCIsImlkIjoiMTg2NTI0MjA0MzQifQ.b1ejSpbEVS7LhbsveZ5kvbWgUs7fnUd0-CBakPwNUu4" ) # print(result) data, msg = api.query( view="lb.indexCons", fields="symbol", filter="index_code=000016.SH&start_date=20180801&end_date=20180831", data_format='pandas') print(msg) # symbols = data['symbol'].tolist() symbols = [ '600887.SH', '601988.SH', '600048.SH', '601006.SH', '601398.SH', '601628.SH', '601166.SH', '601318.SH', '601328.SH', '601169.SH', '601088.SH', '601857.SH', '601390.SH', '601601.SH', '601186.SH', '601668.SH', '601766.SH', '600999.SH', '601989.SH', '601688.SH', '601288.SH', '601818.SH', '601800.SH', '601360.SH', '601336.SH', '603993.SH', '601211.SH', '600958.SH', '601878.SH', '601229.SH', '601881.SH' ] print(symbols) for sym in symbols: # df = load_data(sym) # close = [float(x) for x in df['close']] # # prepare macd data # df['MACD'], df['MACDsignal'], df['MACDhist'] = talib.MACD(np.array(close), # fastperiod=12, slowperiod=26, signalperiod=9) # df = df.sort_index() # df.index = pd.to_datetime(df.index, format='%Y%m%d') # df = prepare_kdj(df, 9, 'close') # 计算好kdj之后从13行开始取数据,计算出来的kdj比较准确 # df = df[34:] # df = calculator_close(df) # save_csv(sym, df) draw_pic(sym, 'macd_j') else: df = load_data(symbol) close = [float(x) for x in df['close']] # prepare macd data df['MACD'], df['MACDsignal'], df['MACDhist'] = talib.MACD( np.array(close), fastperiod=12, slowperiod=26, signalperiod=9) # df = df.sort_index() # df.index = pd.to_datetime(df.index, format='%Y%m%d') df = prepare_kdj(df, 9, 'close') # 计算好kdj之后从13行开始取数据,计算出来的kdj比较准确 df = df[34:] df = calculator_close(df) save_csv(symbol, df) draw_pic(symbol, 'macd_j')
def getQuantApi(): token = 'eyJhbGciOiJIUzI1NiJ9.eyJjcmVhdGVfdGltZSI6IjE1MzI5MzM3MjAwOTEiLCJpc3MiOiJhdXRoMCIsImlkIjoiMTg2MDExMTA5NTkifQ.nN08iv14dxEtd1r3EMmASxfkWqXoJ0xlyif8peWaFg4' addr = 'tcp://data.quantos.org:8910' phone = '18601110959' try: api = DataApi(addr=addr) api.login(phone, token) return api except Exception as e: print('Init Api Failed')
def __init__(self, api_url=None, user=None, token=None): global _api from jaqs.data import DataApi if _api is None: url = api_url or os.environ.get("QUANTOS_URL", "tcp://data.quantos.org:8910") user = user or os.environ.get("QUANTOS_USER") token = token or os.environ.get("QUANTOS_TOKEN") _api = DataApi(addr=url) _api.login(user, token) self._api = _api
def __init__(self, storage, user, password, jaqs_addr="tcp://data.quantos.org:8910"): self.user = user self.password = password self.jaqs_addr = jaqs_addr self.api = DataApi(self.jaqs_addr) assert isinstance(storage, BarStorage) self.storage = storage
def init_from_config(self, props): # do not initialize and login again if self.data_api is not None and self.data_api._loggined: return if props is None: props = dict() if self.data_api is not None: if len(props) == 0: return else: self.data_api.close() def get_from_list_of_dict(l, key, default=None): res = None for dic in l: res = dic.get(key, None) if res is not None: break if res is None: res = default return res props_default = dict( ) # jutil.read_json(jutil.join_relative_path('etc/data_config.json')) dic_list = [props, props_default] address = get_from_list_of_dict(dic_list, "remote.data.address", "") username = get_from_list_of_dict(dic_list, "remote.data.username", "") password = get_from_list_of_dict(dic_list, "remote.data.password", "") if address is None or username is None or password is None: raise ValueError("no address, username or password available!") time_out = get_from_list_of_dict(dic_list, "timeout", 60) self.data_api = DataApi(address, use_jrpc=False) self.data_api.set_timeout(timeout=time_out) print("\nDataApi login: {}@{}".format(username, address)) r, msg = self.data_api.login(username=username, password=password) if not r: print(" login failed: msg = '{}'\n".format(msg)) else: print " login success \n"
def connect(self): """连接""" try: dataServer = self.setting['DATA_SERVER'] tradeServer = self.setting['TRADE_SERVER'] userID = self.setting['USER_ID'] token = self.setting['TOKEN'] except KeyError: self.writeLog(text.CONFIG_KEY_MISSING) return # 连接行情API try: api = DataApi(addr=dataServer) api.login(userID, token) self.writeLog(text.DATA_SERVER_CONNECTED) except: self.writeLog(text.DATA_SERVER_FAILED) return # 连接交易API t_api = TradeApi(addr=tradeServer) user_info, msg = t_api.login(userID, token) self.writeLog(text.TRADING_SERVER_CONNECTED) if msg == self.setting['ISOK']: self.user_info = user_info strategy_list = self.user_info['strategies'] self.strategyID = strategy_list[ self.setting['DEFAULT_STRATEGYID']] # 设置默认的策略编号 else: self.writeLog(text.TRADING_SERVER_FAILED) return self.qsqApi = api self.qstApi = t_api self.initContract() # 初始化合约信息 self.initTradingEnv() # 初始化交易环境 self.qryAccount() # 获取账户信息 self.qryPosition() # 获取交易信息 self.initOrder() # 获取订单信息 self.initTrade() # 获取成交信息
def get_api(): login = CONF["login"] api = DataApi(login["addr"]) api.login(login["username"], login["password"]) return api
def setUp(self): self.api = DataApi(addr='tcp://data.tushare.org:8910') self.api.login( self.user_id, "eyJhbGciOiJIUzI1NiJ9.eyJjcmVhdGVfdGltZSI6IjE1MjY5MDQ0NTgwODAiLCJpc3MiOiJhdXRoMCIsImlkIjoiMTg4MTAyOTkyMDAifQ.M9d07UvpEWVWugga0d2UtbWsRRZubS5mgMXaTxrieSk" )
import pyfolio as pf from fxdayu_data import DataAPI try: from functools import lru_cache, partial except ImportError: from fastcache import lru_cache from jaqs.data import DataApi data_config = { "remote.data.address": "tcp://192.168.0.102:23000", "remote.data.username": "******", "remote.data.password": "******" } api = DataApi(data_config["remote.data.address"]) api.login(username=data_config["remote.data.username"], password=data_config["remote.data.password"]) def memorized_method(*lru_args, **lru_kwargs): def decorator(func): @functools.wraps(func) def wrapped_func(self, *args, **kwargs): # We're storing the wrapped method inside the instance. If we had # a strong reference to self the instance would never die. self_weak = weakref.ref(self) @functools.wraps(func) @lru_cache(*lru_args, **lru_kwargs) def cached_method(*args, **kwargs):
def login(username, password): api = DataApi(env.ADDR) api.login(username, password) logging.warning("login | %s", username) return api
"18652420434", "remote.data.password": "******" } trade_config = { "remote.trade.address": "tcp://gw.quantos.org:8901", "remote.trade.username": "******", "remote.trade.password": "******" } dataview_store_folder = './read_picture/data/prepared' api = DataApi(addr='tcp://data.quantos.org:8910') api.login( 18652420434, 'eyJhbGciOiJIUzI1NiJ9.eyJjcmVhdGVfdGltZSI6IjE1MTcwNjAxMDgyOTMiLCJpc3MiOiJhdXRoMCIsImlkIjoiMTg2NTI0MjA0MzQifQ.b1ejSpbEVS7LhbsveZ5kvbWgUs7fnUd0-CBakPwNUu4' ) start_date = "20120101" end_date = "20181231" def get_index_info(): df, msg = api.query( view="lb.indexCons", fields="index_code,symbol,in_date,out_date", filter="index_code=000905.SH&start_date=20120101&end_date=20181231", data_format='pandas')
def get_api(): api = DataApi(env.jaqs_addr) api.login(env.jaqs_user, env.jaqs_password) return api
from jaqs.data import DataApi import matplotlib.pyplot as plt # matplotlib inline import pandas as pd import numpy as np api = DataApi(addr='tcp://data.tushare.org:8910') phone = 'phone' token = 'token' df, msg = api.login(phone, token) print(df, msg) df, msg = api.query(view="jz.instrumentInfo", fields="market,symbol,list_date,status", filter="inst_type=1&status=1&market=SH,SZ", data_format="pandas") df.index = df['symbol'] df.sort_index(inplace=True) print(len(df)) print(len(df[df['market'] == 'SZ'])) print(len(df[df['market'] == 'SH']))
performance_metrics['Sharpe Ratio'] = \ (performance_metrics['Annual Return (%)'] / performance_metrics['Annual Volatility (%)']) risk_metrics = dict() risk_metrics['Beta'] = np.corrcoef(df_returns.loc[:, 'bench'], df_returns.loc[:, 'strat'])[0, 1] risk_metrics['Maximum Drawdown (%)'] = max_dd * 100 risk_metrics['Maximum Drawdown start'] = df_returns.index[max_dd_start] risk_metrics['Maximum Drawdown end'] = df_returns.index[max_dd_end] # print(performance_metrics, risk_metrics, df_returns) return performance_metrics, risk_metrics, df_returns if __name__ == "__main__": api = DataApi(addr="tcp://data.tushare.org:8910") df, msg = api.login(data_config["remote.data.username"], data_config["remote.data.password"]) # print(df, msg) df = get_fundlist(api, u'股票型', u'增强指数型') symbols = ",".join(df['symbol']) start_data = 20170101 curr_data = 20180110 index_map = get_index_map(api, symbols, start_data) # print(index_map) indicators = list() for (symbol, index) in index_map.items(): performance_metrics, risk_metrics, df_returns = cal_active_return(
def init_from_config(self, props): """ Parameters ---------- props : dict Configurations used for initialization. Example ------- {"remote.data.address": "tcp://*****:*****@{}".format(username, address)) INDENT = ' ' * 4 if self.data_api is None: if (address == "") or (username == "") or (password == ""): raise InitializeError( "no address, username or password available!") elif self.data_api._loggined: if ((address == self._address) and (time_out == self._timeout) and (username == self._username) and (password == self._password)): print(INDENT + "Already login as {:s}, skip init_from_config".format( username)) return # do not login with the same props again else: self.data_api.close() self.data_api = None self._address = address self._username = username self._password = password self._timeout = time_out data_api = DataApi(self._address, use_jrpc=False) data_api.set_timeout(timeout=self._timeout) r, err_msg = data_api.login(username=self._username, password=self._password) if not r: print(INDENT + "login failed: err_msg = '{}'\n".format(err_msg)) else: self.data_api = data_api print(INDENT + "login success \n")
class Api(object): __api = DataApi(addr='tcp://data.tushare.org:8910') def __lazy_login(self): if self.__login_flag is False: ret = self.__api.login(self.id, self.token) if ret[0] is None: raise Exception('login failed %s - %s' % (self.id, ret)) else: log.info('%s login success' % self.id) self.__login_flag = ret def __init__(self): self.__login_flag = False with open('.config.json') as f: info = json.load(f) self.id = info.get('id') self.token = info.get('token') # 获取市场股市列表 def instrumentinfo(self, _fields="", _filter="inst_type=1&status=1&market=SH,SZ"): self.__lazy_login() df, msg = self.__api.query(view="jz.instrumentInfo", fields=_fields, filter=_filter, data_format='pandas') log.debug('request jz.instrumentInfo') return df # 停复牌resu_date 未设置 # def secsusp(self, _filter="", _fields=""): # self.__lazy_login() # df, msg = self.__api.query( # view="lb.secSusp", # fields=_fields, # filter=_filter, # data_format='pandas') # log.debug('request lb.secSusp') # return CecsuspDataFrame(df) # 获取限售股解禁列表 # TODO #def secrestricted(self, _start_date, _end_date, _fields=""): # self.__lazy_login() # filters = urlencode({'start_date': TradeCalendar.date2int(_start_date), # 'end_date': TradeCalendar.date2int(_end_date)}) # df, msg = self.__api.query( # view="lb.secRestricted", # fields=_fields, # filter=filters, # data_format='pandas') # log.debug('request lb.secRestricted') # return df # 日交易行情 def daily(self, symbol, start_date, end_date, freq='1d', fields= "", adjust_mode='post'): self.__lazy_login() df, msg = self.__api.daily( symbol=symbol, freq=freq, start_date=start_date, end_date=end_date, fields=fields, adjust_mode=adjust_mode) log.debug('request daily %s' % msg) return df # 交易日历 def tradecal(self, _fields="trade_date, istradeday"): self.__lazy_login() df, msg = self.__api.query( view="jz.secTradeCal", fields=_fields) log.debug('request jz.secTradeCal') return df # 订阅 def subscribe(self, _symbol, _func, _fields): self.__lazy_login() sublist, msg = self.__api.subscribe(_symbol, func=_func, fields=_fields) log.debug('request subscribe') return sublist # bar def bar(self, _symbol, _trade_date, _freq="5M", _fields=""): self.__lazy_login() df, msg = self.__api.bar(symbol=_symbol, trade_date=_trade_date, freq=_freq, fields=_fields) log.debug('request bar') return df