在正式学习编写Ptrade策略之前,我们先看一个简单的示例策略。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'''
示例策略:一个简化的网格策略
(1)如果空仓,则买入159919.SZ这只ETF,买入账户总资产的30%。
(2)有持仓时,如果最新价较买入价下跌了10%,则加仓。加仓的数量与首次买入的数量一致,仅加仓一次。
(3)有持仓时,如果最新价较首次买入价上涨了10%,则清仓。
'''

def initialize(context):
"""初始化策略参数"""
g.stock = '159919.SZ' # 交易标的
# 初始化状态变量
g.first_buy_price = None # 首次买入价格
g.first_buy_amount = None # 首次买入股数
g.has_added = False # 是否已执行过加仓


def handle_data(context, data):
"""每日主逻辑"""
# 获取当前最新价(收盘价)
current_price = data[g.stock].close

# 获取当前持仓
position = context.portfolio.positions[g.stock]
current_amount = position.amount if position else 0

# 情况1:空仓 —— 首次买入30%仓位
if current_amount == 0:
# 按总资产的30%下单买入
target_value = context.portfolio.total_value * 0.3
print(f'当前没有持仓。下单,以市价买入总仓位的30%(约¥{target_value:.2f})。')
order_target_value(g.stock, target_value)
# 记录首次买入价格和股数
position = context.portfolio.positions[g.stock]
current_amount = position.amount if position else 0
g.first_buy_price = position.cost_basis
g.first_buy_amount = position.amount
print(f'已完成下单,平均买入成本:¥{g.first_buy_price:.2f},买入数量:{g.first_buy_amount}')

# 情况2:有持仓
elif current_amount > 0:
# 最新价较首次买入价下跌 ≥ 10%,加仓(仅加仓一次)
if not g.has_added and current_price <= g.first_buy_price * 0.90:
print(f'当前的最新价(¥{current_price:.2f})较首次买入价(¥{g.first_buy_price:.2f})下跌了10%,满足加仓要求。')
order(g.stock, g.first_buy_amount) # 买入与首次相同的股数
g.has_added = True # 将状态标记为已加仓
print(f'已完成加仓,买入价格:¥{current_price:.2f},买入数量:{g.first_buy_amount}')

# 最新价较首次买入价上涨 ≥ 10%,清仓
if current_price >= g.first_buy_price * 1.10:
print(f'当前的最新价(¥{current_price:.2f})较首次买入价(¥{g.first_buy_price:.2f})上涨了10%,满足清仓要求。')
order_target(g.stock, 0) # 全部卖出
# 重置所有状态变量
g.first_buy_price = None
g.first_buy_amount = None
g.has_added = False
print(f'已完成清仓,卖出数量:{current_amount}')

else:
print(f'判断持仓数量时出现预期外的情形。参考:current_amount={current_amount}')

  这个简化的策略包含了几个常用的功能模块,包括买入条件、卖出条件、仓位控制等。

  该策略2020/01/01至2020/12/30的回测结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2026-06-15 22:11:33 开始运行回测, 策略名称: test2
2020-01-02 15:00:00 - INFO - 当前没有持仓。下单,以市价买入总仓位的30%(约¥30000.00)。
2020-01-02 15:00:00 - INFO - 生成订单,订单号:a3083a80cfd2415192ff47682bd7df09,股票代码:159919.XSHE,数量:买入7100股
2020-01-02 15:00:00 - INFO - 已完成下单,平均买入成本:¥4.21,买入数量:7100
2020-02-03 15:00:00 - INFO - 当前的最新价(¥3.73)较首次买入价(¥4.21)下跌了10%,满足加仓要求。
2020-02-03 15:00:00 - INFO - 生成订单,订单号:a908057ce38545fdabe41de929b245e2,股票代码:159919.XSHE,数量:买入7100股
2020-02-03 15:00:00 - INFO - 已完成加仓,买入价格:¥3.73,买入数量:7100
2020-07-06 15:00:00 - INFO - 当前的最新价(¥4.88)较首次买入价(¥4.21)上涨了10%,满足清仓要求。
2020-07-06 15:00:00 - INFO - 生成订单,订单号:1b3ff49df63d4233b2b2c4dd6671bddb,股票代码:159919.XSHE,数量:卖出14200股
2020-07-06 15:00:00 - INFO - 已完成清仓,卖出数量:14200
2020-07-07 15:00:00 - INFO - 当前没有持仓。下单,以市价买入总仓位的30%(约¥33818.75)。
2020-07-07 15:00:00 - INFO - 生成订单,订单号:1bc71a2d4cd7458d90fc11da56107612,股票代码:159919.XSHE,数量:买入7000股
2020-07-07 15:00:00 - INFO - 已完成下单,平均买入成本:¥4.80,买入数量:7000
2020-12-30 15:30:00 - INFO - 正在进行回测统计数据汇总
2020-12-30 15:30:00 - INFO - 回测统计数据汇总完成
2026-06-15 22:11:47 - INFO - 回测结束,结束类型:程序结束
2026-06-15 22:11:49 策略回测结束

  从下一篇文章起,阿猪将以这个示例策略为切入点,逐步讲解编写Ptrade量化策略的入门知识点。