同花顺SuperMind 股票策略编写必知必会
同花顺SuperMind 股票策略编写必知必会
1.策略框架
2.设置函数
3.重要对象
4.常用数据接口
5.下单与撤单
1.策略框架
In [1]:
source_code = """
def init(context):
g.is_fired = True
log.info("初始化,仅在策略第一次运行时执行")
def before_trading(context):
date = get_datetime().strftime('%Y-%m-%d %H:%M:%S')
log.info('{} 盘前运行'.format(date))
def open_auction(context,bar_dict):
date = get_datetime().strftime('%Y-%m-%d %H:%M:%S')
log.info('{} 集合竞价后运行'.format(date))
def handle_bar(context,bar_dict):
date = get_datetime().strftime('%Y-%m-%d %H:%M:%S')
log.info('{} 盘中运行'.format(date))
if g.is_fired:
odr_id = order('000001.SZ',100)
g.is_fired = False
def on_order(context,odr):
log.info('委托状态更新推送')
log.info(odr)
def on_trade(context,trade):
log.info('成交推送')
log.info(trade)
def after_trading(context):
date = get_datetime().strftime('%Y-%m-%d %H:%M:%S')
log.info('{} 盘后运行'.format(date))
"""
In [2]:
btest = research_strategy(
source_code=source_code,start_date='20230801',end_date='20230801',
capital_base=float(1000000),frequency='MINUTE',stock_market='STOCK',
benchmark='000300.SH',plot=True
)
2023-08-01 00:00:00.000000 - INFO - 初始化,仅在策略第一次运行时执行
2023-08-01 09:00:00.000000 - INFO - 2023-08-01 09:00:00 盘前运行
2023-08-01 09:26:00.000000 - INFO - 2023-08-01 09:26:00 集合竞价后运行
2023-08-01 09:31:00.000000 - INFO - 2023-08-01 09:31:00 盘中运行
2023-08-01 09:31:00.000000 - INFO - 委托状态更新推送
2023-08-01 09:31:00.000000 - INFO - Order({'id': 'b2e33d44a4874dd59286ef9b04c98b9c', 'symbol': '000001.SZ', 'created': datetime.datetime(2023, 8, 1, 9, 31), 'order_type': 'LONG', 'limit_price': 0, 'amount': 100, 'filled_amount': 0, 'type': ORDER_TYPE.MARKET, 'transaction_cost': 0, 'avg_price': 0, 'status': ORDER_STATUS.PENDING_NEW, 'datetime': datetime.datetime(2023, 8, 1, 9, 31), 'offset_flag': None})
2023-08-01 09:31:00.000000 - INFO - 委托状态更新推送
2023-08-01 09:31:00.000000 - INFO - Order({'id': 'b2e33d44a4874dd59286ef9b04c98b9c', 'symbol': '000001.SZ', 'created': datetime.datetime(2023, 8, 1, 9, 31), 'order_type': 'LONG', 'limit_price': 0, 'amount': 100, 'filled_amount': 0, 'type': ORDER_TYPE.MARKET, 'transaction_cost': 0, 'avg_price': 0, 'status': ORDER_STATUS.ACTIVE, 'datetime': datetime.datetime(2023, 8, 1, 9, 31), 'offset_flag': None})
2023-08-01 09:31:00.000000 - INFO - 委托状态更新推送
2023-08-01 09:31:00.000000 - INFO - Order({'id': 'b2e33d44a4874dd59286ef9b04c98b9c', 'symbol': '000001.SZ', 'created': datetime.datetime(2023, 8, 1, 9, 31), 'order_type': 'LONG', 'limit_price': 0, 'amount': 100, 'filled_amount': 100, 'type': ORDER_TYPE.MARKET, 'transaction_cost': 5, 'avg_price': 12.22221, 'status': ORDER_STATUS.FILLED, 'datetime': datetime.datetime(2023, 8, 1, 9, 31), 'offset_flag': None})
2023-08-01 09:31:00.000000 - INFO - 成交推送
2023-08-01 09:31:00.000000 - INFO - Trade({'order_book_id': '000001.SZ', 'trading_datetime': datetime.datetime(2023, 8, 1, 9, 31), 'datetime': datetime.datetime(2023, 8, 1, 9, 31), 'order_id': 'b2e33d44a4874dd59286ef9b04c98b9c', 'last_price': 12.22221, 'last_quantity': 100, 'commission': 5, 'tax': 0, 'transaction_cost': 5, 'side': SIDE.BUY, 'position_effect': None, 'exec_id': 'a8081b2bddb74ce9af2867b525d61c7d', 'frozen_price': 12.2, 'close_today_amount': 0})
2023-08-01 09:32:00.000000 - INFO - 2023-08-01 09:32:00 盘中运行
2023-08-01 09:33:00.000000 - INFO - 2023-08-01 09:33:00 盘中运行
2023-08-01 09:34:00.000000 - INFO - 2023-08-01 09:34:00 盘中运行
2023-08-01 09:35:00.000000 - INFO - 2023-08-01 09:35:00 盘中运行
2023-08-01 09:36:00.000000 - INFO - 2023-08-01 09:36:00 盘中运行
2023-08-01 09:37:00.000000 - INFO - 2023-08-01 09:37:00 盘中运行
2023-08-01 09:38:00.000000 - INFO - 2023-08-01 09:38:00 盘中运行
2023-08-01 09:39:00.000000 - INFO - 2023-08-01 09:39:00 盘中运行
2023-08-01 09:40:00.000000 - INFO - 2023-08-01 09:40:00 盘中运行
2023-08-01 09:41:00.000000 - INFO - 2023-08-01 09:41:00 盘中运行
2023-08-01 09:42:00.000000 - INFO - 2023-08-01 09:42:00 盘中运行
2023-08-01 09:43:00.000000 - INFO - 2023-08-01 09:43:00 盘中运行
2023-08-01 09:44:00.000000 - INFO - 2023-08-01 09:44:00 盘中运行
2023-08-01 09:45:00.000000 - INFO - 2023-08-01 09:45:00 盘中运行
2023-08-01 09:46:00.000000 - INFO - 2023-08-01 09:46:00 盘中运行
2023-08-01 09:47:00.000000 - INFO - 2023-08-01 09:47:00 盘中运行
2023-08-01 09:48:00.000000 - INFO - 2023-08-01 09:48:00 盘中运行
2023-08-01 09:49:00.000000 - INFO - 2023-08-01 09:49:00 盘中运行
2023-08-01 09:50:00.000000 - INFO - 2023-08-01 09:50:00 盘中运行
2023-08-01 09:51:00.000000 - INFO - 2023-08-01 09:51:00 盘中运行
2023-08-01 09:52:00.000000 - INFO - 2023-08-01 09:52:00 盘中运行
2023-08-01 09:53:00.000000 - INFO - 2023-08-01 09:53:00 盘中运行
2023-08-01 09:54:00.000000 - INFO - 2023-08-01 09:54:00 盘中运行
2023-08-01 09:55:00.000000 - INFO - 2023-08-01 09:55:00 盘中运行
2023-08-01 09:56:00.000000 - INFO - 2023-08-01 09:56:00 盘中运行
2023-08-01 09:57:00.000000 - INFO - 2023-08-01 09:57:00 盘中运行
2023-08-01 09:58:00.000000 - INFO - 2023-08-01 09:58:00 盘中运行
2023-08-01 09:59:00.000000 - INFO - 2023-08-01 09:59:00 盘中运行
2023-08-01 10:00:00.000000 - INFO - 2023-08-01 10:00:00 盘中运行
2023-08-01 10:01:00.000000 - INFO - 2023-08-01 10:01:00 盘中运行
2023-08-01 10:02:00.000000 - INFO - 2023-08-01 10:02:00 盘中运行
2023-08-01 10:03:00.000000 - INFO - 2023-08-01 10:03:00 盘中运行
2023-08-01 10:04:00.000000 - INFO - 2023-08-01 10:04:00 盘中运行
2023-08-01 10:05:00.000000 - INFO - 2023-08-01 10:05:00 盘中运行
2023-08-01 10:06:00.000000 - INFO - 2023-08-01 10:06:00 盘中运行
2023-08-01 10:07:00.000000 - INFO - 2023-08-01 10:07:00 盘中运行
2023-08-01 10:08:00.000000 - INFO - 2023-08-01 10:08:00 盘中运行
2023-08-01 10:09:00.000000 - INFO - 2023-08-01 10:09:00 盘中运行
2023-08-01 10:10:00.000000 - INFO - 2023-08-01 10:10:00 盘中运行
2023-08-01 10:11:00.000000 - INFO - 2023-08-01 10:11:00 盘中运行
2023-08-01 10:12:00.000000 - INFO - 2023-08-01 10:12:00 盘中运行
2023-08-01 10:13:00.000000 - INFO - 2023-08-01 10:13:00 盘中运行
2023-08-01 10:14:00.000000 - INFO - 2023-08-01 10:14:00 盘中运行
2023-08-01 10:15:00.000000 - INFO - 2023-08-01 10:15:00 盘中运行
2023-08-01 10:16:00.000000 - INFO - 2023-08-01 10:16:00 盘中运行
2023-08-01 10:17:00.000000 - INFO - 2023-08-01 10:17:00 盘中运行
2023-08-01 10:18:00.000000 - INFO - 2023-08-01 10:18:00 盘中运行
2023-08-01 10:19:00.000000 - INFO - 2023-08-01 10:19:00 盘中运行
2023-08-01 10:20:00.000000 - INFO - 2023-08-01 10:20:00 盘中运行
2023-08-01 10:21:00.000000 - INFO - 2023-08-01 10:21:00 盘中运行
2023-08-01 10:22:00.000000 - INFO - 2023-08-01 10:22:00 盘中运行
2023-08-01 10:23:00.000000 - INFO - 2023-08-01 10:23:00 盘中运行
2023-08-01 10:24:00.000000 - INFO - 2023-08-01 10:24:00 盘中运行
2023-08-01 10:25:00.000000 - INFO - 2023-08-01 10:25:00 盘中运行
2023-08-01 10:26:00.000000 - INFO - 2023-08-01 10:26:00 盘中运行
2023-08-01 10:27:00.000000 - INFO - 2023-08-01 10:27:00 盘中运行
2023-08-01 10:28:00.000000 - INFO - 2023-08-01 10:28:00 盘中运行
2023-08-01 10:29:00.000000 - INFO - 2023-08-01 10:29:00 盘中运行
2023-08-01 10:30:00.000000 - INFO - 2023-08-01 10:30:00 盘中运行
2023-08-01 10:31:00.000000 - INFO - 2023-08-01 10:31:00 盘中运行
2023-08-01 10:32:00.000000 - INFO - 2023-08-01 10:32:00 盘中运行
2023-08-01 10:33:00.000000 - INFO - 2023-08-01 10:33:00 盘中运行
2023-08-01 10:34:00.000000 - INFO - 2023-08-01 10:34:00 盘中运行
2023-08-01 10:35:00.000000 - INFO - 2023-08-01 10:35:00 盘中运行
2023-08-01 10:36:00.000000 - INFO - 2023-08-01 10:36:00 盘中运行
2023-08-01 10:37:00.000000 - INFO - 2023-08-01 10:37:00 盘中运行
2023-08-01 10:38:00.000000 - INFO - 2023-08-01 10:38:00 盘中运行
2023-08-01 10:39:00.000000 - INFO - 2023-08-01 10:39:00 盘中运行
2023-08-01 10:40:00.000000 - INFO - 2023-08-01 10:40:00 盘中运行
2023-08-01 10:41:00.000000 - INFO - 2023-08-01 10:41:00 盘中运行
2023-08-01 10:42:00.000000 - INFO - 2023-08-01 10:42:00 盘中运行
2023-08-01 10:43:00.000000 - INFO - 2023-08-01 10:43:00 盘中运行
2023-08-01 10:44:00.000000 - INFO - 2023-08-01 10:44:00 盘中运行
2023-08-01 10:45:00.000000 - INFO - 2023-08-01 10:45:00 盘中运行
2023-08-01 10:46:00.000000 - INFO - 2023-08-01 10:46:00 盘中运行
2023-08-01 10:47:00.000000 - INFO - 2023-08-01 10:47:00 盘中运行
2023-08-01 10:48:00.000000 - INFO - 2023-08-01 10:48:00 盘中运行
2023-08-01 10:49:00.000000 - INFO - 2023-08-01 10:49:00 盘中运行
2023-08-01 10:50:00.000000 - INFO - 2023-08-01 10:50:00 盘中运行
2023-08-01 10:51:00.000000 - INFO - 2023-08-01 10:51:00 盘中运行
2023-08-01 10:52:00.000000 - INFO - 2023-08-01 10:52:00 盘中运行
2023-08-01 10:53:00.000000 - INFO - 2023-08-01 10:53:00 盘中运行
2023-08-01 10:54:00.000000 - INFO - 2023-08-01 10:54:00 盘中运行
2023-08-01 10:55:00.000000 - INFO - 2023-08-01 10:55:00 盘中运行
2023-08-01 10:56:00.000000 - INFO - 2023-08-01 10:56:00 盘中运行
2023-08-01 10:57:00.000000 - INFO - 2023-08-01 10:57:00 盘中运行
2023-08-01 10:58:00.000000 - INFO - 2023-08-01 10:58:00 盘中运行
2023-08-01 10:59:00.000000 - INFO - 2023-08-01 10:59:00 盘中运行
2023-08-01 11:00:00.000000 - INFO - 2023-08-01 11:00:00 盘中运行
2023-08-01 11:01:00.000000 - INFO - 2023-08-01 11:01:00 盘中运行
2023-08-01 11:02:00.000000 - INFO - 2023-08-01 11:02:00 盘中运行
2023-08-01 11:03:00.000000 - INFO - 2023-08-01 11:03:00 盘中运行
2023-08-01 11:04:00.000000 - INFO - 2023-08-01 11:04:00 盘中运行
2023-08-01 11:05:00.000000 - INFO - 2023-08-01 11:05:00 盘中运行
2023-08-01 11:06:00.000000 - INFO - 2023-08-01 11:06:00 盘中运行
2023-08-01 11:07:00.000000 - INFO - 2023-08-01 11:07:00 盘中运行
2023-08-01 11:08:00.000000 - INFO - 2023-08-01 11:08:00 盘中运行
2023-08-01 11:09:00.000000 - INFO - 2023-08-01 11:09:00 盘中运行
2023-08-01 11:10:00.000000 - INFO - 2023-08-01 11:10:00 盘中运行
2023-08-01 11:11:00.000000 - INFO - 2023-08-01 11:11:00 盘中运行
2023-08-01 11:12:00.000000 - INFO - 2023-08-01 11:12:00 盘中运行
2023-08-01 11:13:00.000000 - INFO - 2023-08-01 11:13:00 盘中运行
2023-08-01 11:14:00.000000 - INFO - 2023-08-01 11:14:00 盘中运行
2023-08-01 11:15:00.000000 - INFO - 2023-08-01 11:15:00 盘中运行
2023-08-01 11:16:00.000000 - INFO - 2023-08-01 11:16:00 盘中运行
2023-08-01 11:17:00.000000 - INFO - 2023-08-01 11:17:00 盘中运行
2023-08-01 11:18:00.000000 - INFO - 2023-08-01 11:18:00 盘中运行
2023-08-01 11:19:00.000000 - INFO - 2023-08-01 11:19:00 盘中运行
2023-08-01 11:20:00.000000 - INFO - 2023-08-01 11:20:00 盘中运行
2023-08-01 11:21:00.000000 - INFO - 2023-08-01 11:21:00 盘中运行
2023-08-01 11:22:00.000000 - INFO - 2023-08-01 11:22:00 盘中运行
2023-08-01 11:23:00.000000 - INFO - 2023-08-01 11:23:00 盘中运行
2023-08-01 11:24:00.000000 - INFO - 2023-08-01 11:24:00 盘中运行
2023-08-01 11:25:00.000000 - INFO - 2023-08-01 11:25:00 盘中运行
2023-08-01 11:26:00.000000 - INFO - 2023-08-01 11:26:00 盘中运行
2023-08-01 11:27:00.000000 - INFO - 2023-08-01 11:27:00 盘中运行
2023-08-01 11:28:00.000000 - INFO - 2023-08-01 11:28:00 盘中运行
2023-08-01 11:29:00.000000 - INFO - 2023-08-01 11:29:00 盘中运行
2023-08-01 11:30:00.000000 - INFO - 2023-08-01 11:30:00 盘中运行
2023-08-01 13:01:00.000000 - INFO - 2023-08-01 13:01:00 盘中运行
2023-08-01 13:02:00.000000 - INFO - 2023-08-01 13:02:00 盘中运行
2023-08-01 13:03:00.000000 - INFO - 2023-08-01 13:03:00 盘中运行
2023-08-01 13:04:00.000000 - INFO - 2023-08-01 13:04:00 盘中运行
2023-08-01 13:05:00.000000 - INFO - 2023-08-01 13:05:00 盘中运行
2023-08-01 13:06:00.000000 - INFO - 2023-08-01 13:06:00 盘中运行
2023-08-01 13:07:00.000000 - INFO - 2023-08-01 13:07:00 盘中运行
2023-08-01 13:08:00.000000 - INFO - 2023-08-01 13:08:00 盘中运行
2023-08-01 13:09:00.000000 - INFO - 2023-08-01 13:09:00 盘中运行
2023-08-01 13:10:00.000000 - INFO - 2023-08-01 13:10:00 盘中运行
2023-08-01 13:11:00.000000 - INFO - 2023-08-01 13:11:00 盘中运行
2023-08-01 13:12:00.000000 - INFO - 2023-08-01 13:12:00 盘中运行
2023-08-01 13:13:00.000000 - INFO - 2023-08-01 13:13:00 盘中运行
2023-08-01 13:14:00.000000 - INFO - 2023-08-01 13:14:00 盘中运行
2023-08-01 13:15:00.000000 - INFO - 2023-08-01 13:15:00 盘中运行
2023-08-01 13:16:00.000000 - INFO - 2023-08-01 13:16:00 盘中运行
2023-08-01 13:17:00.000000 - INFO - 2023-08-01 13:17:00 盘中运行
2023-08-01 13:18:00.000000 - INFO - 2023-08-01 13:18:00 盘中运行
2023-08-01 13:19:00.000000 - INFO - 2023-08-01 13:19:00 盘中运行
2023-08-01 13:20:00.000000 - INFO - 2023-08-01 13:20:00 盘中运行
2023-08-01 13:21:00.000000 - INFO - 2023-08-01 13:21:00 盘中运行
2023-08-01 13:22:00.000000 - INFO - 2023-08-01 13:22:00 盘中运行
2023-08-01 13:23:00.000000 - INFO - 2023-08-01 13:23:00 盘中运行
2023-08-01 13:24:00.000000 - INFO - 2023-08-01 13:24:00 盘中运行
2023-08-01 13:25:00.000000 - INFO - 2023-08-01 13:25:00 盘中运行
2023-08-01 13:26:00.000000 - INFO - 2023-08-01 13:26:00 盘中运行
2023-08-01 13:27:00.000000 - INFO - 2023-08-01 13:27:00 盘中运行
2023-08-01 13:28:00.000000 - INFO - 2023-08-01 13:28:00 盘中运行
2023-08-01 13:29:00.000000 - INFO - 2023-08-01 13:29:00 盘中运行
2023-08-01 13:30:00.000000 - INFO - 2023-08-01 13:30:00 盘中运行
2023-08-01 13:31:00.000000 - INFO - 2023-08-01 13:31:00 盘中运行
2023-08-01 13:32:00.000000 - INFO - 2023-08-01 13:32:00 盘中运行
2023-08-01 13:33:00.000000 - INFO - 2023-08-01 13:33:00 盘中运行
2023-08-01 13:34:00.000000 - INFO - 2023-08-01 13:34:00 盘中运行
2023-08-01 13:35:00.000000 - INFO - 2023-08-01 13:35:00 盘中运行
2023-08-01 13:36:00.000000 - INFO - 2023-08-01 13:36:00 盘中运行
2023-08-01 13:37:00.000000 - INFO - 2023-08-01 13:37:00 盘中运行
2023-08-01 13:38:00.000000 - INFO - 2023-08-01 13:38:00 盘中运行
2023-08-01 13:39:00.000000 - INFO - 2023-08-01 13:39:00 盘中运行
2023-08-01 13:40:00.000000 - INFO - 2023-08-01 13:40:00 盘中运行
2023-08-01 13:41:00.000000 - INFO - 2023-08-01 13:41:00 盘中运行
2023-08-01 13:42:00.000000 - INFO - 2023-08-01 13:42:00 盘中运行
2023-08-01 13:43:00.000000 - INFO - 2023-08-01 13:43:00 盘中运行
2023-08-01 13:44:00.000000 - INFO - 2023-08-01 13:44:00 盘中运行
2023-08-01 13:45:00.000000 - INFO - 2023-08-01 13:45:00 盘中运行
2023-08-01 13:46:00.000000 - INFO - 2023-08-01 13:46:00 盘中运行
2023-08-01 13:47:00.000000 - INFO - 2023-08-01 13:47:00 盘中运行
2023-08-01 13:48:00.000000 - INFO - 2023-08-01 13:48:00 盘中运行
2023-08-01 13:49:00.000000 - INFO - 2023-08-01 13:49:00 盘中运行
2023-08-01 13:50:00.000000 - INFO - 2023-08-01 13:50:00 盘中运行
2023-08-01 13:51:00.000000 - INFO - 2023-08-01 13:51:00 盘中运行
2023-08-01 13:52:00.000000 - INFO - 2023-08-01 13:52:00 盘中运行
2023-08-01 13:53:00.000000 - INFO - 2023-08-01 13:53:00 盘中运行
2023-08-01 13:54:00.000000 - INFO - 2023-08-01 13:54:00 盘中运行
2023-08-01 13:55:00.000000 - INFO - 2023-08-01 13:55:00 盘中运行
2023-08-01 13:56:00.000000 - INFO - 2023-08-01 13:56:00 盘中运行
2023-08-01 13:57:00.000000 - INFO - 2023-08-01 13:57:00 盘中运行
2023-08-01 13:58:00.000000 - INFO - 2023-08-01 13:58:00 盘中运行
2023-08-01 13:59:00.000000 - INFO - 2023-08-01 13:59:00 盘中运行
2023-08-01 14:00:00.000000 - INFO - 2023-08-01 14:00:00 盘中运行
2023-08-01 14:01:00.000000 - INFO - 2023-08-01 14:01:00 盘中运行
2023-08-01 14:02:00.000000 - INFO - 2023-08-01 14:02:00 盘中运行
2023-08-01 14:03:00.000000 - INFO - 2023-08-01 14:03:00 盘中运行
2023-08-01 14:04:00.000000 - INFO - 2023-08-01 14:04:00 盘中运行
2023-08-01 14:05:00.000000 - INFO - 2023-08-01 14:05:00 盘中运行
2023-08-01 14:06:00.000000 - INFO - 2023-08-01 14:06:00 盘中运行
2023-08-01 14:07:00.000000 - INFO - 2023-08-01 14:07:00 盘中运行
2023-08-01 14:08:00.000000 - INFO - 2023-08-01 14:08:00 盘中运行
2023-08-01 14:09:00.000000 - INFO - 2023-08-01 14:09:00 盘中运行
2023-08-01 14:10:00.000000 - INFO - 2023-08-01 14:10:00 盘中运行
2023-08-01 14:11:00.000000 - INFO - 2023-08-01 14:11:00 盘中运行
2023-08-01 14:12:00.000000 - INFO - 2023-08-01 14:12:00 盘中运行
2023-08-01 14:13:00.000000 - INFO - 2023-08-01 14:13:00 盘中运行
2023-08-01 14:14:00.000000 - INFO - 2023-08-01 14:14:00 盘中运行
2023-08-01 14:15:00.000000 - INFO - 2023-08-01 14:15:00 盘中运行
2023-08-01 14:16:00.000000 - INFO - 2023-08-01 14:16:00 盘中运行
2023-08-01 14:17:00.000000 - INFO - 2023-08-01 14:17:00 盘中运行
2023-08-01 14:18:00.000000 - INFO - 2023-08-01 14:18:00 盘中运行
2023-08-01 14:19:00.000000 - INFO - 2023-08-01 14:19:00 盘中运行
2023-08-01 14:20:00.000000 - INFO - 2023-08-01 14:20:00 盘中运行
2023-08-01 14:21:00.000000 - INFO - 2023-08-01 14:21:00 盘中运行
2023-08-01 14:22:00.000000 - INFO - 2023-08-01 14:22:00 盘中运行
2023-08-01 14:23:00.000000 - INFO - 2023-08-01 14:23:00 盘中运行
2023-08-01 14:24:00.000000 - INFO - 2023-08-01 14:24:00 盘中运行
2023-08-01 14:25:00.000000 - INFO - 2023-08-01 14:25:00 盘中运行
2023-08-01 14:26:00.000000 - INFO - 2023-08-01 14:26:00 盘中运行
2023-08-01 14:27:00.000000 - INFO - 2023-08-01 14:27:00 盘中运行
2023-08-01 14:28:00.000000 - INFO - 2023-08-01 14:28:00 盘中运行
2023-08-01 14:29:00.000000 - INFO - 2023-08-01 14:29:00 盘中运行
2023-08-01 14:30:00.000000 - INFO - 2023-08-01 14:30:00 盘中运行
2023-08-01 14:31:00.000000 - INFO - 2023-08-01 14:31:00 盘中运行
2023-08-01 14:32:00.000000 - INFO - 2023-08-01 14:32:00 盘中运行
2023-08-01 14:33:00.000000 - INFO - 2023-08-01 14:33:00 盘中运行
2023-08-01 14:34:00.000000 - INFO - 2023-08-01 14:34:00 盘中运行
2023-08-01 14:35:00.000000 - INFO - 2023-08-01 14:35:00 盘中运行
2023-08-01 14:36:00.000000 - INFO - 2023-08-01 14:36:00 盘中运行
2023-08-01 14:37:00.000000 - INFO - 2023-08-01 14:37:00 盘中运行
2023-08-01 14:38:00.000000 - INFO - 2023-08-01 14:38:00 盘中运行
2023-08-01 14:39:00.000000 - INFO - 2023-08-01 14:39:00 盘中运行
2023-08-01 14:40:00.000000 - INFO - 2023-08-01 14:40:00 盘中运行
2023-08-01 14:41:00.000000 - INFO - 2023-08-01 14:41:00 盘中运行
2023-08-01 14:42:00.000000 - INFO - 2023-08-01 14:42:00 盘中运行
2023-08-01 14:43:00.000000 - INFO - 2023-08-01 14:43:00 盘中运行
2023-08-01 14:44:00.000000 - INFO - 2023-08-01 14:44:00 盘中运行
2023-08-01 14:45:00.000000 - INFO - 2023-08-01 14:45:00 盘中运行
2023-08-01 14:46:00.000000 - INFO - 2023-08-01 14:46:00 盘中运行
2023-08-01 14:47:00.000000 - INFO - 2023-08-01 14:47:00 盘中运行
2023-08-01 14:48:00.000000 - INFO - 2023-08-01 14:48:00 盘中运行
2023-08-01 14:49:00.000000 - INFO - 2023-08-01 14:49:00 盘中运行
2023-08-01 14:50:00.000000 - INFO - 2023-08-01 14:50:00 盘中运行
2023-08-01 14:51:00.000000 - INFO - 2023-08-01 14:51:00 盘中运行
2023-08-01 14:52:00.000000 - INFO - 2023-08-01 14:52:00 盘中运行
2023-08-01 14:53:00.000000 - INFO - 2023-08-01 14:53:00 盘中运行
2023-08-01 14:54:00.000000 - INFO - 2023-08-01 14:54:00 盘中运行
2023-08-01 14:55:00.000000 - INFO - 2023-08-01 14:55:00 盘中运行
2023-08-01 14:56:00.000000 - INFO - 2023-08-01 14:56:00 盘中运行
2023-08-01 14:57:00.000000 - INFO - 2023-08-01 14:57:00 盘中运行
2023-08-01 14:58:00.000000 - INFO - 2023-08-01 14:58:00 盘中运行
2023-08-01 14:59:00.000000 - INFO - 2023-08-01 14:59:00 盘中运行
2023-08-01 15:00:00.000000 - INFO - 2023-08-01 15:00:00 盘中运行
2023-08-01 15:30:00.000000 - INFO - 2023-08-01 15:30:00 盘后运行
2.设置函数
包括设置基准,手续费,滑点,初始持仓,等设置性函数,可参考帮助-股票API-设置函数,这里不在赘述
3.重要对象
3.1. context
用于存储策略信息(context.run_info),账户持仓数据(context.portfolio),自定义全局变量
- context.run_info
- start_date:策略起始时间
- end_date:策略结束时间
- frequency:策略运行频率
- stock_starting_cash:股票账户初始资金
- future_starting_cash:期货期权账户初始资金
- benchmark:基准指数
- margin_multiplier:保证金率
- context.portfolio
- available_cash:可用资金
- frozen_cash:冻结资金
- returns:收益率
- market_value:持仓市值
- portfolio_value:总资产
- starting_value:初始资金
- pnl:盈亏
- start_date:策略起始时间
- positions:持仓信息
- datetime:当前bar时间
- stock_account: 股票账户数据
- future_account: 期货期权账户数据
In [3]:
source_code = """
def init(context):
context.fired=True
log.info(context.run_info)
log.info(context.portfolio)
log.info(context.fired)
def handle_bar(context,bar_dict):
if context.fired:
odr_id = order('000001.SZ',100)
context.fired = False
log.info(context.run_info)
log.info(context.portfolio)
log.info(context.fired)
log.info(context.portfolio.positions) #输出看起来像个列表,实际类似于字典
log.info(context.portfolio.stock_account.positions)
log.info(context.portfolio.positions['000001.SZ'])
log.info(context.portfolio.stock_account.positions['000001.SZ'])
"""
In [4]:
btest = research_strategy(
source_code=source_code,start_date='20230801',end_date='20230801',
capital_base=float(1000000),frequency='MINUTE',stock_market='STOCK',
benchmark='000300.SH',plot=True
)
2023-08-01 00:00:00.000000 - INFO - RunInfo({'start_date': datetime.date(2023, 8, 1), 'end_date': datetime.date(2023, 8, 1), 'frequency': '1m', 'stock_starting_cash': 1000000.0, 'future_starting_cash': 0, 'benchmark': '000300.SH', 'margin_multiplier': 1, 'run_type': RUN_TYPE.BACKTEST})
2023-08-01 00:00:00.000000 - INFO - Portfolio({'available_cash': 1000000.0, 'frozen_cash': 0, 'returns': 0.0, 'market_value': 0, 'portfolio_value': 1000000.0, 'starting_value': 1000000.0, 'pnl': 0.0, 'start_date': datetime.date(2023, 8, 1), 'positions': [], 'datetime': datetime.datetime(2023, 8, 1, 0, 0)})
2023-08-01 00:00:00.000000 - INFO - True
2023-08-01 09:31:00.000000 - INFO - RunInfo({'start_date': datetime.date(2023, 8, 1), 'end_date': datetime.date(2023, 8, 1), 'frequency': '1m', 'stock_starting_cash': 1000000.0, 'future_starting_cash': 0, 'benchmark': '000300.SH', 'margin_multiplier': 1, 'run_type': RUN_TYPE.BACKTEST})
2023-08-01 09:31:00.000000 - INFO - Portfolio({'available_cash': 998772.779, 'frozen_cash': 0.0, 'returns': -7.2210000000572094e-06, 'market_value': 1220.0, 'portfolio_value': 999992.779, 'starting_value': 1000000.0, 'pnl': -7.221000000057209, 'start_date': datetime.date(2023, 8, 1), 'positions': ['000001.SZ'], 'datetime': datetime.datetime(2023, 8, 1, 9, 31)})
2023-08-01 09:31:00.000000 - INFO - False
2023-08-01 09:31:00.000000 - INFO - ['000001.SZ']
2023-08-01 09:31:00.000000 - INFO - {'000001.SZ': StockPosition({'symbol': '000001.SZ', 'amount': 100, 'available_amount': 0, 'pnl': -7.221000000000011, 'market_value': 1220.0, 'cost_basis': 12.27221, 'last_price': 12.2, 'datetime': datetime.datetime(2023, 8, 1, 9, 31), 'position_days': 0, 'profit_rate': -0.00586015008671335, 'draw_down': 0.005860150086713367, 'markup': -0.009740259740259822, 'pre_price': 12.32})}
2023-08-01 09:31:00.000000 - INFO - StockPosition({'symbol': '000001.SZ', 'amount': 100, 'available_amount': 0, 'pnl': -7.221000000000011, 'market_value': 1220.0, 'cost_basis': 12.27221, 'last_price': 12.2, 'datetime': datetime.datetime(2023, 8, 1, 9, 31), 'position_days': 0, 'profit_rate': -0.00586015008671335, 'draw_down': 0.005860150086713367, 'markup': -0.009740259740259822, 'pre_price': 12.32})
2023-08-01 09:31:00.000000 - INFO - StockPosition({'symbol': '000001.SZ', 'amount': 100, 'available_amount': 0, 'pnl': -7.221000000000011, 'market_value': 1220.0, 'cost_basis': 12.27221, 'last_price': 12.2, 'datetime': datetime.datetime(2023, 8, 1, 9, 31), 'position_days': 0, 'profit_rate': -0.00586015008671335, 'draw_down': 0.005860150086713367, 'markup': -0.009740259740259822, 'pre_price': 12.32})
3.2. g
和context的功能类似,存储全局变量,自定义的全局变量推荐存在g里,避免将系统全局变量覆盖
In [5]:
source_code = """
def init(context):
g.abc=123
def handle_bar(context,bar_dict):
print(g.abc)
"""
In [6]:
btest = research_strategy(
source_code=source_code,start_date='20230801',end_date='20230801',
capital_base=float(1000000),frequency='MINUTE',stock_market='STOCK',
benchmark='000300.SH',plot=True
)
2023-08-01 09:31:00.000000 - INFO - 123
2023-08-01 09:32:00.000000 - INFO - 123
2023-08-01 09:33:00.000000 - INFO - 123
2023-08-01 09:34:00.000000 - INFO - 123
2023-08-01 09:35:00.000000 - INFO - 123
2023-08-01 09:36:00.000000 - INFO - 123
2023-08-01 09:37:00.000000 - INFO - 123
2023-08-01 09:38:00.000000 - INFO - 123
2023-08-01 09:39:00.000000 - INFO - 123
2023-08-01 09:40:00.000000 - INFO - 123
2023-08-01 09:41:00.000000 - INFO - 123
2023-08-01 09:42:00.000000 - INFO - 123
2023-08-01 09:43:00.000000 - INFO - 123
2023-08-01 09:44:00.000000 - INFO - 123
2023-08-01 09:45:00.000000 - INFO - 123
2023-08-01 09:46:00.000000 - INFO - 123
2023-08-01 09:47:00.000000 - INFO - 123
2023-08-01 09:48:00.000000 - INFO - 123
2023-08-01 09:49:00.000000 - INFO - 123
2023-08-01 09:50:00.000000 - INFO - 123
2023-08-01 09:51:00.000000 - INFO - 123
2023-08-01 09:52:00.000000 - INFO - 123
2023-08-01 09:53:00.000000 - INFO - 123
2023-08-01 09:54:00.000000 - INFO - 123
2023-08-01 09:55:00.000000 - INFO - 123
2023-08-01 09:56:00.000000 - INFO - 123
2023-08-01 09:57:00.000000 - INFO - 123
2023-08-01 09:58:00.000000 - INFO - 123
2023-08-01 09:59:00.000000 - INFO - 123
2023-08-01 10:00:00.000000 - INFO - 123
2023-08-01 10:01:00.000000 - INFO - 123
2023-08-01 10:02:00.000000 - INFO - 123
2023-08-01 10:03:00.000000 - INFO - 123
2023-08-01 10:04:00.000000 - INFO - 123
2023-08-01 10:05:00.000000 - INFO - 123
2023-08-01 10:06:00.000000 - INFO - 123
2023-08-01 10:07:00.000000 - INFO - 123
2023-08-01 10:08:00.000000 - INFO - 123
2023-08-01 10:09:00.000000 - INFO - 123
2023-08-01 10:10:00.000000 - INFO - 123
2023-08-01 10:11:00.000000 - INFO - 123
2023-08-01 10:12:00.000000 - INFO - 123
2023-08-01 10:13:00.000000 - INFO - 123
2023-08-01 10:14:00.000000 - INFO - 123
2023-08-01 10:15:00.000000 - INFO - 123
2023-08-01 10:16:00.000000 - INFO - 123
2023-08-01 10:17:00.000000 - INFO - 123
2023-08-01 10:18:00.000000 - INFO - 123
2023-08-01 10:19:00.000000 - INFO - 123
2023-08-01 10:20:00.000000 - INFO - 123
2023-08-01 10:21:00.000000 - INFO - 123
2023-08-01 10:22:00.000000 - INFO - 123
2023-08-01 10:23:00.000000 - INFO - 123
2023-08-01 10:24:00.000000 - INFO - 123
2023-08-01 10:25:00.000000 - INFO - 123
2023-08-01 10:26:00.000000 - INFO - 123
2023-08-01 10:27:00.000000 - INFO - 123
2023-08-01 10:28:00.000000 - INFO - 123
2023-08-01 10:29:00.000000 - INFO - 123
2023-08-01 10:30:00.000000 - INFO - 123
2023-08-01 10:31:00.000000 - INFO - 123
2023-08-01 10:32:00.000000 - INFO - 123
2023-08-01 10:33:00.000000 - INFO - 123
2023-08-01 10:34:00.000000 - INFO - 123
2023-08-01 10:35:00.000000 - INFO - 123
2023-08-01 10:36:00.000000 - INFO - 123
2023-08-01 10:37:00.000000 - INFO - 123
2023-08-01 10:38:00.000000 - INFO - 123
2023-08-01 10:39:00.000000 - INFO - 123
2023-08-01 10:40:00.000000 - INFO - 123
2023-08-01 10:41:00.000000 - INFO - 123
2023-08-01 10:42:00.000000 - INFO - 123
2023-08-01 10:43:00.000000 - INFO - 123
2023-08-01 10:44:00.000000 - INFO - 123
2023-08-01 10:45:00.000000 - INFO - 123
2023-08-01 10:46:00.000000 - INFO - 123
2023-08-01 10:47:00.000000 - INFO - 123
2023-08-01 10:48:00.000000 - INFO - 123
2023-08-01 10:49:00.000000 - INFO - 123
2023-08-01 10:50:00.000000 - INFO - 123
2023-08-01 10:51:00.000000 - INFO - 123
2023-08-01 10:52:00.000000 - INFO - 123
2023-08-01 10:53:00.000000 - INFO - 123
2023-08-01 10:54:00.000000 - INFO - 123
2023-08-01 10:55:00.000000 - INFO - 123
2023-08-01 10:56:00.000000 - INFO - 123
2023-08-01 10:57:00.000000 - INFO - 123
2023-08-01 10:58:00.000000 - INFO - 123
2023-08-01 10:59:00.000000 - INFO - 123
2023-08-01 11:00:00.000000 - INFO - 123
2023-08-01 11:01:00.000000 - INFO - 123
2023-08-01 11:02:00.000000 - INFO - 123
2023-08-01 11:03:00.000000 - INFO - 123
2023-08-01 11:04:00.000000 - INFO - 123
2023-08-01 11:05:00.000000 - INFO - 123
2023-08-01 11:06:00.000000 - INFO - 123
2023-08-01 11:07:00.000000 - INFO - 123
2023-08-01 11:08:00.000000 - INFO - 123
2023-08-01 11:09:00.000000 - INFO - 123
2023-08-01 11:10:00.000000 - INFO - 123
2023-08-01 11:11:00.000000 - INFO - 123
2023-08-01 11:12:00.000000 - INFO - 123
2023-08-01 11:13:00.000000 - INFO - 123
2023-08-01 11:14:00.000000 - INFO - 123
2023-08-01 11:15:00.000000 - INFO - 123
2023-08-01 11:16:00.000000 - INFO - 123
2023-08-01 11:17:00.000000 - INFO - 123
2023-08-01 11:18:00.000000 - INFO - 123
2023-08-01 11:19:00.000000 - INFO - 123
2023-08-01 11:20:00.000000 - INFO - 123
2023-08-01 11:21:00.000000 - INFO - 123
2023-08-01 11:22:00.000000 - INFO - 123
2023-08-01 11:23:00.000000 - INFO - 123
2023-08-01 11:24:00.000000 - INFO - 123
2023-08-01 11:25:00.000000 - INFO - 123
2023-08-01 11:26:00.000000 - INFO - 123
2023-08-01 11:27:00.000000 - INFO - 123
2023-08-01 11:28:00.000000 - INFO - 123
2023-08-01 11:29:00.000000 - INFO - 123
2023-08-01 11:30:00.000000 - INFO - 123
2023-08-01 13:01:00.000000 - INFO - 123
2023-08-01 13:02:00.000000 - INFO - 123
2023-08-01 13:03:00.000000 - INFO - 123
2023-08-01 13:04:00.000000 - INFO - 123
2023-08-01 13:05:00.000000 - INFO - 123
2023-08-01 13:06:00.000000 - INFO - 123
2023-08-01 13:07:00.000000 - INFO - 123
2023-08-01 13:08:00.000000 - INFO - 123
2023-08-01 13:09:00.000000 - INFO - 123
2023-08-01 13:10:00.000000 - INFO - 123
2023-08-01 13:11:00.000000 - INFO - 123
2023-08-01 13:12:00.000000 - INFO - 123
2023-08-01 13:13:00.000000 - INFO - 123
2023-08-01 13:14:00.000000 - INFO - 123
2023-08-01 13:15:00.000000 - INFO - 123
2023-08-01 13:16:00.000000 - INFO - 123
2023-08-01 13:17:00.000000 - INFO - 123
2023-08-01 13:18:00.000000 - INFO - 123
2023-08-01 13:19:00.000000 - INFO - 123
2023-08-01 13:20:00.000000 - INFO - 123
2023-08-01 13:21:00.000000 - INFO - 123
2023-08-01 13:22:00.000000 - INFO - 123
2023-08-01 13:23:00.000000 - INFO - 123
2023-08-01 13:24:00.000000 - INFO - 123
2023-08-01 13:25:00.000000 - INFO - 123
2023-08-01 13:26:00.000000 - INFO - 123
2023-08-01 13:27:00.000000 - INFO - 123
2023-08-01 13:28:00.000000 - INFO - 123
2023-08-01 13:29:00.000000 - INFO - 123
2023-08-01 13:30:00.000000 - INFO - 123
2023-08-01 13:31:00.000000 - INFO - 123
2023-08-01 13:32:00.000000 - INFO - 123
2023-08-01 13:33:00.000000 - INFO - 123
2023-08-01 13:34:00.000000 - INFO - 123
2023-08-01 13:35:00.000000 - INFO - 123
2023-08-01 13:36:00.000000 - INFO - 123
2023-08-01 13:37:00.000000 - INFO - 123
2023-08-01 13:38:00.000000 - INFO - 123
2023-08-01 13:39:00.000000 - INFO - 123
2023-08-01 13:40:00.000000 - INFO - 123
2023-08-01 13:41:00.000000 - INFO - 123
2023-08-01 13:42:00.000000 - INFO - 123
2023-08-01 13:43:00.000000 - INFO - 123
2023-08-01 13:44:00.000000 - INFO - 123
2023-08-01 13:45:00.000000 - INFO - 123
2023-08-01 13:46:00.000000 - INFO - 123
2023-08-01 13:47:00.000000 - INFO - 123
2023-08-01 13:48:00.000000 - INFO - 123
2023-08-01 13:49:00.000000 - INFO - 123
2023-08-01 13:50:00.000000 - INFO - 123
2023-08-01 13:51:00.000000 - INFO - 123
2023-08-01 13:52:00.000000 - INFO - 123
2023-08-01 13:53:00.000000 - INFO - 123
2023-08-01 13:54:00.000000 - INFO - 123
2023-08-01 13:55:00.000000 - INFO - 123
2023-08-01 13:56:00.000000 - INFO - 123
2023-08-01 13:57:00.000000 - INFO - 123
2023-08-01 13:58:00.000000 - INFO - 123
2023-08-01 13:59:00.000000 - INFO - 123
2023-08-01 14:00:00.000000 - INFO - 123
2023-08-01 14:01:00.000000 - INFO - 123
2023-08-01 14:02:00.000000 - INFO - 123
2023-08-01 14:03:00.000000 - INFO - 123
2023-08-01 14:04:00.000000 - INFO - 123
2023-08-01 14:05:00.000000 - INFO - 123
2023-08-01 14:06:00.000000 - INFO - 123
2023-08-01 14:07:00.000000 - INFO - 123
2023-08-01 14:08:00.000000 - INFO - 123
2023-08-01 14:09:00.000000 - INFO - 123
2023-08-01 14:10:00.000000 - INFO - 123
2023-08-01 14:11:00.000000 - INFO - 123
2023-08-01 14:12:00.000000 - INFO - 123
2023-08-01 14:13:00.000000 - INFO - 123
2023-08-01 14:14:00.000000 - INFO - 123
2023-08-01 14:15:00.000000 - INFO - 123
2023-08-01 14:16:00.000000 - INFO - 123
2023-08-01 14:17:00.000000 - INFO - 123
2023-08-01 14:18:00.000000 - INFO - 123
2023-08-01 14:19:00.000000 - INFO - 123
2023-08-01 14:20:00.000000 - INFO - 123
2023-08-01 14:21:00.000000 - INFO - 123
2023-08-01 14:22:00.000000 - INFO - 123
2023-08-01 14:23:00.000000 - INFO - 123
2023-08-01 14:24:00.000000 - INFO - 123
2023-08-01 14:25:00.000000 - INFO - 123
2023-08-01 14:26:00.000000 - INFO - 123
2023-08-01 14:27:00.000000 - INFO - 123
2023-08-01 14:28:00.000000 - INFO - 123
2023-08-01 14:29:00.000000 - INFO - 123
2023-08-01 14:30:00.000000 - INFO - 123
2023-08-01 14:31:00.000000 - INFO - 123
2023-08-01 14:32:00.000000 - INFO - 123
2023-08-01 14:33:00.000000 - INFO - 123
2023-08-01 14:34:00.000000 - INFO - 123
2023-08-01 14:35:00.000000 - INFO - 123
2023-08-01 14:36:00.000000 - INFO - 123
2023-08-01 14:37:00.000000 - INFO - 123
2023-08-01 14:38:00.000000 - INFO - 123
2023-08-01 14:39:00.000000 - INFO - 123
2023-08-01 14:40:00.000000 - INFO - 123
2023-08-01 14:41:00.000000 - INFO - 123
2023-08-01 14:42:00.000000 - INFO - 123
2023-08-01 14:43:00.000000 - INFO - 123
2023-08-01 14:44:00.000000 - INFO - 123
2023-08-01 14:45:00.000000 - INFO - 123
2023-08-01 14:46:00.000000 - INFO - 123
2023-08-01 14:47:00.000000 - INFO - 123
2023-08-01 14:48:00.000000 - INFO - 123
2023-08-01 14:49:00.000000 - INFO - 123
2023-08-01 14:50:00.000000 - INFO - 123
2023-08-01 14:51:00.000000 - INFO - 123
2023-08-01 14:52:00.000000 - INFO - 123
2023-08-01 14:53:00.000000 - INFO - 123
2023-08-01 14:54:00.000000 - INFO - 123
2023-08-01 14:55:00.000000 - INFO - 123
2023-08-01 14:56:00.000000 - INFO - 123
2023-08-01 14:57:00.000000 - INFO - 123
2023-08-01 14:58:00.000000 - INFO - 123
2023-08-01 14:59:00.000000 - INFO - 123
2023-08-01 15:00:00.000000 - INFO - 123
3.3. 委托对象order
on_order函数推送,或使用get_order/get_orders/get_open_orders函数查询
- order_id:委托订单id
- symbol:标的代码
- created:订单创建时间
- order_type:订单类型LONG/SHORT
- limit_price:限价,市价单为0
- amount:委托数量
- filled_amount:成交数量
- type:委托类型(市价/限价)
- transaction_cost:交易成本
- avg_price:平均成交价
- status:委托状态
- ORDER_STATUS.PENDING_NEW:初始化中
- ORDER_STATUS.ACTIVE:活动中(未成交、部分成交)
- ORDER_STATUS.FILLED:已完成(全部成交)
- ORDER_STATUS.REJECTED:废单
- ORDER_STATUS.CANCELLED:已撤单(全撤、部成部撤)
- datetime:当前时间
- offset_flag:延迟成交信息
3.4. 成交对象trade
on_trade函数推送,或使用get_tradelogs函数查询
- order_id:委托订单id
- order_book_id:标的代码
- trading_datetime:成交时间
- side:订单类型SIDE.BUY/SIDE.SELL
- last_price:成交价(包含交易成本在内)
- last_quantity:成交数量
- transaction_cost:交易成本
- commission:手续费
- tax:印花税
- datetime:当前时间
3.5. bar_dict对象
In [7]:
source_code = """
def init(context):
g.fired = True
def handle_bar(context,bar_dict):
if g.fired:
log.info(bar_dict['000001.SZ'])
log.info(bar_dict['000001.SZ'].datetime)
log.info(bar_dict['000001.SZ'].close)
g.fired=False
"""
In [8]:
btest = research_strategy(
source_code=source_code,start_date='20230801',end_date='20230801',
capital_base=float(1000000),frequency='MINUTE',stock_market='STOCK',
benchmark='000300.SH',plot=True
)
2023-08-01 09:31:00.000000 - INFO - Bar(symbol: '000001.SZ', datetime: datetime.datetime(2023, 8, 1, 9, 31), high_limit: 13.55, low_limit: 11.09, prev_close: 12.32, avg_price: 12.221932316118936, is_st: False, is_paused: False, open: 12.26, close: 12.2, volume: 2556000.0, turnover: 31239259.0, is_paused: False)
2023-08-01 09:31:00.000000 - INFO - 2023-08-01 09:31:00
2023-08-01 09:31:00.000000 - INFO - 12.2
4.常用数据接口
4.1 历史行情数据
调用方式:history(symbol_list, fields, bar_count, fre_step, skip_paused = False, fq = 'pre', is_panel=0)
取当前K线之前的{bar_dict}根K线的数据
- symbol_list:股票代码,list/str
- fields:字段,list,支持字段可参考日频行情,分钟行情
- bar_count:向前取多少根K线的数据
- fre_step:步长,常用为'1d'(1天),'1m'(1分钟)
- skip_paused:是否跳过停牌
- fq:
- fq = None:不复权
- fq = 'post':后复权
- fq = 'pre':前复权(以当前K线作为基准向前复权)
- is_panel:
- False: 返回格式为key为股票代码,value为dataframe的字典
- True: 返回panel格式
In [9]:
source_code = '''
def init(context):
pass
def before_trading(context):
ohlcv = history(['000001.SZ','600519.SH'], ['open','high','low','close','volume'], 5, '1d', skip_paused = False, fq = 'pre', is_panel=0)
log.info(ohlcv)
ohlcv = history(['000001.SZ','600519.SH'], ['open','high','low','close','volume'], 5, '1d', skip_paused = False, fq = 'pre', is_panel=1)
log.info(ohlcv)
log.info(ohlcv.to_frame())
'''
In [10]:
btest = research_strategy(
source_code=source_code,start_date='20230801',end_date='20230801',
capital_base=float(1000000),frequency='MINUTE',stock_market='STOCK',
benchmark='000300.SH',plot=True
)
2023-08-01 09:00:00.000000 - INFO - {'000001.SZ': open high low close volume
2023-07-25 11.45 11.73 11.44 11.72 203295819.0
2023-07-26 11.65 11.74 11.56 11.67 75166268.0
2023-07-27 11.68 11.87 11.65 11.75 111519586.0
2023-07-28 11.69 12.30 11.67 12.26 234701346.0
2023-07-31 12.30 12.50 12.23 12.32 185495239.0, '600519.SH': open high low close volume
2023-07-25 1800.00 1828.88 1799.99 1828.55 4308646.0
2023-07-26 1828.55 1835.99 1815.08 1828.55 1961145.0
2023-07-27 1836.00 1854.79 1828.70 1838.03 2034043.0
2023-07-28 1832.00 1900.00 1828.01 1897.00 3901792.0
2023-07-31 1908.00 1935.00 1874.00 1880.58 4537861.0}
2023-08-01 09:00:00.000000 - INFO - <class 'pd_Panel'>
Dimensions: 5 (items) x 5 (major_axis) x 2 (minor_axis)
Items axis: open to volume
Major_axis axis: 2023-07-25T00:00:00.000000000 to 2023-07-31T00:00:00.000000000
Minor_axis axis: 000001.SZ to 600519.SH
2023-08-01 09:00:00.000000 - INFO - items open high low close volume
major minor
2023-07-25 000001.SZ 11.45 11.73 11.44 11.72 203295819.0
600519.SH 1800.00 1828.88 1799.99 1828.55 4308646.0
2023-07-26 000001.SZ 11.65 11.74 11.56 11.67 75166268.0
600519.SH 1828.55 1835.99 1815.08 1828.55 1961145.0
2023-07-27 000001.SZ 11.68 11.87 11.65 11.75 111519586.0
600519.SH 1836.00 1854.79 1828.70 1838.03 2034043.0
2023-07-28 000001.SZ 11.69 12.30 11.67 12.26 234701346.0
600519.SH 1832.00 1900.00 1828.01 1897.00 3901792.0
2023-07-31 000001.SZ 12.30 12.50 12.23 12.32 185495239.0
600519.SH 1908.00 1935.00 1874.00 1880.58 4537861.0
4.2 表数据
4.2.1 query对象
这边只讲简单的使用方法,具体可以参考官方文档继续学习sqlalchemy文档
表与字段
In [11]:
q = query(
concept_classification#表名
)
In [ ]:
run_query(q) #取整张表太大了,不进行演示
In [12]:
q = query(
concept_classification.symbol,#表名.字段
concept_classification.date,
concept_classification.concept
)
filter设置条件
相当于SQL中的where
In [13]:
run_query(
q.filter(
concept_classification.symbol=='300033.SZ',
concept_classification.date>'20230201'
)
)
Out[13]:
concept_classification_symbol | concept_classification_date | concept_classification_concept | |
---|---|---|---|
0 | 300033.SZ | 20230202 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
1 | 300033.SZ | 20230203 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
2 | 300033.SZ | 20230206 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
3 | 300033.SZ | 20230207 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
4 | 300033.SZ | 20230208 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
... | ... | ... | ... |
118 | 300033.SZ | 20230726 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
119 | 300033.SZ | 20230727 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
120 | 300033.SZ | 20230728 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
121 | 300033.SZ | 20230731 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
122 | 300033.SZ | 20230801 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
123 rows × 3 columns
In [14]:
run_query(
q.filter(
concept_classification.symbol.in_(['300033.SZ','600519.SH']),
concept_classification.date=='20230201'
)
)
Out[14]:
concept_classification_symbol | concept_classification_date | concept_classification_concept | |
---|---|---|---|
0 | 300033.SZ | 20230201 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
1 | 600519.SH | 20230201 | 同花顺漂亮100,超级品牌,富时罗素概念,融资融券,国企改革,MSCI概念,央视财经50,标... |
指定查前几条数据:limit
In [15]:
run_query(
q.filter(
concept_classification.symbol=='300033.SZ',
concept_classification.date>'20230201'
).limit(10)
)
Out[15]:
concept_classification_symbol | concept_classification_date | concept_classification_concept | |
---|---|---|---|
0 | 300033.SZ | 20230202 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
1 | 300033.SZ | 20230203 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
2 | 300033.SZ | 20230206 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
3 | 300033.SZ | 20230207 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
4 | 300033.SZ | 20230208 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
5 | 300033.SZ | 20230209 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
6 | 300033.SZ | 20230210 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
7 | 300033.SZ | 20230213 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
8 | 300033.SZ | 20230214 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
9 | 300033.SZ | 20230215 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
排序:order_by
In [16]:
run_query(
q.filter(
concept_classification.symbol=='300033.SZ',
concept_classification.date>'20230201'
).order_by(
concept_classification.date.desc()
).limit(10)
)
Out[16]:
concept_classification_symbol | concept_classification_date | concept_classification_concept | |
---|---|---|---|
0 | 300033.SZ | 20230801 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
1 | 300033.SZ | 20230731 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
2 | 300033.SZ | 20230728 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
3 | 300033.SZ | 20230727 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
4 | 300033.SZ | 20230726 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
5 | 300033.SZ | 20230725 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
6 | 300033.SZ | 20230724 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
7 | 300033.SZ | 20230721 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
8 | 300033.SZ | 20230720 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
9 | 300033.SZ | 20230719 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
In [17]:
run_query(
q.filter(
concept_classification.symbol=='300033.SZ',
concept_classification.date>'20230201'
).order_by(
concept_classification.date.asc()
).limit(10)
)
Out[17]:
concept_classification_symbol | concept_classification_date | concept_classification_concept | |
---|---|---|---|
0 | 300033.SZ | 20230202 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
1 | 300033.SZ | 20230203 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
2 | 300033.SZ | 20230206 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
3 | 300033.SZ | 20230207 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
4 | 300033.SZ | 20230208 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
5 | 300033.SZ | 20230209 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
6 | 300033.SZ | 20230210 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
7 | 300033.SZ | 20230213 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
8 | 300033.SZ | 20230214 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
9 | 300033.SZ | 20230215 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
In [18]:
run_query(
q.filter(
concept_classification.symbol=='300033.SZ',
concept_classification.date>'20230201'
).order_by(
concept_classification.date
).limit(10)
)
Out[18]:
concept_classification_symbol | concept_classification_date | concept_classification_concept | |
---|---|---|---|
0 | 300033.SZ | 20230202 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
1 | 300033.SZ | 20230203 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
2 | 300033.SZ | 20230206 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
3 | 300033.SZ | 20230207 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
4 | 300033.SZ | 20230208 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
5 | 300033.SZ | 20230209 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
6 | 300033.SZ | 20230210 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
7 | 300033.SZ | 20230213 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
8 | 300033.SZ | 20230214 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
9 | 300033.SZ | 20230215 | 电子商务,证金持股,融资融券,基金三方销售,机器人概念,ChatGPT概念,金融科技,转融券... |
4.2.2 财务数据表
调用方式: get_fundamentals(query_object, date=None, statDate=None, latest=False)
- query_object:query对象
- date:查询指定日期的财务数据
- statDate:查询指定季度或年份的财务数据
- latest:是否取最新报告期
In [19]:
#获取全市场2022年报资产负债表中的总资产数据,仅取合并报表
data = get_fundamentals(
query(
balance.symbol,
balance.report_date,
balance.stat_date,
balance.total_assets
).filter(
balance.reporttypecode == 'HB'
),
statDate = '2022q4'
)
In [20]:
data
Out[20]:
balance_stat_symbol | balance_stat_report_date | balance_stat_stat_date | balance_stat_total_assets | |
---|---|---|---|---|
0 | 000001.SZ | 2023-03-09 | 2022-12-31 | 5.321514e+12 |
1 | 000002.SZ | 2023-03-31 | 2022-12-31 | 1.757124e+12 |
2 | 000003.SZ | 2023-04-28 | 2022-12-31 | 4.733694e+07 |
3 | 000004.SZ | 2023-04-29 | 2022-12-31 | 5.753907e+08 |
4 | 000005.SZ | 2023-04-29 | 2022-12-31 | 2.602190e+09 |
... | ... | ... | ... | ... |
5356 | 873305.BJ | 2023-04-26 | 2022-12-31 | 3.578005e+08 |
5357 | 873339.BJ | 2023-04-12 | 2022-12-31 | 7.227376e+08 |
5358 | 873527.BJ | 2023-04-17 | 2022-12-31 | 5.867523e+08 |
5359 | 873576.BJ | 2023-03-02 | 2022-12-31 | 6.605258e+08 |
5360 | 873593.BJ | 2023-02-24 | 2022-12-31 | 3.661453e+08 |
5361 rows × 4 columns
In [21]:
#获取全市场2023年2月1日全市场股票最近一期资产负债表中的总资产数据,取最新报告数据
data = get_fundamentals(
query(
balance.symbol,
balance.date,
balance.report_date,
balance.stat_date,
balance.total_assets
),
date = '20230201',
latest = True
)
In [22]:
data
Out[22]:
balance_symbol | balance_date | balance_report_date | balance_stat_date | balance_total_assets | |
---|---|---|---|---|---|
0 | 000001.SZ | 2023-02-01 | 2022-10-25 | 2022-09-30 | 5.195133e+12 |
1 | 000002.SZ | 2023-02-01 | 2022-10-29 | 2022-09-30 | 1.832624e+12 |
2 | 000004.SZ | 2023-02-01 | 2022-10-28 | 2022-09-30 | 1.053263e+09 |
3 | 000005.SZ | 2023-02-01 | 2022-10-31 | 2022-09-30 | 2.359511e+09 |
4 | 000006.SZ | 2023-02-01 | 2022-10-28 | 2022-09-30 | 2.631976e+10 |
... | ... | ... | ... | ... | ... |
5073 | 873169.BJ | 2023-02-01 | 2022-10-28 | 2022-09-30 | 3.459986e+08 |
5074 | 873223.BJ | 2023-02-01 | 2022-10-26 | 2022-09-30 | 4.906200e+08 |
5075 | 873305.BJ | 2023-02-01 | 2022-10-20 | 2022-09-30 | 2.281411e+08 |
5076 | 873339.BJ | 2023-02-01 | 2022-10-17 | 2022-09-30 | 6.065306e+08 |
5077 | 873527.BJ | 2023-02-01 | 2022-10-27 | 2022-09-30 | 4.367683e+08 |
5078 rows × 5 columns
5.下单与撤单
5.1下单
跟据绝对数量下单
- order(id_or_ins,amount,price=None) 指定数量下单
- order_value(id_or_ins,cash_amount,price=None) 指定金额下单,自动跟据金额与价格计算下单数量,并下单
- order_percent(id_or_ins,percent,price=None) 指定总资产占比下单,自动跟据比例、总计产、价格计算下单数量,并下单
跟据目标值下单,可能会下0股,导致日志出现warn
- order_target(id_or_ins,amount,price=None) 设定目标数量下单,跟据目标数量和当前实际持有数量计算下单数量
- order_target_value(id_or_ins,cash_amount,price=None) 设定目标金额下单,跟据目标金额和当前实际持有市值计算下单数量
- order_target_percent(id_or_ins,percent,price=None) 设定目标持仓占比,跟据目标持仓占比和当前持仓占计算下单数量
下单成功,返回订单委托id,失败则返回None
在回测中,以市价单买入涨停股或者卖出跌停股,订单会作为失败处理,而限价单会挂着
5.2 撤单
指定id撤单 cancel_order(order_id)
全撤 cancel_order_all()
In [23]:
source_code = '''
def init(context):
g.fired = True
def handle_bar(context,bar_dict):
if g.fired:
order_id = order('000001.SZ',100,bar_dict['000001.SZ'].close*0.98)
cancel_order(order_id)
g.fired = False
def on_order(context,odr):
print(odr)
'''
In [24]:
btest = research_strategy(
source_code=source_code,start_date='20230801',end_date='20230801',
capital_base=float(1000000),frequency='MINUTE',stock_market='STOCK',
benchmark='000300.SH',plot=True
)
2023-08-01 09:31:00.000000 - INFO - Order({'id': '5d091e5c20174815a3cc840cd5f6c8f9', 'symbol': '000001.SZ', 'created': datetime.datetime(2023, 8, 1, 9, 31), 'order_type': 'LONG', 'limit_price': 11.956, 'amount': 100, 'filled_amount': 0, 'type': ORDER_TYPE.LIMIT, 'transaction_cost': 0, 'avg_price': 0, 'status': ORDER_STATUS.PENDING_NEW, 'datetime': datetime.datetime(2023, 8, 1, 9, 31), 'offset_flag': None})
2023-08-01 09:31:00.000000 - INFO - Order({'id': '5d091e5c20174815a3cc840cd5f6c8f9', 'symbol': '000001.SZ', 'created': datetime.datetime(2023, 8, 1, 9, 31), 'order_type': 'LONG', 'limit_price': 11.956, 'amount': 100, 'filled_amount': 0, 'type': ORDER_TYPE.LIMIT, 'transaction_cost': 0, 'avg_price': 0, 'status': ORDER_STATUS.ACTIVE, 'datetime': datetime.datetime(2023, 8, 1, 9, 31), 'offset_flag': None})
2023-08-01 09:31:00.000000 - INFO - Order({'id': '5d091e5c20174815a3cc840cd5f6c8f9', 'symbol': '000001.SZ', 'created': datetime.datetime(2023, 8, 1, 9, 31), 'order_type': 'LONG', 'limit_price': 11.956, 'amount': 100, 'filled_amount': 0, 'type': ORDER_TYPE.LIMIT, 'transaction_cost': 0, 'avg_price': 0, 'status': ORDER_STATUS.ACTIVE, 'datetime': datetime.datetime(2023, 8, 1, 9, 31), 'offset_flag': None})
2023-08-01 09:31:00.000000 - WARN - 订单 5d091e5c20174815a3cc840cd5f6c8f9 被取消。
2023-08-01 09:31:00.000000 - INFO - Order({'id': '5d091e5c20174815a3cc840cd5f6c8f9', 'symbol': '000001.SZ', 'created': datetime.datetime(2023, 8, 1, 9, 31), 'order_type': 'LONG', 'limit_price': 11.956, 'amount': 100, 'filled_amount': 0, 'type': ORDER_TYPE.LIMIT, 'transaction_cost': 0, 'avg_price': 0, 'status': ORDER_STATUS.CANCELLED, 'datetime': datetime.datetime(2023, 8, 1, 9, 31), 'offset_flag': None})
In [ ]:
In [ ]:
In [ ]:
标题:同花顺SuperMind 股票策略编写必知必会
作者:michael
地址:https://blog.junxworks.cn/articles/2023/10/23/1698023555540.html