13.【QMT使用指南】- 完整示例

获取行情示例
按品种划分
两融
获取融资融券账户可融资买入标的
# coding:gbkdef init(C): r = get_assure_contract('123456789') if len(r) == 0: print('未取到担保明细') else: finable = [o.m_strInstrumentID+'.'+o.m_strExchangeID for o in r if o.m_eFinStatus==48] print('可融资买入标的:', finable)
按功能划分
订阅 K 线全推
提示:K 线全推需要 VIP 权限,非 VIP 用户请勿使用此功能。
订阅全市场 1m 周期 K 线
# coding:gbkimport pandas as pdimport numpy as npdef init(C): stock_list = C.get_stock_list_in_sector("沪深A股") sub_num_dict = {i: C.subscribe_quote( stock_code=i, period='1m', dividend_type='none', result_type='dict', # 回调函数的行情数据格式 callback=call_back # 指定一个自定义的函数接收行情,自定义的函数只能有一个位置参数 ) for i in stock_list}def call_back(data): print(data)
获取 N 分钟周期 K 线数据
提示:
-
获取历史 N 分钟数据前,需要先下载历史数据。
-
1m 以上,5m 以下的数据,是通过 1m 数据合成的。
-
5m 以上,1d 以下的数据,是通过 5m 数据合成的。
-
1d 以上的数据,是通过 1d 的数据合成的。
# coding:gbkimport pandas as pdimport numpy as npdef init(C): # start_date = '20231001'# 格式"YYYYMMDD",开始下载的日期,date = ""时全量下载 start_date = '20231001'# 格式"YYYYMMDD",开始下载的日期,date = ""时增量下载 end_date = "" # 格式同上,下载结束时间 period = "3m" # 数据周期 need_download = 1 # 取数据是空值时,将need_download赋值为1,确保正确下载了历史数据 # code_list = ["110052.SH"] # 可转债 # code_list = ["rb2401.SF", "FG403.ZF"] # 期货列表 # code_list = ["HO2310-P-2500.IF"] # 期权列表 code_list = ["000001.SZ", "600519.SH"] # 股票列表 # 判断要不要下载数据 if need_download: my_download(code_list, period, start_date, end_date) # 取数据 data = C.get_market_data_ex([], code_list, period=period, start_time=start_date, end_time=end_date, dividend_type="back_ratio") print(data) # 行情数据查看 print(C.get_instrumentdetAIl(code_list[0])) # 合约信息查看def hanldebar(C): returndef my_download(stock_list, period, start_date='', end_date=''): ''' 用于显示下载进度 ''' if "d" in period: period = "1d" elif "m" in period: if int(period[0]) < 5: period = "1m" else: period = "5m" elif "tick" == period: pass else: raise KeyboardInterrupt("周期传入错误") n = 1 num = len(stock_list) for i in stock_list: print(f"当前正在下载{n}/{num}") download_history_data(i, period, start_date, end_date) n += 1 print("下载任务结束")
获取 Lv1 行情数据
本示例用于说明如何通过函数获取行情数据。
# coding:gbk# get_market_data_ex(subscribe=True)有订阅股票数量限制# 即stock_list参数的数量不能超过500# get_market_data_ex(subscribe=False) 该模式下(非订阅模式),接口会从本地行情文件里获取数据,不会获取动态行情数,且不受订阅数限制,但需要提前下载数据# 下载数据在 操作/数据管理/补充数据选项卡里,按照页面提示下载数据# get_market_data_ex(subscribe=True) 该模式下(订阅模式),受订阅数量上限限制,可以取到动态行情# 建议每天盘后增量补充对应周期的行情import timedef init(C): C.stock = C.stockcode + '.' + C.market # 获取指定时间的k线 price = C.get_market_data_ex(['open','high','low','close'], [C.stock], start_time='', end_time='', period='1d', subscribe=False) print(price[C.stock].head())def handlebar(C): bar_timetag = C.get_bar_timetag(C.barpos) bar_date = timetag_to_datetime(bar_timetag, '%Y%m%d%H%M%S') print('获取截至到%s为止前5根k线的开高低收等字段:' % (bar_date)) # 获取截至今天为止前30根k线 price = C.get_market_data_ex( [], [C.stock], end_time=bar_date, period=C.period, subscribe=True, count=5, ) print(price[C.stock].to_dict('dict'))
获取 Lv2 数据(需要数据源支持)
方法 1 - 查询 LV2 数据
使用该函数后,会定期查询最新数据,并进行数据返回。
# coding:gbkdef init(C): C.sub_nums = [] C.stock = C.stockcode + '.' + C.market for field in ['l2transaction', 'l2order', 'l2transactioncount', 'l2quote']: num = C.subscribe_quote(C.stock, period=field, dividend_type='follow', ) C.sub_nums.append(num)def handlebar(C): if not C.is_last_bar(): return price = C.get_market_data_ex([],[C.stock], period='l2transaction', count=10)[C.stock] price_dict = price.to_dict('index') print(price_dict) for pos, t in enumerate(price_dict): print(f" 逐笔成交:{pos+1} 时间:{price_dict[t]['stime']}, 时间戳:{price_dict[t]['time']}, 成交价:{price_dict[t]['price']}, \成交量:{price_dict[t]['volume']}, 成交额:{price_dict[t]['amount']} \成交记录号:{price_dict[t]['tradeIndex']}, 买方委托号:{price_dict[t]['buyNo']},\卖方委托号:{price_dict[t]['sellNo']}, 成交类型:{price_dict[t]['tradeType']}, \成交标志:{price_dict[t]['tradeFlag']}, ") price = C.get_market_data_ex([],[C.stock], period='l2quote', count=10)[C.stock] price_dict = price.to_dict('index') print(price_dict) for pos, t in enumerate(price_dict): print(f" 十档快照:{pos+1} 时间:{price_dict[t]['stime']}, 时间戳:{price_dict[t]['time']}, 最新价:{price_dict[t]['lastPrice']}, \开盘价:{price_dict[t]['open']}, 最高价:{price_dict[t]['high']} 最低价:{price_dict[t]['low']}, 成交额:{price_dict[t]['amount']},\成交总量:{price_dict[t]['volume']}, 原始成交总量:{price_dict[t]['pvolume']}, 证券状态:{price_dict[t]['stockStatus']}, 持仓量:{price_dict[t]['openInt']},\成交笔数:{price_dict[t]['transactionNum']},前收盘价:{price_dict[t]['lastClose']},多档委卖价:{price_dict[t]['askPrice']},多档委卖量:{price_dict[t]['askVol']},\多档委买价:{price_dict[t]['bidPrice']},多档委买量:{price_dict[t]['bidVol']}")
ETF
#coding:gbkdef handlebar(ContextInfo): if not ContextInfo.is_last_bar(): return # 单股单账号 最新价买入上证etf 2000份 passorder(23, 1101, 'test', '510050.SH', 5, -1, 2000, ContextInfo)
组合交易
一键买卖(一篮子下单)
功能描述:该示例演示如何用Python进行一揽子股票买卖的交易操作
代码示例:
#coding:gbkdef init(C): table=[ {'stock':'600000.SH','weight':0.11,'quantity':100,'optType':23}, {'stock':'600028.SH','weight':0.11,'quantity':200,'optType':24}, ] basket={'name':'basket1','stocks':table} set_basket(basket) # 按篮子数量下单, 下2份 # 即下两倍篮子 pice = 2 passorder(35, #一键买卖 2101, # 表示按股票数量下单 account, 'basket1', # 篮子名称 5, # 最新价下单 1, # 价格,最新价时 该参数无效,需要填任意数占位 pice, # 篮子份数 '',2,'strReMark',C) # 按篮子权重下单 table=[ {'stock':'600000.SH','weight':0.4,'quantity':0,'optType':23}, # 40% {'stock':'600028.SH','weight':0.6,'quantity':0,'optType':24}, # 60% ] basket={'name':'basket2','stocks':table} set_basket(basket) # 按组合权重 总额10000元 money = 10000 passorder(35,2102,account,'basket2',5,1,money,'',2,'strReMark',C)
组合套利交易
提示
(accountID、orderType 特殊设置)
用法
释义:
参数
参数名称 | 描述 |
---|---|
accountID | ‘stockAccountID, futureAccountID’ |
orderCode | ‘basketName, futureName’ |
hedgeRatio | 套利比例(0 ~ 2 之间值,相当于 %0 至 200% 套利) |
volume | 份数 \ 资金 \ 比例 |
orderType | 参考下方orderType-下单方式(特殊设置) |
orderType - 下单方式(特殊设置)
编号 | 项目 |
---|---|
2331 | 组合、套利、合约价值自动套利、按组合股票数量方式下单 |
2332 | 组合、套利、按合约价值自动套利、按组合股票权重方式下单 |
2333 | 组合、套利、按合约价值自动套利、按账号可用方式下单 |
示例
python返回值
按功能划分
passorder 下单函数
本示例用于演示K线走完下单及立即下单的参数写法差异,旨在帮助您了解如何快速实现下单操作。
#coding:gbkc = 0s = '000001.SZ'def init(ContextInfo): # 立即下单 用最新价买入股票s 100股,且指定投资备注 passorder(23,1101,account,s,5,0,100,'1',2,'tzbz',ContextInfo) passdef handlebar(ContextInfo): if not ContextInfo.is_last_bar(): #历史k线不应该发出实盘信号 跳过 return if ContextInfo.is_last_bar(): global c c +=1 if c ==1: # 用14.00元限价买入股票s 100股 passorder(23,1101,account,s,11,14.00,100,1,ContextInfo) # 当前k线为最新k线 则立即下单 # 用最新价限价买入股票s 100股 passorder(23,1101,account,s,5,-1,100,0,ContextInfo) # K线走完下单 # 用最新价限价买入股票s 1000元 passorder(23, 1102, account, s, 5, 0,1000, 2, ContextInfo) # 不管是不是最新K线,立即下单
集合竞价下单
本示例演示了利用定时器函数和passorder下单函数在集合竞价期间以指定价买入平安银行100股。
#coding:gbkimport timec = 0s = '000001.SZ'def init(ContextInfo): # 设置定时器,历史时间表示会在一次间隔时间后开始调用回调函数 比如本例中 5秒后会后第一次触发myHandlebar调用 之后五秒触发一次 ContextInfo.run_time("myHandlebar","5nSecond","2019-10-14 13:20:00")def myHandlebar(ContextInfo): global c now = time.strftime('%H%M%S') if c ==0 and '092500' >= now >= '091500': c += 1 passorder(23,1101,account,s,11,14.00,100,2,ContextInfo) # 立即下单def handlebar(ContextInfo): return
止盈止损示例
#coding:gbk"""1.账户内所有股票,当股价低于买入价10%止损卖出。2.账户内所有股票,当股价高于前一天的收盘价10%时,开始监控一旦股价炸板(开板),以买三价卖出"""def init(C): C.ratio = 1 if accountType == 'STOCK': C.sell_code = 24 if accountType == 'CREDIT': C.sell_code = 34 C.spare_list = C.get_stock_list_in_sector('不卖品种')def handlebar(C): if not C.is_last_bar(): return holdings = get_trade_detail_data(account, accountType, 'position') stock_list = [holding.m_strInstrumentID + '.' + holding.m_strExchangeID for holding in holdings] if stock_list: full_tick = C.get_full_tick(stock_list) for holding in holdings: stock = holding.m_strInstrumentID + '.' + holding.m_strExchangeID rate = holding.m_dProfitRate volume = holding.m_nCanUseVolume if not volume >= 100: continue if stock in C.spare_list: continue if rate < -0.1: msg = f'{stock} 盈亏比例 {rate} 小于-10% 卖出 {volume}股' print(msg) passorder(C.sell_code, 1101, account, stock, 14, -1, volume, '减仓模型', 2, msg, C) continue if stock in full_tick: current_price = full_tick[stock]['lastPrice'] pre_price = full_tick[stock]['lastClose'] high_price = full_tick[stock]['high'] stop_price = pre_price * 1.2 if stock[:2] in ['30', '68'] else pre_price * 1.1 stop_price = round(stop_price, 2) ask_price_3 = full_tick[stock]['bidPrice'][2] if not ask_price_3: print(f"{stock} {full_tick[stock]} 未取到三档盘口价 请检查客户端右下角 行情界面 是否选择了五档行情 本次跳过卖出") continue if high_price == stop_price and current_price < stop_price: msg = f"{stock} 涨停后 开板 卖出 {volume}股" print(msg) passorder(C.sell_code, 1101, account, stock, 14, -1, volume, '减仓模型', 2, msg, C) continue
passorder 下算法单函数
本示例由于演示如何下达算法单,具体算法参数请参考迅投投研平台客户端参数说明。
#coding:gbkimport timedef init(C): userparam={ 'OrderType':1, #表示要下算法 'PriceType':0, # 卖5价下单 'MaxOrderCount':12, # 最大委托次数 'SuperPriceType':0, # 超价类型,0表示按比例 'SuperPriceRate':0.02, # 超价2%下单 'VolumeRate':0.1, # 单笔下单比率 每次拆10% 'VolumeType': 10, # 单笔基准量类型 'SingleNumMax':1000000, # 单笔拆单最大值 'PriceRangeType':0, # 波动区间类型 'PriceRangeRate':1, # 波动区间值 'ValidTimeType':1, # 有效时间类型 1 表示按执行时间 'ValidTimeStart':int(time.time()), # 算法开始时间 'ValidTimeEnd':int(time.time()+60*60), # 算法结束时间 'PlaceOrderInterval':10, # 报撤间隔 'UndealtEntrustRule':0, # 未成委托处理数值 用卖5加挂单 } target_vol = 2000000 # 股, 算法目标总量 algo_passorder(23, 1101, account, '600000.SH', -1, -1, target_vol, '', 2, '普通算法', userparam, C) print('finish')def handlebar(C): pass
如何使用投资备注
投资备注功能是模型下单时指定的任意字符串(长度小于24),即passorder的userOrderId参数,可以用于匹配委托或成交。有且只有passorder, algo_passorder, smart_algo_passorder下单函数支持投资备注功能。
# encoding:gbknote = 0def get_new_note(): global note note += 1 return str(note)def init(ContextInfo): ContextInfo.set_account(account) passorder(23, 1101, account, '000001.SZ', 5 ,0, 100, '', 2, get_new_note(), ContextInfo) orders = get_trade_detail_data(account, accountType, 'order') remark = [o.m_strRemark for o in orders] sysid_list = [o.m_strOrderSysID for o in orders] print(remark)def handlebar(C): passdef order_callback(C, O): print(O.m_strRemark, O.m_strOrderSysID)def deal_callback(C, D): print(D.m_strRemark, D.m_strOrderSysID)
如何获取委托持仓及资金数据
本示例用于演示如何通过函数获取指定账户的委托、持仓、资金数据。
#coding:gbkdef to_dict(obj): attr_dict = {} for attr in dir(obj): try: if attr[:2] == 'm_': attr_dict[attr] = getattr(obj, attr) except: pass return attr_dictdef init(C): pass #orders, deals, positions, accounts = query_info(C)def handlebar(C): if not C.is_last_bar(): return orders, deals, positions, accounts = query_info(C)def query_info(C): orders = get_trade_detail_data('8000000213', 'stock', 'order') for o in orders: print(f'股票代码: {o.m_strInstrumentID}, 市场类型: {o.m_strExchangeID}, 证券名称: {o.m_strInstrumentName}, 买卖方向: {o.m_nOffsetFlag}', f'委托数量: {o.m_nVolumeTotalOriginal}, 成交均价: {o.m_dTradedPrice}, 成交数量: {o.m_nVolumeTraded}, 成交金额:{o.m_dTradeAmount}') deals = get_trade_detail_data('8000000213', 'stock', 'deal') for dt in deals: print(f'股票代码: {dt.m_strInstrumentID}, 市场类型: {dt.m_strExchangeID}, 证券名称: {dt.m_strInstrumentName}, 买卖方向: {dt.m_nOffsetFlag}', f'成交价格: {dt.m_dPrice}, 成交数量: {dt.m_nVolume}, 成交金额: {dt.m_dTradeAmount}') positions = get_trade_detail_data('8000000213', 'stock', 'position') for dt in positions: print(f'股票代码: {dt.m_strInstrumentID}, 市场类型: {dt.m_strExchangeID}, 证券名称: {dt.m_strInstrumentName}, 持仓量: {dt.m_nVolume}, 可用数量: {dt.m_nCanUseVolume}', f'成本价: {dt.m_dOpenPrice:.2f}, 市值: {dt.m_dInstrumentValue:.2f}, 持仓成本: {dt.m_dPositionCost:.2f}, 盈亏: {dt.m_dPositionProfit:.2f}') accounts = get_trade_detail_data('8000000213', 'stock', 'account') for dt in accounts: print(f'总资产: {dt.m_dBalance:.2f}, 净资产: {dt.m_dAssureAsset:.2f}, 总市值: {dt.m_dInstrumentValue:.2f}', f'总负债: {dt.m_dTotalDebit:.2f}, 可用金额: {dt.m_dAvailable:.2f}, 盈亏: {dt.m_dPositionProfit:.2f}') return orders, deals, positions, accounts
使用快速交易参数委托
本例展示如何使用快速交易参数(quickTrade)立刻进行委托。
#coding:gbkdef after_init(C): #account变量是模型交易界面 添加策略时选择的资金账号 不需要手动填写 #快速交易参数(quickTrade )填2 passorder函数执行后立刻下单 不会等待k线走完再委托。 可以在after_init函数 run_time函数注册的回调函数里进行委托 msg = f"投资备注字符串 用来区分不同委托" passorder(23, 1101, account, '600000.SH', 5, -1, 200, '测试下单', 2, msg, C)
调整至目标持仓
本示例由于演示如何调仓。
#encoding:gbk'''调仓到指定篮子'''import pandas as pdimport numpy as npimport timefrom datetime import timedelta,datetime#自定义类 用来保存状态 class a():passA=a()A.waiting_dict = {}A.all_order_ref_dict = {}#撤单间隔 单位秒 超过间隔未成交的委托撤回重报A.withdraw_secs = 30#定义策略开始结束时间 在两者间时进行下单判断 其他时间跳过A.start_time = '093000'A.end_time = '150000'def init(C): '''读取目标仓位 字典格式 品种代码:持仓股数, 可以读本地文件/数据库,当前在代码里写死''' A.final_dict = {"600000.SH" :10000, '000001.SZ' : 20000} '''设置交易账号 acount accountType是界面上选的账号 账号类型''' A.acct = account A.acct_type = accountType #定时器 定时触发指定函数 C.run_time("f","1nSecond","2019-10-14 13:20:00","SH")def f(C): '''定义定时触发的函数 入参是ContextInfo对象''' #记录本次调用时间戳 t0 = time.time() final_dict=A.final_dict #本次运行时间字符串 now = datetime.now() now_timestr = now.strftime("%H%M%S") #跳过非交易时间 if now_timestr < A.start_time or now_timestr > A.end_time: return #获取账号信息 acct = get_trade_detail_data(A.acct, A.acct_type, 'account') if len(acct) == 0: print(A.acct, '账号未登录 停止委托') return acct = acct[0] #获取可用资金 available_cash = acct.m_dAvailable print(now, '可用资金', available_cash) #获取持仓信息 position_list = get_trade_detail_data(A.acct, A.acct_type, 'position') #持仓数据 组合为字典 position_dict = {i.m_strInstrumentID + '.' + i.m_strExchangeID : int(i.m_nVolume) for i in position_list} position_dict_available = {i.m_strInstrumentID + '.' + i.m_strExchangeID : int(i.m_nCanUseVolume) for i in position_list} #未持有的品种填充持股数0 not_in_position_stock_dict = {i : 0 for i in final_dict if i not in position_dict} position_dict.update(not_in_position_stock_dict) #print(position_dict) stock_list = list(position_dict.keys()) # print(stock_list) #获取全推行情 full_tick = C.get_full_tick(stock_list) #print('fulltick', full_tick) #更新持仓状态记录 refresh_waiting_dict(C) #撤超时委托 order_list = get_trade_detail_data(A.acct, 'stock', 'order') if '091500'<= now_timestr <= '093000':#指定的范围內不撤单 pass else: for order in order_list: #非本策略 本次运行记录的委托 不撤 if order.m_strRemark not in A.all_order_ref_dict: continue #委托后 时间不到撤单等待时间的 不撤 if time.time() - A.all_order_ref_dict[order.m_strRemark] < A.withdraw_secs: continue #对所有可撤状态的委托 撤单 if order.m_nOrderStatus in [48,49,50,51,52,55,86,255]: print(f"超时撤单 停止等待 {order.m_strRemark}") cancel(order.m_strOrderSysID,A.acct,'stock',C) #下单判断 for stock in position_dict: #有未查到的委托的品种 跳过下单 防止超单 if stock in A.waiting_dict: print(f"{stock} 未查到或存在未撤回委托 {A.waiting_dict[stock]} 暂停后续报单") continue if stock in position_dict.keys(): #print(position_dict[stock],target_vol,'1111') #到达目标数量的品种 停止委托 target_vol = final_dict[stock] if stock in final_dict else 0 if int(abs(position_dict[stock] - target_vol)) == 0: print(stock, C.get_stock_name(stock), '与目标一致') continue #与目标数量差值小于100股的品种 停止委托 if abs(position_dict[stock] - target_vol) < 100: print(f"{stock} {C.get_stock_name(stock)} 目标持仓{target_vol} 当前持仓{position_dict[stock]} 差额小于100 停止委托") continue #持仓大于目标持仓 卖出 if position_dict[stock]>target_vol: vol = int((position_dict[stock] - target_vol)/100)*100 if stock not in position_dict_available: continue vol = min(vol, position_dict_available[stock]) #获取买一价 print(stock,'应该卖出') buy_one_price = full_tick[stock]['bidPrice'][0] #买一价无效时 跳过委托 if not buy_one_price > 0: print(f"{stock} {C.get_stock_name(stock)} 取到的价格{buy_one_price}无效,跳过此次推送") continue print(f"{stock} {C.get_stock_name(stock)} 目标股数{target_vol} 当前股数{position_dict[stock]}") msg = f"{now.strftime('%Y%m%d%H%M%S')}_{stock}_sell_{vol}股" print(msg) #对手价卖出 passorder(24,1101,A.acct,stock,14,-1,vol,'调仓策略',2,msg,C) A.waiting_dict[stock] = msg A.all_order_ref_dict[msg] = time.time() #持仓小于目标持仓 买入 if position_dict[stock]<target_vol: vol = int((target_vol-position_dict[stock])/100)*100 #获取卖一价 sell_one_price = full_tick[stock]['askPrice'][0] #卖一价无效时 跳过委托 if not sell_one_price > 0: print(f"{stock} {C.get_stock_name(stock)} 取到的价格{sell_one_price}无效,跳过此次推送") continue target_value = sell_one_price * vol if target_value > available_cash: print(f"{stock} 目标市值{target_value} 大于 可用资金{available_cash} 跳过委托") continue print(f"{stock} {C.get_stock_name(stock)} 目标股数{target_vol} 当前股数{position_dict[stock]}") msg = f"{now.strftime('%Y%m%d%H%M%S')}_{stock}_buy_{vol}股" print(msg) #对手价买入 passorder(23,1101,A.acct,stock,14,-1,vol,'调仓策略',2,msg,C) A.waiting_dict[stock] = msg A.all_order_ref_dict[msg] = time.time() available_cash -= target_value #打印函数运行耗时 定时器间隔应大于该值 print(f"下单判断函数运行完成 耗时{time.time() - t0}秒")def refresh_waiting_dict(C): """更新委托状态 入参为ContextInfo对象""" #获取委托信息 order_list = get_trade_detail_data(A.acct,A.acct_type,'order') #取出委托对象的 投资备注 : 委托状态 ref_dict = {i.m_strRemark : int(i.m_nOrderStatus) for i in order_list} del_list = [] for stock in A.waiting_dict: if A.waiting_dict[stock] in ref_dict and ref_dict[A.waiting_dict[stock]] in [56, 53, 54]: #查到对应投资备注 且状态为成交 / 已撤 / 部撤, 从等待字典中删除 print(f'查到投资备注 {A.waiting_dict[stock]},的委托 状态{ref_dict[A.waiting_dict[stock]]} (56已成 53部撤 54已撤)从等待等待字典中删除') del_list.append(stock) if A.waiting_dict[stock] in ref_dict and ref_dict[A.waiting_dict[stock]] == 57: #委托状态是废单的 也停止等待 从等待字典中删除 print(f"投资备注为{A.waiting_dict[stock]}的委托状态为废单 停止等待") del_list.append(stock) for stock in del_list: del A.waiting_dict[stock]
获取融资融券账户可融资买入标的
#coding:gbkdef init(C): r = get_assure_contract('123456789') if len(r) == 0: print('未取到担保明细') else: finable = [o.m_strInstrumentID+'.'+o.m_strExchangeID for o in r if o.m_eFinStatus==48] print('可融资买入标的:', finable)
获取两融账号信息示例
#coding:gbkdef init(C): account_str = '11800028' credit_account = query_credit_account(account_str, 1234, C)def credit_account_callback(C,seq,result): print('可买担保品资金', result.m_dAssureEnbuyBalance)
直接还款示例
该示例演示使用python进行融资融券账户的还款操作。
#coding:gbk def init(ContextInfo): # 用passorder函数进行融资融券账号的直接还款操作 money = 10000 #还款金额 #account='123456' s = '000001.SZ' # 代码填任意股票,占位用 passorder(32, 1101, account, s, 5, 0, money, 2, ContextInfo) # passorder(75, 1101, account, s, 5, 0, money, 2, ContextInfo) 专项直接还款
交易数据查询示例
#coding:gbkdef to_dict(obj): attr_dict = {} for attr in dir(obj): try: if attr[:2] == 'm_': attr_dict[attr] = getattr(obj, attr) except: pass return attr_dictdef init(C): pass #orders, deals, positions, accounts = query_info(C)def handlebar(C): if not C.is_last_bar(): return orders, deals, positions, accounts = query_info(C)def query_info(C): orders = get_trade_detail_data('8000000213', 'stock', 'order') for o in orders: print(f'股票代码: {o.m_strInstrumentID}, 市场类型: {o.m_strExchangeID}, 证券名称: {o.m_strInstrumentName}, 买卖方向: {o.m_nOffsetFlag}', f'委托数量: {o.m_nVolumeTotalOriginal}, 成交均价: {o.m_dTradedPrice}, 成交数量: {o.m_nVolumeTraded}, 成交金额:{o.m_dTradeAmount}') deals = get_trade_detail_data('8000000213', 'stock', 'deal') for dt in deals: print(f'股票代码: {dt.m_strInstrumentID}, 市场类型: {dt.m_strExchangeID}, 证券名称: {dt.m_strInstrumentName}, 买卖方向: {dt.m_nOffsetFlag}', f'成交价格: {dt.m_dPrice}, 成交数量: {dt.m_nVolume}, 成交金额: {dt.m_dTradeAmount}') positions = get_trade_detail_data('8000000213', 'stock', 'position') for dt in positions: print(f'股票代码: {dt.m_strInstrumentID}, 市场类型: {dt.m_strExchangeID}, 证券名称: {dt.m_strInstrumentName}, 持仓量: {dt.m_nVolume}, 可用数量: {dt.m_nCanUseVolume}', f'成本价: {dt.m_dOpenPrice:.2f}, 市值: {dt.m_dInstrumentValue:.2f}, 持仓成本: {dt.m_dPositionCost:.2f}, 盈亏: {dt.m_dPositionProfit:.2f}') accounts = get_trade_detail_data('8000000213', 'stock', 'account') for dt in accounts: print(f'总资产: {dt.m_dBalance:.2f}, 净资产: {dt.m_dAssureAsset:.2f}, 总市值: {dt.m_dInstrumentValue:.2f}', f'总负债: {dt.m_dTotalDebit:.2f}, 可用金额: {dt.m_dAvailable:.2f}, 盈亏: {dt.m_dPositionProfit:.2f}') return orders, deals, positions, accounts
