handle_data - 生命周期函数

handle_data 是 PTrade 中用于生命周期的接口。

提示

  • live-or-trading-module-only
  • backtest-only

原型

handle_data(context, data)

释义

handle_data 是 PTrade 中用于生命周期的接口。

研究:不支持;回测:支持;模拟:官方目录未明确;实盘:支持。

参数

参数 是否必填 官方原始定义
context context
data data

返回值

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)

详细说明

使用场景

该函数仅在回测、交易模块可用

接口说明

该函数在交易时间内按指定的周期频率运行,是用于处理策略交易的主要模块,根据策略保存时的周期参数分为每分钟运行和每天运行,是策略运行的唯二必须定义函数之一。

注意事项

该函数每个单位周期执行一次

如果是日线级别策略,每天执行一次。股票回测场景下,在15:00执行;股票交易场景下,执行时间为券商实际配置时间。

如果是分钟级别策略,每分钟执行一次,股票回测场景下,执行时间为9:31 -- 15:00,股票交易场景下,执行时间为9:30 -- 14:59。

回测与交易中,handle_data函数不会在非交易日触发(如回测或交易起始日期为2015年12月21日,则策略在2016年1月1日-3日时, handle_data不会运行,4日继续运行)。

可调用接口

set_parameters(回测/交易) get_trading_day(回测/交易) get_all_trades_days(研究/回测/交易) get_trade_days(研究/回测/交易) get_trading_day_by_date(研究/回测/交易)
get_history(研究/回测/交易) get_price(研究/回测/交易) get_individual_entrust(交易) get_individual_transaction(交易) get_tick_direction(交易)
get_sort_msg(交易) get_underlying_code(交易) get_etf_info(交易) get_etf_stock_info(交易) get_gear_price(交易)
get_snapshot(交易) get_cb_info(研究/交易) get_trend_data(研究/回测/交易) get_stock_name(研究/回测/交易) get_stock_info(研究/回测/交易)
get_stock_status(研究/回测/交易) get_stock_exrights(研究/回测/交易) get_stock_blocks(研究/回测/交易) get_index_stocks(研究/回测/交易) get_etf_stock_list(交易)
get_industry_stocks(研究/回测/交易) get_fundamentals(研究/回测/交易) get_Ashares(研究/回测/交易) get_etf_list(交易) get_ipo_stocks(交易)
get_cb_list(交易) get_reits_list(研究/回测/交易) get_position(回测/交易) get_positions(回测/交易) get_all_positions(交易)
order(回测/交易) order_target(回测/交易) order_value(回测/交易) order_target_value(回测/交易) order_market(交易)
ipo_stocks_order(交易) after_trading_order(交易) after_trading_cancel_order(交易) etf_basket_order(交易) etf_purchase_redemption(交易)
cancel_order(回测/交易) cancel_order_ex(交易) debt_to_stock_order(交易) get_open_orders(回测/交易) get_order(回测/交易)
get_orders(回测/交易) get_all_orders(交易) get_trades(回测/交易) margin_trade(回测/交易) margincash_open(交易)
margincash_close(交易) margincash_direct_refund(交易) marginsec_open(交易) marginsec_close(交易) marginsec_direct_refund(交易)
get_margin_contract(交易) get_margin_contractreal(交易) get_margin_asset(交易) get_assure_security_list(交易) get_margincash_open_amount(交易)
get_margincash_close_amount(交易) get_marginsec_open_amount(交易) get_marginsec_close_amount(交易) get_margin_entrans_amount(交易) get_enslo_security_info(交易)
buy_open(回测/交易(期货)) sell_close(回测/交易(期货)) sell_open(回测/交易(期货)) buy_close(回测/交易(期货))
get_MACD(回测/交易) get_KDJ(回测/交易) get_RSI(回测/交易) get_CCI(回测/交易) log(回测/交易)
check_limit(回测/交易) send_email(交易) send_qywx(交易) create_dir(研究/回测/交易) get_current_kline_count(研究/回测/交易)
get_dominant_contract(研究/回测/交易(期货)) get_crdt_fund(交易) fund_transfer(交易) market_fund_transfer(交易)

参数详细说明

context: Context对象,存放有当前的账户及持仓信息;

data:是一个类对象,实现了类似字典的通过key获取value的方法,可以通过股票代码获取代码对应的BarData对象,对象中包含当前周期(日线策略是当天,分钟策略是当前分钟)的数据;

注意:为了加速,data中的数据只包含股票池中所订阅标的的信息,可使用data[security]的方式来获取当前周期对应的标的信息;

返回值

None