用以测试效果 :return: """ # self.average_line_compensates() df_bias = self.data df_bias.loc[:, 'num'] = range(len(df_bias)) df_bias.plot('num', ['close', self.bias_col, self.bias_col + '_rank'], subplots=True, style='*--') def update_rt_rank_compare(self, rt_p): pass if __name__ == '__main__': jq.auth('18300210801', 'Ypw@522109') stk = 'M2101.XDCE' bias_obj_1d = Bias(stk_code=stk, freq='1m', span_q=1, span_s=10, hist_count=3600) bias_obj_1d.save_hist_data() """ bias_obj_1d.plot_test() plt.show() """
order(['000001', '000004'], [100, 200], context) elif context.current_date == pd.to_datetime('2010-3-8'): order_target(['000001', '000002', '000005'], [100, 300, 500], context) else: position_adjust(context, percentage=0.9, method='equal weight') # pass # print(context.stock_account.detail()) # print(context.current_date) def market_close(context): pass if __name__ == "__main__": jq.auth('13375779399', '88923392') # jq.auth('15869688376', '688376') count = 12 unit = '1M' stock_df = jq.get_all_securities(['stock'], '22000101') stock_df_exc_ST_DELIST = stock_df[ (~stock_df.display_name.str.contains('ST')) & (~stock_df.display_name.str.contains('退市'))] price_df = jq.get_price(list(stock_df_exc_ST_DELIST.index), end_date=datetime.date.today(), count=1, fill_paused=False) price_df.sort_values('code', inplace=True) avai_stock_list = list(price_df[~pd.isna(price_df['volume'])].code) # margin_list = jq.get_marginsec_stocks() 获取可融券列表 market_df = jq.get_bars(avai_stock_list,
''' Created on 2019年5月1日 # -*- coding:utf-8 -*- @author: Administrator ''' import pymysql import jqdatasdk as jq import time from _datetime import datetime #超参 jzrq = '2020-06-05' #jqdata认证 jq.auth('13401179853', 'king179853') #定义当前日期 d = time.strftime('%Y-%m-%d', time.localtime(time.time())) print(d) #建立连接 获取游标 connect = pymysql.Connect(host='localhost', port=3306, user='******', passwd='123456', db='xg', charset='utf8') cursor = connect.cursor() #获取现有dm数据 dm_list = []
for i in tqdm(zz800): # data = QA.QA_fetch_stock_day_adv(i[:6],start,et).data try: data = QA.QA_fetch_stock_day_adv(i[:6], start, et).data #print(data) divergence(data) codes[i[:6]] = [i[:6], i, data[0 - period:]] except: wrong.append(i[:6]) return codes, wrong if __name__ == '__main__': #auth('15196652660', 'JyNN20120606jyn') # 账号是申请时所填写的手机号;密码为聚宽官网登录密码,新申请用户默认为手机号后6位 auth('18011529069', 'JyNN20120606jyn') font = {'family': 'SimHei', 'weight': 'bold', 'size': 12} matplotlib.rc("font", **font) # zz800 指数 index_code = '000906.XSHG' # 行业分类标准 #sw_l1: 申万一级行业 industry_type = 'sw_l1' # 宽度日期 market_breadth_days = 80 # 获取每个行业 以及 获取每个行业包含中证800的成分股的个数 zz800 = get_index_stocks('000906.XSHG') #zz800.to_csv()
import jqdatasdk as jq jq.auth('18602166903', '13773275') df = jq.get_all_securities()[:3] print(df) bd = jq.get_baidu_factor( category='csi800', day='2018-06-25', stock='600519.XSHG', province=None) print(bd)
def save_data(start_date, end_date): jqdatasdk.auth(username=user_passwd.user_name, password=user_passwd.passwd) names_to_id = red.get('names_to_id') names_to_id = json.loads(names_to_id) indices = pd.read_csv( r'C:\Users\never\Desktop\website-practice\indices.csv') names = [] display_names = [] index_name_to_id = {} for i in range(len(indices)): if str(indices.start_date[i]) <= start_date and end_date <= str( indices.end_date[i]): names.append(indices.id[i]) display_names.append(indices.display_name[i]) index_name_to_id[indices.display_name[i]] = indices.id[i] indices = display_names names_to_id['index'] = index_name_to_id industries_df = jqdatasdk.get_industries('sw_l1') industry_end_date = { '建筑建材I': '2014-02-21', '机械设备I': '2014-02-21', '交运设备I': '2014-02-21', '信息设备I': '2014-02-21', '金融服务I': '2014-02-21', '信息服务I': '2014-02-21', } industries = [] industry_name_to_id = {} for industry_name, industry_id, start_time in zip( industries_df.name.values, industries_df.index.values, industries_df.start_date.values): st = str(start_time) if industry_name in industry_end_date.keys( ) and st <= start_date and end_date < industry_end_date[ industry_name]: industries.append(industry_name) industry_name_to_id.update({industry_name: industry_id}) elif industry_name not in industry_end_date.keys( ) and st <= start_date: industries.append(industry_name) industry_name_to_id.update({industry_name: industry_id}) names_to_id['industry_index'] = industry_name_to_id stock_pool = {} stock_ids, stock_names = [], [] stock_name_to_id = {} for i in range(len(display_names)): display, pool_id = display_names[i], names[i] p = jqdatasdk.get_all_securities(types=['stock'], date=start_date) p = p[p.end_date >= end_date] q = jqdatasdk.get_index_stocks(pool_id) id_to_name = dict(zip(p.index.values, p.display_name.values)) stock_name_to_id.update( dict(zip(p.display_name.values, p.index.values))) stocks = [] for item in q: try: stocks.append(id_to_name[item]) stock_ids.append(item) stock_names.append(id_to_name[item]) except Exception as e: pass stocks = sorted(stocks, key=lambda x: ' '.join(lazy_pinyin(x))) stock_pool[display] = stocks names_to_id['stock'] = stock_name_to_id get_funds = jqdatasdk.get_all_securities('fund', date=start_date) display_names = ['场内ETF', '场内LOF', '分级基金A', '分级基金B', '场内货币基金'] names = ['etf', 'lof', 'fja', 'fjb', 'money_market_fund'] exchanged_fund_pool = {} exchanged_name_to_id = dict(zip(display_names, names)) for i in range(len(display_names)): display, pool_id = display_names[i], names[i] p = get_funds[get_funds['type'] == pool_id] y1 = (p.start_date <= start_date).values y2 = (p.end_date >= end_date).values p = p[np.logical_and(y1, y2)] q = p.index.values id_to_name = dict(zip(p.index.values, p.display_name.values)) exchanged_name_to_id.update( dict(zip(p.display_name.values, p.index.values))) funds = [] for item in q: try: funds.append(id_to_name[item]) except Exception as e: pass funds = sorted(funds, key=lambda x: ' '.join(lazy_pinyin(x))) exchanged_fund_pool[display] = funds names_to_id['exchanged_fund'] = exchanged_name_to_id get_funds = jqdatasdk.get_all_securities('open_fund', date=start_date) get_funds = get_funds[get_funds.end_date >= end_date] display_names = ['股票型', '混合型', '债券型', '货币型', 'ETF联接', '黄金', 'QDII'] names = [ 'stock_fund', 'mixture_fund', 'bond_fund', 'money_market_fund', 'fund_fund', 'noble_metal_fund', 'QDII_fund' ] open_fund_pool = {} open_fund_name_to_id = dict(zip(display_names, names)) for i in range(len(display_names)): display, pool_id = display_names[i], names[i] if pool_id != 'QDII_fund': p = get_funds[get_funds['type'] == pool_id] else: p = jqdatasdk.get_all_securities(pool_id) q = p.index.values id_to_name = dict(zip(p.index.values, p.display_name.values)) open_fund_name_to_id.update( dict(zip(p.display_name.values, p.index.values))) funds = [] for item in q: try: funds.append(id_to_name[item]) except Exception as e: pass funds = sorted(funds, key=lambda x: ' '.join(lazy_pinyin(x))) open_fund_pool[display] = funds names_to_id['open_fund'] = open_fund_name_to_id data = { 'stock_pool': stock_pool, 'exchanged_fund_pool': exchanged_fund_pool, 'open_fund_pool': open_fund_pool, 'industry_pool': industries, 'index_pool': indices } key_name = 'data-' + start_date + '-' + end_date + '.pkl' red[key_name] = json.dumps(data) red['names_to_id'] = json.dumps(names_to_id)
def getAuthJQ(): jq.auth('13501253295', 'inside')
import pandas as pd from scipy.stats import rankdata from pyfinance.ols import PandasRollingOLS import datetime import graphviz import pickle from gplearn import genetic from gplearn.functions import make_function from gplearn.genetic import SymbolicTransformer, SymbolicRegressor from gplearn.fitness import make_fitness from sklearn.utils import check_random_state from sklearn.model_selection import train_test_split import jqdatasdk as jq jq.auth('18903041915', 'iamaman369') #%% 1. 数据读入 stock_list = jq.get_index_stocks('000300.XSHG') start_date = '2020-01-01' end_date = '2020-06-30' fields = ['open', 'close', 'low', 'high', 'volume', 'avg', 'pre_close'] stock_price = jq.get_price(stock_list, start_date=start_date, end_date=end_date, fq=None, fields=fields) stock_price = stock_price.sort_values(by=['code', 'time']).reset_index( drop=True) stock_price['time'] = stock_price['time'].apply(
def find_matches_subprocess(self, sub_sto_ids, enable_realtime, ts_fn, ts_jq): if enable_realtime: login = jq.auth('15808061188', 'allan2jq') match_infos = [] minimum_df_size = self.daily_count * 5 * 20 for sto_id in sub_sto_ids: try: df = BaosAgent.read_freq_df(self.delta, sto_id) ### add_macd_indicators minimum data size: 11198 if len(df) < 11198: continue curp = df.iloc[-1].close if curp > 50: continue if enable_realtime: df = DataUtil.add_rt_data(sto_id, df, self.delta, ts_jq) # df = df.iloc[-self.daily_count * 5 * 30:] # df = df.reset_index(drop=True) # df = self.add_strategy_features(df) df = self.add_macd_indicators(df) df = df.dropna() df = df.reset_index(drop=True) last_week_minp = min(df.close.iloc[-self.daily_count * 5:]) last_week_cr = (curp - last_week_minp) / last_week_minp last_month_minp = min(df.close.iloc[-self.daily_count * 20:]) last_month_cr = (curp - last_month_minp) / last_month_minp last_quart_minp = min(df.close.iloc[-self.daily_count * 60:]) last_quart_cr = (curp - last_quart_minp) / last_quart_minp last_hyear_minp = min(df.close.iloc[-self.daily_count * 120:]) last_hyear_cr = (curp - last_hyear_minp) / last_hyear_minp # if last_hyear_cr > .5: # and last_quart_cr < .5 and last_month_cr < .3 and last_week_cr < .2# and last_quart_cr < .5 and last_month_cr < .3 and last_week_cr < .2 : # continue # cond_long_9t = SFStrategy.compute_cond_long(df.k_9t, df.d_9t) # cond_long_8t = SFStrategy.compute_cond_long(df.k_8t, df.d_8t) # cond_long_7t = SFStrategy.compute_cond_long(df.k_7t, df.d_7t) # cond_long_6t = SFStrategy.compute_cond_long(df.k_6t, df.d_6t) # cond_long_5t = SFStrategy.compute_cond_long(df.k_5t, df.d_5t) # cond_long_4t = SFStrategy.compute_cond_long(df.k_4t, df.d_4t) # cond_long_3t = SFStrategy.compute_cond_long(df.k_3t, df.d_3t) # cond_long_2t = SFStrategy.compute_cond_long(df.k_2t, df.d_2t) # cond_long_1t = SFStrategy.compute_cond_long(df.k_1t, df.d_1t) # cond_all_long = cond_long_1t & cond_long_2t & cond_long_3t & cond_long_4t & cond_long_5t & cond_long_6t & cond_long_7t & cond_long_8t & cond_long_9t # if not (cond_all_long.iloc[-2]) and cond_all_long.iloc[-1]: cond_macd_allabove0 = (df.macd_1h > 0) & (df.macd_1t > 0) & ( df.macd_2t > 0) & (df.macd_3t > 0) & (df.macd_4t > 0) & (df.macd_5t > 0) if len(cond_macd_allabove0 ) > 0 and not cond_macd_allabove0.iloc[ -2] and cond_macd_allabove0.iloc[-1]: target_t = df.time.iloc[-1] print('{} {} {:>5.2f} {:>7.2%} {:>7.2%} {:>7.2%} {:>7.2%}'. format(sto_id, target_t, curp, last_hyear_cr, last_quart_cr, last_month_cr, last_week_cr)) match_infos.append([ sto_id, target_t, curp, last_hyear_cr, last_quart_cr, last_month_cr, last_week_cr, curp ]) except Exception as e: # print(sto_id, e) pass # if enable_realtime: # jq.logout() return match_infos
import jqdatasdk as jq import datetime from pymongo import MongoClient jq.auth('15804268950','3834259hanK') print(jq.get_query_count()) """ **Table: share_flo** --- ##### stock_a_db['share_flo'].find_one() ```python {'_id': ObjectId('5d5f4f3492600ddf9285d516'), 'index': '000001.XSHE', 'share_flow': 8258245083.0, # 流通股(单位 股) 'day': '2019-08-01'} ``` """ client = MongoClient('127.0.0.1', 27017) stock_a_db = client["stock_a"] def get_real_share(stock): # 10大流通股东加和 #print(stock._name) merge_df = jq.finance.run_query(jq.query(jq.finance.STK_SHAREHOLDER_FLOATING_TOP10).filter( jq.finance.STK_SHAREHOLDER_FLOATING_TOP10.code == stock._name).limit(10))
import jqdatasdk import pandas as pd import QUANTAXIS as QA import QUANTAXIS.QAFactor.analyze as analyze import QUANTAXIS.QAFactor.data as data import QUANTAXIS.QAFactor.preprocess as preprocess import QUANTAXIS.QAFactor.tears as tears import QUANTAXIS.QAFactor.utils as utils print('此测试文件需要配合jqdata使用, 详情请参考 QA_JQDATA') JQ_USERNAME = input('JQDATA_USERNAME: '******'JQDATA_PASSWORD: '******'2016-01-01', end='2018-12-31' ).data if len(df_finance) ==0: print('请先存储QUANTAXIS 财务数据 ==> 输入quantaxis进入cli后 输入save financialfiles') else: factor = df_finance['ROE']
open_interest=open_interest, gateway_name="DB") bars.append(bar) print("save size:%d" % bars.__len__()) database_manager.save_bar_data(bars) batch_start = batch_end #+ timedelta(days = 1) pass # start main start_day = datetime.strptime("2018-03-29", "%Y-%m-%d") end_day = datetime.strptime("2020-03-29", "%Y-%m-%d") database_manager.clean("600519.") if (not jq.is_auth()): jq.auth('13530336157', 'Qwer4321') #ID是申请时所填写的手机号;Password为聚宽官网登录密码,新申请用户默认为手机号后6位 if (not jq.is_auth()): print('jq is not auth,exist') exit() print('jq is auth') code = '600519' database_manager.clean(code) save_bar_data_from_jqdata(code, Interval.MINUTE, start_date=start_day, end_date=end_day)
def log_in(self): """ 登陆 """ user = '******' pass_wd = 'dfc19921208' auth(user, pass_wd)
'''这是一个获取worldquant101因子的脚本''' import jqdatasdk jqdatasdk.auth('17812137093', 'Sy16241098') from jqdatasdk.alpha101 import * import pandas as pd import numpy as np import os from get_trade_date import get_trade_date ######请在此处设置历史数据的查询范围,适合机构用户或者土豪散户 #STARTDATE='20160101' STARTDATE = '20190907' ENDDATE = '20200101' #ENDDATE='20200101' trade_date_list = get_trade_date(STARTDATE, ENDDATE) #get worldquant 101 alpha: for i in range(len(trade_date_list) - 1): a = pd.DataFrame() #for j in range(1,82): a.insert(0, 'alpha_001', alpha_001(trade_date_list[i], '000300.XSHG')) a.insert(1, 'alpha_002', alpha_002(trade_date_list[i], '000300.XSHG')) a.insert(2, 'alpha_003', alpha_003(trade_date_list[i], '000300.XSHG')) a.insert(3, 'alpha_004', alpha_004(trade_date_list[i], '000300.XSHG')) a.insert(4, 'alpha_005', alpha_005(trade_date_list[i], '000300.XSHG')) a.insert(5, 'alpha_006', alpha_006(trade_date_list[i], '000300.XSHG')) a.insert(6, 'alpha_007', alpha_007(trade_date_list[i], '000300.XSHG')) a.insert(7, 'alpha_008', alpha_008(trade_date_list[i], '000300.XSHG')) a.insert(8, 'alpha_009', alpha_009(trade_date_list[i], '000300.XSHG')) a.insert(9, 'alpha_010', alpha_010(trade_date_list[i], '000300.XSHG')) a.insert(10, 'alpha_011', alpha_011(trade_date_list[i], '000300.XSHG'))
def QA_SU_save_stock_min(client=DATABASE, ui_log=None, ui_progress=None): """ 聚宽实现方式 save current day's stock_min data """ # 导入聚宽模块且进行登录 try: import jqdatasdk # 请自行将 JQUSERNAME 和 JQUSERPASSWD 修改为自己的账号密码 jqdatasdk.auth("JQUSERNAME", "JQUSERPASSWD") except: raise ModuleNotFoundError # 股票代码格式化 code_list = list( map( lambda x: x + ".XSHG" if x[0] == "6" else x + ".XSHE", QA_fetch_get_stock_list().code.unique().tolist(), )) coll = client.stock_min coll.create_index([ ("code", pymongo.ASCENDING), ("time_stamp", pymongo.ASCENDING), ("date_stamp", pymongo.ASCENDING), ]) err = [] def __transform_jq_to_qa(df, code, type_): """ 处理 jqdata 分钟数据为 qa 格式,并存入数据库 1. jdatasdk 数据格式: open close high low volume money 2018-12-03 09:31:00 10.59 10.61 10.61 10.59 8339100.0 88377836.0 2. 与 QUANTAXIS.QAFetch.QATdx.QA_fetch_get_stock_min 获取数据进行匹配,具体处理详见相应源码 open close high low vol amount ... datetime 2018-12-03 09:31:00 10.99 10.90 10.99 10.90 2.211700e+06 2.425626e+07 ... """ if df is None or len(df) == 0: raise ValueError("没有聚宽数据") df = df.reset_index().rename(columns={ "index": "datetime", "volume": "vol", "money": "amount" }) df["code"] = code df["date"] = df.datetime.map(str).str.slice(0, 10) df = df.set_index("datetime", drop=False) df["date_stamp"] = df["date"].apply(lambda x: QA_util_date_stamp(x)) df["time_stamp"] = ( df["datetime"].map(str).apply(lambda x: QA_util_time_stamp(x))) df["type"] = type_ return df[[ "open", "close", "high", "low", "vol", "amount", "datetime", "code", "date", "date_stamp", "time_stamp", "type", ]] def __saving_work(code, coll): QA_util_log_info( "##JOB03 Now Saving STOCK_MIN ==== {}".format(code), ui_log=ui_log) try: for type_ in ["1min", "5min", "15min", "30min", "60min"]: col_filter = {"code": str(code)[0:6], "type": type_} ref_ = coll.find(col_filter) end_time = str(now_time())[0:19] if coll.count_documents(col_filter) > 0: start_time = ref_[coll.count_documents( col_filter) - 1]["datetime"] QA_util_log_info( "##JOB03.{} Now Saving {} from {} to {} == {}".format( ["1min", "5min", "15min", "30min", "60min"].index(type_), str(code)[0:6], start_time, end_time, type_, ), ui_log=ui_log, ) if start_time != end_time: df = jqdatasdk.get_price( security=code, start_date=start_time, end_date=end_time, frequency=type_.split("min")[0]+"m", ) __data = __transform_jq_to_qa( df, code=code[:6], type_=type_) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data)[1::]) else: start_time = "2015-01-01 09:30:00" QA_util_log_info( "##JOB03.{} Now Saving {} from {} to {} == {}".format( ["1min", "5min", "15min", "30min", "60min"].index(type_), str(code)[0:6], start_time, end_time, type_, ), ui_log=ui_log, ) if start_time != end_time: __data == __transform_jq_to_qa( jqdatasdk.get_price( security=code, start_date=start_time, end_date=end_time, frequency=type_.split("min")[0]+"m", ), code=code[:6], type_=type_ ) if len(__data) > 1: coll.insert_many( QA_util_to_json_from_pandas(__data)[1::]) except Exception as e: QA_util_log_info(e, ui_log=ui_log) err.append(code) QA_util_log_info(err, ui_log=ui_log) # 聚宽之多允许三个线程连接 executor = ThreadPoolExecutor(max_workers=2) res = { executor.submit(__saving_work, code_list[i_], coll) for i_ in range(len(code_list)) } count = 0 for i_ in concurrent.futures.as_completed(res): QA_util_log_info( 'The {} of Total {}'.format(count, len(code_list)), ui_log=ui_log ) strProgress = "DOWNLOAD PROGRESS {} ".format( str(float(count / len(code_list) * 100))[0:4] + "%") intProgress = int(count / len(code_list) * 10000.0) QA_util_log_info( strProgress, ui_log, ui_progress=ui_progress, ui_progress_int_value=intProgress ) count = count + 1 if len(err) < 1: QA_util_log_info("SUCCESS", ui_log=ui_log) else: QA_util_log_info(" ERROR CODE \n ", ui_log=ui_log) QA_util_log_info(err, ui_log=ui_log)
def load_data(self): jq.auth("15825675534", 'Programming123') return jq.get_price("000001.XSHE", start_date="2017-01-01", end_date="2017-12-31")
""" 测试文件 """ import jqdatasdk import pandas as pd import QUANTAXIS as QA import QUANTAXIS.QAFactor.analyze as analyze import QUANTAXIS.QAFactor.data as data import QUANTAXIS.QAFactor.preprocess as preprocess import QUANTAXIS.QAFactor.tears as tears import QUANTAXIS.QAFactor.utils as utils jqdatasdk.auth("聚宽用户名", "聚宽密码") code_list = QA.QA_fetch_stock_block_adv().get_block( "白酒").index.get_level_values("code").tolist() # code_list = QA.QA_fetch_stock_block_adv().get_block( # "中证200").index.get_level_values("code").tolist() df_finance = QA.QA_fetch_financial_report_adv(code=code_list, start='2016-01-01', end='2019-09-30').data factor = df_finance['ROE'] # 因子格式化 factor = preprocess.QA_fmt_factor(factor) # 上市日期过滤 factor_data = preprocess.QA_fetch_get_factor_start_date(factor) factor_data = factor_data.reset_index() factor_data = factor_data.loc[(factor_data.datetime - factor_data.start_date
daily_count = int(240/delta) jq_sto_id = JQDataUtil.get_jq_stoid(sto_id) df_rt = jq.get_price(jq_sto_id, count = daily_count * 3, end_date=ts_jq, frequency=str(delta) + 'm', fields=['open','high','low','close','volume']) df_rt.insert(0, 'code', sto_id) df_rt.insert(0, 'time', pd.Series(df_rt.index).dt.strftime('%Y%m%d%H%M').values) df_rt.insert(0, 'date', pd.Series(df_rt.index).dt.strftime('%Y-%m-%d').values) df_rt = df_rt.reset_index(drop=True) exist_times = org_df.time.values for idx in df_rt.index: new_data = df_rt.iloc[idx] if new_data.time not in exist_times: org_df = org_df.append(new_data, ignore_index=True) return org_df if __name__ == "__main__": jq.auth('15808061188', 'allan2jq') all_securities = jq.get_all_securities(date=datetime.now()) all_securities = all_securities[all_securities.display_name.str.find('ST') == -1] all_securities = all_securities[all_securities.display_name.str.find('退') == -1] all_securities = all_securities[all_securities.index.str.startswith('688') == False] all_sto_id = all_securities.index.str[:6].to_list() bs_agent = BaosAgent(all_sto_id) freq = 15 start_date = '2020-03-01' num_processes = 6 data_path = bs_agent.path_src+str(freq) + os.path.sep if not os.path.exists(data_path): os.mkdir(data_path) bs_agent.persist_freq_data(num_processes, start_date, freq)
from cindex import CIndex from jqdatasdk import auth from ccalendar import CCalendar from cstock_info import CStockInfo from base.cdate import get_day_nday_ago, get_dates_array if __name__ == '__main__': num = 5500 end_date = '2019-08-13' stock_info_client = CStockInfo() df = stock_info_client.get() code_list = df['code'].tolist() name_list = df['name'].tolist() code2namedict = dict(zip(code_list, name_list)) start_date = get_day_nday_ago(end_date, num=num, dformat="%Y-%m-%d") date_array = get_dates_array(start_date, end_date) auth('18701683341', '52448oo78') for code in [ '000001', '000016', '000300', '000905', '399001', '399005', '399673' ]: obj = CIndex(code) for mdate in date_array: if CCalendar.is_trading_day(mdate): table_name = obj.get_components_table_name(mdate) if obj.is_table_exists(table_name): obj.mysql_client.delete(table_name) for mdate in date_array: if CCalendar.is_trading_day(mdate): if not obj.set_components_data_from_joinquant( code2namedict, mdate):
# outside module import random import numpy as np import pandas as pd import statsmodels.api as sm import linearmodels from loguru import logger import matplotlib.pyplot as plt import datetime from jqdatasdk import auth, is_auth, get_price auth("18622056381", "1510103Yhw") logger.info("Connected to JQData: {}".format(is_auth())) # inside module from Utils import format_for_print from Pipeline import rets, sector_rets, market_cap_raw_value, market_cap, pe, pe_lyr, pb, ps, pcf, turnover # In sample fit X_in = pd.concat([ market_cap.stack(), pe.stack(), pe_lyr.stack(), pb.stack(), ps.stack(), pcf.stack(), turnover.stack() ], axis=1) X_in.columns = ["market_cap", "pe", "pe_lyr", "pb", "ps", "pcf", "turnover"] y_in = rets.stack()
import pandas as pd import numpy as np import jqdatasdk as jq from jqdatasdk import auth, get_query_count, get_price, opt, query, get_fundamentals, finance, get_trade_days, \ valuation, get_security_info, get_index_stocks, get_bars,indicator,income from tqdm import tqdm from datetime import datetime, time, timedelta import matplotlib.pyplot as plt import os from basic_funcs.basic_function import * import seaborn as sns auth('15951961478', '961478') get_query_count() dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d') plt.rcParams['font.sans-serif'] = ['Songti SC'] plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 hs300 = pd.read_excel('/Users/caichaohong/Desktop/Zenki/price/510300.XSHG.xlsx', index_col='Unnamed: 0') hs300['rts_1'] = hs300['close'].pct_change(1) close = pd.read_csv('/Users/caichaohong/Desktop/Zenki/price/daily/close.csv', index_col='Unnamed: 0',date_parser=dateparse) close_rts_1 = close.pct_change(1) volume = pd.read_csv('/Users/caichaohong/Desktop/Zenki/price/daily/volume.csv', index_col='Unnamed: 0',date_parser=dateparse) money = close * volume * 10 ** (-8) market_cap = pd.read_csv('/Users/caichaohong/Desktop/Zenki/financials/market_cap.csv', index_col='Unnamed: 0',date_parser=dateparse) roe_yeayly = pd.read_csv('/Users/caichaohong/Desktop/Zenki/financials/roe_yearly.csv', index_col='statDate') net_profit = pd.read_csv('/Users/caichaohong/Desktop/Zenki/financials/net_profit_yearly.csv', index_col='statDate') pe = pd.read_csv('/Users/caichaohong/Desktop/Zenki/financials/pe_ratio.csv', index_col='Unnamed: 0', date_parser=dateparse)
import os import pandas as pd import numpy as np from trading_gym.envs.portfolio_gym.portfolio_gym import PortfolioTradingGym import jqdatasdk from xqdata.api import history_bars jqdata_username = os.environ["JQDATA_USERNAME"] jqdata_password = os.environ["JQDATA_PASSWORD"] jqdatasdk.auth(username=jqdata_username, password=jqdata_password) # ================================================== # # step1: create environment # # ================================================== # order_book_ids = ['000001.XSHE', '000002.XSHE', '300520.XSHE'] fields = ["close"] rolling_window = 20 bar_count = 200 dt = "2020-03-12" frequency = "1d" # data = history_bars(order_book_ids=order_book_ids, bar_count=bar_count + rolling_window, frequency=frequency, fields=fields, dt=dt) pct_change = data.groupby(level="order_book_id")["close"].pct_change() pct_change.name = "returns" feature_df = pct_change.to_frame() feature_df = feature_df.fillna(0)
import jqdatasdk import jqdata jqdatasdk.auth('18813173861', 'wuyifan1994')
def load_data(self): jq.auth("15825675534",'Programming123') self.nonredundant=list(set(self.stock_list)) return stock_panel
def main(): jq.auth('18620668927', 'minpeng123') get_all_securities() jq.logout()
def __init__(self): jq.auth(config.jqauth['name'], config.jqauth['passwd'])
pd_all.to_csv(file_path, index=False) def csv_contain_day(pd_csv, day): return not pd_csv.empty and len(pd_csv[pd_csv['date'] == day]) > 0 def func(df): # print(df) return df['turnover_ratio'].corr(df['ret']) if __name__ == "__main__": user_name = '18602166903' passwd = '13773275' jq.auth(user_name, passwd) frame = get_ndays_frame('2018-08-01', 20, 'fr_data.csv') pd_save_to_csv(frame, 'fr_data.csv') # file_path = g_path + "/fr.csv" # frame.to_csv(file_path) # signal_fr = frame.groupby('code').apply(func) signal_fr = frame.groupby('code').apply( lambda df: df['turnover_ratio'].corr(df['ret'], method='pearson')) # signal_fr.dropna(axis=1) frame_positive = frame[frame['ret'] > 0] frame_negative = frame[frame['ret'] < 0] signal_fru = frame_positive.groupby('code').apply( lambda df: df['turnover_ratio'].corr(df['ret'], method='pearson'))
def __init__(self, batch_size=10, force_update=True, sleeping_time=10) -> None: super().__init__(batch_size, force_update, sleeping_time) auth(zvt_env['jq_username'], zvt_env['jq_password'])
def yyyymmdd_date(dt: datetime) -> int: return dt.year * 10**4 + dt.month * 10**2 + dt.day if __name__ == "__main__": start = datetime.datetime(2005, 1, 4) end = datetime.datetime(2019, 12, 31) sus, trading = gen_sh000001(start, end) trading = [yyyymmdd_date(day) for day in trading] print(len(trading)) print(trading[0]) print(trading[-1]) # 使用 JQdata 接口的数据做测试 import jqdatasdk as jqsdk jqsdk.auth('15626046299', '046299') # print(type(jqsdk.get_all_trade_days())) res1 = jqsdk.get_all_trade_days() res2 = res1.tolist() res2 = [yyyymmdd_date(day) for day in res2] print(len(res2)) print(res2[0]) # 2005-01-04 print(res2[-1]) # 2019-12-31 print(trading == res2)
import numpy as np import pandas as pd import datetime import jqdatasdk import sys import getopt import time if __name__ == "__main__": stock_name = "" file_name = "" jqdatasdk.auth('18380152997','2wsx3edc') try: # 获取命令行的数据 # 输入:股票代码 、保存的文件名 # 输出:将股票的当前数据保存到指定文件 # 时间:从2011-01-01 至今 opts,args = getopt.getopt(args = sys.argv[1:],shortopts = "f:s:",longopts=["stockname=","filename="]) print(sys.argv[1:]) except getopt.GetoptError: print('Error:python3 get_stock.py -f 000651.csv -s 000651.XSHE') print('or python3 get_stock.py --filename=000651.csv --stockname=000651.XSHE')
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from quant_benchmark.data.base_data_source.default_data_source import DefaultDataSource import jqdatasdk #see https://github.com/JoinQuant/jqdatasdk/blob/master/tests/test_api.py jqdatasdk.auth(username='******', password='******') data_source = DefaultDataSource() order_book_id = "000001.XSHE" bar_count = 10 dt = "2019-09-20" data = data_source.history_bars(order_book_id=order_book_id, bar_count=bar_count, frequency="1w", dt=dt)
# coding:utf-8 try: import jqdatasdk jqdatasdk.auth(input('account:'),input('password:'******'2018-05-14') if __name__ =='__main__': print(get_price()) """ get_price 可查询股票、基金、指数、期货的历史及当前交易日的行情数据 可指定单位时间长度,如一天、一分钟、五分钟等 可查询开盘价、收盘价、最高价、最低价、成交量、成交额、涨停、跌停、均价、前收价、是否停牌 支持不同的复权方式