Repository navigation
Expand file tree
/
Copy pathunit_test.py
More file actions
148 lines (122 loc) · 6.47 KB
/
Copy pathunit_test.py
File metadata and controls
148 lines (122 loc) · 6.47 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import unittest
from unittest.mock import patch, MagicMock, Mock
from datetime import datetime
import sys
import os
# 获取当前文件所在目录的父目录(即项目根目录)
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
# 将项目根目录添加到sys.path
sys.path.insert(0, project_root)
import config
from position_manager import PositionManager, get_position_manager
class TestPositionManagerCalculateStopLossPrice(unittest.TestCase):
"""测试PositionManager的calculate_stop_loss_price方法"""
def setUp(self):
"""测试前的准备工作"""
# 创建一个PositionManager实例
self.position_manager = get_position_manager()
# 清空持仓数据
self.position_manager.conn.execute("DELETE FROM positions")
self.position_manager.conn.commit()
# 重置配置参数
config.STOP_LOSS_RATIO = -0.095
config.INITIAL_TAKE_PROFIT_RATIO = 0.05
config.ENABLE_DYNAMIC_STOP_PROFIT = True
config.DYNAMIC_TAKE_PROFIT = [
(0.05, 0.96), # 建仓后最高价涨幅曾大于5%时,止盈位为最高价*96%
(0.10, 0.93), # 建仓后最高价涨幅曾大于10%时,止盈位为最高价*93%
(0.15, 0.90), # 建仓后最高价涨幅曾大于15%时,止盈位为最高价*90%
(0.30, 0.87), # 建仓后最高价涨幅曾大于30%时,止盈位为最高价*87%
(0.40, 0.85) # 建仓后最高价涨幅曾大于40%时,止盈位为最高价*85%
]
def tearDown(self):
"""测试后的清理工作"""
# 清空持仓数据
self.position_manager.conn.execute("DELETE FROM positions")
self.position_manager.conn.commit()
# 重置配置参数
config.STOP_LOSS_RATIO = -0.095
config.INITIAL_TAKE_PROFIT_RATIO = 0.05
config.ENABLE_DYNAMIC_STOP_PROFIT = True
config.DYNAMIC_TAKE_PROFIT = [
(0.05, 0.96), # 建仓后最高价涨幅曾大于5%时,止盈位为最高价*96%
(0.10, 0.93), # 建仓后最高价涨幅曾大于10%时,止盈位为最高价*93%
(0.15, 0.90), # 建仓后最高价涨幅曾大于15%时,止盈位为最高价*90%
(0.30, 0.87), # 建仓后最高价涨幅曾大于30%时,止盈位为最高价*87%
(0.40, 0.85) # 建仓后最高价涨幅曾大于40%时,止盈位为最高价*85%
]
def test_calculate_stop_loss_price_fixed(self):
"""测试固定止损"""
config.STOP_LOSS_RATIO = -0.10
cost_price = 100.0
highest_price = 100.0
profit_triggered = False
stop_loss_price = self.position_manager.calculate_stop_loss_price(cost_price, highest_price, profit_triggered)
self.assertAlmostEqual(stop_loss_price, 90.0,places=2)
def test_calculate_stop_loss_price_dynamic_level1(self):
"""测试动态止损-第一级"""
cost_price = 100.0
highest_price = 105.0 # 盈利5%
profit_triggered = True
stop_loss_price = self.position_manager.calculate_stop_loss_price(cost_price, highest_price, profit_triggered)
self.assertAlmostEqual(stop_loss_price, 100.8,places=2) #105*0.96
def test_calculate_stop_loss_price_dynamic_level2(self):
"""测试动态止损-第二级"""
cost_price = 100.0
highest_price = 110.0 # 盈利10%
profit_triggered = True
stop_loss_price = self.position_manager.calculate_stop_loss_price(cost_price, highest_price, profit_triggered)
self.assertAlmostEqual(stop_loss_price, 102.3,places=2) #110*0.93
def test_calculate_stop_loss_price_dynamic_level3(self):
"""测试动态止损-第三级"""
cost_price = 100.0
highest_price = 115.0 # 盈利15%
profit_triggered = True
stop_loss_price = self.position_manager.calculate_stop_loss_price(cost_price, highest_price, profit_triggered)
self.assertAlmostEqual(stop_loss_price, 103.5,places=2) #115*0.90
def test_calculate_stop_loss_price_dynamic_level4(self):
"""测试动态止损-第四级"""
cost_price = 100.0
highest_price = 130.0 # 盈利30%
profit_triggered = True
stop_loss_price = self.position_manager.calculate_stop_loss_price(cost_price, highest_price, profit_triggered)
self.assertAlmostEqual(stop_loss_price, 113.1,places=2) #130*0.87
def test_calculate_stop_loss_price_dynamic_level5(self):
"""测试动态止损-第五级"""
cost_price = 100.0
highest_price = 150.0 # 盈利50%
profit_triggered = True
stop_loss_price = self.position_manager.calculate_stop_loss_price(cost_price, highest_price, profit_triggered)
self.assertAlmostEqual(stop_loss_price, 127.5,places=2) #150*0.85
def test_calculate_stop_loss_price_dynamic_no_profit(self):
"""测试动态止损-未盈利"""
cost_price = 100.0
highest_price = 90.0
profit_triggered = True
stop_loss_price = self.position_manager.calculate_stop_loss_price(cost_price, highest_price, profit_triggered)
self.assertAlmostEqual(stop_loss_price, 87.3,places=2) #90*0.97
def test_calculate_stop_loss_price_dynamic_no_profit_triggered(self):
"""测试动态止损-未触发止盈"""
config.STOP_LOSS_RATIO = -0.10
cost_price = 100.0
highest_price = 110.0
profit_triggered = False
stop_loss_price = self.position_manager.calculate_stop_loss_price(cost_price, highest_price, profit_triggered)
self.assertAlmostEqual(stop_loss_price, 90.0,places=2)
def test_calculate_stop_loss_price_zero_cost(self):
"""测试成本为0的情况"""
cost_price = 0.0
highest_price = 110.0
profit_triggered = False
stop_loss_price = self.position_manager.calculate_stop_loss_price(cost_price, highest_price, profit_triggered)
self.assertAlmostEqual(stop_loss_price, 0.0,places=2)
def test_calculate_stop_loss_price_dynamic_empty_config(self):
"""测试动态止损配置为空的情况"""
config.DYNAMIC_TAKE_PROFIT = []
cost_price = 100.0
highest_price = 110.0
profit_triggered = True
stop_loss_price = self.position_manager.calculate_stop_loss_price(cost_price, highest_price, profit_triggered)
self.assertAlmostEqual(stop_loss_price, 106.7,places=2) #110*0.97
if __name__ == '__main__':
unittest.main()