## 提示

- `live-or-trading-module-only`
- `backtest-only`


## 原型

```python
before_trading_start(context, data)
```

## 释义

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

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

## 参数

| 参数 | 是否必填 | 官方原始定义 |
|---|---:|---|
| `context` | 是 | context |
| `data` | 是 | data |

## 返回值

None

## 示例

```python
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)
```

## 详细说明

### 使用场景

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

### 接口说明

该函数在每天开始交易前被调用一次，用于添加每天都要初始化的信息，如无盘前初始化需求，该函数可以在策略中不做定义。

### 注意事项

在回测中，该函数在每个回测交易日8:30分执行。

在交易中，该函数在开启交易时立即执行，从隔日开始每天9:10分(默认)执行。

当在9:10前开启交易时，受行情未更新原因在该函数内调用实时行情接口会导致数据有误。 可通过在该函数内sleep至9:10分或调用实时行情接口改为run_daily执行等方式进行避免。

可调用接口

| set_parameters(回测/交易) | get_trading_day(回测/交易) | get_all_trades_days(研究/回测/交易) | get_trade_days(研究/回测/交易) | get_trading_day_by_date(研究/回测/交易) |
| --- | --- | --- | --- | --- |
| get_market_list(研究/回测/交易) | get_market_detail(研究/回测/交易) | 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_position(回测/交易) | get_positions(回测/交易) | get_all_positions(交易) |
| get_trades_file(回测) | get_deliver(交易) | get_fundjour(交易) | get_lucky_info(交易) | order(回测/交易) |
| order_target(回测/交易) | order_value(回测/交易) | order_target_value(回测/交易) | order_market(交易) | ipo_stocks_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_margincash_stocks(交易) | get_marginsec_stocks(交易) | 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_margin_rate(回测(期货)) | get_instruments(回测/交易(期货)) |
| get_dominant_contract(研究/回测/交易(期货)) |  |  |  |
| get_MACD(回测/交易) | get_KDJ(回测/交易) | get_RSI(回测/交易) | get_CCI(回测/交易) |
| log(回测/交易) | check_limit(回测/交易) | send_email(交易) | send_qywx(交易) | permission_test(交易) |
| create_dir(研究/回测/交易) | filter_stock_by_status(研究/回测/交易) | get_cb_list(交易) | get_reits_list(研究/回测/交易) | get_crdt_fund(交易) |
| fund_transfer(交易) | market_fund_transfer(交易) |  |  |

### 参数详细说明

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

data：保留字段暂无数据；

### 返回值

None
