开始回测和交易前需要先新建策略,点击下图中左上角标识进行策略添加。可以选择不同的业务类型(比如股票),然后给策略设定一个名称,添加成功后可以在默认策略模板基础上进行策略编写。
策略添加完成后就可以开始进行回测操作了。回测之前需要对开始时间、结束时间、回测资金、回测基准、回测频率几个要素进行设定,设定完毕后点击保存。然后再点击回测按键,系统就会开始运行回测,回测的评价指标、收益曲线、日志都会在界面中展现。
交易界面点击新增按键进行新增交易操作,策略方案中的对象为所有策略列表中的策略,给本次交易设定名称并点击确定后系统就开始运行交易了。
交易开始运行后,可以实时看到总资产和可用资金情况,同时可以在交易列表查询交易状态。
交易开始运行后,可以点击交易详情,查看策略评价指标、交易明细、持仓明细、交易日志。
回测支持日线级别、分钟级别运行,详见handle_data方法。
交易支持日线级别、分钟级别、tick级别运行,日线级别和分钟级别详见handle_data方法,tick级别运行详见run_interval和tick_data方法。
频率:日线级别
当选择日线频率时,回测和交易都是每天运行一次,回测运行时间为每个交易日的15:00,交易运行时间为尾盘固定时间(允许券商可配),默认为14:50分。
频率:分钟级别
当选择分钟频率时,回测和交易都是每分钟运行一次,运行时间为每根分钟K线结束。
频率:tick级别
当选择tick频率时,交易最小频率可以达到3秒运行一次。
盘前运行:
9:30分钟之前为盘前运行时间,交易环境支持运行在run_daily中指定交易时间(如time='09:15')运行的函数;回测环境和交易环境支持运行before_trading_start函数
盘中运行:
9:31(回测)/9:30(交易)~15:00分钟为盘中运行时间,分钟级别回测环境和交易环境支持运行在run_daily中指定交易时间(如time='14:30')运行的函数;回测环境和交易环境支持运行handle_data函数;交易环境支持运行run_interval函数和tick_data函数
盘后运行:
15:30分钟为盘后运行时间,回测环境和交易环境支持运行after_trading_end函数(该函数为定时运行);15:00之后交易环境支持运行在run_daily中指定交易时间(如time='15:10')运行的函数
使用order系列接口进行股票委托下单,将直接报单到柜台。
目前所支持的业务类型:
1.普通股票买卖(单位:股)。
2.可转债买卖(单位:张,T+0)。
3.融资融券担保品买卖(单位:股)。
4.期货投机类型交易(单位:手,T+0)。
5.LOF基金买卖(单位:股)。
6.ETF基金买卖(单位:股)。
目前所支持的业务类型:
1.普通股票买卖(单位:股)。
2.可转债买卖(具体单位请咨询券商,T+0)。
3.融资融券交易(单位:股)。
4.ETF申赎、套利(单位:份)。
5.国债逆回购(单位:份)。
6.期货投机类型交易(单位:手,T+0)。
7.LOF基金买卖(单位:股)。
8.ETF基金买卖(单位:股)。
9.期权交易(单位:手)。
10.港股通交易(单位:股)。
1.股票买卖(最小价差:0.01)。
2.可转债买卖(最小价差:0.001)。
3.LOF买卖(最小价差:0.001)。
4.ETF买卖(最小价差:0.001)。
5.国债逆回购(最小价差:0.005)。
6.股指期货投机类型交易(最小价差:0.2)。
7.国债期货投机类型交易(最小价差:0.005)。
先来看一个简单但是完整的策略:
def initialize(context):
set_universe('600570.SS')
def handle_data(context, data):
pass
一个完整策略只需要两步:
这是一个完整的策略,但是我们没有任何交易,下面我们来添加一些交易
def initialize(context):
g.security = '600570.SS'
# 是否创建订单标识
g.flag = False
set_universe(g.security)
def handle_data(context, data):
if not g.flag:
order(g.security, 1000)
g.flag = True
在这个策略里,当创建订单标识为False,也即尚未创建过订单时,买入1000股'600570.SS',具体的下单API请看order函数。这里我们进行了交易,但只是没有经过条件判断的委托下达。
下面我们来看一个真正实用的策略
在这个策略里,我们会根据历史价格做出判断:
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
security = g.security
sid = g.security
# 获取过去五天的历史价格
df = get_history(5, '1d', 'close', security, fq=None, include=False)
# 获取过去五天的平均价格
average_price = round(df['close'][-5:].mean(), 3)
# 获取上一时间点价格
current_price = data[sid]['close']
# 获取当前的现金
cash = context.portfolio.cash
# 如果上一时间点价格高出五天平均价1%, 则全仓买入
if current_price > 1.01*average_price:
# 用所有 cash 买入股票
order_value(g.security, cash)
log.info('buy %s' % g.security)
# 如果上一时间点价格低于五天平均价, 则清仓卖出
elif current_price < average_price and get_position(security).amount > 0:
# 卖出所有股票,使这只股票的最终持有量为0
order_target(g.security, 0)
log.info('sell %s' % g.security)
服务器异常、策略优化等诸多场景,都会使得正在进行的模拟盘和实盘策略存在中断后再重启的需求,但是一旦交易中止后,策略中存储在内存中的全局变量就清空了,因此通过持久化处理为量化交易保驾护航必不可少。
使用pickle模块保存股票池、账户信息、订单信息、全局变量g定义的变量等内容。
注意事项:
class Test(object):
count = 5
def print_info(self):
self.count += 1
log.info("a" * self.count)
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
# 初始化无法被序列化类对象,并赋值为私有变量,落地持久化信息时跳过保存该变量
g.__test_class = Test()
def handle_data(context, data):
# 调用私有变量中定义的方法
g.__test_class.print_info()
使用pickle模块保存 g 对象(全局变量)。
import pickle
from collections import defaultdict
'''
持仓N日后卖出,仓龄变量每日pickle进行保存,重启策略后可以保证逻辑连贯
'''
def initialize(context):
g.notebook_path = get_research_path()
#尝试启动pickle文件
try:
with open(g.notebook_path+'hold_days.pkl','rb') as f:
g.hold_days = pickle.load(f)
#定义空的全局字典变量
except:
g.hold_days = defaultdict(list)
g.security = '600570.SS'
set_universe(g.security)
# 仓龄增加一天
def before_trading_start(context, data):
if g.hold_days:
g.hold_days[g.security] += 1
# 每天将存储仓龄的字典对象进行pickle保存
def handle_data(context, data):
if g.security not in list(context.portfolio.positions.keys()) and g.security not in g.hold_days:
order(g.security, 100)
g.hold_days[g.security] = 1
if g.hold_days:
if g.hold_days[g.security] > 5:
order(g.security, -100)
del g.hold_days[g.security]
with open(g.notebook_path+'hold_days.pkl','wb') as f:
pickle.dump(g.hold_days,f,-1)
市场品种 | 尾缀全称 | 尾缀简称 |
上海市场证券 | XSHG | SS |
深圳市场证券 | XSHE | SZ |
指数 | XBHS | |
中金所期货 | CCFX | |
上海股票期权 | XSHO | |
深圳股票期权 | XSZO | |
上海港股通 | XHKG-SS | |
深圳港股通 | XHKG-SZ |
交易场景数据缺失等原因会导致策略运行过程中常规的处理出现语法错误,导致策略终止,所以需要做一些异常处理的保护。以下是一些基本的处理方法介绍。
try:
# 尝试执行的代码
print(a)
except:
# 如果在try块执行异常
# 则执行except块代码
a = 1
print(a)
try:
# 尝试执行的代码
print(a)
except Exception as e:
# 使用as关键字可以获取异常的实例
print("出现异常,error为: %s" % e)
a = 1
print(a)
try:
a = 1
print(a)
except:
print(a)
else:
# 如果try块成功执行,没有引发异常,可以选择性地添加一个else块。
print('执行正常')
try:
a = 1
print(a)
except:
print(a)
finally:
# 无论是否发生异常,finally块中的代码都将被执行。这可以用来执行一些清理工作,比如关闭文件或释放资源。
print('执行完毕')
可转债、ETF、LOF的价格是小数点三位。
股票的价格是小数点两位。
股指期货的价格是小数点一位。
ETF期权的价格是小数点四位。
用户在使用限价单委托(如order()入参limit_price)和市价委托保护限价(order_market()入参limit_price)的场景时务必要对入参价格的小数点位数进行处理,否则会导致委托失败。
ptrade量化引擎以事件触发为基础,通过初始化事件(initialize)、盘前事件(before_trading_start)、盘中事件(handle_data)、盘后事件(after_trading_end)来完成每个交易日的策略任务。
initialize和handle_data是一个允许运行策略的最基础结构,也就是必选项,before_trading_start和after_trading_end是可以按需运行的。
handle_data仅满足日线和分钟级别的盘中处理,tick级别的盘中处理则需要通过tick_data或者run_interval来实现。
ptrade还支持委托主推事件(on_order_response)、交易主推事件(on_trade_response),可以通过委托和成交的信息来处理策略逻辑,是tick级的一个补充。
除了以上的一些事件以外,ptrade也支持通过定时任务来运行策略逻辑,可以通过run_daily接口实现。
initialize(context)
该函数仅在回测、交易模块可用
该函数用于初始化一些全局变量,是策略运行的唯二必须定义函数之一。
注意事项:
context: Context对象,存放有当前的账户及持仓信息;
None
def initialize(context):
#g为全局对象
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
order('600570.SS',100)
before_trading_start(context, data)
该函数仅在回测、交易模块可用
该函数在每天开始交易前被调用一次,用于添加每天都要初始化的信息,如无盘前初始化需求,该函数可以在策略中不做定义。
注意事项:
context: Context对象,存放有当前的账户及持仓信息;
data:保留字段暂无数据;
None
def initialize(context):
#g为全局变量
g.security = '600570.SS'
set_universe(g.security)
def before_trading_start(context, data):
log.info(g.security)
def handle_data(context, data):
order('600570.SS',100)
handle_data(context, data)
该函数仅在回测、交易模块可用
该函数在交易时间内按指定的周期频率运行,是用于处理策略交易的主要模块,根据策略保存时的周期参数分为每分钟运行和每天运行,是策略运行的唯二必须定义函数之一。
注意事项:
context: Context对象,存放有当前的账户及持仓信息;
data:是一个类对象,实现了类似字典的通过key获取value的方法,可以通过股票代码获取代码对应的BarData对象,对象中包含当前周期(日线策略是当天,分钟策略是当前分钟)的数据;
注意:为了加速,data中的数据只包含股票池中所订阅标的的信息,可使用data[security]的方式来获取当前周期对应的标的信息;
None
def initialize(context):
#g为全局变量
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 通过data对象获取股票当前周期最新价
current_price = data[g.security].price
# 用当前最新价委托下单
order('600570.SS', 100, limit_price=current_price)
after_trading_end(context, data)
该函数仅在回测、交易模块可用
该函数会在每天交易结束之后调用,用于处理每天收盘后的操作,如无盘后处理需求,该函数可以在策略中不做定义。
注意事项:
context: Context对象,存放有当前的账户及持仓信息;
data:保留字段暂无数据;
None
def initialize(context):
#g为全局变量
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
order('600570.SS',100)
def after_trading_end(context, data):
log.info(g.security)
tick_data(context, data)
该函数仅交易模块可用
该函数可以用于处理tick级别策略的交易逻辑,每隔3秒执行一次,如无tick处理需求,该函数可以在策略中不做定义。
注意事项:
context: Context对象,存放有当前的账户及持仓信息;
data: 一个字典(dict),key为对应的标的代码(如:'600570.SS'),value为一个字典(dict),包含order(逐笔委托)、tick(当前tick数据)、transaction(逐笔成交)三项
结构如下:
{'股票代码':
{
'order(最近一条逐笔委托)':DataFrame/None,
'tick(当前tick数据)':DataFrame,
'transaction(最近一条逐笔成交)':DataFrame/None,
}
}
每项具体介绍:
order - 逐笔委托对应DataFrame包含字段:
business_time:时间戳毫秒级
hq_px:价格
business_amount:委托量
order_no:委托编号
business_direction:成交方向
trans_kind:委托类型
tick - tick数据对应DataFrame包含字段:
amount:持仓量
bid_grp:买档位,dict类型,内容如:{1:[42.71,200,0],2:[42.74,200,0],3:[42.75,700,...,以档位为Key,以list为Value,每个Value包含:委托价格、委托数量和委托笔数;
business_amount:成交数量;
business_amount_in:内盘成交量;
business_amount_out:外盘成交量
business_balance:成交金额;
business_count:成交笔数;
circulation_amount:流通股本;
current_amount:最近成交量(现手);
down_px:跌停价格;
end_trade_date:最后交易日
entrust_diff:委差;
entrust_rate:委比;
high_px:最高价;
hsTimeStamp:时间戳,格式为YYYYMMDDHHMISS,如20170711141612,表示2017年7月11日14时16分12秒的tick数据信息;
last_px:最新成交价;
low_px:最低价;
offer_grp:卖档位,dict类型,内容如:{1:[42.71,200,0],2:[42.74,200,0],3:[42.75,700,...,以档位为Key,以list为Value,每个Value包含:委托价格、委托数量和委托笔数;
open_px:今开盘价;
pb_rate:市净率;
pe_rate:动态市盈率;
preclose_px:昨收价;
prev_settlement:昨结算
px_change_rate: 涨跌幅
settlement:结算价
start_trade_date:首个交易日
tick_size:最小报价单位
total_bid_turnover: 委买金额
total_bidqty: 委买量
total_offer_turnover: 委卖金额
total_offerqty: 委卖量
trade_mins:交易时间,距离开盘已过交易时间,如100则表示每日240分钟交易时间中的第100分钟;
trade_status:交易状态;
turnover_ratio:换手率
up_px:涨停价格;
vol_ratio:量比;
wavg_px:加权平均价;
transaction - 逐笔成交对应DataFrame包含字段:
business_time:时间戳毫秒级;
hq_px:价格;
business_amount:成交量;
trade_index:成交编号;
business_direction:成交方向;
buy_no: 叫买方编号;
sell_no: 叫卖方编号;
trans_flag: 成交标记;
trans_identify_am: 盘后逐笔成交序号标识;
channel_num: 成交通道信息;
返回
None
import ast
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def tick_data(context,data):
# 获取买一价
security = g.security
current_price = ast.literal_eval(data[security]['tick']['bid_grp'][0])[1][0]
log.info(current_price)
# 获取买二价
# current_price = ast.literal_eval(data[security]['tick']['bid_grp'][0])[2][0]
# 获取买三量
# current_amount = ast.literal_eval(data[security]['tick']['bid_grp'][0])[3][1]
# 获取tick最高价
# current_high_price = data[security]['tick']['high_px'][0]
# 获取最近一笔逐笔成交的成交量
# transaction = data[security]["transaction"]
# business_amount = list(transaction["business_amount"])
# if len(business_amount) > 0:
# log.info("最近一笔逐笔成交的成交量:%s" % business_amount[0])
# 获取最近一笔逐笔委托的委托类型
# order = data[security]["order"]
# trans_kind = list(order["trans_kind"])
# if len(trans_kind) > 0:
# log.info("最近一笔逐笔委托的委托类型:%s" % trans_kind[0])
if current_price > 38.19:
# 按买一档价格下单
order_tick(security, 100, 1)
def handle_data(context, data):
pass
on_order_response(context, order_list)
该函数仅在交易模块可用,对接jz_ufx不支持该函数
该函数会在委托主推回调时响应,比引擎、get_order()和get_orders()函数更新Order状态的速度更快,适合对速度要求比较高的策略。
注意事项:
context: Context对象,存放有当前的账户及持仓信息;
order_list:一个列表,当前委托单发生变化时,发生变化的委托单列表。委托单以字典形式展现,内容包括:'entrust_no'(委托编号), 'error_info'(错误信息), 'order_time'(委托时间), 'stock_code'(股票代码), 'amount'(委托数量), 'price'(委托价格), 'business_amount'(成交数量), 'status'(委托状态), 'entrust_type'(委托类别), 'entrust_prop'(委托属性), 'order_id'(Order对象编号);
None
本交易委托产生的主推:[{'price': 32.82, 'status': '2', 'amount': 1100, 'order_id': '0e27467920464390aa10a7a53da4d49a', 'stock_code': '600570.SS', 'order_time': '2022-09-21 14:38:35', 'business_amount': 0.0, 'entrust_type': '0', 'entrust_no': '700104', 'error_info': '', 'entrust_prop': '0'}]
本交易撤单产生的主推:[{'price': 32.82, 'status': '2', 'amount': 1100, 'order_id': '0e27467920464390aa10a7a53da4d49a', 'stock_code': '600570.SS', 'order_time': '2022-09-21 14:38:37', 'business_amount': 0.0, 'entrust_type': '2', 'entrust_no': '700105', 'error_info': '', 'entrust_prop': '0'}]
非本交易委托产生的主推:[{'price': 32.82, 'status': '2', 'amount': 1100, 'order_id': '', 'stock_code': '600570.SS', 'order_time': '2022-09-21 14:41:19', 'business_amount': 0.0, 'entrust_type': '0', 'entrust_no': '700106', 'error_info': '', 'entrust_prop': '0'}]
非本交易撤单产生的主推:[{'price': 32.82, 'status': '2', 'amount': 1100, 'order_id': '', 'stock_code': '600570.SS', 'order_time': '2022-09-21 14:41:30', 'business_amount': 0.0, 'entrust_type': '2', 'entrust_no': '700107', 'error_info': '', 'entrust_prop': '0'}]
def initialize(context):
g.security = ['600570.SS','002416.SZ']
set_universe(g.security)
g.flag = 0
def on_order_response(context, order_list):
log.info(order_list)
if(g.flag==0):
order('600570.SS', 100)
g.flag = 1
else:
log.info("end")
def handle_data(context, data):
order('600570.SS', 100)
on_trade_response(context, trade_list)
该函数仅在交易模块可用
该函数会在成交主推回调时响应,比引擎和get_trades()函数更新Order状态的速度更快,适合对速度要求比较高的策略。
注意事项:
context: Context对象,存放有当前的账户及持仓信息;
trade_list:一个列表,当前成交单发生变化时,发生变化的成交单列表。成交单以字典形式展现,内容包括:'entrust_no'(委托编号),'business_time'(成交时间),'stock_code'(股票代码),'entrust_bs'(委托方向),'business_amount'(成交数量),'business_price'(成交价格),'business_balance'(成交额),'business_id'(成交编号),'status',(委托状态)(对接jz_ufx、ctp期货柜台该字段为空),'order_id'(Order对象编号),'cancel_info'(废单原因),'withdraw_no'(撤单原委托号),'real_type' (成交类型编号),'real_status'(成交状态编号);
None
本交易委托产生的主推:[{'order_id': '0e27467920464390aa10a7a53da4d49a', 'entrust_bs': '1', 'status': '7', 'business_id': '58', 'withdraw_no': '0', 'business_time': '2022-09-21 14:38:11', 'stock_code': '600570.SS', 'business_balance': 32820.0, 'business_price': 32.82, 'business_amount': 1000, 'entrust_no': '700104', 'cancel_info': ' ', 'real_type': '0', 'real_status': '0'}]
本交易撤单产生的主推:[{'order_id': '0e27467920464390aa10a7a53da4d49a', 'entrust_bs': '1', 'status': '5', 'business_id': '0', 'withdraw_no': '700104', 'business_time': '2022-09-21 14:38:13', 'stock_code': '600570.SS', 'business_balance': -3282.0, 'business_price': 32.82, 'business_amount': 100, 'entrust_no': '700105', 'cancel_info': ' ', 'real_type': '2', 'real_status': '0'}]
非本交易委托产生的主推:[{'order_id': '', 'entrust_bs': '1', 'status': '7', 'business_id': '59', 'withdraw_no': '0', 'business_time': '2022-09-21 14:40:56', 'stock_code': '600570.SS', 'business_balance': 32820.0, 'business_price': 32.82, 'business_amount': 1000, 'entrust_no': '700106', 'cancel_info': ' ', 'real_type': '0', 'real_status': '0'}]
非本交易撤单产生的主推:[{'order_id': '', 'entrust_bs': '1', 'status': '7', 'business_id': '0', 'withdraw_no': '700106', 'business_time': '2022-09-21 14:41:06', 'stock_code': '600570.SS', 'business_balance': 0.0, 'business_price': 32.82, 'business_amount': 0, 'entrust_no': '700107', 'cancel_info': '交易主机繁忙', 'real_type': '2', 'real_status': '0'}]
def initialize(context):
g.security = ['600570.SS','002416.SZ']
set_universe(g.security)
g.flag = 0
def on_trade_response(context, trade_list):
log.info(trade_list)
if(g.flag==0):
order('600570.SS', 100)
g.flag = 1
else:
log.info("end")
def handle_data(context, data):
order('600570.SS', 100)
set_universe(security_list)
该函数仅在回测、交易模块可用
该函数用于设置或者更新此策略要操作的股票池。
注意事项:
security_list: 股票列表,支持单支或者多支股票(list[str]/str)
None
def initialize(context):
g.security = ['600570.SS','600571.SS']
# 将g.security中的股票设置为股票池
set_universe(g.security)
def handle_data(context, data):
# 获取初始化设定的股票池行情数据
his = get_history(5, '1d', 'close', security_list=None)
set_benchmark(sids)
该函数仅在回测、交易模块可用
该函数用于设置策略的比较基准,前端展现的策略评价指标都基于此处设置的基准标的。
注意事项:
sids:股票/指数/ETF代码(str)
如果不做基准设置,默认选定沪深300指数(000300.SS)的每日价格作为判断策略好坏和一系列风险值计算的基准。如果要指定其他股票/指数/ETF的价格作为基准,就需要使用set_benchmark。
None
def initialize(context):
g.security = '000001.SZ'
set_universe(g.security)
#将上证50(000016.SS)设置为参考基准
set_benchmark('000016.SS')
def handle_data(context, data):
order('000001.SZ',100)
set_commission(commission_ratio=0.0003, min_commission=5.0, type="STOCK")
该函数仅在回测模块可用
该函数用于设置佣金费率。
注意事项:
commission_ratio:佣金费率,默认股票每笔交易的佣金费率是万分之三,ETF基金、LOF基金每笔交易的佣金费率是万分之八。(float)
min_commission:最低交易佣金,默认每笔交易最低扣5元佣金。(float)
type:交易类型,不传参默认为STOCK(目前只支持STOCK, ETF, LOF)。(string)
None
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
#将佣金费率设置为万分之三,将最低手续费设置为3元
set_commission(commission_ratio =0.0003, min_commission=3.0)
def handle_data(context, data):
pass
set_fixed_slippage(fixedslippage=0.0)
该函数仅在回测模块可用
该函数用于设置固定滑点,滑点在真实交易场景是不可避免的,因此回测中设置合理的滑点有利于让回测逼近真实场景。
注意事项:
fixedslippage:固定滑点,委托价格与最后的成交价格的价差设置,这个价差是一个固定的值(比如0.02元,撮合成交时委托价格±0.01元)。最终的成交价格=委托价格±float(fixedslippage)/2。
None
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
# 将滑点设置为固定的0.2元,即原本买入交易的成交价为10元,则设置之后成交价将变成10.1元
set_fixed_slippage(fixedslippage=0.2)
def handle_data(context, data):
pass
set_slippage(slippage=0.001)
该函数仅在回测模块可用
该函数用于设置滑点比例,滑点在真实交易场景是不可避免的,因此回测中设置合理的滑点有利于让回测逼近真实场景。
注意事项:
无
slippage:滑点比例,委托价格与最后的成交价格的价差设置,这个价差是当时价格的一个百分比(比如设置0.002时,撮合成交时委托价格±当前周期价格*0.001)。最终成交价格=委托价格±委托价格*float(slippage)/2。
None
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
# 将滑点设置为0.002
set_slippage(slippage=0.002)
def handle_data(context, data):
pass
set_volume_ratio(volume_ratio=0.25)
该函数仅在回测模块可用
该函数用于设置回测中单笔委托的成交比例,使得盘口流动性方面的设置尽量逼近真实交易场景。
注意事项:
volume_ratio:设置成交比例,默认0.25,即指本周期最大成交数量为本周期市场可成交总量的四分之一(float)
None
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
#将最大成交数量设置为本周期可成交总量的二分之一
set_volume_ratio(volume_ratio = 0.5)
def handle_data(context, data):
pass
set_limit_mode(limit_mode='LIMIT')
该函数仅在回测模块可用
该函数用于设置回测的成交数量限制模式。对于月度调仓等低频策略,对流动性冲击不是很敏感,不做成交量限制可以让回测更加便捷。
注意事项:
limit_mode:设置成交数量限制模式,即指回测撮合交易时对成交数量是否做限制进行控制(str)
默认为限制,入参'LIMIT',不做限制则入参'UNLIMITED'
None
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
#回测中不限制成交数量
set_limit_mode('UNLIMITED')
def handle_data(context, data):
pass
set_yesterday_position(poslist)
该函数仅在回测模块可用
该函数用于设置回测的初始底仓。
注意事项:
poslist:list类型数据,该list中是字典类型的元素,参数不能为空(list[dict[str:str],...]);
数据格式及参数字段如下:
[{
'sid':标的代码,
'amount':持仓数量,
'enable_amount':可用数量,
'cost_basis':每股的持仓成本价格,
}]
参数也可通过csv文件的形式传入,参考接口convert_position_from_csv
None
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
# 设置底仓
pos={}
pos['sid'] = "600570.SS"
pos['amount'] = "1000"
pos['enable_amount'] = "600"
pos['cost_basis'] = "55"
set_yesterday_position([pos])
def handle_data(context, data):
#卖出100股
order(g.security, -100)
set_parameters(**kwargs)
该函数仅在交易模块可用
该函数用于设置策略中的配置参数。
注意事项:
holiday_not_do_before:交易中节假日是否执行before_trading_start。0,执行(缺省);1,不执行。
tick_data_no_l2:tick_data中data是否包含order和transaction。0,包含(缺省);1,不包含。
receive_other_response:策略中是否接收非本交易产生的主推。0,不接收(缺省);1,接收。
receive_cancel_response:策略中是否接收撤单委托产生的主推。0,不接收(缺省);1,接收。
individual_data_in_dict:策略中调用get_individual_entrust/transaction返回的数据类型。0,Panel(缺省);1,dict。
tick_direction_in_dict:策略中调用get_tick_direction返回的数据类型。0,OrderedDict(缺省);1,dict。
not_restart_trade:交易时间段若服务器重启,是否自动执行重新拉起本交易。0,执行(缺省);1,不执行。
server_restart_not_do_before:若服务器重启导致重拉交易,是否重复执行before_trading_start函数。0,执行(缺省);1,不执行。
None
def initialize(context):
# 初始化策略
g.security = "600570.SS"
set_universe(g.security)
# 设置非交易日不执行before_trading_start
# 设置tick_data中data不包含order和transaction
# 设置接收非本交易产生的主推
# 设置接收撤单委托产生的主推
# 设置交易时间段服务器重启不再拉起本交易
# 设置服务器重启重拉交易时不再执行before_trading_start函数
set_parameters(holiday_not_do_before="1", tick_data_no_l2="1", receive_other_response="1",
receive_cancel_response="1", not_restart_trade="1", server_restart_not_do_before="1")
# 取消交易时间段服务器重启不再拉起本交易设置
# 取消服务器重启重拉交易时不再执行before_trading_start函数设置
set_parameters(not_restart_trade="0", server_restart_not_do_before="0")
def before_trading_start(context, data):
log.info("do before_trading_start")
# 取消非交易日不执行before_trading_start设置
# 取消tick_data中data不包含order和transaction设置
# 取消接收非本交易产生的主推设置
# 取消接收撤单委托产生的主推设置
set_parameters(holiday_not_do_before="0", tick_data_no_l2="0", receive_other_response="0",
receive_cancel_response="0")
def on_order_response(context, order_list):
log.info("委托主推:%s" % order_list)
def on_trade_response(context, trade_list):
log.info("成交主推:%s" % trade_list)
def handle_data(context, data):
pass
set_email_info(email_address, smtp_code, email_subject)
该函数仅在交易模块可用
该函数用于设置邮件信息,当交易报错终止时会发送提示邮件。
注意事项:
email_address(str):邮箱地址(发送方与接收方一致)。
smtp_code(str):邮箱SMTP授权码。
email_subject(str):邮件主题。
返回设置是否成功True/False(bool)。
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
# 设置邮件信息
set_email_info("2222@qq.com", "AABB", "【PTrade量化-策略交易异常终止提醒】")
def before_trading_start(context, data):
raise BaseException("test send error email")
def handle_data(context, data):
pass
run_daily(context, func, time='9:31')
该函数仅在回测、交易模块可用
该函数用于以日为单位周期性运行指定函数,可对运行触发时间进行指定。
注意事项:
context: Context对象,存放有当前的账户及持仓信息(Context);
func:自定义函数名称,此函数必须以context作为参数(Callable[[Context], None]);
time:指定周期运行具体触发运行时间点,默认为9:31分(str),交易场景可设置范围:00:00~23:59。
None
# 定义一个财务数据获取函数,每天执行一次
def initialize(context):
run_daily(context, get_finance)
g.security = '600570.SS'
set_universe(g.security)
def get_finance(context):
re = get_fundamentals(g.security,'balance_statement','total_assets')
log.info(re)
def handle_data(context, data):
pass
run_interval(context, func, seconds=10)
该函数仅在交易模块可用
该函数用于以设定时间间隔(单位为秒)周期性运行指定函数,可对运行触发时间间隔进行指定。
注意事项:
context: Context对象,存放有当前的账户及持仓信息(Context);
func:自定义函数名称,此函数必须以context作为参数(Callable[[Context], None]);
seconds:设定时间间隔(单位为秒),取值为正整数(int)。
None
# 定义一个周期处理函数,每10秒执行一次
def initialize(context):
run_interval(context, interval_handle, seconds=10)
g.security = "600570.SS"
set_universe(g.security)
def interval_handle(context):
snapshot = get_snapshot(g.security)
log.info(snapshot)
def handle_data(context, data):
pass
get_trading_day(day)
该函数在研究、回测、交易模块可用
该函数用于获取当前时间数天前或数天后的交易日期。
注意事项:
day:表示天数,正的为数天后,负的为数天前,day取0表示获取当前交易日,如果当前日期为非交易日则返回上一交易日的日期。day默认取值为0,不建议获取交易所还未公布的交易日期(int);
date:datetime.date日期对象
def initialize(context):
g.security = ['600670.SS', '000001.SZ']
set_universe(g.security)
def handle_data(context, data):
# 获取后一天的交易日期
next_trading_date = get_trading_day(1)
log.info(next_trading_date)
# 获取前一天的交易日期
previous_trading_date = get_trading_day(-1)
log.info(previous_trading_date)
get_all_trades_days(date=None)
该函数在研究、回测、交易模块可用
该函数用于获取某个日期之前的所有交易日日期。
注意事项:
date:如'2016-02-13'或'20160213'
一个包含所有交易日的numpy.ndarray
def initialize(context):
# 获取当前回测日期之前的所有交易日
all_trades_days = get_all_trades_days()
log.info(all_trades_days)
all_trades_days_date = get_all_trades_days('20150312')
log.info(all_trades_days_date)
g.security = ['600570.SS', '000001.SZ']
set_universe(g.security)
def handle_data(context, data):
pass
get_trade_days(start_date=None, end_date=None, count=None)
该函数在研究、回测、交易模块可用
该函数用于获取指定范围交易日期。
注意事项:
start_date:开始日期,与count二选一,不可同时使用。如'2016-02-13'或'20160213',开始日期最早不超过1990年(str);
end_date:结束日期,如'2016-02-13'或'20160213'。如果输入的结束日期大于今年则至多返回截止到今年的数据(str);
count:数量,与start_date二选一,不可同时使用,必须大于0。表示获取end_date往前的count个交易日,包含end_date当天。count建议不大于3000,即返回数据的开始日期不早于1990年(int);
一个包含指定范围交易日的numpy.ndarray
def initialize(context):
# 获取指定范围内交易日
trade_days = get_trade_days('2016-01-01', '2016-02-01')
log.info(trade_days)
g.security = ['600570.SS', '000001.SZ']
set_universe(g.security)
def handle_data(context, data):
# 获取回测日期往前10天的所有交易日,包含历史回测日期
trading_days = get_trade_days(count=10)
log.info(trading_days)
get_trading_day_by_date(query_date, day=0)
该函数在研究、回测、交易模块可用
该函数用于根据输入日期获取指定的交易日。
注意事项:
query_date:查询日期,如"20230213"(str);
day:表示天数,正的为数天后,负的为数天前,day取0表示获取当前交易日,如果当前日期为非交易日则返回下一交易日的日期。day默认取值为0(int);
date:交易日日期(str)
def initialize(context):
g.security = ['600570.SS', '000001.SZ']
set_universe(g.security)
def handle_data(context, data):
current_date = context.blotter.current_dt.strftime('%Y-%m-%d')
trading_date = get_trading_day_by_date("20230501", 0)
if trading_date == current_date:
log.info("今日是5月1日之后首个交易日")
get_market_list()
该函数在研究、回测、交易模块可用
该函数用于返回当前市场列表目录。
注意事项:
无
返回pandas.DataFrame对象,返回字段包括:
finance_mic - 市场编码(str:str)
finance_name - 市场名称(str:str)
get_market_list()
如返回:
finance_mic | finance_name | |
---|---|---|
0 | A | 美国证券交易所 |
1 | CBJC | 北京邮票 |
2 | CBOT | 芝加哥商品期货 |
3 | CCFX | 中国金融期货交易所 |
4 | CCGG | 中国国际文交所 |
5 | CCJC | 卡巴拉长江交易所 |
… | … | … |
66 | XFUND | 基金 |
67 | XHKG-SS | 沪港通 |
68 | XHKG-SZ | 深港通 |
69 | XSGE | 上海期货交易所 |
70 | XZCE | 郑州商品交易所 |
71 | YCME | 渝川玉石 |
72 | XSHO | 上海个股期权 |
get_market_detail(finance_mic)
该函数在研究、回测、交易模块可用
该函数用于返回市场编码对应的详细信息。
注意事项:
finance_mic: 市场代码,相关市场编码参考get_market_list返回信息(str)。
返回市场详细信息,类型为pandas.DataFrame对象,返回字段包括:
产品代码: prod_code(str:str)
产品名称: prod_name(str:str)
类型代码: hq_type_code(str:str)
时间规则: trade_time_rule(str:numpy.int64)
返回如下:
hq_type_code prod_code prod_name trade_time_rule
0 MRI 000001 上证指数 0
1 MRI 000002 A股指数 0
2 MRI 000003 B股指数 0
3 MRI 000004 工业指数 0
4 MRI 000005 商业指数 0
5 MRI 000006 地产指数 0
6 MRI 000007 公用指数 0
7 MRI 000008 综合指数 0
# 获取上海证券交易所相关信息 'XSHG'/'SS'
get_market_detail('XSHG')
get_history(count, frequency='1d', field='close', security_list=None, fq=None, include=False, fill='nan', is_dict=False)
该函数仅在回测、交易、研究模块可用
该接口用于获取最近N条历史行情K线数据。支持多股票、多行情字段获取。
注意事项:
count: K线数量,大于0,返回指定数量的K线行情;必填参数;入参类型:int;
frequency:K线周期,现有支持1分钟线(1m)、5分钟线(5m)、15分钟线(15m)、30分钟线(30m)、60分钟线(60m)、120分钟线(120m)、日线(1d)、周线(1w/weekly)、月线(mo/monthly)、季度线(1q/quarter)和年线(1y/yearly)频率的数据;选填参数,默认为'1d';入参类型:str;
field:指明数据结果集中所支持输出的行情字段;选填参数,默认为['open','high','low','close','volume','money','price'];入参类型:list[str,str]或str;输出字段包括:
security_list:要获取数据的股票列表;选填参数,None表示在上下文中的universe中选中的所有股票;入参类型:list[str,str]或str;
fq:数据复权选项,支持包括,pre-前复权,post-后复权,dypre-动态前复权,None-不复权;选填参数,默认为None;入参类型:str;
include:是否包含当前周期,True -包含,False-不包含;选填参数,默认为False;入参类型:bool;
fill:行情获取不到某一时刻的分钟数据时,是否用上一分钟的数据进行填充该时刻数据,'pre'-用上一分钟数据填充,'nan'-NaN进行填充(仅交易有效);选填参数,默认为'nan';入参类型:str;
is_dict:返回是否是字典(dict)格式{str: array()},True -是,False-不是;选填参数,默认为False;返回为字典格式取数速度相对较快;入参类型:bool;
正常返回dict类型数据,异常时返回None(NoneType)。
OrderedDict([(股票代码(str), array([日期时间(int), 开盘价(float), 最高价(float), 最低价(float), 收盘价(float), 成交量(float), 成交额(float), 最新价(float)]]))])
OrderedDict([('000001.SZ', array([(202309220931, 11.03, 11.08, 11.03, 11.07, 2289400.0, 25302018.0, 11.07),... ]))])
当获取单支股票(单只股票必须为字符串类型security_list='600570.SS',不能用security_list=['600570.SS'])的时候,无论行情字段field入参单个或多个,返回的都是pandas.DataFrame对象,行索引是datetime.datetime对象,列索引是行情字段,为str类型。比如:
如果当前时间是2017-04-18,get_history(5, '1d', 'open', '600570.SS', fq=None, include=False)将返回:
open | |
---|---|
2017-04-11 | 40.30 |
2017-04-12 | 40.08 |
2017-04-13 | 40.03 |
2017-04-14 | 40.04 |
2017-04-17 | 39.90 |
当获取多支股票(多只股票必须为list类型,特殊情况:当list只有一个股票时仍然当做多股票处理,比如security_list=['600570.SS'])的时候,无论行情字段field入参是单个还是多个,返回的是pandas.DataFrame对象,行索引是datetime.datetime对象,列索引是股票代码code和取的字段,为str类型。比如:
如果当前时间是2017-04-18,get_history(5, '1d', 'open', ['600570.SS','600571.SS'], fq=None, include=False)将返回:
code | open | |
---|---|---|
2017-04-11 | 600570.SS | 40.30 |
2017-04-12 | 600570.SS | 40.08 |
2017-04-13 | 600570.SS | 40.03 |
2017-04-14 | 600570.SS | 40.04 |
2017-04-17 | 600570.SS | 39.90 |
2017-04-11 | 600571.SS | 17.81 |
2017-04-12 | 600571.SS | 17.56 |
2017-04-13 | 600571.SS | 17.42 |
2017-04-14 | 600571.SS | 17.40 |
2017-04-17 | 600571.SS | 17.49 |
假如要对获取查询多只代码种某单只代码或多只代码的数据,可以通过x.query('code in ["xxxxxx.SS"]')的方法获取。
比如:
dataframe_info = get_history(2, frequency='1d', field=['open','close'], security_list=['600570.SS', '600571.SS'], fq=None, include=False)
则获取600570.SS的数据为:df = dataframe_info.query('code in ["600570.SS"]')
当获取多支股票(多只股票必须为list类型,特殊情况:当list只有一个股票时仍然当做多股票处理,比如security_list=['600570.SS'])的时候,如果行情字段field入参为单个,返回的是pandas.DataFrame对象,行索引是datetime.datetime对象,列索引是股票代码的编号,为str类型。比如:
如果当前时间是2017-04-18,get_history(5, '1d', 'open', ['600570.SS','600571.SS'], fq=None, include=False)将返回:
600570.SS | 600571.SS | |
---|---|---|
2017-04-11 | 40.30 | 17.81 |
2017-04-12 | 40.08 | 17.56 |
2017-04-13 | 40.03 | 17.42 |
2017-04-14 | 40.04 | 17.40 |
2017-04-17 | 39.90 | 17.49 |
当获取多支股票(多只股票必须为list类型,特殊情况:当list只有一个股票时仍然当做多股票处理,比如security_list=['600570.SS'])的时候,如果行情字段field入参为多个,则返回pandas.Panel对象,items索引是行情字段(如'open'、'close'等),里面是很多pandas.DataFrame对象,每个pandas.DataFrame的行索引是datetime.datetime对象, 列索引是股票代码,为str类型,比如:
如果当前时间是2015-01-07,get_history(2, frequency='1d', field=['open','close'], security_list=['600570.SS', '600571.SS'], fq=None, include=False)['open']将返回:
600570.SS | 600571.SS | |
---|---|---|
2015-01-05 | 54.77 | 26.93 |
2015-01-06 | 51.00 | 25.83 |
假如要对panel索引中的对象进行转换,比如将items索引由行情字段转换成股票代码,可以通过panel_info = panel_info.swapaxes("minor_axis", "items")的方法转换。
比如:
panel_info = get_history(2, frequency='1d', field=['open','close'], security_list=['600570.SS', '600571.SS'], fq=None, include=False)
按默认索引:df = panel_info['open']
对默认索引做转换:panel_info = panel_info.swapaxes("minor_axis", "items")
转换之后的索引:df = panel_info['600570.SS']
def initialize(context):
g.security = ['600570.SS', '000001.SZ']
set_universe(g.security)
def before_trading_start(context, data):
# 获取农业版块过去10天的每日收盘价
industry_info = get_history(10, frequency="1d", field="close", security_list="A01000.XBHS")
log.info(industry_info)
def handle_data(context, data):
# 股票池中全部股票过去5天的每日收盘价
his = get_history(5, '1d', 'close', security_list=g.security)
log.info('股票池中全部股票过去5天的每日收盘价')
log.info(his)
# 获取600570(恒生电子)过去5天的每天收盘价,
# 一个pd.Series对象, index是datatime
log.info('获取600570(恒生电子)过去5天的每天收盘价')
his_ss = his.query('code in ["600570.SS"]')['close']
log.info(his_ss)
# 获取600570(恒生电子)昨天(数组最后一项)的收盘价
log.info('获取600570(恒生电子)昨天的收盘价')
log.info(his_ss[-1])
# 获取每一列的平均值
log.info('获取600570(恒生电子)每一列的平均值')
log.info(his_ss.mean())
# 获取股票池中全部股票的过去10分钟的成交量
his1 = get_history(10, '1m', 'volume')
log.info('获取股票池中全部股票的过去10分钟的成交量')
log.info(his1)
# 获取恒生电子的过去5天的每天的收盘价
his2 = get_history(5, '1d', 'close', security_list='600570.SS')
log.info('获取恒生电子的过去5天的每天的收盘价')
log.info(his2)
# 获取恒生电子的过去5天的每天的后复权收盘价
his3 = get_history(5, '1d', 'close', security_list='600570.SS', fq='post')
log.info('获取恒生电子的过去5天的每天的后复权收盘价')
log.info(his3)
# 获取恒生电子的过去5周的每周的收盘价
his4 = get_history(5, '1w', 'close', security_list='600570.SS')
log.info('获取恒生电子的过去5周的每周的收盘价')
log.info(his4)
# 获取多只股票的开盘价和收盘价数据
dataframe_info = get_history(2, frequency='1d', field=['open','close'], security_list=g.security)
open_df = dataframe_info[['code', 'open']]
log.info('获所有股票的取开盘价数据')
log.info(open_df)
df = open_df.query('code in ["600570.SS"]')['open']
log.info('仅获取恒生电子的开盘价数据')
log.info(df)
get_price(security, start_date=None, end_date=None, frequency='1d', fields=None, fq=None, count=None, is_dict=False)
该函数在研究、回测、交易模块可用
该接口用于获取指定日期前N条的历史行情K线数据或者指定时间段内的历史行情K线数据。支持多股票、多行情字段获取。
注意事项:
security:一支股票代码或者一个股票代码的list(list[str]/str)
start_date:开始时间,默认为空,回测中输入请小于回测日期,交易、研究中输入请小于当前日期,且均小于等于end_date。传入格式仅支持:YYYYmmdd、YYYY-mm-dd、YYYY-mm-dd HH:MM、YYYYmmddHHMM,如'20150601'、'2015-06-01'、'2015-06-01 10:00'、'201506011000'(str);
end_date:结束时间,默认为空,回测中输入请小于回测日期,交易、研究中输入请小于当前日期。传入格式仅支持:YYYYmmdd、YYYY-mm-dd、YYYY-mm-dd HH:MM、YYYYmmddHHMM,如'20150601'、'2015-06-01'、'2015-06-01 14:00'、'201506011400'(str);
frequency: 单位时间长度,现有支持1分钟线(1m)、5分钟线(5m)、15分钟线(15m)、30分钟线(30m)、60分钟线(60m)、120分钟线(120m)、日线(1d)、周线(1w/weekly)、月线(mo/monthly)、季度线(1q/quarter)和年线(1y/yearly)频率数据(str);
fields:指明数据结果集中所支持输出字段(list[str]/str),输出字段包括 :
fq:数据复权选项,支持包括,pre-前复权,post-后复权,dypre-动态前复权,None-不复权(str);
count:大于0,不能与start_date同时输入,获取end_date前count根的数据,不支持除天('daily'/'1d')、分钟('1m')、5分钟线('5m')、15分钟线('15m')、30分钟线('30m')、60分钟线('60m')、120分钟线('120m')、周('weekly'/'1w')、('monthly'/'mo')、('quarter'/'1q')和('yearly'/'1y')以外的其它频率(int);
is_dict:返回是否是字典(dict)格式{str: array()},True -是,False-不是;选填参数,默认为False;返回为字典格式取数速度相对较快,入参类型:bool;
正常返回dict类型数据,异常时返回None(NoneType)。
OrderedDict([(股票代码(str), array([日期时间(int), 开盘价(float), 最高价(float), 最低价(float), 收盘价(float), 成交量(float), 成交额(float), 最新价(float)]]))])
OrderedDict([('600570.SS', array([(201706010931, 37.1, 37.14, 37.05, 37.09, 128200.0, 4756263.0, 37.09),...]))])
get_price对于多股票和多字段不同场景下获取返回数据的规则与get_history一致,如下:
当获取单支股票(单只股票必须为字符串类型security='600570.SS',不能用security=['600570.SS'])和单个或多个字段的时候,返回的是pandas.DataFrame对象,行索引是datetime.datetime对象,列索引是行情字段,为str类型。
例如,输入为get_price(security='600570.SS',start_date='20170201',end_date='20170213',frequency='1d')时,将返回:
open high low close volume money price is_open preclose high_limit low_limit unlimited
2017-02-03 44.47 44.50 43.58 43.90 4418325.0 193895820.0 43.90 1 44.26 48.69 39.83 0
2017-02-06 43.91 44.30 43.66 44.10 4428487.0 194979290.0 44.10 1 43.90 48.29 39.51 0
2017-02-07 44.05 44.07 43.34 43.52 5649251.0 246776480.0 43.52 1 44.10 48.51 39.69 0
2017-02-08 43.59 44.78 43.53 44.59 12570233.0 557883600.0 44.59 1 43.52 47.87 39.17 0
2017-02-09 44.74 45.28 44.39 44.74 9240223.0 413875390.0 44.74 1 44.59 49.05 40.13 0
2017-02-10 44.80 44.98 44.41 44.62 8097465.0 361757300.0 44.62 1 44.74 49.21 40.27 0
2017-02-13 44.32 45.98 44.02 44.89 14931596.0 672360490.0 44.89 1 44.62 49.08 40.16 0
当获取多支股票(多只股票必须为list类型,特殊情况:当list只有一个股票时仍然当做多股票处理,比如security=['600570.SS'])时候,返回的是pandas.DataFrame对象,行索引是datetime.datetime对象,列索引是股票代码code和取的字段,为str类型。
例如,输入为get_price(['600570.SS'], start_date='20170201', end_date='20170213', frequency='1d', fields='open')时,将返回:
code open
2017-02-03 600570.SS 44.47
2017-02-06 600570.SS 43.91
2017-02-07 600570.SS 44.05
2017-02-08 600570.SS 43.59
2017-02-09 600570.SS 44.74
2017-02-10 600570.SS 44.80
2017-02-13 600570.SS 44.32
例如,输入为get_price(['600570.SS','600571.SS'], start_date='20170201', end_date='20170213', frequency='1d', fields=['open','close'])[['code', 'open']]时,将返回:
code open
2017-02-03 600570.SS 44.47
2017-02-06 600570.SS 43.91
2017-02-07 600570.SS 44.05
2017-02-08 600570.SS 43.59
2017-02-09 600570.SS 44.74
2017-02-10 600570.SS 44.80
2017-02-13 600570.SS 44.32
2017-02-03 600571.SS 19.36
2017-02-06 600571.SS 19.00
2017-02-07 600571.SS 19.27
2017-02-08 600571.SS 19.10
2017-02-09 600571.SS 19.47
2017-02-10 600571.SS 19.57
2017-02-13 600571.SS 19.22
假如要对获取查询多只代码种某单只代码或多只代码的数据,可以通过x.query('code in ["xxxxxx.SS"]')的方法获取。
当获取多支股票(多只股票必须为list类型,特殊情况:当list只有一个股票时仍然当做多股票处理,比如security=['600570.SS'])和单个字段的时候,返回的是pandas.DataFrame对象,行索引是datetime.datetime对象,列索引是股票代码的编号,为str类型。
例如,输入为get_price(['600570.SS'], start_date='20170201', end_date='20170213', frequency='1d', fields='open')时,将返回:
600570.SS
2017-02-03 44.47
2017-02-06 43.91
2017-02-07 44.05
2017-02-08 43.59
2017-02-09 44.74
2017-02-10 44.80
2017-02-13 44.32
如果是获取多支股票(多只股票必须为list类型,特殊情况:当list只有一个股票时仍然当做多股票处理,比如security=['600570.SS'])和多个字段,则返回pandas.Panel对象,items索引是行情字段,为str类型(如'open'、'close'等),里面是很多pandas.DataFrame对象,每个pandas.DataFrame的行索引是datetime.datetime对象, 列索引是股票代码,为str类型。
例如,输入为get_price(['600570.SS','600571.SS'], start_date='20170201', end_date='20170213', frequency='1d', fields=['open','close'])['open']时,将返回:
600570.SS 600571.SS
2017-02-03 44.47 19.36
2017-02-06 43.91 19.00
2017-02-07 44.05 19.27
2017-02-08 43.59 19.10
2017-02-09 44.74 19.47
2017-02-10 44.80 19.57
2017-02-13 44.32 19.22
假如要对panel索引中的对象进行转换,比如将items索引由行情字段转换成股票代码,可以通过panel_info = panel_info.swapaxes("minor_axis", "items")的方法转换。
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 获得600570.SS(恒生电子)的2015年01月的天数据,只获取open字段
price_open = get_price('600570.SS', start_date='20150101', end_date='20150131', frequency='1d')['open']
log.info(price_open)
# 获取指定结束日期前count天到结束日期的所有开盘数据
# price_open = get_price('600570.SS', end_date='20150131', frequency='daily', count=10)['open']
# log.info(price_open)
# 获取股票指定结束时间前count分钟到指定结束时间的所有数据
# stock_info = get_price('600570.SS', end_date='2015-01-31 10:00', frequency='1m', count=10)
# log.info(stock_info)
# 获取指定结束日期前count周到结束日期所在周的所有开盘数据
# week_open = get_price('600570.SS', end_date='20150131', frequency='1w', count=10)['open']
# log.info(week_open)
# 获取多只股票
# 获取沪深300的2015年1月的天数据,返回一个[pandas.DataFrame]
security_list = get_index_stocks('000300.XBHS', '20150101')
price = get_price(security_list, start_date='20150101', end_date='20150131')
log.info(price)
# 获取某股票开盘价,行索引是[datetime.datetime]对象,列索引是行情字段
price_open = price.query('code in [@security_list[0]]')['open']
log.info(price_open)
# 获取农业版块指定结束日期前count天到结束日期的数据
industry_info = get_price("A01000.XBHS", end_date="20210315", frequency="daily", count=10)
log.info(industry_info)
get_individual_entrust(stocks=None, data_count=50, start_pos=0, search_direction=1, is_dict=False)
该函数在交易模块可用
该接口用于获取当日逐笔委托行情数据。
注意事项:
stocks: 默认为当前股票池中代码列表(list[str]);
data_count: 数据条数,默认为50,最大为200(int);
start_pos: 起始位置,默认为0(int);
search_direction: 搜索方向(1向前,2向后),默认为1(int);
is_dict: 返回类型(False-(python3.11版本支持)DataFrame,(python3.5版本支持)Panel; True-dict),默认为False;
正常返回dict类型数据,异常时返回None(NoneType)。
返回的数据格式如下:
{股票代码(str): [[时间戳毫秒级(int), 价格(float), 委托数量(int), 委托编号(int), 委托方向(int)], ...], "fields": ["business_time", "hq_px", "business_amount", "order_no", "business_direction", "trans_kind"]}
{"600570.SS": [[20220913105747848, 36.16, 700, 5383145, 0, 4], ...], "fields": ["business_time", "hq_px", "business_amount", "order_no", "business_direction", "trans_kind"]}
默认返回(python3.11版本支持)DataFrame,(python3.5版本支持)Panel类型,入参is_dict为True时返回dict类型。
输出字段如下所示:
Items axis: 股票代码列表(str);
Major_axis axis: 数据索引为自然数列(DataFrame);
Minor_axis axis: 包含以下信息:
def initialize(context):
g.security = "000001.SZ"
set_universe(g.security)
def before_trading_start(context, data):
g.flag = False
def handle_data(context, data):
if not g.flag:
# 获取当前股票池逐笔委托数据
entrust = get_individual_entrust()
log.info(entrust)
# 获取指定股票列表逐笔委托数据
entrust = get_individual_entrust(["000002.SZ", "000032.SZ"])
log.info(entrust)
# 获取委托量
if entrust is not None:
business_amount = entrust.query('code in ["000002.SZ"]')["business_amount"]
log.info("逐笔数据的委托量为:%s" % business_amount)
# 返回字典类型数据
entrust = get_individual_entrust([g.security], is_dict=True)
log.info("逐笔委托数据为:%s" % entrust)
g.flag = True
get_individual_transaction(stocks=None, data_count=50, start_pos=0, search_direction=1, is_dict=False)
该函数在交易模块可用
该接口用于获取当日逐笔成交行情数据。
注意事项:
stocks: 默认为当前股票池中代码列表(list[str]);
data_count: 数据条数,默认为50,最大为200(int);
start_pos: 起始位置,默认为0(int);
search_direction: 搜索方向(1向前,2向后),默认为1(int);
is_dict: 返回类型(False-(python3.11版本支持)DataFrame,(python3.5版本支持)Panel; True-dict),默认为False;
正常返回dict类型数据,异常时返回None(NoneType)。
返回的数据格式如下:
{股票代码(str): [[时间戳毫秒级(int), 价格(float), 成交数量(int), 成交编号(int), 成交方向(int), 叫买方编号(int), 叫卖方编号(int), 成交标记(int), 盘后逐笔成交序号标识(int), 成交通道信息(int)], ...], "fields": ["business_time", "hq_px", "business_amount", "trade_index", "business_direction", "buy_no", "sell_no", "trans_flag", 'trans_identify_am", "channel_num"]}
{"600570.SS": [[20220913111141472, 36.47, 100, 3286989, 1, 5807243, 5804930, 0, 0, 2], ...], "fields": ["business_time", "hq_px", "business_amount", "trade_index", "business_direction", "buy_no", "sell_no", "trans_flag", 'trans_identify_am", "channel_num"]}
默认返回(python3.11版本支持)DataFrame,(python3.5版本支持)Panel类型,入参is_dict为True时返回dict类型。
输出字段如下所示:
Items axis: 股票代码列表(str);
Major_axis axis: 数据索引为自然数列(DataFrame);
Minor_axis axis: 包含以下信息:
def initialize(context):
g.security = "000001.SZ"
set_universe(g.security)
def before_trading_start(context, data):
g.flag = False
def handle_data(context, data):
if not g.flag:
# 获取当前股票池逐笔成交数据
transaction = get_individual_transaction()
log.info(transaction)
# 获取指定股票列表逐笔成交数据
transaction = get_individual_transaction(["000002.SZ", "000032.SZ"])
log.info(transaction)
# 获取成交量
if transaction is not None:
business_amount = transaction.query('code in ["000002.SZ"]')["business_amount"]
log.info("逐笔数据的成交量为:%s" % business_amount)
# 返回字典类型数据
transaction = get_individual_transaction([g.security], is_dict=True)
log.info("逐笔成交数据为:%s" % transaction)
g.flag = True
get_tick_direction(symbols=None, query_date=0, start_pos=0, search_direction=1, data_count=50, is_dict=False)
该函数在交易模块可用
该接口用于获取当日分时成交行情数据。
注意事项:
symbols: 单只标的代码(str)或代码列表(list[str]);
query_date: 查询日期,默认为0,返回当日日期数据(目前行情只支持查询当日的数据,格式为YYYYMMDD)(int);
start_pos: 起始位置,默认为0(int);
search_direction: 搜索方向(1向前,2向后),默认为1(int);
data_count: 数据条数,默认为50,最大为200(int);
is_dict: 返回类型(False-OrderedDict; True-dict),默认为False;
入参is_dict为True时返回dict类型,为False(默认)时返回OrderedDict类型。
返回的数据格式如下:
{股票代码(str): [[时间戳毫秒级(int), 价格(float), 价格(int), 成交数量(int), 成交金额(int), 成交笔数(int), 成交方向(int), 持仓量(int), 分笔关联的逐笔开始序号(int), 分笔关联的逐笔结束序号(int)], ...], "fields": ["time_stamp", "hq_px", "hq_px64", "business_amount", "business_balance", "business_count", "business_direction", "amount", "start_index", "end_index"]}
{"600570.SS": [[20220915132138000, 36.18, 0, 2600, 94062, 6, 1, 0, 0, 0], "fields": ["time_stamp", "hq_px", "hq_px64", "business_amount", "business_balance", "business_count", "business_direction", "amount", "start_index", "end_index"]}
返回结果字段介绍:
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
def handle_data(context, data):
# 获取分时成交数据
direction_data = get_tick_direction([g.security])
log.info(direction_data)
# 获取成交量
business_amount = direction_data[g.security]["business_amount"]
log.info("分时成交的成交量为:%s" % business_amount)
# 返回字典类型数据
# 获取字典类型分时成交数据
direction_data = get_tick_direction([g.security], is_dict=True)
log.info(direction_data)
get_sort_msg(sort_type_grp=None, sort_field_name=None, sort_type=1, data_count=100)
该函数在交易模块可用
该接口用于获取板块、行业的快照信息(可按指定字段进行排序展示)。
注意事项:
证监会行业、聚源行业、概念板块、地域板块所对应标的的行情数据为非标准的交易所下发数据,是由数据源自行按照成分股分类规则进行计算的,存在与三方数据源不一致的情况。如用户需要在策略中使用,应自行评估该数据的合理性
sort_type_grp: 板块或行业的代码(list[str]/str);(暂时只支持XBHS.DY地域、XBHS.GN概念、XBHS.ZJHHY证监会行业、XBHS.ZS指数、XBHS.HY行业等)
sort_field_name: 需要排序的字段(str);该字段支持输入的参数如下:
sort_type: 排序方式,默认降序(0:升序,1:降序)(int);
data_count: 数据条数,默认为100,最大为10000(int);
正常返回一个List列表,里面包含板块、行业代码的涨幅排名信息(list[dict{str:str,...},...]),
返回每个代码的信息包含以下字段内容:
def initialize(context):
g.security = '000001.SZ'
set_universe(g.security)
def handle_data(context, data):
#获取XBHS.DY板块按preclose_px字段排序的排名信息
sort_data = get_sort_msg(sort_type_grp='XBHS.DY', sort_field_name='preclose_px', sort_type=1, data_count=100)
log.info(sort_data)
#获取sort_data排序第一条代码的数据
sort_data_first = sort_data[0]
log.info(sort_data_first)
get_gear_price(sids)
该函数仅在交易模块可用
该接口用于获取指定代码的档位行情价格。
注意事项:
sids:股票代码(list[str]/str);
包含以下信息(dict[str:dict[int:list[float,int,int],...],...]):
单只代码返回:
{'bid_grp': {1: [价格, 委托量,委托笔数], 2: [价格, 委托量,委托笔数], 3: [价格, 委托量,委托笔数], 4: [价格, 委托量,委托笔数], 5: [价格, 委托量,委托笔数]},
'offer_grp': {1: [价格, 委托量,委托笔数], 2: [价格, 委托量,委托笔数], 3: [价格, 委托量,委托笔数], 4: [价格, 委托量,委托笔数], 5: [价格, 委托量,委托笔数]}}
多只代码返回:
{代码:{'bid_grp': {1: [价格, 委托量,委托笔数], 2: [价格, 委托量,委托笔数], 3: [价格, 委托量,委托笔数], 4: [价格, 委托量,委托笔数], 5: [价格, 委托量,委托笔数]},
'offer_grp': {1: [价格, 委托量,委托笔数], 2: [价格, 委托量,委托笔数], 3: [价格, 委托量,委托笔数], 4: [价格, 委托量,委托笔数], 5: [价格, 委托量,委托笔数]}}
}
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
#获取600570.SS当前档位行情
gear_price = get_gear_price('600570.SS')
log.info(gear_price)
#获取600571.SS当前档位行情
gear_price = get_gear_price('600571.SS')
log.info(gear_price)
get_snapshot(security)
该函数仅在交易模块可用
该接口用于获取实时行情快照。
注意事项:
证监会行业、聚源行业、概念板块、地域板块所对应标的的行情数据为非标准的交易所下发数据,是由数据源自行按照成分股分类规则进行计算的,存在与三方数据源不一致的情况。如用户需要在策略中使用,应自行评估该数据的合理性
security: 单只股票代码或者多只股票代码组成的列表,必填字段(list[str]/str);
正常返回一个dict类型数据,包含每只股票代码的行情快照信息,其中key为股票代码,value为对应的快照信息。异常返回空dict,如{}(dict[str:dict[...]])
快照包含以下信息:
字段备注:
返回如下:
{'600570.SS': {'offer_grp': {1: [44.47, 3300, 0, {}], 2: [44.48, 2800, 0], 3: [44.49, 3900, 0], 4: [44.5, 17300, 0], 5: [44.51, 1600, 0]}, 'open_px': 44.91, 'pe_rate': 4294573.83, 'pb_rate': 11.42, 'entrust_diff': -100.0, 'entrust_rate': -0.2092, 'total_bidqty': 18900, 'preclose_px': 45.2, 'total_offer_turnover': 0, 'business_amount_out': 2600706, 'px_change_rate': -1.62, 'turnover_ratio': 0.0042, 'total_bid_turnover': 0, 'vol_ratio': 1.12, 'hsTimeStamp': 20220622102358580, 'amount': 0, 'prev_settlement': 0.0, 'circulation_amount': 1461560480, 'low_px': 44.31, 'down_px': 40.68, 'bid_grp': {1: [44.45, 600, 0, {}], 2: [44.44, 600, 0], 3: [44.43, 8300, 0], 4: [44.42, 9200, 0], 5: [44.41, 200, 0]}, 'business_balance': 274847503.0, 'business_amount': 6161800, 'business_amount_in': 3561094, 'last_px': 44.47, 'total_offerqty': 28900, 'up_px': 49.72, 'wavg_px': 44.6, 'high_px': 45.05, 'trade_status': 'TRADE', 'iopv': '0.0'}}
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 行情快照
snapshot = get_snapshot(g.security)
log.info(snapshot)
get_trend_data(date=None, stocks=None, market=None)
该函数在研究、回测、交易模块可用
获取集中竞价期间代码数据。
注意事项:
获取失败时返回空dict{}
date:日期(格式为:YYYYmmdd)(str);
stocks:股票代码(str/list[str]);
market:市场(str/list[str])
正常返回一个dict类型数据,包含每只代码的信息
包含以下信息:
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
trend_data = get_trend_data(stocks='600570.SS')
log.info(trend_data)
trend_data = get_trend_data("20230308")
log.info(trend_data['600570.SS'])
trend_data = get_trend_data(market=["XSHG", "XSHE"])
log.info(trend_data['600570.SS'])
get_stock_name(stocks)
该函数在研究、回测、交易模块可用
该接口可获取股票、可转债、ETF等名称。
注意事项:
stocks:证券代码(list[str]/str);
证券名称字典,dict类型,key为证券代码,value为证券名称(dict[str:str])。当没有查询到相关数据或者输入有误时value为None(NoneType);
{'600570.SS': '恒生电子'}
def initialize(context):
g.security = ['600570.SS', '600571.SS']
set_universe(g.security)
def handle_data(context, data):
#获取600570.SS股票名称
stock_name = get_stock_name(g.security[0])
log.info(stock_name)
#获取股票池所有的证券名称
stock_names = get_stock_name(g.security)
log.info(stock_names)
get_stock_info(stocks, field=None)
该函数在研究、回测、交易模块可用
该接口可获取股票、可转债、ETF等基础信息。
注意事项:
stocks:证券代码(list[str]/str);
field:指明数据结果集中所支持输出字段(list[str]/str),输出字段包括 :
嵌套dict类型,包含内容为field中指定内容,若field=None,返回证券基础信息仅包含对应公司名(dict[str:dict[str:str,...],...])
{'600570.SS': {'stock_name': '恒生电子', 'listed_date': '2003-12-16', 'de_listed_date': '2900-01-01'}}
def initialize(context):
g.security = ['600570.SS', '600571.SS']
set_universe(g.security)
def handle_data(context, data):
#获取单支证券的基础信息
stock_info = get_stock_info(g.security[0])
log.info(stock_info)
#获取多支证券的基础信息
stock_infos = get_stock_info(g.security, ['stock_name','listed_date','de_listed_date'])
log.info(stock_infos)
get_stock_status(stocks, query_type='ST', query_date=None)
该函数在研究、回测、交易模块可用
该接口用于获取指定日期证券的ST、停牌、退市等属性。
注意事项:
无
stocks: 例如 ['000001.SZ','000003.SZ']。该字段必须输入,否则返回None(list[str]/str);
query_type: 支持以下四种类型属性的查询,默认为'ST'(str);
具体支持输入的字段包括 :
query_date: 格式为YYYYmmdd,默认为None,表示当前日期(回测为回测当前周期,研究与交易则取系统当前时间)(str);
返回dict类型,每支证券对应的值为True或False(dict[str:bool,...])。当没有查询到相关数据或者输入有误时返回None(NoneType);
{'600570': None}
def initialize(context):
g.security = ['600397.SS', '600701.SS', '000001.SZ']
set_universe(g.security)
def handle_data(context, data):
stocks_list = g.security
filter_stocks = []
# 判断证券是否为ST、停牌或者退市
st_status = get_stock_status(stocks_list, 'ST')
# 将不是ST的证券筛选出来
for i in stocks_list:
if st_status[i] is not True:
filter_stocks.append(i)
# 获取证券停牌信息
# halt_status = get_stock_status(stocks_list, 'HALT')
# 获取指定日期的对应属性
# halt_status = get_stock_status(stocks_list, 'HALT', '20180312')
# 获取证券退市信息
# delist_status = get_stock_status(stocks_list, 'DELISTING')
log.info('筛选不是ST的证券列表: %s' % filter_stocks)
get_underlying_code(symbols)
该函数在交易模块可用
该接口用于获取证券的关联代码。
注意事项:
无
symbols: 需要查询的代码(str/list)
正常返回一个dict字典,里面包含需要查询的证券,关联类型和关联代码(dict{str:[int,str],...}),
返回每个代码的信息包含以下字段内容:
def initialize(context):
g.security = '000001.SZ'
set_universe(g.security)
def handle_data(context, data):
#获取110063.SS的关联的代码信息
underlying_code_info = get_underlying_code("110063.SS")
log.info(underlying_code_info)
#获取110063.SS的正股代码
underlying_code = underlying_code_info["110063.SS"][1]
log.info(underlying_code)
get_stock_exrights(stock_code, date=None)
该函数在研究、回测、交易模块可用
该接口用于获取证券除权除息信息。
注意事项:
无
stock_code; str类型, 证券代码(str);
date: 查询该日期的除权除息信息,默认获取该证券历史上所有除权除息信息,e.g. '20180228'/20180228/datetime.date(2018,2,28)(str/int/datetime.date)
输入日期若没有除权除息信息则返回None(NoneType),有相关数据则返回pandas.DataFrame类型数据
例如输入get_stock_exrights('600570.SS'),返回
allotted_ps rationed_ps rationed_px bonus_ps exer_forward_a exer_forward_b exer_backward_a exer_backward_b
date
20040604 0.0 0.0 0.0 0.43 0.046077 -1.433 1.000000 0.430
20050601 0.5 0.0 0.0 0.20 0.046077 -1.413 1.500000 0.630
20050809 0.4 0.0 0.0 0.00 0.069115 -1.404 2.100000 0.630
20060601 0.4 0.0 0.0 0.11 0.096762 -1.404 2.940000 0.861
20070423 0.3 0.0 0.0 0.10 0.135466 -1.394 3.822000 1.155
20080528 0.6 0.0 0.0 0.07 0.176106 -1.380 6.115200 1.422
20090423 0.5 0.0 0.0 0.10 0.281770 -1.368 9.172799 2.034
20100510 0.4 0.0 0.0 0.05 0.422654 -1.340 12.841919 2.492
20110517 0.0 0.0 0.0 0.05 0.591716 -1.318 12.841919 3.134
20120618 0.0 0.0 0.0 0.08 0.591716 -1.289 12.841919 4.162
20130514 0.0 0.0 0.0 0.10 0.591716 -1.242 12.841919 5.446
20140523 0.0 0.0 0.0 0.16 0.591716 -1.182 12.841919 7.501
20150529 0.0 0.0 0.0 0.18 0.591716 -1.088 12.841919 9.812
20160530 0.0 0.0 0.0 0.26 0.591716 -0.981 12.841919 13.151
20170510 0.0 0.0 0.0 0.10 0.591716 -0.827 12.841919 14.435
20180524 0.0 0.0 0.0 0.29 0.591716 -0.768 12.841919 18.159
20190515 0.3 0.0 0.0 0.32 0.591716 -0.597 16.694494 22.269
20200605 0.3 0.0 0.0 0.53 0.769231 -0.407 21.702843 31.117
返回结果字段介绍:
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
stock_exrights = get_stock_exrights(g.security)
log.info('the stock exrights info of security %s:\n%s' % (g.security, stock_exrights))
get_stock_blocks(stock_code)
该函数在研究、回测、交易模块可用
该接口用于获取证券所属板块。
注意事项:
stock_code: 证券代码(str);
获取成功返回dict类型,包含所属行业、板块等详细信息(dict[str:list[list[str,str],...],...]),获取失败返回None(NoneType)。返回数据如:
{
'HGT': [['HGTHGT.XBHK', '沪股通']],
'HY': [['710200.XBHS', '计算机应用']],
'DY': [['DY1172.XBHS', '浙江板块']],
'ZJHHY': [['I65000.XBHS', '软件和信息技术服务业']],
'GN': [['003596.XBHS', '融资融券'], ['003631.XBHS', '转融券标的'], ['003637.XBHS', '互联网金融'], ['003665.XBHS', '电商概念'], ['003707.XBHS', '沪股通'], ['003718.XBHS', '证金持股'], ['003800.XBHS', '人工智能'], ['003830.XBHS', '区块链'], ['031027.XBHS', 'MSCI概念'], ['B10003.XBHS', '蚂蚁金服概念']]
}
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
blocks = get_stock_blocks(g.security)
log.info('security %s in these blocks:\n%s' % (g.security, blocks))
get_index_stocks(index_code,date)
该函数在研究、回测、交易模块可用
该接口用于获取一个指数在平台可交易的成分股列表,指数列表
注意事项:
index_code:指数代码,如沪深300:000300.SS或000300.XBHS(str)
date:日期,输入形式必须为'YYYYMMDD',如'20170620',不输入默认为当前日期(str);
返回股票代码的list(list[str,...])。
['000001.SZ', '000002.SZ', '000063.SZ', '000069.SZ', '000100.SZ', '000157.SZ', '000425.SZ', '000538.SZ', '000568.SZ', '000625.SZ', '000651.SZ', '000725.SZ', '000728.SZ', '000768.SZ', '000776.SZ',
'000783.SZ', '000786.SZ', ..., '603338.SS', '603939.SS', '603233.SS', '600426.SS', '688126.SS', '600079.SS', '600521.SS', '600143.SS', '000800.SZ']
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def before_trading_start(context, data):
# 获取当前所有沪深300的股票
g.stocks = get_index_stocks('000300.XBHS')
log.info(g.stocks)
# 获取2016年6月20日所有沪深300的股票, 设为股票池
g.stocks = get_index_stocks('000300.XBHS','20160620')
set_universe(g.stocks)
log.info(g.stocks)
def handle_data(context, data):
pass
get_industry_stocks(industry_code)
该函数在研究、回测、交易模块可用
该接口用于获取一个行业的所有股票,行业列表
注意事项:
industry_code: 行业编码,尾缀必须是.XBHS 如农业股:A01000.XBHS(str)
返回股票代码的list(list[str,...])
['300970.SZ', '300087.SZ', '300972.SZ', '002772.SZ', '000998.SZ', '002041.SZ', '600598.SS', '600371.SS', '600506.SS', '300511.SZ', '600359.SS', '600354.SS', '601118.SS', '600540.SS', '300189.SZ',
'600313.SS', '600108.SS']
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def before_trading_start(context, data):
# 获取农业的股票, 设为股票池
stocks = get_industry_stocks('A01000.XBHS')
set_universe(stocks)
log.info(stocks)
def handle_data(context, data):
pass
get_fundamentals(security, table, fields = None, date = None, start_year = None, end_year = None, report_types = None, merge_type = None, is_dataframe = False)
该函数可在研究、回测、交易模块使用
该接口用于获取财务三大报表数据、日频估值数据、各项财务能力指标数据。
注意事项:
为保持各表接口统一,输入字段略有不同,具体可参见 财务数据的API接口说明
security:一支股票代码或者多只股票代码组成的list(list[str])
table:财务数据表名,输入具体表名可查询对应表中信息(str)
表名 | 包含内容 |
---|---|
valuation | 估值数据 |
balance_statement | 资产负债表 |
income_statement | 利润表 |
cashflow_statement | 现金流量表 |
growth_ability | 成长能力指标 |
profit_ability | 盈利能力指标 |
eps | 每股指标 |
operating_ability | 营运能力指标 |
debt_paying_ability | 偿债能力指标 |
fields:指明数据结果集中所需输出业务字段,支持多个业务字段输出(list类型),如fields=['settlement_provi', 'client_provi'](list[str]);输出具体字段请参考 财务数据的API接口说明
date:查询日期,按日期查询模式,返回查询日期之前对应的财务数据,输入形式如'20170620';支持datetime.date时间格式输入,不能与start_year与end_year同时作用;支持按日期查询模式,不传入date时默认取回测日期的上一个交易日数据(str);
start_year:查询开始年份,按年份查询模式,返回输入年份范围内对应的财务数据,如'2015',start_year与end_year必须同时输入,且不能与date同时作用(str)
end_year:查询截止年份,按年份查询模式,返回输入年份范围内对应的财务数据,如'2015',start_year与end_year必须同时输入,且不能与date同时作用(str)
report_types:财报类型;如果为年份查询模式(start_year/end_year),不输入report_types返回当年可查询到的全部类型财报;如果为日期查询模式(date),不输入report_types返回距离指定日期最近一份财报(str)。
(已废弃)date_type:数据参考时间设置,该参数只适用于按日期查询模式(date参数模式)(int) :
merge_type:数据更新设置;相关财务数据信息会不断进行修正更新,为了避免未来数据影响,可以通过参数获取原始发布或最新发布数据信息;只有部分表包含此字段(int) :
is_dataframe:True-返回DataFrame格式;False-返回pandas.Panel格式(默认,仅python3.5的按年份查询模式有效)。
注意:
返回值形式根据输入参数类型不同而有所区分:
1.(python3.11、python3.5版本均支持)按日期查询模式(date参数模式)返回数据类型为pandas.DataFrame类型,索引为股票代码,如get_fundamentals('600000.SS','balance_statement',date='20161201')将返回:
secu_abbr | end_date | publ_date | total_assets | …… | total_liability | |
---|---|---|---|---|---|---|
600000.SS | 浦发银行 | 2016-09-30 | 2016-10-29 | 5.56e+12 | ...... | 5.20e+12 |
2.(python3.11版本支持)按年份查询模式(start_year/end_year参数模式)返回数据类型为pandas.DataFrame类型,索引为股票代码(secu_code)和对应会计日期(end_date)。
3.(python3.5版本支持)按年份查询模式(start_year/end_year参数模式)返回数据类型为pandas.Panel类型,索引为股票代码,其中包含的DataFrame索引为返回股票对应会计日期(end_date),如get_fundamentals(['600000.SS', '600570.SS', '000002.SZ'], 'balance_statement', start_year='2016', end_year='2016')将返回:
import time
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def before_trading_start(context, data):
# 假设取4000股*10年一季报数据为4万条,之后再取中报又是4万条,因为规则要求每秒不得调用超过100次(单次最大调用量是500条数据),调用过程就需要sleep1秒,防止流控触发。
funda_data = get_fundamentals(g.security, 'balance_statement', fields = 'total_assets', start_year='2011', end_year='2020', report_types = '1')
time.sleep(1)
funda_data = get_fundamentals(g.security, 'balance_statement', fields = 'total_assets', start_year='2010', end_year='2020', report_types = '2')
def handle_data(context, data):
# 获取股票池
stocks = get_index_stocks('000906.XBHS')
# 指定股票池
stocks = ['600000.SS','600570.SS']
# 获取数据的两种模式
# 1. 按日期查询模式(默认以发布日期为参考时间):返回输入日期之前对应的财务数据
# 在回测中获取单一股票中对应回测日期资产负债表中资产总计(total_assets)数据
#(回测中date默认获取回测日期,无需传入date,除非在回测中获取指定某个日期的数据,日期格式如”20160628”)
get_fundamentals('600000.SS', 'balance_statement', 'total_assets')
# 获取股票池中对应上市公司在2016年6月28日之前发布的最近季度(即2016年一季度)
# 的资产负债表中资产总计(total_assets)数据,如果到查询日期为止一季度数据还,未发布则所有数据用Nan填充
get_fundamentals(stocks, 'balance_statement', 'total_assets','20160628')
# 获取股票池中对应上市公司在2016年6月28日最近会计周期(即20160331)的资产负
# 债表中资产总计(total_assets)数据,如果未查到相关数据则用Nan填充
get_fundamentals(stocks, 'balance_statement', 'total_assets','20160628', date_type=1)
# 获取股票池中对应上市公司发布日期在2016年6月28日之前,年度(即2015年年报)
# 资产负债表中资产总计(total_assets)数据,如果到查询日期为止还未发布则所有数据用Nan填充
get_fundamentals(stocks, 'balance_statement', 'total_assets', '20160628', report_types='4')
# 获取股票池中对应上市公司2016年6月28日最近季度资产负债表中对应fields字段数据
fields =['sold_buyback_secu_proceeds','specific_account_payable']
get_fundamentals(stocks, 'balance_statement', fields,'20160628',)
# 获取股票池中对应上市公司2016年6月28日最近季度资产负债表中对应fields字段最新数据,
# 如果最近更新日期(发布日期)在2016年6月28日之后则无法获取对应数据
fields =['sold_buyback_secu_proceeds','specific_account_payable']
get_fundamentals(stocks, 'balance_statement', fields,'20160628',merge_type=1)
# 2. 按年份查询模式:返回输入年份范围内对应季度的财务数据
# 获取公司浦发银行(600000.SS)从2013年至2015年第一季度资产负债表中资产总计(total_assets)数据
get_fundamentals('600000.SS','balance_statement','total_assets',start_year='2013',end_year='2015', report_types='1')
# 获取股票池中对应上市公司从2013年至2015年年度资产负债表中对应fields字段数据
fields =['sold_buyback_secu_proceeds','specific_account_payable']
get_fundamentals(stocks,'balance_statement',fields,start_year='2013',end_year='2015', report_types='4')
get_Ashares(date=None)
该函数在研究、回测、交易模块可用
该接口用于获取指定日期沪深市场的所有A股代码列表
注意事项:
date:格式为YYYYmmdd
股票代码列表,list类型(list[str,...])
['000001.SZ', '000002.SZ', '000004.SZ', '000005.SZ', '000006.SZ', '000007.SZ', '000008.SZ', '000009.SZ', '000010.SZ', '000011.SZ', '000012.SZ', '000014.SZ', '000016.SZ', '000017.SZ', '000018.SZ', '000019.SZ',
'000020.SZ', '000021.SZ', '000023.SZ', '000024.SZ', '000025.SZ', '000026.SZ', '000027.SZ',..., '603128.SS', '603167.SS', '603333.SS', '603366.SS', '603399.SS', '603766.SS', '603993.SS']
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
#沪深A股代码
ashares = get_Ashares()
log.info('%s A股数量为%s' % (context.blotter.current_dt,len(ashares)))
ashares = get_Ashares('20130512')
log.info('20130512 A股数量为%s'%len(ashares))
get_etf_list()
该函数仅支持PTrade客户端可用、仅在股票交易模块可用,对接jz_ufx、ATP、云订柜台不支持该函数
该接口用于获取柜台返回的ETF代码列表
注意事项:
无
正常返回一个list类型对象,包含所有ETF代码。异常返回空list,如[](list[str,...])。
['510010.SS', '510020.SS', '510030.SS', '510050.SS', '510060.SS', '510180.SS', '510300.SS', '510310.SS', '510330.SS', '511800.SS', '511810.SS', '511820.SS', '511830.SS', '511880.SS', '511990.SS', '512010.SS',
'512510.SS', '159001.SZ', '159003.SZ', '159005.SZ', '159901.SZ', '159903.SZ', '159905.SZ', '159906.SZ', '159909.SZ', '159910.SZ', '159919.SZ', '159923.SZ', '159923.SZ', '159924.SZ', '159925.SZ', '159927.SZ',
'159928.SZ', '159929.SZ']
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
#ETF代码列表
etf_code_list = get_etf_list()
log.info('ETF列表为%s' % etf_code_list)
get_etf_info(etf_code)
该函数仅支持PTrade客户端可用、仅在股票交易模块可用,对接jz_ufx、ATP、云订柜台不支持该函数
该接口用于获取单支或者多支ETF的信息。
注意事项:
无
etf_code : 单支ETF代码或者一个ETF代码的list,必传参数(list[str]/str)
正常返回一个dict类型字段,包含每只ETF信息,key为ETF代码,values为包含etf信息的dict。异常返回空dict,如{}(dict[str:dict[...]])
返回结果字段介绍:
字段备注:
返回如下:
{'510020.SS': {'nav_percu': 206601.39, 'redeem_max': 0.0, 'nav_pre': 0.207, 'report_unit': 1000000, 'max_cash_ratio': 0.4,
'cash_balance': -813.75, 'etf_redemption_code': '510021', 'pre_cash_component': 598.39, 'allot_max': 0.0, 'publish': 1}}
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
#ETF信息
etf_info = get_etf_info('510020.SS')
log.info(etf_info)
etfs_info = get_etf_info(['510020.SS','510050.SS'])
log.info(etfs_info)
get_etf_stock_list(etf_code)
该函数仅支持PTrade客户端可用、仅在股票交易模块可用,对接jz_ufx、ATP、云订柜台不支持该函数
该接口用于获取目标ETF的成分券列表
注意事项:
无
etf_code : 单支ETF代码,必传参数(str)
正常返回一个list类型字段,包含每只etf代码所对应的成分股。异常返回空list,如[](list[str,...])
['600000.SS', '600010.SS', '600016.SS']
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def before_trading_start(context, data):
#ETF成分券列表
stock_list = get_etf_stock_list('510020.SS')
log.info(stock_list)
def handle_data(context, data):
pass
get_etf_stock_info(etf_code,security)
该函数仅支持PTrade客户端可用、仅在股票交易模块可用,对接jz_ufx、ATP、云订柜台不支持该函数
该接口用于获取ETF成分券信息。
注意事项:
无
etf_code : 单支ETF代码,必传参数(str)
security : 单只股票代码或者一个由多只股票代码组成的列表,必传参数(list[str]/str)
正常返回一个dict类型字段,包含每只etf代码中成分股的信息。异常返回空dict,如{}(dict[str:dict[...]])
返回结果字段介绍:
返回如下:
{'600000.SS': {'cash_replace_flag': '1', 'replace_ratio': 0.1, 'is_open': 1, 'code_num': 4700.0, 'replace_balance': 0.0}}
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
#ETF成分券信息
stock_info = get_etf_stock_info('510050.SS','600000.SS')
log.info(stock_info)
stocks_info = get_etf_stock_info('510050.SS',['600000.SS','600036.SS'])
log.info(stocks_info)
get_ipo_stocks()
该函数仅支持Ptrade客户端可用、仅在股票交易模块可用,对接jz_ufx不支持该函数
该接口用于获取当日IPO申购标的信息
注意事项:
无
正常返回一个dict类型对象,key为各个分类市场,value为市场对应的申购代码列表。异常返回空dict,如{}({str:[],str:[],...})。分类市场明细如下:
{'深证普通代码': ['002952.SZ', '072318.SZ'], '深证创业板代码': ['300765.SZ'], '上证普通代码': ['732116.SS', '732136.SS', '732367.SS', '732378.SS', '732380.SS', '732616.SS', '780086.SS', '780211.SS', '780860.SS', '718001.SS', '783012.SS', '783127.SS'], '可转债代码': ['718001.SS', '783012.SS', '783127.SS', '072318.SZ'], '上证科创板代码': ['787006.SS']}
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 当日可转债IPO申购标的
ipo_stocks = get_ipo_stocks().get('可转债代码')
log.info('可转债IPO申购标的列表为%s' % ipo_stocks)
get_cb_list()
该函数仅在交易模块可用
返回当前可转债市场的所有代码列表(包含停牌代码)。
注意事项:
无
返回当前可转债市场的所有代码列表(包含停牌代码)(list)。失败则返回空列表[]。
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
run_daily(context, get_trade_cb_list, "9:25")
def before_trading_start(context, data):
# 每日清空,避免取到昨日市场代码表
g.trade_cb_list = []
def handle_data(context, data):
pass
# 获取当天可交易的可转债代码列表
def get_trade_cb_list(context):
cb_list = get_cb_list()
cb_snapshot = get_snapshot(cb_list)
# 代码有行情快照并且交易状态不在暂停交易、停盘、长期停盘、退市状态的判定为可交易代码
g.trade_cb_list = [cb_code for cb_code in cb_list if
cb_snapshot.get(cb_code, {}).get("trade_status") not in
[None, "HALT", "SUSP", "STOPT", "DELISTED"]]
log.info("当天可交易的可转债代码列表为:%s" % g.trade_cb_list)
get_cb_info()
该函数仅在研究、交易模块可用
获取可转债基础信息。
注意事项:
无
正常返回一个DataFrame类型数据,包含每只可转债的信息
包含以下信息:
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
df = get_cb_info()
log.info(df)
get_reits_list(date=None)
该函数在研究、回测、交易模块可用
该接口用于获取指定日期沪深市场的所有公募REITs基金代码列表
注意事项:
date:格式为YYYYmmdd
公募REITs基金代码列表,list类型(list[str,...])
['180101.SZ', '180102.SZ', '180103.SZ', '180201.SZ', '180202.SZ', '180301.SZ', '180401.SZ', '180501.SZ', '180801.SZ', '508000.SS', '508001.SS', '508006.SS', '508008.SS', '508009.SS', '508018.SS', '508021.SS',
'508027.SS', '508028.SS', '508056.SS', '508058.SS', '508066.SS', '508068.SS', '508077.SS', '508088.SS', '508096.SS', '508098.SS', '508099.SS']
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 公募REITs基金代码
ashares = get_reits_list()
log.info('%s 公募REITs基金数量为%s' % (context.blotter.current_dt,len(ashares)))
ashares = get_reits_list('20230403')
log.info('20230403 公募REITs基金数量为%s'%len(ashares))
get_position(security)
该函数仅在回测、交易模块可用
该接口用于获取某个标的持仓信息详情。
注意事项:
无
security:标的代码,如'600570.SS'。
支持品种:
返回一个Position对象(Position)。
<Position {'business_type': 'stock', 'short_amount': 0, 'contract_multiplier': 1, 'short_pnl': 0, 'delivery_date': None, 'today_short_amount': 0, 'last_sale_price': 118.7, 'sid': '600570.SS',
'enable_amount': 100, 'margin_rate': 1, 'amount': 200, 'long_amount': 0, 'short_cost_basis': 0, 'today_long_amount': 0, 'cost_basis': 117.9, 'long_pnl': 0, 'long_cost_basis': 0}>
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
position = get_position(g.security)
log.info(position)
get_positions(security)
该函数仅在回测、交易模块可用
该接口用于获取多个标的的持仓信息详情。
注意事项:
无
security:标的代码,可以是一个列表,不传时默认为获取所有持仓(list[str]/str);
支持品种:
返回一个数据字典,键为股票代码,值为Position对象(dict[str:Position]),如下:
注意:四位尾缀或者两位尾缀代码皆可作为键取到返回的数据字典值,如'600570.XSHG'或者'600570.SS'。
{'600570.XSHG': <Position {'business_type': 'stock', 'short_amount': 0, 'contract_multiplier': 1, 'short_pnl': 0, 'delivery_date': None, 'today_short_amount': 0, 'last_sale_price': 118.7, 'sid': '600570.SS',
'enable_amount': 100, 'margin_rate': 1, 'amount': 200, 'long_amount': 0, 'short_cost_basis': 0, 'today_long_amount': 0, 'cost_basis': 117.9, 'long_pnl': 0, 'long_cost_basis': 0}>}
def initialize(context):
g.security = ['600570.SS','600000.SS']
set_universe(g.security)
def handle_data(context, data):
log.info(get_positions('600570.SS'))
log.info(get_positions(g.security))
log.info(get_positions())
get_all_positions()
该函数仅在交易模块可用
该接口用于获取当前账户的持仓信息详情。
注意事项:
无。
返回一个列表,包含不同标的字典类型的持仓信息。不同交易类型返回不同字段的持仓信息。
[{'position_str': '0111900000000001926516100010000000000A027483621600010', 'fund_account': '19265161', 'exchange_type': '1', 'stock_account': 'A027483621', 'stock_code': '600010', 'stock_name': '包钢股份', 'stock_type': '0', 'current_amount': 8300.0, 'enable_amount': 0.0, 'last_price': 1.86, 'cost_price': 1.862, 'keep_cost_price': 1.862, 'income_balance': -18.22, 'hand_flag': '0', 'market_value': 15438.0, 'av_buy_price': 0.0, 'av_income_balance': 0.0, 'client_id': '19265161', 'cost_balance': 15443.25, 'hold_amount': 0.0, 'uncome_buy_amount': 0.0, 'uncome_sell_amount': 0.0, 'entrust_sell_amount': 0.0, 'real_buy_amount': 8300.0, 'real_sell_amount': 0.0, 'asset_price': 1.86, 'delist_flag': '0', 'delist_date': 0, 'par_value': 1.0, 'income_balance_nofare': -5.25, 'frozen_amount': 0.0, 'profit_ratio': -0.11, 'sub_stock_type': '!', 'stbtrans_type': ' ', 'stb_layer_flag': ' ', 'av_cost_price': 1.861, 'income_flag': ' ', 'real_sell_balance': 0.0, 'real_buy_balance': 15443.25, 'sum_buy_amount': 0.0, 'sum_buy_balance': 0.0, 'sum_sell_amount': 0.0, 'sum_sell_balance': 0.0, 'correct_amount': 0.0, 'stbtrans_flag': ' ', 'stock_name_long': '包钢股份', 'pre_dr_price': 1.86, 'close_price': 1.86, 'hold_cost_price': 1.861, 'comb_hold_flag': '0', 'store_unit': 1},]
exchange_type 交易类别
stock_code 证券代码
stock_name 证券名称
stock_type 证券类别
hold_amount 持有数量
current_amount 当前数量
enable_amount 可用数量
real_buy_amount 回报买入数量
real_sell_amount 回报卖出数量
uncome_buy_amount 未回买入数量
uncome_sell_amount 未回卖出数量
entrust_sell_amount 委托卖出数量
last_price 最新价
cost_price 成本价
keep_cost_price 保本价
income_balance 盈亏金额
market_value 证券市值
av_buy_price 买入均价
av_income_balance 实现盈亏
cost_balance 持仓成本
delist_flag 退市标志
delist_date 退市日期
income_balance_nofare 无费用盈亏
frozen_amount 冻结数量
profit_ratio 盈亏比例(%)
asset_price 市值价
av_cost_price 摊薄成本价
exchange_type 交易类别
stock_code 证券代码
stock_name 证券名称
current_amount 当前数量
hold_amount 持有数量
enable_amount 可用数量
last_price 最新价
cost_price 成本价
income_balance 盈亏金额
income_balance_nofare 无费用盈亏
market_value 证券市值
av_buy_price 买入均价
av_income_balance 实现盈亏
cost_balance 持仓成本
uncome_buy_amount 未回买入数量
uncome_sell_amount 未回卖出数量
entrust_sell_amount 委托卖出数量
real_buy_amount 回报买入数量
real_sell_amount 回报卖出数量
asset_price 市值价
assure_ratio 担保折算率
profit_ratio 盈亏比例(%)
sum_buy_amount 累计买入数量
sum_buy_balance 累计买入金额
sum_sell_amount 累计卖出数量
sum_sell_balance 累计卖出金额
real_buy_balance 回报买入金额
real_sell_balance 回报卖出金额
av_cost_price 摊薄成本价
futu_exch_type 交易类别
futu_code 合约代码
entrust_bs 委托方向
begin_amount 期初数量
enable_amount 可用数量
real_enable_amount 当日开仓可用数量
hold_income_float 持仓浮动盈亏
hold_income 期货盯市盈亏
hold_margin 持仓保证金
average_price 平均价
average_hold_price 持仓均价
tas_average_hold_price TAS持仓均价
futu_last_price 最新价格
hedge_type 投机/套保类型
real_amount 成交数量
real_open_balance 回报开仓金额
old_open_balance 老仓持仓成交额
real_current_amount 今总持仓
old_current_amount 老仓持仓数量
tas_current_amount TAS持仓数量
combinable_amount 可组合持仓数量
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
def before_trading_start(context, data):
g.flag = False
def handle_data(context, data):
if not g.flag:
# 打印当前账户全部持仓
log.info(get_all_positions())
g.flag = True
get_trades_file(save_path='')
该函数仅在回测模块可用
该接口用于获取对账数据文件
注意事项:
save_path:导出对账数据存储的路径, 默认在notebook的根目录下(str);
成功返回导出文件的路径(str),失败返回None(NoneType);
导出数据格式的说明:
交易数据文件的组织格式为csv文件,表头信息为:
订单编号,成交编号,委托编号,标的代码,交易类型,成交数量,成交价,成交金额,交易费用,交易时间,对应的表头字段为:
[order_id,trading_id,entrust_id,security_code,order_type,volume,price,total_money,trading_fee, trade_time]
注意:
order_id列中可能出现如下几种取值:
1、M000000,通过外部系统委托的成交数据;
2、类似a6fbc145958843cc86639b23fbcfdc4c的字符串,通过平台委托的成交数据;
3、H000000,引入对账数据接口前的版本产生的交易数据;
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 委托
order_obj = order(g.security, 100)
log.info('订单编号为:%s'% order_obj)
def after_trading_end(context, data):
# 获取对账数据,存放到默认目录
data_path = get_trades_file()
log.info(data_path)
# 获取对账数据,存放到notebook下的指定目录
user_data_path = get_trades_file('user_data/data')
log.info(user_data_path)
convert_position_from_csv(path)
该函数仅在回测模块可用
该接口用于从csv文件中获取设置底仓的参数列表
注意事项:
path: csv文件对应路径及文件名(需要在研究中上传该文件)(str);
csv文件内容格式要求如下:
sid,enable_amount,amount,cost_basis
600570.SS,10000,10000,45
用于设置底仓的参数列表,该list中是字典类型的元素;
返回一个list,该list中是一个字典类型的元素(list[dict[str:str],...]),如:
[{
'sid':标的代码,
'amount':持仓数量,
'enable_amount':可用数量,
'cost_basis':每股的持仓成本价格,
}]
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
# 设置底仓
poslist= convert_position_from_csv("Poslist.csv")
set_yesterday_position(poslist)
def handle_data(context, data):
# 卖出100股
order(g.security, -100)
get_user_name(login_account=True)
该函数仅在回测、交易模块可用
该接口用于获取登录终端的账号
注意事项:
login_account(bool):默认返回登录终端的资金账号,交易中传入False时返回当前策略绑定账号;
返回登录终端的资金账号/当前策略绑定账号(str)或者None。如果查询成功返回登录终端的资金账号/当前交易策略绑定账号(str),失败则返回None(NoneType)。
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
g.current_id = get_user_name(False)
def before_trading_start(context, data):
g.flag = False
def handle_data(context, data):
# 账号为234567890且当日未委托过,担保品买卖100股
if g.current_id == "234567890" and not g.flag:
margin_trade(g.security, 100)
g.flag = True
get_deliver(start_date, end_date)
该函数仅在交易模块使用;仅支持before_trading_start和after_trading_end阶段调用,对接ATP柜台不支持该函数
该接口用来获取账户历史交割单信息。
注意事项:
start_date: 开始日期,输入形式仅支持"YYYYmmdd",如'20170620';
end_date: 结束日期,输入形式仅支持"YYYYmmdd",如'20170620';
返回一个list类型对象(list[dict,...]),包含一个或N个dict,每个dict为一条交割单信息,其中包含柜台返回的字段信息,失败则返回[]。
[{'entrust_way': '7', 'exchange_fare': 0.04, 'post_balance': 3539128.83, 'stock_account': '0010110920', 'exchange_farex': 0.0, 'fare0': 0.5, 'report_milltime': 110400187, 'business_balance': 2987.0, 'exchange_fare5': 0.0, 'fare_remark': '内部:.5( | ,费用类别:9999)', 'client_id': '10110920', 'uncome_flag': '0', 'exchange_fare0': 0.03, 'exchange_fare2': 0.0, 'fare1': 0.0, 'init_date': 20210811, 'stock_code': '162605', 'occur_amount': 1000.0, 'report_time': 110400, 'entrust_bs': '1', 'seat_no': '123456', 'business_id': '0110351000000242', 'business_amount': 1000.0, 'business_time': 110351, 'fund_account': '10110920', 'begin_issueno': ' ', 'post_amount': 1000.0, 'correct_amount': 0.0, 'money_type': '0', 'client_name': '客户10110920', 'business_type': '0', 'business_flag': 4002, 'clear_balance': -2987.5, 'exchange_fare1': 0.0, 'date_back': 20210811, 'branch_no': 1011, 'serial_no': 153, 'occur_balance': -2987.5, 'stock_name': '景顺鼎益', 'curr_time': 173028, 'exchange_fare4': 0.0, 'brokerage': 0.0, 'business_name': '证券买入', 'order_id': 'F04Z', 'business_times': 1, 'entrust_date': 20210811, 'remark': '证券买入;uft节点:31;', 'exchange_fare6': 0.0, 'standard_fare0': 0.5, 'exchange_fare3': 0.01, 'farex': 0.0, 'clear_fare0': 0.46, 'entrust_no': '38', 'profit': 0.0, 'exchange_type': '2', 'fare2': 0.0, 'business_no': 181, 'stock_type': 'L', 'fare3': 0.0, 'business_status': '0', 'business_price': 2.987, 'position_str': '020210811010110000000153', 'stock_name_long': '景顺鼎益LOF', 'report_no': '38', 'correct_balance': 0.0, 'exchange_rate': 0.0}]
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
def before_trading_start(context, data):
h = get_deliver('20210101', '20211117')
log.info(h)
def handle_data(context, data):
pass
get_fundjour(start_date, end_date)
该函数仅在交易模块使用;仅支持before_trading_start和after_trading_end阶段调用,对接jz_ufx、ATP、云订柜台不支持该函数
该接口用来获取账户历史资金流水信息。
注意事项:
start_date: 开始日期,输入形式仅支持"YYYYmmdd",如'20170620';
end_date: 结束日期,输入形式仅支持"YYYYmmdd",如'20170620';
返回一个list类型对象(list[dict,...]),包含一个或N个dict,每个dict为一条资金流水,其中包含柜台返回的字段信息,失败则返回[]。
[{'post_balance': 3260341.36, 'init_date': 20210104, 'asset_prop': '0', 'serial_no': 1, 'business_flag': 4002, 'occur_balance': -10598.21, 'exchange_type': '0', 'stock_name': ' ', 'business_date': 20210104, 'business_price': 0.0, 'bank_no': '0', 'occur_amount': 0.0, 'remark': '证券买入,恒生电子,100股,价格105.93', 'stock_account': ' ', 'money_type': '0', 'fund_account': '10110920', 'position_str': '20210104010110000000001', 'bank_name': '内部银行', 'business_name': '证券买入', 'stock_code': ' ', 'curr_date': 20210104, 'entrust_bs': ' ', 'business_time': 171730}]
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
def before_trading_start(context, data):
h = get_fundjour('20210101', '20211117')
log.info(h)
def handle_data(context, data):
pass
get_research_path()
该函数可在回测、交易模块使用
该接口用于获取研究界面根目录路径。
注意事项:
无
无
返回一个字符串类型对象(str)
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
path = get_research_path()
def handle_data(context, data):
pass
get_trade_name()
该函数仅在交易模块使用
该接口用于获取当前交易的名称。
注意事项:
无
无
返回一个字符串类型对象(str)
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
def handle_data(context, data):
name = get_trade_name()
get_lucky_info(start_date, end_date)
该函数仅在交易模块使用,对接jz_ufx不支持该函数
该接口用于获取指定时间范围内的中签信息。
注意事项:
start_date:开始日期(str),输入形式仅支持"YYYYmmdd",如"20220928"。
end_date:结束日期(str),输入形式仅支持"YYYYmmdd",如"20220929"。
正常返回一个列表套字典数据,异常或无中签信息时返回一个空列表。
返回的数据格式如下:
[{'stock_code': 证券代码(str), 'occur_amount': 发生数量(float), 'business_price': 成交价格(float), 'stock_name': 证券名称(str), 'init_date': 交易日期(int)}, ...]
[{'stock_code': '371002.SZ', 'occur_amount': 10.0, 'business_price': 100.0, 'stock_name': '崧盛发债', 'init_date': 20220928}, ...]
def initialize(context):
# 初始化策略
g.security = "600570.SS"
set_universe(g.security)
def before_trading_start(context, data):
pre_date = str(get_trading_day(-1)).replace("-", "")
current_date = context.blotter.current_dt.strftime("%Y%m%d")
# 获取上一交易日至今天中签信息
lucky_info = get_lucky_info(pre_date, current_date)
log.info(lucky_info)
def handle_data(context, data):
pass
注意:代码精度位为3位小数的类型(后台已保护为3位),如ETF、国债;代码精度为2位小数类型,需要在传参时限制价格参数的精度,如股票。
order(security, amount, limit_price=None)
该函数仅在回测、交易模块可用
该接口用于买卖指定数量为amount的股票,同时支持国债逆回购
注意事项:
security: 股票代码(str);
amount: 交易数量,正数表示买入,负数表示卖出(int);
limit_price:买卖限价(float);
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = ['600570.SS', '000001.SZ']
set_universe(g.security)
def handle_data(context, data):
#以系统最新价委托
order('600570.SS', 100)
# 逆回购1000元
order('131810.SZ', -10)
#以39块价格下一个限价单
order('600570.SS', 100, limit_price=39)
order_target(security, amount, limit_price=None)
该函数仅在回测、交易模块可用
该接口用于买卖股票,直到股票最终数量达到指定的amount
注意事项:
security: 股票代码(str);
amount: 期望的最终数量(int);
limit_price:买卖限价(float);
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = ['600570.SS', '000001.SZ']
set_universe(g.security)
def handle_data(context, data):
#买卖恒生电子股票数量到100股
order_target('600570.SS', 100)
#卖出恒生电子所有股票
if data['600570.SS']['close'] > 39:
order_target('600570.SS', 0)
order_value(security, value, limit_price=None)
该函数仅在回测、交易模块可用
该接口用于买卖指定价值为value的股票
注意事项:
security:股票代码(str);
value:股票价值(float)
limit_price:买卖限价(float)
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = ['600570.SS', '000001.SZ']
set_universe(g.security)
def handle_data(context, data):
#买入价值为10000元的恒生电子股票
order_value('600570.SS', 10000)
if data['600570.SS']['close'] > 39:
#卖出价值为10000元的恒生电子股票
order_value('600570.SS', -10000)
order_target_value(security, value, limit_price=None)
该函数仅在回测、交易模块可用
该接口用于调整股票持仓市值到value价值
注意事项:
针对以上几种情况,假如要在交易场景使用该接口,首先要确定券商柜台的配置,是否实时更新持仓情况,其次需要增加订单和持仓同步的管理,来配合order_target_value使用。
security: 股票代码(str);
value: 期望的股票最终价值(float);
limit_price:买卖限价(float);
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = ['600570.SS', '000001.SZ']
set_universe(g.security)
def handle_data(context, data):
#买卖股票到指定价值
order_target_value('600570.SS', 10000)
#卖出当前所有恒生电子的股票
if data['600570.SS']['close'] > 39:
order_target_value('600570.SS', 0)
order_market(security, amount, market_type, limit_price=None)
该函数仅在交易模块可用
该接口用于使用多种市价类型进行委托
注意事项:
security:股票代码(str);
amount:交易数量(int),正数表示买入,负数表示卖出;
market_type:市价委托类型(int),上证股票支持参数0、1、2、4,深证股票支持参数0、2、3、4、5,必传参数;
limit_price:保护限价(float),委托上证股票时必传参数;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
def before_trading_start(context, data):
g.flag = False
def handle_data(context, data):
if not g.flag:
# 以35保护限价按对手方最优价格买入100股
order_market(g.security, 100, 0, 35)
# 以35保护限价按最优五档即时成交剩余转限价买入100股
order_market(g.security, 100, 1, 35)
# 以35保护限价按本方最优价格买入100股
order_market(g.security, 100, 2, 35)
# 以35保护限价按最优五档即时成交剩余撤销买入100股
order_market(g.security, 100, 4, 35)
# 按对手方最优价格买入100股
order_market("000001.SZ", 100, 0)
# 按本方最优价格买入100股
order_market("000001.SZ", 100, 2)
# 按即时成交剩余撤销买入100股
order_market("000001.SZ", 100, 3)
# 按最优五档即时成交剩余撤销买入100股
order_market("000001.SZ", 100, 4)
# 按全额成交或撤单买入100股
order_market("000001.SZ", 100, 5)
g.flag = True
ipo_stocks_order(submarket_type=None, black_stocks=None)
该函数仅在交易模块可用,对接jz_ufx不支持该函数
该接口用于一键申购当日全部新股
注意事项:
submarket_type:申购代码所属市场,不传时默认申购全部新股(int);
black_stocks:黑名单股票,可以是单个股票或者股票列表,传入的黑名单股票将不做申购,不传时默认申购全部新股(str/list);
返回dict类型,包含委托代码、委托编号、委托状态(委托失败为0,委托成功为1)、委托数量等信息(dict[str:dict[str:str,str:int,str:float],...])
{'732116.SS': {'entrust_no': '205001', 'entrust_status': 1, 'redemption_amount': 1000}, '732100.SS': {'entrust_no': '205002', 'entrust_status': 1, 'redemption_amount': 2000}}
import time
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
g.flag = False
def before_trading_start(context, data):
g.flag = False
def handle_data(context, data):
if not g.flag:
# 上证普通代码
log.info("申购上证普通代码:")
ipo_stocks_order(submarket_type=0)
time.sleep(5)
# 上证科创板代码
log.info("申购上证科创板代码:")
ipo_stocks_order(submarket_type=1)
time.sleep(5)
# 深证普通代码
log.info("申购深证普通代码:")
ipo_stocks_order(submarket_type=2)
time.sleep(5)
# 深证创业板代码
log.info("申购深证创业板代码:")
ipo_stocks_order(submarket_type=3)
time.sleep(5)
# 可转债代码
log.info("申购可转债代码:")
ipo_stocks_order(submarket_type=4)
time.sleep(5)
g.flag = True
after_trading_order(security, amount, entrust_price)
该函数仅支持PTrade客户端可用、仅在股票交易模块可用,对接ATP柜台不支持该函数
该接口用于盘后固定价委托申报
注意事项:
security: 股票代码(str);
amount: 交易数量,正数表示买入,负数表示卖出(int);
entrust_price:买卖限价(float);
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = "300001.SZ"
set_universe(g.security)
# 15:00-15:30期间使用run_daily进行盘后固定价委托
run_daily(context, order_test, time="15:15")
g.flag = False
def order_test(context):
snapshot = get_snapshot(g.security)
if snapshot is not None:
last_px = snapshot[g.security].get("last_px", 0)
if last_px > 0:
after_trading_order(g.security, 200, float(last_px))
def handle_data(context, data):
if not g.flag:
snapshot = get_snapshot(g.security)
if snapshot is not None:
last_px = snapshot[g.security].get("last_px", 0)
if last_px > 0:
after_trading_order(g.security, 200, float(last_px))
g.flag = True
after_trading_cancel_order(order_param)
该函数仅支持PTrade客户端可用、仅在股票交易模块可用,对接ATP柜台不支持该函数
该接口用于盘后固定价委托取消订单,根据Order对象或order_id取消订单。
注意事项:
无
order_param: Order对象或者order_id(Order/str)
None(NoneType)
import time
def initialize(context):
g.security = "300001.SZ"
set_universe(g.security)
# 15:00-15:30期间使用run_daily进行盘后固定价委托、盘后固定价委托撤单
run_daily(context, order_test, time="15:15")
g.flag = False
def order_test(context):
snapshot = get_snapshot(g.security)
if snapshot is not None:
last_px = snapshot[g.security].get("last_px", 0)
if last_px > 0:
order_id = after_trading_order(g.security, 200, float(last_px))
time.sleep(5)
after_trading_cancel_order(order_id)
def handle_data(context, data):
if not g.flag:
snapshot = get_snapshot(g.security)
if snapshot is not None:
last_px = snapshot[g.security].get("last_px", 0)
if last_px > 0:
order_id = after_trading_order(g.security, 200, float(last_px))
time.sleep(5)
after_trading_cancel_order(order_id)
g.flag = True
etf_basket_order(etf_code ,amount, price_style=None, position=True, info=None)
该函数仅支持PTrade客户端可用、仅在股票交易模块可用,对接jz_ufx、ATP、云订柜台不支持该函数
该接口用于ETF成分券篮子下单。
注意事项:
无
etf_code : 单支ETF代码,必传参数(str)
amount : 下单篮子份数, 正数表示买入, 负数表示卖出,必传参数(int)
price_style : 设定委托价位,可传入’B1’、’B2’、’B3’、’B4’、’B5’、’S1’、’S2’、’S3’、’S4’、’S5’、’new’,分别为买一~买五、卖一~卖五、最新价,默认为最新价(str)
position : 取值True和False,仅在篮子买入时使用。申购是否使用持仓替代,True为使用,该情况下篮子股票买入时使用已有的持仓部分;False为不使用。默认使用持仓替代(bool)
info : dict类型,成份股信息。key为成分股代码,values为dict类型,包含的成分股信息字段作为key(Mapping[str, Mapping[str, Union[int, float]]]):
创建订单成功,正常返回一个dict类型字段, key为股票代码,values为Order对象的id,失败则返回空dict,如{}(dict[str:str]))
{'600010.SS': '34e6733d26c14056b2096cafdec253b2', '600028.SS': '4299f7ad527842dd89f2a04cef48b935', '600030.SS': '1729b80b107d408d882a39814fef667d', '600031.SS': 'c17f28961d1248f0b914c62f2e44cd13', '600036.SS': 'ea7274a4e06349308f552d60181bbec8', '600048.SS': 'bd69c204a653483e975d2914c5fe5705', '600104.SS': 'ac8a890df68c453fb93333e19f58be91', '600111.SS': '9c9c5f604c2d43d396c811189699f072'}
def initialize(context):
g.security = get_Ashares()
set_universe(g.security)
def handle_data(context, data):
#ETF成分券篮子下单
etf_basket_order('510050.SS' ,1, price_style='S3',position=True)
stock_info = {'600000.SS':{'cash_replace_flag':1,'position_replace_flag':1,'limit_price':12}}
etf_basket_order('510050.SS' ,1, price_style='S2',position=False, info=stock_info)
etf_purchase_redemption(etf_code,amount,limit_price=None)
该函数仅支持PTrade客户端可用、仅在股票交易模块可用,对接jz_ufx、ATP、云订柜台不支持该函数
该接口用于单只ETF基金申赎。
注意事项:
无
etf_code : 单支ETF代码,必传参数(str)
amount : 基金申赎数量, 正数表示申购, 负数表示赎回(int)
创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = '510050.SS'
set_universe(g.security)
def handle_data(context, data):
#ETF申购
etf_purchase_redemption('510050.SS',900000)
#ETF赎回
etf_purchase_redemption('510050.SS',-900000,limit_price = 2.9995)
order_tick(sid, amount, priceGear='1', limit_price=None)
该函数仅在交易模块可用
该接口用于在tick_data模块中进行买卖股票下单,可设定价格档位进行委托。
注意事项:
sid:股票代码(str);
amount:交易数量,正数表示买入,负数表示卖出(int)
priceGear:盘口档位,level1:1~5买档/-1~-5卖档,level2:1~10买档/-1~-10卖档(str)
limit_price:买卖限价,当输入参数中也包含priceGear时,下单价格以limit_price为主(float);
返回一个委托流水编号(str)
import ast
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
def tick_data(context,data):
security = g.security
current_price = ast.literal_eval(data[security]['tick']['bid_grp'][0])[1][0]
if current_price > 56 and current_price < 57:
# 以买一档下单
order_tick(g.security, -100, "1")
# 以卖二档下单
order_tick(g.security, 100, "-2")
# 以指定价格下单
order_tick(g.security, 100, limit_price=56.5)
def handle_data(context, data):
pass
cancel_order(order_param)
该函数仅在回测、交易模块可用
该接口用于取消订单,根据Order对象或order_id取消订单。
注意事项:
无
order_param: Order对象或者order_id(Order/str)
None(NoneType)
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
_id = order(g.security, 100)
cancel_order(_id)
log.info(get_order(_id))
cancel_order_ex(order_param)
该函数仅在交易模块可用
该接口用于取消订单,根据get_all_orders返回列表中的单个字典取消订单。
注意事项:
order_param: get_all_orders函数返回列表的单个字典(dict)
None(NoneType)
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
g.count = 0
def handle_data(context, data):
if g.count == 0:
log.info("当日全部订单为:%s" % get_all_orders())
# 遍历账户当日全部订单,对已报、部成状态订单进行撤单操作
for _order in get_all_orders():
if _order['status'] in ['2', '7']:
cancel_order_ex(_order)
if g.count == 1:
# 查看撤单是否成功
log.info("当日全部订单为:%s" % get_all_orders())
g.count += 1
debt_to_stock_order(security, amount)
该函数仅在交易模块可用
该接口用于可转债转股操作。
注意事项:
无
security: 可转债代码(str)
amount: 委托数量(int)
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
def before_trading_start(context, data):
g.count = 0
def handle_data(context, data):
if g.count == 0:
# 对持仓内的国贸转债进行转股操作
debt_to_stock_order("110033.SS", -1000)
g.count += 1
# 查看委托状态
log.info(get_orders())
g.count += 1
get_open_orders(security=None)
该函数仅在回测、交易模块可用
该接口用于获取当天所有未完成的订单,或按条件获取指定未完成的订单。
注意事项:
security:标的代码,如'600570.SS',不传时默认为获取所有未成交订单(str);
返回一个list,该list中包含多个Order对象(list[Order,...])。
[<Order {'id': '52e6a3f8a2b7468e92258c52dfcb6d42', 'dt': datetime.datetime(2025, 2, 21, 11, 25, 1, 229575), 'priceGear': 0, 'limit': 34.1, 'symbol': '600570.XSHG', 'amount': 1000, 'created': datetime.datetime(2025, 2, 21, 11, 25, 1, 229575), 'filled': 0, 'status': '2', 'entrust_no': '3596', 'cancel_entrust_no': None}>]
def initialize(context):
g.security = ['600570.SS', '000001.SZ']
set_universe(g.security)
def handle_data(context, data):
for _sec in g.security:
_id = order(_sec, 100, limit_price = 30)
# 当运行周期为分钟则可获取本周期及之前所有未完成的订单
dict_list = get_open_orders()
log.info(dict_list)
# 当运行周期为天,可在after_trading_end中调用此函数获取当天未完成的订单
def after_trading_end(context, data):
dict_list = get_open_orders()
log.info(dict_list)
get_order(order_id)
该函数仅在回测、交易模块可用
该接口用于获取指定编号订单。
注意事项:
无
获取指定编号订单。
order_id:订单编号(str)
返回一个list,该list中只包含一个Order对象(list[Order])。
[<Order {'id': '52e6a3f8a2b7468e92258c52dfcb6d42', 'dt': datetime.datetime(2025, 2, 21, 11, 25, 1, 229575), 'priceGear': 0, 'limit': 34.1, 'symbol': '600570.XSHG', 'amount': 1000, 'created': datetime.datetime(2025, 2, 21, 11, 25, 1, 229575), 'filled': 0, 'status': '2', 'entrust_no': '3596', 'cancel_entrust_no': None}>]
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
order_id = order(g.security, 100)
current_order = get_order(order_id)
log.info(current_order)
get_orders(security=None)
该函数仅在回测、交易模块可用
该接口用于获取策略内所有订单,或按条件获取指定订单。
注意事项:
无
security:标的代码,如'600570.SS',不传时默认为获取所有订单(str);
返回一个list,该list中包含多个Order对象(list[Order,...])。
[<Order {'id': '52e6a3f8a2b7468e92258c52dfcb6d42', 'dt': datetime.datetime(2025, 2, 21, 11, 25, 1, 229575), 'priceGear': 0, 'limit': 34.1, 'symbol': '600570.XSHG', 'amount': 1000, 'created': datetime.datetime(2025, 2, 21, 11, 25, 1, 229575), 'filled': 0, 'status': '2', 'entrust_no': '3596', 'cancel_entrust_no': None}>]
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
_id = order(g.security, 100)
order_obj = get_orders()
log.info(order_obj)
get_all_orders(security=None)
该函数仅在交易模块可用
该接口用于获取账户当日所有订单(包含非本交易的订单记录),或按条件获取指定代码的订单。
注意事项:
security:标的代码,如'600570.SS',不传时默认为获取所有订单(str);
返回一个list,该list中包含多条订单记录(list[dict, ...]):
股票、两融返回如下:
[{'symbol': , 'entrust_no': , 'amount': , 'entrust_bs': , 'price': , 'status': , 'filled_amount': , 'entrust_time': }, ...]
期货返回如下:
[{'symbol': , 'entrust_no': , 'amount': , 'entrust_bs': , 'price': , 'status': , 'filled_amount': , 'entrust_time': , 'futures_direction': }, ...]
期权返回如下:
[{'symbol': , 'entrust_no': , 'amount': , 'entrust_bs': , 'price': , 'status': , 'filled_amount': , 'entrust_time': , 'option_direction': , 'covered_flag': }, ...]
港股通返回如下:
[{'symbol': , 'entrust_no': , 'amount': , 'entrust_bs': , 'price': , 'status': , 'filled_amount': , 'entrust_time': }, ...]
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 获取账户当日委托600570代码的全部订单
log.info('当日委托600570代码的全部订单:%s' % get_all_orders(g.security))
# 获取账户当日全部订单
log.info('当日全部订单:%s' % get_all_orders())
get_trades()
该函数仅在回测、交易模块可用
该接口用于获取策略内当日已成交订单详情。
注意事项:
无
返回数据:
一个订单编号一笔成交:{'订单编号': [[]]} (dict{str: list[list[]]})
一个订单编号多笔成交:{'订单编号': [[], [], ...]} (dict{str: list[list[], list[], ...]})
股票返回字段:{'订单编号': [[成交编号, 委托编号, 标的代码, 买卖类型, 成交数量, 成交价格, 成交金额, 成交时间]]}
期货返回字段:{'订单编号': [[成交编号, 委托编号, 标的代码, 买卖类型, 开平仓类型, 成交数量, 成交价格, 成交金额, 成交时间]]}
期权返回字段:{'订单编号': [[成交编号, 委托编号, 标的代码, 买卖类型, 开平仓类型, 成交数量, 成交价格, 成交金额, 成交时间, 备兑标志]]}
港股通返回字段:{'订单编号': [[成交编号, 委托编号, 标的代码, 买卖类型, 成交数量, 成交价格, 成交金额, 成交时间]]}
示例如下(股票):
{'ba6a80d9746347a99c050b29069807c7': [['5001', '700001', '600570.XSHG', '买', 100000.0, 86.60, 8660000.0, '2021-08-15 09:32:00']]}
def initialize(context):
# 初始化策略
g.security = "600570.SS"
set_universe(g.security)
def before_trading_start(context, data):
g.count = 0
def handle_data(context, data):
if g.count == 0:
# 按照回购利率1.76委托国债逆回购
order("204001.SS", -1000, 1.76)
g.count += 1
log.info(get_trades())
margin_trade(security, amount, limit_price=None, market_type=None)
该函数仅支持PTrade客户端可用,仅在两融回测、两融交易模块可用。
该接口用于担保品买卖。
注意事项:
security:股票代码(str);
amount:交易数量(int),正数表示买入,负数表示卖出;
limit_price:买卖限价/保护限价(float);
market_type:市价委托类型(int),上证股票支持参数0、1、2、4,深证股票支持参数0、2、3、4、5;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
def before_trading_start(context, data):
g.flag = False
def handle_data(context, data):
if not g.flag:
# 以系统最新价委托
margin_trade(g.security, 100)
# 以46块价格下一个限价单
margin_trade(g.security, 100, limit_price=46)
# 以46保护限价按最优五档即时成交剩余转限价买入100股
margin_trade(g.security, 100, limit_price=46, market_type=1)
# 按全额成交或撤单买入100股
margin_trade("000001.SZ", 100, market_type=5)
g.flag = True
margincash_open(security, amount, limit_price=None, market_type=None, cash_group=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于融资买入。
注意事项:
security:股票代码(str);
amount:交易数量,输入正数(int);
limit_price:买卖限价(float);
market_type:市价委托类型(int),上证股票支持参数0、1、2、4,深证股票支持参数0、2、3、4、5;
cash_group:两融头寸性质(int),1为普通头寸,2为专项头寸,该字段不入参默认表示普通头寸;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
def before_trading_start(context, data):
g.flag = False
def handle_data(context, data):
if not g.flag:
# 以系统最新价委托
margincash_open(g.security, 100)
# 以46块价格下一个限价单
margincash_open(g.security, 100, limit_price=46)
# 以46保护限价按最优五档即时成交剩余转限价买入100股
margincash_open(g.security, 100, limit_price=46, market_type=1)
# 按全额成交或撤单买入100股
margincash_open("000001.SZ", 100, market_type=5)
g.flag = True
margincash_close(security, amount, limit_price=None, market_type=None, cash_group=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于卖券还款。
注意事项:
security:股票代码(str);
amount:交易数量,输入正数(int);
limit_price:买卖限价(float);
market_type:市价委托类型(int),上证股票支持参数0、1、2、4,深证股票支持参数0、2、3、4、5;
cash_group:两融头寸性质(int),1为普通头寸,2为专项头寸,该字段不入参默认表示普通头寸;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
def before_trading_start(context, data):
g.flag = False
def handle_data(context, data):
if not g.flag:
# 以系统最新价委托
margincash_close(g.security, 100)
# 以46块价格下一个限价单
margincash_close(g.security, 100, limit_price=46)
# 以46保护限价按最优五档即时成交剩余转限价卖100股还款
margincash_close(g.security, 100, limit_price=46, market_type=1)
# 按全额成交或撤单卖100股还款
margincash_close("000001.SZ", 100, market_type=5)
g.flag = True
margincash_direct_refund(value, cash_group=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于直接还款。
注意事项:
无
value:还款金额(float);
cash_group:两融头寸性质(int),1为普通头寸,2为专项头寸,该字段不入参默认表示普通头寸;
None
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 获取负债总额
fin_compact_balance = get_margin_asset().get('fin_compact_balance')
# 还款
margincash_direct_refund(fin_compact_balance)
marginsec_open(security, amount, limit_price=None, cash_group=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于融券卖出。
注意事项:
无
security:股票代码(str);
amount:交易数量,输入正数(int);
limit_price:买卖限价(float);
cash_group:两融头寸性质(int),1为普通头寸,2为专项头寸,该字段不入参默认表示普通头寸;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = '600030.SS'
set_universe(g.security)
def handle_data(context, data):
security = g.security
# 融券卖出100股
marginsec_open(security, 100)
marginsec_close(security, amount, limit_price=None, market_type=None, cash_group=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于买券还券。
注意事项:
security:股票代码(str);
amount:交易数量,输入正数(int);
limit_price:买卖限价(float);
market_type:市价委托类型(int),上证股票支持参数0、1、2、4,深证股票支持参数0、2、3、4、5;
cash_group:两融头寸性质(int),1为普通头寸,2为专项头寸,该字段不入参默认表示普通头寸;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = "600030.SS"
set_universe(g.security)
def before_trading_start(context, data):
g.flag = False
def handle_data(context, data):
if not g.flag:
# 以系统最新价委托
marginsec_close(g.security, 100)
# 以46块价格下一个限价单
marginsec_close(g.security, 100, limit_price=46)
# 以46保护限价按最优五档即时成交剩余转限价买100股还券
marginsec_close(g.security, 100, limit_price=46, market_type=1)
# 按全额成交或撤单买100股还券
marginsec_close("000001.SZ", 100, market_type=5)
g.flag = True
marginsec_direct_refund(security, amount, cash_group=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于直接还券。
注意事项:
无
security:股票代码(str);
amount:交易数量,输入正数(int);
cash_group:两融头寸性质(int),1为普通头寸,2为专项头寸,该字段不入参默认表示普通头寸;
None
def initialize(context):
g.security = '600030.SS'
set_universe(g.security)
def handle_data(context, data):
security = g.security
#买100股
marginsec_direct_refund(security, 100)
get_margincash_stocks()
该函数仅支持PTrade客户端可用,仅在两融交易模块可用,对接顶点HTS柜台暂不支持该函数。
该接口用于获取融资标的。
注意事项:
无
无
返回上交所、深交所最近一次披露的的可融资标的列表的list(list[str,...])
['000002.SZ', '000519.SZ', '600570.SS', '600519.SS']
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 获取最新的融资标的列表
margincash_stocks = get_margincash_stocks()
log.info(margincash_stocks)
get_marginsec_stocks()
该函数仅支持PTrade客户端可用,仅在两融交易模块可用,对接顶点HTS柜台暂不支持该函数。
该接口用于获取融券标的。
注意事项:
无
无
返回上交所、深交所最近一次披露的的可融券标的列表的list(list[str,...])
['000002.SZ', '000519.SZ', '600570.SS', '600519.SS']
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 获取最新的融券标的列表
marginsec_stocks = get_marginsec_stocks()
log.info(marginsec_stocks)
get_margin_contract(compact_source=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于合约查询。
注意事项:
无
compact_source:合约来源(int),0为普通头寸,1为专项头寸,该字段不入参默认表示普通头寸;
正常返回一个DataFrame类型字段,columns为每个合约所包含的信息(相应字段无数据时返回None),异常返回None(NoneType)
合约包含以下信息:
open_date compact_id stock_code ... real_compact_balance real_compact_amount
0 20250218 20250131234567 600570.SS ... 103235.31 2800
1 20250219 20250131232321 000002.SZ ... 532581.10 72130
2 20250220 20250131232131 600519.SS ... 444000.00 300
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 获取最新合约
df = get_margin_contract()
log.info(df)
get_margin_contractreal()
该函数仅支持PTrade客户端可用,仅在两融交易模块可用,对接金证集中,金证快订、云订柜台暂不支持该函数。
该接口用于实时合约流水查询。
注意事项:
无
无
正常返回一个DataFrame类型字段,columns为每个合约所包含的信息(相应字段无数据时返回None),异常返回None
实时合约流水包含以下信息:
init_date compact_id client_id ... post_interest remark
0 20250218 20250131234567 339200779 ... 58.2 利息
1 20250219 20250131232321 339200779 ... 61.3 利息
2 20250220 20250131232131 339200779 ... 77.1 利息
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 获取实时流水
df = get_margin_contractreal()
log.info(df)
get_margin_asset()
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于信用资产查询。
注意事项:
无
无
正常返回一个dict类型字段,包含所有信用资产信息。异常返回空dict,如{}(dict[str:float,...])
信用资产包含以下信息(相应字段无数据时返回None):
{'slo_compact_fare': 0.0, 'assure_asset': 22647586233.8, 'fin_compact_interest': 0.0, 'fin_used_quota': 424057.23, 'slo_compact_interest': 0.59, 'fin_enable_quota': 575942.77, 'assure_enbuy_balance': 15796927.64, 'fin_enrepaid_balance': 15796927.64, 'total_debit': 288878.59, 'fin_compact_fare': 0.0, 'slo_enable_quota': 751589.0, 'enable_bail_balance': 16638502122.05, 'fin_max_quota': 1000000.0, 'fin_compact_balance': 156078.0}
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 获取信用账户资产信息
margin_asset = get_margin_asset()
log.info(margin_asset)
get_assure_security_list()
该函数仅支持PTrade客户端可用,仅在两融交易模块可用,对接顶点HTS柜台暂不支持该函数。
该接口用于担保券查询。
注意事项:
无
无
返回上交所、深交所最近一次披露的担保券列表的list(list[str,...])
['000002.SZ', '000519.SZ', '600570.SS', '600519.SS']
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 获取最新的担保券列表
assure_security = get_assure_security_list()
log.info(assure_security)
get_margincash_open_amount(security, price=None, cash_group=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于融资标的最大可买数量查询。
注意事项:
无
security:股票代码(str);
price:限定价格(float);
cash_group:两融头寸性质(int),1为普通头寸,2为专项头寸,该字段不入参默认表示普通头寸;
正常返回一个dict类型对象,key为股票代码,values为最大数量。异常返回空dict,如{}(dict[str:int])
{'600570.SS': 1900}
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
security = g.security
# 查询恒生电子最大可融资买入数量
margincash_open_dict = get_margincash_open_amount(security)
if margincash_open_dict is not None:
log.info(margincash_open_dict.get(security))
get_margincash_close_amount(security, price=None, cash_group=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于卖券还款标的最大可卖数量查询。
注意事项:
无
security:股票代码(str);
price:限定价格(float);
cash_group:两融头寸性质(int),1为普通头寸,2为专项头寸,该字段不入参默认表示普通头寸;
正常返回一个dict类型对象,key为股票代码,values为最大数量。异常返回空dict,如{}(dict[str:int])
{'600570.SS': 1500}
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
security = g.security
# 查询恒生电子最大可卖券还款数量
margincash_close_dict = get_margincash_close_amount(security)
if margincash_close_dict is not None:
log.info(margincash_close_dict.get(security))
get_marginsec_open_amount(security, price=None, cash_group=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于融券标的最大可卖数量查询。
注意事项:
无
security:股票代码(str);
price:限定价格(float);
cash_group:两融头寸性质(int),1为普通头寸,2为专项头寸,该字段不入参默认表示普通头寸;
正常返回一个dict类型对象,key为股票代码,values为最大数量。异常返回空dict,如{}(dict[str:int])
{'600570.SS': 2500}
def initialize(context):
g.security = '600030.SS'
set_universe(g.security)
def handle_data(context, data):
security = g.security
# 查询中信证券最大可融券卖出数量
marginsec_open_dict = get_marginsec_open_amount(security)
if marginsec_open_dict is not None:
log.info(marginsec_open_dict.get(security))
get_marginsec_close_amount(security, price=None, cash_group=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于买券还券标的最大可买数量查询。
注意事项:
无
security:股票代码(str);
price:限定价格(float);
cash_group:两融头寸性质(int),1为普通头寸,2为专项头寸,该字段不入参默认表示普通头寸;
正常返回一个dict类型对象,key为股票代码,values为最大数量。异常返回空dict,如{}(dict[str:int])
{'600570.SS': 3000}
def initialize(context):
g.security = '600030.SS'
set_universe(g.security)
def handle_data(context, data):
security = g.security
# 查询中信证券最大可买券还券数量
marginsec_close_dict = get_marginsec_close_amount(security)
if marginsec_close_dict is not None:
log.info(marginsec_close_dict.get(security))
get_margin_entrans_amount(security, cash_group=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于现券还券数量查询。
注意事项:
无
security:股票代码(str);
cash_group:两融头寸性质(int),1为普通头寸,2为专项头寸,该字段不入参默认表示普通头寸;
正常返回一个dict类型对象,key为股票代码,values为最大数量。异常返回空dict,如{}(dict[str:int])
{'600570.SS': 1300}
def initialize(context):
g.security = '600030.SS'
set_universe(g.security)
def handle_data(context, data):
security = g.security
# 查询中信证券最大可现券还券数量
margin_entrans_dict = get_margin_entrans_amount(security)
if margin_entrans_dict is not None:
log.info(margin_entrans_dict.get(security))
get_enslo_security_info(cash_group=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于获取融券信息。
注意事项:
无
cash_group:两融头寸性质(int),1为普通头寸,2为专项头寸,该字段不入参默认表示普通头寸;
正常返回一个dict类型对象,key为股票代码,values为dict,包含返回的相关字段信息,如(dict[{}, {}])。异常返回None(NoneType)。
包含以下信息(相应字段无数据时返回None):
{'688001.SS': {'slo_status': '0', 'real_buy_amount': 0, 'cashgroup_prop': '1', 'enable_amount': 100000000000000, 'slo_ratio': 0.6, 'real_sell_amount': 0, 'exchange_type': '1'}, '010303.SS': {'slo_status': '0', 'real_buy_amount': 0, 'cashgroup_prop': '1', 'enable_amount': 100000000000000, 'slo_ratio': 0.6, 'real_sell_amount': 0, 'exchange_type': '1'}, '810004': {'slo_status': '0', 'real_buy_amount': 0, 'cashgroup_prop': '1', 'enable_amount': 10000, 'slo_ratio': 0.6, 'real_sell_amount': 0, 'exchange_type': '9'}}
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 获取最新的融券信息
h = get_enslo_security_info()
log.info(h)
get_crdt_fund(cash_group=None)
该函数仅支持PTrade客户端可用,仅在两融交易模块可用。
该接口用于获取可融资金信息查询。
注意事项:
无
cash_group:两融头寸性质(int),1为普通头寸,2为专项头寸,该字段不入参默认表示普通头寸;
正常返回一个dict类型对象,key为股票代码,values为dict,包含返回的相关字段信息,如(dict[{}, {}])。异常返回None(NoneType)。
包含以下信息(相应字段无数据时返回None):
{'enable_balance': 68258.96, 'real_sell_balance': 446720.12, 'real_buy_balance': 491809.45}
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 获取可融资金信息
h = get_crdt_fund()
log.info(h)
buy_open(contract, amount, limit_price=None)
该函数仅在回测、交易模块可用
买入开仓
注意:
不同期货品种每一跳的价格变动都不一样,limit_price入参的时候要参考对应品种的价格变动规则,如limit_price不做入参则会以交易的行情快照最新价或者回测的分钟最新价进行报单;
根据交易所规则,每天结束时会取消所有未完成交易;
contract:期货合约代码;
amount:交易数量,正数;
limit_price:买卖限价;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = ['IF2312.CCFX']
set_universe(g.security)
def handle_data(context, data):
#买入开仓
buy_open('IF2312.CCFX', 1)
sell_close(contract, amount, limit_price=None, close_today=False)
该函数仅在回测、交易模块可用
卖出平仓
注意:
不同期货品种每一跳的价格变动都不一样,limit_price入参的时候要参考对应品种的价格变动规则,如limit_price不做入参则会以交易的行情快照最新价或者回测的分钟最新价进行报单;
根据交易所规则,每天结束时会取消所有未完成交易;
contract:期货合约代码;
amount:交易数量,正数;
limit_price:买卖限价;
close_today:平仓方式。close_today=False为优先平昨仓,不足部分再平今仓;close_today=True为仅平今仓,委托数量若大于今仓系统会调整为今仓数量。close_today=True仅对上海期货交易所生效,其他交易所无需入参close_today字段,若设置为True系统会警告,并强行转换为close_today=False。
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = ['IF2312.CCFX']
set_universe(g.security)
def handle_data(context, data):
#卖出平仓
sell_close('IF2312.CCFX', 1)
sell_open(contract, amount, limit_price=None)
该函数仅在回测、交易模块可用
卖出开仓
注意:
不同期货品种每一跳的价格变动都不一样,limit_price入参的时候要参考对应品种的价格变动规则,如limit_price不做入参则会以交易的行情快照最新价或者回测的分钟最新价进行报单;
根据交易所规则,每天结束时会取消所有未完成交易;
contract:期货合约代码;
amount:交易数量,正数;
limit_price:买卖限价;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = ['IF2312.CCFX']
set_universe(g.security)
def handle_data(context, data):
#卖出开仓
sell_open('IF2312.CCFX', 1)
buy_close(contract, amount, limit_price=None, close_today=False)
该函数仅在回测、交易模块可用
买入平仓
注意:
不同期货品种每一跳的价格变动都不一样,limit_price入参的时候要参考对应品种的价格变动规则,如limit_price不做入参则会以交易的行情快照最新价或者回测的分钟最新价进行报单;
根据交易所规则,每天结束时会取消所有未完成交易;
contract:期货合约代码;
amount:交易数量,正数;
limit_price:买卖限价;
close_today:平仓方式。close_today=False为优先平昨仓,不足部分再平今仓;close_today=True为仅平今仓,委托数量若大于今仓系统会调整为今仓数量。close_today=True仅对上海期货交易所生效,其他交易所无需入参close_today字段,若设置为True系统会警告,并强行转换为close_today=False。
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = ['IF2312.CCFX']
set_universe(g.security)
def handle_data(context, data):
#买入平仓
buy_close('IF2312.CCFX', 1)
get_margin_rate(transaction_code)
该函数仅在回测模块可用
获取用户设置的保证金比例
注意事项:
无
transaction_code:期货合约的交易代码,str类型,如沪铜2112("CU2112")的交易代码为"CU";
用户设置的保证金比例,float浮点型数据,默认返回交易所设定的保证金比例;
def initialize(context):
g.security = "IF2312.CCFX"
set_universe(g.security)
# 设置沪深300指数的保证金比例为8%
set_margin_rate("IF", 0.08)
def before_trading_start(context, data):
# 获取沪深300指数的保证金比例
margin_rate = get_margin_rate("IF")
log.info(margin_rate)
# 获取5年期国债的保证金比例
margin_rate = get_margin_rate("TF")
log.info(margin_rate)
def handle_data(context, data):
pass
get_instruments(contract)
该函数仅在回测、交易模块可用
获取合约的上市的具体信息
注意事项:
contract:字符串,期货的合约代码,str类型;
FutureParams对象,dict类型,key为字段名,value为字段值,主要返回的字段为:
def initialize(context):
g.security = ["IF2312.CCFX"]
set_universe(g.security)
def before_trading_start(context, data):
# 获取股票池代码合约信息
for security in g.security:
info = get_instruments(security)
log.info(info)
def handle_data(context, data):
pass
get_dominant_contract(contract, date=None)
该函数在研究、回测、交易模块可用
获取连续合约的主力合约代码
注意事项:
contract:字符串,期货的连续合约代码,str类型;
date:查询日期,不入参默认为当前日期,入参查询历史日期时支持datetime类型和str类型(仅支持'YYYY-mm-dd'和'YYYYmmdd'格式);
期货连续合约对应的主力合约相关信息,dict类型,key为主力合约,value为dict类型,包含以下字段;
def initialize(context):
g.security = "IF2312.CCFX"
set_universe(g.security)
def handle_data(context, data):
# 获取2023年1月3日的IF主力合约代码
main_code_info = get_dominant_contract("IF888.CCFX",date='2023-01-03')
log.info(main_code_info)
# 获取当前交易日的IF主力合约代码
main_code = get_dominant_contract("IF888.CCFX")["IF888.CCFX"]['corr_month_code']
log.info(main_code)
set_future_commission(transaction_code, commission)
该函数仅在回测模块可用
设置期货手续费,手续费是按照交易代码进行设置的
注意事项:
transaction_code:期货合约的交易代码,str类型,如沪铜2112("CU2112")的交易代码为"CU";
commission:手续费,浮点型,设置说明:
None
def initialize(context):
g.security = "IF2312.CCFX"
set_universe(g.security)
# 设置沪深300指数的手续费,0.4/万
set_future_commission("IF", 0.00004)
def handle_data(context, data):
# 买入指数2312
buy_open(g.security, 2)
set_margin_rate(transaction_code, margin_rate)
该函数仅在回测模块可用
设置期货收取的保证金比例,保证金比例是按照交易代码进行设置的
注意事项:
无
transaction_code:期货合约的交易代码,str类型,如沪铜2112("CU2112")的交易代码为"CU";
margin_rate:保证金比例,浮点型,将对应期货的保证金比例设置为5%则输入0.05;
None
def initialize(context):
g.security = "IF2312.CCFX"
set_universe(g.security)
# 设置沪深300指数收取的保证金比例设置为5%
set_margin_rate("IF", 0.05)
def handle_data(context, data):
# 买入指数2312
buy_open(g.security, 10)
get_opt_objects(date=None)
该函数在研究、回测、交易模块可用
用于获取某日期的ETF期权标的,默认返回当前交易日的期权标的
注意事项:
date:查询日期,str类型,仅支持YYYYmmdd或者YYYY-mm-dd格式;
期权标的列表,list类型,调用异常返回[]。
def initialize(context):
pass
def handle_data(context, data):
# 获取2022年07月03日的所有ETF期权标的列表
get_opt_objects(date='20220703')
生成的列表:['510050.SS', '510300.SS', '159919.SZ']
get_opt_last_dates(security, date=None)
该函数在研究、回测、交易模块可用
用于获取某日期的ETF期权标的到期日,默认返回当前交易日的到期日
注意事项:
security:查询标的,str类型,如'510050.SS';
date:查询日期,str类型,仅支持YYYYmmdd或者YYYY-mm-dd格式;
期权标的到期日列表,list类型,调用异常返回[]。
def initialize(context):
pass
def handle_data(context, data):
# 获取2022年07月03日的所有50ETF期权标的到期日列表
get_opt_last_dates('510050.SS', date='20220703')
生成的列表:['2022-07-27', '2022-09-28', '2022-12-28']
get_opt_contracts(security, date=None)
该函数在研究、回测、交易模块可用
用于获取某期权标的某交易日处于挂牌期间的合约列表
注意事项:
security:查询标的,str类型,如'510050.SS';
date:查询日期,str类型,仅支持YYYYmmdd或者YYYY-mm-dd格式;
期权标的合约列表,list类型,调用异常返回[]。
def initialize(context):
pass
def handle_data(context, data):
# 获取2022年07月03日50ETF期权标的对应的挂牌期合约列表
get_opt_contracts('510050.SS', date='20220703')
生成的列表:['10003975.XSHO', '10003976.XSHO', '10003977.XSHO', '10003978.XSHO', '10003979.XSHO', '10003980.XSHO', '10003981.XSHO', ... , '10004332.XSHO', '10004337.XSHO', '10004338.XSHO', '10004339.XSHO', '10004340.XSHO']
get_contract_info(contract)
该函数在研究、回测、交易模块可用
用于获取期权合约信息
注意事项:
contract:合约编号,str类型,如上证期权'10003975.XSHO'、深圳期权'90000961.XSZO';
字典类型,主要返回的字段为:
def initialize(context):
pass
def handle_data(context, data):
# 获取'10003975.XSHO'合约的详细信息
get_contract_info('10003975.XSHO')
生成的字典:{'exercise_price': 2.85, 'contract_name': '50ETF购9月2850', 'contract_code': '10003975.ZSHO', 'exchange': '上交所', 'warrant_way': '10', 'trade_code': '510050C2209M02850', 'listing_date': '2022-01-27', 'expiration_date': '2022-09-28', 'contract_multiplier': 10000.0, 'littlest_changeunit': 0.0001, 'option_type': 'C'}
get_covered_lock_amount(code)
该函数仅在交易模块可用
获取期权标的允许备兑锁定数量
注意事项:
code:期权标的代码,str类型,如50ETF:'510050.SS';
允许备兑锁定数量: int类型:
def initialize(context):
pass
def handle_data(context, data):
# 获取'510050.SS'允许备兑锁定数量
get_covered_lock_amount('510050.SS')
get_covered_unlock_amount(code)
该函数仅在交易模块可用
获取期权标的允许备兑解锁数量
注意事项:
code:期权标的代码,str类型,如50ETF:'510050.SS';
允许备兑解锁数量: int类型:
def initialize(context):
pass
def handle_data(context, data):
# 获取'510050.SS'允许备兑解锁数量
get_covered_unlock_amount('510050.SS')
option_buy_open(contract, amount, limit_price=None)
该函数仅在回测、交易模块可用
权利仓开仓
注意事项:
无
contract:期权合约代码;
amount:交易数量,正数;
limit_price:买卖限价;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = '10004335.XSHO'
set_universe(g.security)
def handle_data(context, data):
# 权利仓开仓
option_buy_open(g.security, 1)
# 权利仓开仓(限定价格0.0501)
option_buy_open(g.security, 1, limit_price=0.0501)
option_sell_close(contract, amount, limit_price=None)
该函数仅在回测、交易模块可用
权利仓平仓
注意事项:
无
contract:期权合约代码;
amount:交易数量,正数;
limit_price:买卖限价;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = '10004335.XSHO'
set_universe(g.security)
def handle_data(context, data):
# 权利仓平仓
option_sell_close(g.security, 1)
# 权利仓平仓(限定价格为0.0499)
option_sell_close (g.security, 1, limit_price=0.0499)
option_sell_open(contract, amount, limit_price=None)
该函数仅在回测、交易模块可用
义务仓开仓
注意事项:
无
contract:期权合约代码;
amount:交易数量,正数;
limit_price:买卖限价;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = '10004335.XSHO'
set_universe(g.security)
def handle_data(context, data):
# 义务仓开仓
option_sell_open(g.security, 1)
# 义务仓开仓(限定价格为0.0501)
option_sell_open (g.security, 1, limit_price=0.0501)
option_buy_close(contract, amount, limit_price=None)
该函数仅在回测、交易模块可用
义务仓平仓
注意事项:
无
contract:期权合约代码;
amount:交易数量,正数;
limit_price:买卖限价;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = '10004335.XSHO'
set_universe(g.security)
def handle_data(context, data):
# 义务仓平仓
option_buy_close(g.security, 1)
# 义务仓平仓(限定价格为0.0499)
option_buy_close (g.security, 1, limit_price=0.0499)
open_prepared(contract, amount, limit_price=None)
该函数仅在交易模块可用
备兑开仓
注意事项:
contract:期权合约代码;
amount:交易数量,正数;
limit_price:买卖限价;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = '10004335.XSHO'
set_universe(g.security)
def handle_data(context, data):
# 备兑开仓
open_prepared(g.security, 1)
# 备兑开仓(限定价格为0.0501)
open_prepared(g.security, 1, limit_price=0.0501)
close_prepared(contract, amount, limit_price=None)
该函数仅在交易模块可用
备兑平仓
注意事项:
contract:期权合约代码;
amount:交易数量,正数;
limit_price:买卖限价;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = '10004335.XSHO'
set_universe(g.security)
def handle_data(context, data):
# 备兑平仓
close_prepared(g.security, 1)
# 备兑平仓(限定价格为0.0499)
close_prepared(g.security, 1, limit_price=0.0499)
option_exercise(contract, amount)
该函数仅在交易模块可用
合约在行权日支持行权
注意事项:
contract:期货合约代码;
amount:交易数量,正数;
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = '10004335.XSHO'
set_universe(g.security)
def handle_data(context, data):
# 行权
option_exercise(g.security, 1)
option_covered_lock(code, amount)
该函数仅在交易模块可用
期权标的备兑锁定,锁定现货账户标的;
注意事项:
code:期权标的代码;
amount:交易数量,正数;
返回None。
def initialize(context):
g.security = '10004335.XSHO'
set_universe(g.security)
def handle_data(context, data):
# 510050.SS备兑锁定
option_covered_lock('510050.SS', 1000)
option_covered_unlock(code, amount)
该函数仅在交易模块可用
期权标的备兑解锁,解锁现货账户标的;
注意事项:
code:期权标的代码;
amount:交易数量,正数;
返回None。
def initialize(context):
g.security = '10004335.XSHO'
set_universe(g.security)
def handle_data(context, data):
# 510050.SS备兑解锁
option_covered_unlock('510050.SS', 1000)
get_hks_list(market)
该函数仅支持PTrade客户端可用、仅在港股通交易模块可用
该接口用于获取行情返回的港股通代码列表
注意事项:
market:不输入默认获取XHKG-SS和XHKG-SZ两个市场代码,或输入XHKG-SS;XHKG-SZ也可以获取两个市场代码。(str)
正常返回一个list类型对象,包含所有港股通代码。异常返回空list,如[](list[str,...])。
['02800.XHKG-SS', '02828.XHKG-SS','03033.XHKG-SS','03067.XHKG-SS', '08328.XHKG-SS', '00001.XHKG-SS', '00002.XHKG-SS', '00003.XHKG-SS', '00004.XHKG-SS', '00005.XHKG-SS', '00006.XHKG-SS', '00008.XHKG-SS',
'00010.XHKG-SS', '00011.XHKG-SS','00012.XHKG-SS', '00013.XHKG-SS', '00014.XHKG-SS', '00016.XHKG-SS', '00017.XHKG-SS', '00019.XHKG-SS', '00020.XHKG-SS', '00023.XHKG-SS', '00027.XHKG-SS', '00038.XHKG-SS',
'00041.XHKG-SS', '00045.XHKG-SS','00066.XHKG-SS', '00069.XHKG-SS', '00081.XHKG-SS', '00083.XHKG-SS', '00086.XHKG-SS', '00087.XHKG-SS', '00101.XHKG-SS', '00107.XHKG-SS', '00119.XHKG-SS', '00123.XHKG-SS',
'00127.XHKG-SS', '00135.XHKG-SS','00136.XHKG-SS', '00142.XHKG-SS', '00144.XHKG-SS', '00148.XHKG-SS', ...]
def initialize(context):
g.security = '02899.XHKG-SS'
set_universe(g.security)
def handle_data(context, data):
#港股通代码列表
hks_code_list = get_hks_list()
log.info('港股通列表为%s' % hks_code_list)
get_hks_price_gap()
该函数在交易模块可用
港股不同价格股票的最小价格变动幅度是不同的,该接口用于获取港股通业务的价差信息。
注意事项:
无
无
dataframe类型,主要返回的字段为:
def initialize(context):
pass
def handle_data(context, data):
# 获取港股通价差信息
h = get_hks_price_gap()
log.info(h)
get_hks_unit_amount(security, trade_type)
该函数在交易模块可用
港股每个标的的最小交易单位是不同的,比如有的为500股一手,有的为2000股一手,交易前需要通过该接口进行查询获取。
注意事项:
无
security:股票代码,支持单个或多个股票查询,多个股票入参用list类型。(str/list)
trade_type:交易方式,非零股交易入参'1',零股交易入参'2'。(str)
dict类型,key为股票代码,value也是一个dict,包含以下字段信息:
def initialize(context):
pass
def handle_data(context, data):
# 获取02899.XHKG-SS的限价交易的最小交易单位信息
h = get_hks_unit_amount('02899.XHKG-SS', '1')
log.info(h)
返回的信息:{'02333.XHKG-SS': {'buy_unit_amount': 500, 'sell_unit_amount': 500}}
hks_order(security, amount, limit_price=None, style=None)
该函数仅支持PTrade客户端可用、仅在港股通交易模块可用
该接口用于港股通标的委托
注意事项:
security:股票代码。(str)
amount:委托数量,买入为正数,卖出为负数。(int)
limit_price:委托价格。(float)
style:委托类型,'1'为增强限价委托,'2'为竞价限价委托,不入参默认按增强限价委托处理。(str)
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = '02899.XHKG-SS'
set_universe(g.security)
def handle_data(context, data):
# 获取02899.XHKG-SS用最新价以竞价限价委托方式买入
order_id = hks_order(g.security, 2000, limit_price=None, style='2')
log.info(order_id)
# 获取02899.XHKG-SS用7.79元价格以增强限价委托方式买入
order_id = hks_order(g.security, 2000, limit_price=7.79, style='1')
log.info(order_id)
hks_odd_lot_order(security, amount, limit_price=None)
该函数仅支持PTrade客户端可用、仅在港股通交易模块可用
该接口用于港股通标的委托
注意事项:
security:股票代码。(str)
amount:委托数量,仅为负数。(int)
limit_price:委托价格。(float)
Order对象中的id或者None。如果创建订单成功,则返回Order对象的id(str),失败则返回None(NoneType)。
def initialize(context):
g.security = '02899.XHKG-SS'
set_universe(g.security)
def handle_data(context, data):
# 获取02899.XHKG-SS用7.79元价格卖出
order_id = hks_odd_lot_order(g.security, -500, limit_price=7.79)
log.info(order_id)
get_MACD(close, short=12, long=26, m=9)
该函数仅在回测、交易模块可用
获取异同移动平均线MACD指标的计算结果
注意事项:
无
close:价格的时间序列数据, numpy.ndarray类型;
short: 短周期, int类型;
long: 长周期, int类型;
m: 移动平均线的周期, int类型;
MACD指标dif值的时间序列, numpy.ndarray类型
MACD指标dea值的时间序列, numpy.ndarray类型
MACD指标macd值的时间序列, numpy.ndarray类型
def initialize(context):
g.security = "600570.XSHG"
set_universe(g.security)
def handle_data(context, data):
h = get_history(100, '1d', ['close','high','low'], security_list=g.security)
close_data = h['close'].values
macdDIF_data, macdDEA_data, macd_data = get_MACD(close_data, 12, 26, 9)
dif = macdDIF_data[-1]
dea = macdDEA_data[-1]
macd = macd_data[-1]
get_KDJ(high, low, close, n=9, m1=3, m2=3)
该函数仅在回测、交易模块可用
获取KDJ指标的计算结果,KDJ指标(随机指标)是一种动量指标,主要用于识别金融资产(如股票、期货等)的超买超卖状态、潜在趋势转折点及价格波动强度。它由三条曲线组成:K线(快速线)、D线(慢速线)和J线(方向敏感线),通过价格波动的统计计算反映市场短期动能。
注意事项:
无
high:最高价的时间序列数据, numpy.ndarray类型;
low:最低价的时间序列数据, numpy.ndarray类型;
close:收盘价的时间序列数据, numpy.ndarray类型;
n: 周期参数,用来计算未成熟随机值(RSV)的周期长度(RSV是计算KDJ的过程变量),决定指标对价格波动的敏感度。周期越短(如N=5),指标反应越灵敏;周期越长(如N=14),信号越平滑但可能滞后。默认为9天, int类型;
m1: K值的平滑周期,对RSV进行指数移动平均(EMA)处理,进一步平滑K值曲线,默认为3天, int类型;
m2: D值的平滑周期,对RSV进行指数移动平均(EMA)处理,进一步平滑D值曲线,默认为3天, int类型;
KDJ指标k值的时间序列, numpy.ndarray类型
KDJ指标d值的时间序列, numpy.ndarray类型
KDJ指标j值的时间序列, numpy.ndarray类型
def initialize(context):
g.security = "600570.XSHG"
set_universe(g.security)
def handle_data(context, data):
h = get_history(100, '1d', ['close','high','low'], security_list=g.security)
high_data = h['high'].values
low_data = h['low'].values
close_data = h['close'].values
k_data, d_data, j_data = get_KDJ(high_data, low_data, close_data, 9, 3, 3)
k = k_data[-1]
d = d_data[-1]
j = j_data[-1]
get_RSI(close, n=6)
该函数仅在回测、交易模块可用
获取相对强弱指标RSI指标的计算结果
注意事项:
无
close:价格的时间序列数据, numpy.ndarray类型;
n: 周期, int类型;
RSI指标rsi值的时间序列, numpy.ndarray类型
def initialize(context):
g.security = "600570.XSHG"
set_universe(g.security)
def handle_data(context, data):
h = get_history(100, '1d', ['close','high','low'], security_list=g.security)
close_data = h['close'].values
rsi_data = get_RSI(close_data, 6)
rsi = rsi_data[-1]
get_CCI(close, n=14)
该函数仅在回测、交易模块可用
获取顺势指标CCI指标的计算结果
注意事项:
无
high:最高价的时间序列数据, numpy.ndarray类型;
low:最低价的时间序列数据, numpy.ndarray类型;
close:收盘价的时间序列数据, numpy.ndarray类型;
n: 周期, int类型;
CCI指标cci值的时间序列, numpy.ndarray类型
def initialize(context):
g.security = "600570.XSHG"
set_universe(g.security)
def handle_data(context, data):
h = get_history(100, '1d', ['close','high','low'], security_list=g.security)
high_data = h['high'].values
low_data = h['low'].values
close_data = h['close'].values
cci_data = get_CCI(high_data, low_data, close_data, 14)
cci = cci_data[-1]
log(content)
该函数仅在回测、交易模块可用。
该接口用于打印日志。
支持如下场景的日志记录:
log.debug("debug")
log.info("info")
log.warning("warning")
log.error("error")
log.critical("critical")
与python的logging模块用法一致
注意事项:
无
参数可以是字符串、对象等。
None
# 打印出一个格式化后的字符串
g.security='600570.SS'
log.info("Selling %s, amount=%s" % (g.security, 10000))
is_trade()
该函数仅在回测、交易模块可用。
该接口用于提供业务代码执行场景判断依据,明确标识当前业务代码运行场景为回测还是交易。因部分函数仅限回测或交易场景使用,该函数可以协助区分对应场景,以便限制函数可以在一套策略代码同时兼容回测与交易场景。
注意事项:
无
无
布尔类型,当前代码在交易中运行返回True,当前代码在回测中运行返回False(bool)。
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
_id = order(g.security, 100)
if is_trade():
log.info("当前运行场景:交易")
else:
log.info("当前运行场景:回测")
check_limit(security, query_date=None)
该函数在研究、回测、交易模块可用。
该接口用于标识股票的涨跌停情况。
注意事项:
security:单只股票代码或者多只股票代码组成的列表,必填字段(list[str]/str);
query_date:查询日期,查询指定日期股票代码的涨跌停状态,回测不传默认是回测当日时间,交易和研究不传默认是执行当日时间,非必填字段(str);
正常返回一个dict类型数据,包含每只股票代码的涨停状态。多只股票代码查询时其中部分股票代码查询异常则该代码返回既不涨停也不跌停状态0。(dict[str:int])
涨跌停状态说明:
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
# 代码涨跌停状态
stock_flag = check_limit(g.security)[g.security]
log.info(stock_flag)
send_email(send_email_info, get_email_info, smtp_code, info='', path='', subject='')
该函数仅在交易模块可用。
该接口用于通过QQ邮箱发送邮件内容。
注意事项:
send_email_info:发送方的邮箱地址,必填字段,如:50xxx00@qq.com(str);
get_email_info:接收方的邮箱地址,必填字段,如:[50xxx00@qq.com, 1xxx10@126.com](list[str]/str);
smtp_code:邮箱的smtp授权码,注意,不是邮箱密码,必填字段(str);
info:发送内容,选填字段,默认空字符串(str);
path:附件路径,选填字段,如:get_research_path() + 'stock.csv',默认空字符串(str);
subject:邮件主题,默认空字符串(str);
None
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
#发送文字信息
send_email('53xxxxxx7@qq.com', ['53xxxxx7@qq.com', 'Kxxxxn@126.com'], 'phfxxxxxxxxxxcd', info='今天的股票池信息')
send_qywx(corp_id, secret, agent_id, info='', path='', toparty='', touser= '', totag= '')
该函数仅在交易模块可用。
该接口用于通过企业微信发送内容,使用方法请查看 企业微信功能使用手册。
注意事项:
corp_id:企业ID,必填字段(str);
secret:企业微信应用的密码,必填字段(str);
agent_id:企业微信应用的ID,必填字段(str);
info:发送内容,选填字段,默认空字符串(str);
path:发送文件,选填字段,如:get_research_path() + 'stock.csv',默认空字符串(str);
toparty:发送对象为部门,选填字段,默认空字符串(str),多个对象之间用 '|' 符号分割;
touser:发送内容为个人,选填字段,默认空字符串(str),多个对象之间用 '|' 符号分割;
totag:发送内容为分组,选填字段,默认空字符串(str),多个对象之间用 '|' 符号分割;
注意:toparty、touser、totag如果都不传入,接口默认发送至应用中设定的第一个toparty
None
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
#发送文字信息
send_qywx('wwxxxxxxxxxxxxf9', 'hixxxxxxxxxxxxxxxxxxxBX8', '10xxxx3', info='已触发委托买入', toparty='1|2')
permission_test(account=None, end_date=None)
该函数仅在交易模块可用
该接口用于账号和有效期的权限校验,用户可以在接口中入参指定账号和指定有效期截止日,策略运行时会校验运行策略的账户与指定账户是否相符,以及运行当日日期是否超过指定的有效期截止日,任一条件校验失败,接口都会返回False,两者同时校验成功则返回True。校验失败会在策略日志中提示原因。
注意事项:
account:授权账号,选填字段,如果不填就代表不需要验证账号(str);
end_date:授权有效期截止日,选题字段,如果不填就代表不需要验证有效期(str),日期格式必须为'YYYYmmdd'的8位日期格式,如'20200101';
布尔类型,校验成功返回True,校验失败返回False(bool)。
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
pass
def after_trading_end(context, data):
# 需要用授权模式下载功能的情况下不用入参
flag = permission_test()
if not flag:
raise RuntimeError('授权不通过,终止程序,抛出异常')
# 不需要用授权模式下载功能的情况下通过入参来进行授权校验
flag = permission_test(account='10110922',end_date='20220101')
if not flag:
raise RuntimeError('授权不通过,终止程序,抛出异常')
create_dir(user_path)
该函数在研究、回测、交易模块可用
由于PTrade量化引擎禁用了os模块,因此用户无法在策略中通过编写代码实现子目录创建。用户可以通过此接口来创建文件的子目录路径。
注意事项:
user_path(str):待创建目录相对路径,必传字段。
比如user_path='download',会在研究界面生成download的目录;
比如user_path='download/2022',会在研究界面生成download/2022的目录;
是否创建成功(True/False)(bool)。
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
# 在研究界面创建600570.SS目录
create_dir(g.security)
def handle_data(context, data):
pass
get_frequency()
该函数在回测、交易模块可用
该接口用于返回当前业务代码的周期,如在周期为分钟的情况下执行回测或交易,该函数返回minute;在周期为每日的情况下执行回测或交易,该函数返回daily。
注意事项:
无
无
周期为分钟返回minute,周期为每日返回daily(str)
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
log.info(get_frequency())
def handle_data(context, data):
pass
get_business_type()
该函数在回测、交易模块可用
该接口用于返回当前策略的业务类型。
注意事项:
无
无
策略业务类型(str):
def initialize(context):
# 初始化策略
g.security = "600570.SS"
set_universe(g.security)
def before_trading_start(context, data):
g.flag = False
g.business_type = get_business_type()
log.info("当前策略的业务类型为:%s" % g.business_type)
def handle_data(context, data):
if g.flag is False:
if g.business_type == "stock":
order("600570.SS", 100)
elif g.business_type == "future":
buy_open("IF2309.CCFX", 1, 3816.0)
g.flag = True
get_current_kline_count()
该函数在回测、交易、研究模块可用
该接口获取当前时间股票的k线根数。
注意事项:
无
当前时间的分钟bar数量(int)
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
log.info(get_current_kline_count())
filter_stock_by_status(stocks, filter_type=["ST", "HALT", "DELISTING"], query_date=None)
该函数在回测、交易、研究模块可用
该接口用于过滤指定状态的股票代码。
注意事项:
仅支持before_trading_start模块调用
stocks: 例如 ['000001.SZ','000003.SZ']。该字段必须输入。(list[str]/str);
filter_type: 支持以下四种类型属性的过滤条件,默认为["ST", "HALT", "DELISTING"](str/list)
具体支持输入的字段包括 :
query_date: 格式为YYYYmmdd,默认为None,表示当前日期(回测为回测当前周期,研究与交易则取系统当前时间)(str);
股票列表(该列表已剔除符合任一指定状态的标的)(list)
def initialize(context):
g.security = ['123002.SZ',"688500.SS","000001.SZ", "603997.SS", '123181.SZ']
set_universe(g.security)
def before_trading_start(context, data):
filter_stock = filter_stock_by_status(g.security, ["ST", "HALT", "DELISTING"])
log.info(filter_stock)
def handle_data(context, data):
pass
check_strategy(strategy_content=None, strategy_path=None)
该函数在研究模块可用
该接口用于检查策略内容是否涉及升级过程中变动的API和Python库。
注意事项:
strategy_content: 策略内容(str)。
strategy_path: 策略路径(str)。
策略内容涉及升级过程中变动的API和Python库信息(list)。
{
"api_change_list": [
"margincash_open",
"get_history",
"get_fundamentals",
"get_etf_info",
"get_individual_transaction",
"get_individual_transcation",
"check_limit",
"get_price",
"get_snapshot",
"on_trade_response",
"set_parameters",
"set_yesterday_position",
"marginsec_open",
"order_market",
"margin_trade",
"get_user_name",
"debt_to_stock_order",
"get_instruments",
"get_margincash_open_amount",
"get_all_orders",
"run_interval",
"get_trades",
"margincash_close",
"marginsec_close",
"get_margin_assert",
"ipo_stocks_order",
"get_enslo_security_info",
"get_hks_unit_amount",
"get_individual_entrust",
"get_tick_direction",
"get_margin_contractreal",
"get_gear_price",
"get_stock_status"],
"package_change_list": [
"walrus",
"keras",
"pykalman",
"arch",
"cvxopt",
"pulp"],
}
check_strategy(strategy_content="""
import arch
import cvxopt
import keras
import pulp
import pykalman
import tensorflow
import walrus
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
pos={}
pos["sid"] = "600570.SS"
pos["amount"] = "1000"
pos["enable_amount"] = "600"
pos["cost_basis"] = "55"
set_yesterday_position([pos])
run_interval(context, interval_handle, seconds=10)
def interval_handle(context):
pass
def before_trading_start(context, data):
get_history(100, frequency="1d", field=["close"], security_list=g.security)
get_fundamentals(g.security, "balance_statement", "total_assets")
get_etf_info("510020.SS")
get_individual_transaction()
get_individual_transcation()
check_limit(g.security)
get_price(g.security, start_date="20150101", end_date="20150131", frequency="1d")
get_snapshot(g.security)
set_parameters(holiday_not_do_before="1")
get_user_name(False)
get_instruments(g.security)
get_all_orders()
get_trades()
get_margin_assert()
get_enslo_security_info()
get_hks_unit_amount("02899.XHKG-SS", "1")
get_individual_entrust()
get_tick_direction([g.security])
get_margin_contractreal()
get_gear_price(g.security)
get_stock_status([g.security], "ST")
def on_trade_response(context, trade_list):
pass
def handle_data(context, data):
margincash_open(g.security, 100)
marginsec_open(g.security, 100)
order_market(g.security, 100, 0, 35)
margin_trade(g.security, 100)
get_margincash_open_amount(g.security)
debt_to_stock_order("110033.SS", -1000)
margincash_close(g.security, 100)
marginsec_close(g.security, 100)
ipo_stocks_order(submarket_type=0)
""")
check_strategy(strategy_path="./strategy.txt")
fund_transfer(trans_direction, occur_balance, exchange_type="1")
该函数仅在股票交易模块可用
用于UF20柜台与极速柜台、UF20柜台与极速柜台双中心资金调拨。
注意事项:
trans_direction(str):调拨方向,0为转入极速、1为转出极速。
occur_balance(float):发生金额(单位:元,最小精度:0.01元)。
exchange_type(str):交易类别,1为上海、2为深圳。
返回调拨是否成功True/False(bool)。
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def before_trading_start(context, data):
# 转出深A极速柜台100000元
fund_transfer('1', 100000, exchange_type='2')
def handle_data(context, data):
pass
market_fund_transfer(exchange_type, occur_balance)
该函数仅在股票交易模块可用
用于极速柜台双中心之间资金调拨。
注意事项:
exchange_type(str):交易类别,1为上海、2为深圳。
occur_balance(float):发生金额(单位:元,最小精度:0.01元)。
返回调拨是否成功True/False(bool)。
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def before_trading_start(context, data):
# 转入沪A极速柜台100000元
market_fund_transfer('1', 100000)
def handle_data(context, data):
pass
该对象仅支持回测、交易模块。
全局对象g,用于存储用户的各类可被不同函数(包括自定义函数)调用的全局数据,如:
g.security = None #股票池
注意事项:
无
def initialize(context):
g.security = "600570.SS"
g.count = 1
g.flag = 0
set_universe(g.security)
def handle_data(context, data):
log.info(g.security)
log.info(g.count)
log.info(g.flag)
该对象仅支持回测、交易模块。
类型为业务上下文对象
注意事项:
capital_base -- 起始资金
previous_date -- 前一个交易日
sim_params -- SimulationParameters对象
capital_base -- 起始资金
data_frequency -- 数据频率
portfolio -- 账户信息,可参考Portfolio对象
initialized -- 是否执行初始化
slippage -- 滑点,VolumeShareSlippage对象
volume_limit -- 成交限量
price_impact -- 价格影响力
commission -- 佣金费用,Commission对象
tax—印花税费率
cost—佣金费率
min_trade_cost—最小佣金
blotter -- Blotter对象(记录)
current_dt -- 当前单位时间的开始时间,datetime.datetime对象(北京时间)
recorded_vars -- 收益曲线值
def initialize(context):
g.security = ['600570.SS', '000001.SZ']
set_universe(g.security)
def handle_data(context, data):
# 获得当前回测相关时间
pre_date = context.previous_date
log.info(pre_date)
year = context.blotter.current_dt.year
log.info(year)
month = context.blotter.current_dt.month
log.info(month)
day = context.blotter.current_dt.day
log.info(day)
hour = context.blotter.current_dt.hour
log.info(hour)
minute = context.blotter.current_dt.minute
log.info(minute)
second = context.blotter.current_dt.second
log.info(second)
# 获取"年-月-日"格式
date = context.blotter.current_dt.strftime("%Y-%m-%d")
log.info(date)
# 获取周几
weekday = context.blotter.current_dt.isoweekday()
log.info(weekday)
该对象仅支持回测、交易模块。
一个单位时间内代码的K线数据,是一个类对象。
注意事项:
以下属性也能通过get_history()/get_price()获取到
symbol 标的代码
name 代码名称
dt 当前周期时间
is_open 停牌标志,0-停牌,1-非停牌
open 当前周期开盘价
close 当前周期收盘价
price 当前周期最新价
low 当前周期最低价
high 当前周期最高价
volume 当前周期成交量
money 当前周期成交额
preclose 昨收盘价(仅日线返回)
high_limit 涨停价(仅日线返回)
low_limit 跌停价(仅日线返回)
unlimited 是否无涨跌停限制(仅日线返回)
datetime 当前周期时间
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
def before_trading_start(context, data):
g.flag = False
def handle_data(context, data):
if not g.flag:
# 打印代码BarData对象
log.info(data[g.security])
# 打印标的代码
log.info(data[g.security].symbol)
# 打印代码名称
log.info(data[g.security].name)
# 打印当前周期时间
log.info(data[g.security].dt)
# 打印当前周期是否开盘
log.info(data[g.security].is_open)
# 打印当前周期开盘价
log.info(data[g.security].open)
# 打印当前周期收盘价
log.info(data[g.security].close)
# 打印当前周期最新价
log.info(data[g.security].price)
# 打印当前周期最低价
log.info(data[g.security].low)
# 打印当前周期最高价
log.info(data[g.security].high)
# 打印当前周期成交量
log.info(data[g.security].volume)
# 打印当前周期成交额
log.info(data[g.security].money)
# 打印昨收盘价(仅日线返回)
log.info(data[g.security].preclose)
# 打印涨停价(仅日线返回)
log.info(data[g.security].high_limit)
# 打印跌停价(仅日线返回)
log.info(data[g.security].low_limit)
# 打印是否无涨跌停限制(仅日线返回)
log.info(data[g.security].unlimited)
# 打印当前周期时间
log.info(data[g.security].datetime)
g.flag = True
该对象仅支持回测、交易模块。
对象数据包含账户当前的资金,标的信息,即所有标的操作仓位的信息汇总
注意事项:
股票账户返回
cash 当前可用资金(不包含冻结资金)
positions 当前持有的标的(包含不可卖出的标的),dict类型,key是标的代码,value是Position对象
portfolio_value 当前持有的标的和现金的总价值
positions_value 持仓价值
capital_used 已使用的现金
returns 当前的收益比例, 相对于初始资金
pnl 当前账户总资产-初始账户总资产
start_date 开始时间
期货账户返回:
cash 当前可用资金(不包含冻结资金)
positions 当前持有的标的(包含不可卖出的标的),dict类型,key是标的代码,value是Position对象
portfolio_value 当前持有的保证金和现金的总价值
positions_value 持仓价值
returns 当前的收益比例, 相对于初始资金
pnl 当前账户总资产-初始账户总资产
start_date 开始时间
margin 保证金
def initialize(context):
g.security = "600570.SS"
set_universe([g.security])
def handle_data(context, data):
log.info(context.portfolio.portfolio_value)
该对象仅支持回测、交易模块。
持有的某个标的的信息。
注意事项:
股票账户返回
sid 标的代码
enable_amount 可用数量
amount 总持仓数量
last_sale_price 最新价格
cost_basis 持仓成本价格
today_amount 今日开仓数量
business_type 持仓类型
update_time 持仓更新时间
期货账户返回:
sid 标的代码
short_enable_amount 空头仓可用数量
long_enable_amount 多头仓可用数量
today_short_amount 空头仓今仓数量
today_long_amount 多头仓今仓数量
long_cost_basis 多头仓持仓成本
short_cost_basis 空头仓持仓成本
long_amount 多头仓总持仓量
short_amount 空头仓总持仓量
long_pnl 多头仓浮动盈亏
short_pnl 空头仓浮动盈亏
amount 总持仓数量
enable_amount 可用数量
last_sale_price 最新价格
business_type 持仓类型
delivery_date 交割日,期货使用
margin 持仓保证金
contract_multiplier 合约乘数
update_time 持仓更新时间
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
def handle_data(context, data):
order(g.security,1000)
position = get_position(g.security)
log.info(position)
该对象仅支持回测、交易模块。
买卖订单信息
注意事项:
股票账户返回
id -- 订单号
dt -- 订单产生时间,datetime.datetime类型
limit -- 指定价格
symbol -- 标的代码(备注:标的代码尾缀为四位,上证为XSHG,深圳为XSHE,如需对应到代码请做代码尾缀兼容)
amount -- 下单数量,买入是正数,卖出是负数
created -- 订单生成时间,datetime.datetime类型
filled -- 成交数量,买入时为正数,卖出时为负数
entrust_no -- 委托编号
cancel_entrust_no -- 撤单委托编号
priceGear -- 盘口档位
status -- 委托状态
期货账户返回:
id -- 订单号
dt -- 订单产生时间,datetime.datetime类型
limit -- 指定价格
symbol -- 标的代码
amount -- 下单数量,正数
created -- 订单生成时间,datetime.datetime类型
side -- 多空仓标志(str类型,long:多头仓,short:空头仓)
action -- 开平仓方向(str类型,open:开仓,close:平仓)
entrust_direction -- 买卖方向(str类型,buy:买入,sell:卖出)
filled -- 成交数量,正数
entrust_no -- 委托编号
cancel_entrust_no -- 撤单委托编号
priceGear -- 盘口档位
status -- 委托状态
def initialize(context):
g.security = "600570.SS"
set_universe(g.security)
def handle_data(context, data):
order(g.security, 100)
log.info(get_orders())
使用本平台受阻,可参考常见问题说明
类名/函数名 | Python3.5实现 | Python3.11实现 | 说明 | |
---|---|---|---|---|
Numpy | numpy.linalg.lstsq | def lstsq(a, b, rcond=-1): | def lstsq(a, b, rcond="warn"): | 参数默认值改变 |
Pandas | pandas.concat | def concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=None, copy=True): | def concat(objs:Iterable[NDFrame]|Mapping[HashableT, NDFrame], axis:Axis=0, join:str="outer", ignore_index:bool=False, keys=None, levels=None, names=None, verify_integrity:bool=False, sort:bool=False, copy:bool=True)->DataFrame|Series: | 弃用join_axes参数 |
pandas.DatetimeIndex | def __new__(cls, data=None, freq=None, start=None, end=None, periods=None, tz=None, normalize=False, closed=None, ambiguous='raise', dayfirst=False, yearfirst=False, dtype=None, copy=False, name=None, verify_integrity=True): | def __new__(cls, data=None, freq:str|BaseOffset|lib.NoDefault=lib.no_default, tz=None, normalize:bool=False, closed=None, ambiguous="raise", dayfirst:bool=False, yearfirst:bool=False, dtype:Dtype|None=None, copy:bool=False, name:Hashable=None)->DatetimeIndex: | 弃用start参数 | |
pandas.DataFrame.append | def append(self, other, ignore_index=False, verify_integrity=False, sort=None): | def append(self, other, ignore_index:bool=False, verify_integrity:bool=False, sort:bool=False)->DataFrame: | 参数默认值改变 | |
pandas.DataFrame.apply | def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds): | def apply(self, func:AggFuncType, axis:Axis=0, raw:bool=False, result_type:Literal["expand", "reduce", "broadcast"]|None=None, args=(), **kwargs): | 弃用broadcast参数 | |
pandas.DataFrame.astype | def astype(self, dtype, copy=True, errors='raise', **kwargs): | def astype(self:NDFrameT, dtype, copy:bool_t=True, errors:IgnoreRaise="raise")->NDFrameT: | 不再支持以kwargs方式传入额外入参 | |
pandas.DataFrame.quantile | def quantile(self, q=0.5, axis=0, numeric_only=True, interpolation='linear'): | def quantile(self, q:float|AnyArrayLike|Sequence[float]=0.5, axis:Axis=0, numeric_only:bool|lib.NoDefault=no_default, interpolation:QuantileInterpolation="linear", method:Literal["single", "table"]="single")->Series|DataFrame: | 参数默认值改变 | |
pandas.DataFrame.replace | def replace(self, to_replace=None, value=None, inplace=False, limit=None, regex=False, method='pad'): | def replace(self, to_replace=None, value=lib.no_default, inplace:bool=False, limit:int|None=None, regex:bool=False, method:Literal["pad", "ffill", "bfill"]|lib.NoDefault=lib.no_default)->DataFrame|None: | 参数默认值改变 | |
pandas.DataFrame.resample | def resample(self, rule, how=None, axis=0, fill_method=None, closed=None, label=None, convention='start', kind=None, loffset=None, limit=None, base=0, on=None, level=None): | def resample(self, rule, axis:Axis=0, closed:str|None=None, label:str|None=None, convention:str="start", kind:str|None=None, loffset=None, base:int|None=None, on:Level=None, level:Level=None, origin:str|TimestampConvertibleTypes="start_day", offset:TimedeltaConvertibleTypes|None=None, group_keys:bool|lib.NoDefault=no_default, )->Resampler: | 弃用how参数 | |
pandas.DataFrame.sort_index | def sort_index(self, axis=0, level=None, ascending=True, inplace=False, kind='quicksort', na_position='last', sort_remaining=True, by=None): | def sort_index(self, axis:Axis=0, level:IndexLabel=None, ascending:bool|Sequence[bool]=True, inplace:bool=False, kind:SortKind="quicksort", na_position:NaPosition="last", sort_remaining:bool=True, ignore_index:bool=False, key:IndexKeyFunc=None)->DataFrame|None: | 弃用by参数 | |
pandas.DataFrame.to_csv | def to_csv(self, path_or_buf=None, sep=",", "", na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, mode='w', encoding=None, compression=None, quoting=None, quotechar='"', line_terminator='\n', chunksize=None, tupleize_cols=None, date_format=None, doublequote=True, escapechar=None, decimal='.'): | def to_csv(self, path_or_buf:FilePath|WriteBuffer[bytes]|WriteBuffer[str]|None=None, sep:str=",", na_rep:str="", float_format:str|Callable|None=None, columns:Sequence[Hashable]|None=None, header:bool_t|list[str]=True, index:bool_t=True, index_label:IndexLabel|None=None, mode:str="w", encoding:str|None=None, compression:CompressionOptions="infer", quoting:int|None=None, quotechar:str='"', lineterminator:str|None=None, chunksize:int|None=None, date_format:str|None=None, doublequote:bool_t=True, escapechar:str|None=None, decimal:str=".", errors:str="strict", storage_options:StorageOptions=None)->str|None: | 参数默认值改变 | |
pandas.DataFrame.to_excel | def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, startrow=0, startcol=0, engine=None, merge_cells=True, encoding=None, inf_rep='inf', verbose=True, freeze_panes=None): | def to_excel(self, excel_writer, sheet_name:str="Sheet1", na_rep:str="", float_format:str|None=None, columns:Sequence[Hashable]|None=None, header:Sequence[Hashable]|bool_t=True, index:bool_t=True, index_label:IndexLabel=None, startrow:int=0, startcol:int=0, engine:str|None=None, merge_cells:bool_t=True, encoding:lib.NoDefault=lib.no_default, inf_rep:str="inf", verbose:lib.NoDefault=lib.no_default, freeze_panes:tuple[int, int]|None=None, storage_options:StorageOptions=None)->None: | 参数默认值改变 | |
pandas.DataFrame.to_html | def to_html(self, buf=None, columns=None, col_space=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, justify=None, bold_rows=True, classes=None, escape=True, max_rows=None, max_cols=None, show_dimensions=False, notebook=False, decimal='.', border=None, table_id=None): | def to_html(self, buf:FilePath|WriteBuffer[str]|None=None, columns:Sequence[Level]|None=None, col_space:ColspaceArgType|None=None, header:bool|Sequence[str]=True, index:bool=True, na_rep:str="NaN", formatters:FormattersType|None=None, float_format:FloatFormatType|None=None, sparsify:bool|None=None, index_names:bool=True, justify:str|None=None, max_rows:int|None=None, max_cols:int|None=None, show_dimensions:bool|str=False, decimal:str=".", bold_rows:bool=True, classes:str|list|tuple|None=None, escape:bool=True, notebook:bool=False, border:int|bool|None=None, table_id:str|None=None, render_links:bool=False, encoding:str|None=None)->str|None: | 参数顺序改变 | |
pandas.DataFrame.to_json | def to_json(self, path_or_buf=None, orient=None, date_format=None, double_precision=10, force_ascii=True, date_unit='ms', default_handler=None, lines=False, compression=None, index=True): | def to_json(self, path_or_buf:FilePath|WriteBuffer[bytes]|WriteBuffer[str]|None=None, orient:str|None=None, date_format:str|None=None, double_precision:int=10, force_ascii:bool_t=True, date_unit:str="ms", default_handler:Callable[[Any], JSONSerializable]|None=None, lines:bool_t=False, compression:CompressionOptions="infer", index:bool_t=True, indent:int|None=None, storage_options:StorageOptions=None)->str|None: | 参数默认值改变 | |
pandas.DataFrame.to_records | def to_records(self, index=True, convert_datetime64=None): | def to_records(self, index:bool=True, column_dtypes=None, index_dtypes=None)->np.recarray: | 弃用convert_datetime64参数 | |
pandas.DataFrame.to_string | def to_string(self, buf=None, columns=None, col_space=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, justify=None, line_width=None, max_rows=None, max_cols=None, show_dimensions=False): | def to_string(self, buf:FilePath|WriteBuffer[str]|None=None, columns:Sequence[str]|None=None, col_space:int|list[int]|dict[Hashable, int]|None=None, header:bool|Sequence[str]=True, index:bool=True, na_rep:str="NaN", formatters:fmt.FormattersType|None=None, float_format:fmt.FloatFormatType|None=None, sparsify:bool|None=None, index_names:bool=True, justify:str|None=None, max_rows:int|None=None, max_cols:int|None=None, show_dimensions:bool=False, decimal:str=".", line_width:int|None=None, min_rows:int|None=None, max_colwidth:int|None=None, encoding:str|None=None)->str|None: | 参数顺序改变 | |
pandas.DataFrame.update | def update(self, other, join='left', overwrite=True, filter_func=None, raise_conflict=False): | def update(self, other, join:str="left", overwrite:bool=True, filter_func=None, errors:str="ignore")->None: | 弃用raise_conflict参数 | |
pandas.Panel | def __init__(self, data=None, items=None, major_axis=None, minor_axis=None, copy=False, dtype=None): | 在pandas(0.25.0)版本已弃用 | ||
pandas.read_excel | def read_excel(io, sheet_name=0, header=0, names=None, index_col=None, usecols=None, squeeze=False, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skiprows=None, nrows=None, na_values=None, parse_dates=False, date_parser=None, thousands=None, comment=None, skipfooter=0, convert_float=True, **kwds): | def read_excel(io, sheet_name:str|int|list[IntStrT]|None=0, header:int|Sequence[int]|None=0, names:list[str]|None=None, index_col:int|Sequence[int]|None=None, usecols:int|str|Sequence[int]|Sequence[str]|Callable[[str], bool]|None=None, squeeze:bool|None=None, dtype:DtypeArg|None=None, engine:Literal["xlrd", "openpyxl", "odf", "pyxlsb"]|None=None, converters:dict[str, Callable]|dict[int, Callable]|None=None, true_values:Iterable[Hashable]|None=None, false_values:Iterable[Hashable]|None=None, skiprows:Sequence[int]|int|Callable[[int], object]|None=None, nrows:int|None=None, na_values=None, keep_def ault_na:bool=True, na_filter:bool=True, verbose:bool=False, parse_dates:list|dict|bool=False, date_parser:Callable|None=None, thousands:str|None=None, decimal:str=".", comment:str|None=None, skipfooter:int=0, convert_float:bool|None=None, mangle_dupe_cols:bool=True, storage_options:StorageOptions=None)->DataFrame|dict[IntStrT, DataFrame]: | 参数默认值改变 | |
pandas.read_json | def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True, convert_axes=True, convert_dates=True, keep_def ault_dates=True, numpy=False, precise_float=False, date_unit=None, encoding=None, lines=False, chunksize=None, compression='infer'): | def read_json(path_or_buf:FilePath|ReadBuffer[str]|ReadBuffer[bytes], orient:str|None=None, typ:Literal["frame", "series"]="frame", dtype:DtypeArg|None=None, convert_axes=None, convert_dates:bool|list[str]=True, keep_def ault_dates:bool=True, numpy:bool=False, precise_float:bool=False, date_unit:str|None=None, encoding:str|None=None, encoding_errors:str|None="strict", lines:bool=False, chunksize:int|None=None, compression:CompressionOptions="infer", nrows:int|None=None, storage_options:StorageOptions=None)->DataFrame|Series|JsonReader: | 参数默认值改变 | |
pandas.testing.assert_frame_equal | def assert_frame_equal(left, right, check_dtype=True, check_index_type='equiv', check_column_type='equiv', check_frame_type=True, check_less_precise=False, check_names=True, by_blocks=False, check_exact=False, check_datetimelike_compat=False, check_categorical=True, check_like=False, obj='DataFrame'): | def assert_frame_equal(left, right, check_dtype:bool|Literal["equiv"]=True, check_index_type:bool|Literal["equiv"]="equiv", check_column_type="equiv", check_frame_type=True, check_less_precise=no_default, check_names=True, by_blocks=False, check_exact=False, check_datetimelike_compat=False, check_categorical=True, check_like=False, check_freq=True, check_flags=True, rtol=1.0e-5, atol=1.0e-8, obj="DataFrame")->None: | 参数默认值改变 | |
pandas.to_datetime | def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False, origin='unix', cache=False): | def to_datetime(arg:DatetimeScalarOrArrayConvertible|DictConvertible, errors:DateTimeErrorChoices="raise", dayfirst:bool=False, yearfirst:bool=False, utc:bool|None=None, format:str|None=None, exact:bool=True, unit:str|None=None, infer_datetime_format:bool=False, origin="unix", cache:bool=True)->DatetimeIndex|Series|DatetimeScalar|NaTType|None: | 弃用box参数 | |
pandas.to_pickle | def to_pickle(obj, path, compression='infer', protocol=pkl.HIGHEST_PROTOCOL): | def to_pickle(obj:Any, filepath_or_buffer:FilePath|WriteBuffer[bytes], compression:CompressionOptions="infer", protocol:int=pickle.HIGHEST_PROTOCOL, storage_options:StorageOptions=None) -> None: | protocol表示二进制数据序列化协议,Python3.5默认使用4版本协议,Python3.11默认使用5版本协议。使用5版本协议保存的pickle文件将无法在Python3.5版本读取。 |
变动版本 | 变动内容 |
---|---|
PTrade1.0-QTV202401.05.013 |
|
PTrade1.0-QTV202401.05.011 |
|
PTrade1.0-QTV202401.05.008 |
|
PTrade1.0-QTV202401.05.000 |
|
PTrade1.0-QTV202401.04.000 |
|
PTrade1.0-QTV202401.02.000 |
|
PTrade1.0-QTV202401.01.000 |
|
PTrade1.0-QTV202401.00.000 |
|
PTrade1.0-QTV202301.03.000 |
|
PTrade1.0-QTV202301.02.000 |
|
PTrade1.0-QTV202301.01.000 |
|
PTrade1.0-QTV202301.00.000 |
|
PBOXQT1.0V202202.03.000 |
|
PBOXQT1.0V202202.02.000 |
|
PBOXQT1.0V202202.01.000 |
|
PBOXQT1.0V202202.00.000 |
|
PBOXQT1.0V202201.02.000 |
|
PBOXQT1.0V202201.01.000 |
|
PBOXQT1.0V202201.00.000 |
|
PBOXQT1.0V202101.09.000 |
|
PBOXQT1.0V202101.08.000 |
|
PBOXQT1.0V202101.07.000 |
|
PBOXQT1.0V202101.06.000 |
|
PBOXQT1.0V202101.05.000 |
|
PBOXQT1.0V202101.04.000 |
|
PBOXQT1.0V202101.03.000 |
|