-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_integration_manual.py
More file actions
294 lines (229 loc) · 9.51 KB
/
Copy pathtest_integration_manual.py
File metadata and controls
294 lines (229 loc) · 9.51 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
"""
Manual integration test for the migration.
Tests TradingPosition with real-world scenarios.
"""
from python_trading_objects.domain.position_calculator import PositionCalculator
from python_trading_objects.domain.trading_position import TradingPosition
from python_trading_objects.quotes.pair import BotPair
def test_basic_trading_scenario():
"""Test a complete trading scenario"""
print("=" * 60)
print("TEST 1: Basic Trading Scenario")
print("=" * 60)
# Setup
pair = BotPair("BTC/USDT")
# Create position
position = TradingPosition(
id="test-pos-1",
short_id=1,
pair=pair,
purchase_price=pair.create_price(50000),
number_of_tokens=pair.create_token(0.1),
expected_sale_price=pair.create_price(51000),
next_purchase_price=pair.create_price(49000),
variations={"buy": 0.02, "sell": 0.02},
strategy_tag="momentum"
)
print(f"\n📊 Position created: {position}")
print(f" Cost basis: ${float(position.cost_basis.amount):,.2f}")
print(f" Potential profit: ${float(position.potential_profit.amount):,.2f}")
print(f" Potential ROI: {position.potential_roi:.2f}%")
# Test current market conditions
current_price = pair.create_price(52000)
print(f"\n💰 Current market price: ${float(current_price.price):,.2f}")
# Should we sell?
if position.should_sell_at(current_price):
profit = position.calculate_profit(current_price)
roi = position.calculate_roi(current_price)
print(f" ✅ SELL SIGNAL!")
print(f" Profit: ${float(profit.amount):,.2f}")
print(f" ROI: {roi:.2f}%")
# Test DCA opportunity
dca_price = pair.create_price(48000)
print(f"\n📉 Price drops to: ${float(dca_price.price):,.2f}")
if position.should_buy_dca_at(dca_price):
print(f" ✅ DCA BUY SIGNAL!")
print("\n✅ Test 1 passed!\n")
def test_trailing_stop():
"""Test trailing stop functionality"""
print("=" * 60)
print("TEST 2: Trailing Stop")
print("=" * 60)
pair = BotPair("ETH/USDT")
position = TradingPosition(
id="test-pos-2",
pair=pair,
purchase_price=pair.create_price(3000),
number_of_tokens=pair.create_token(1.0),
expected_sale_price=pair.create_price(3100),
next_purchase_price=pair.create_price(2900),
variations={"buy": 0.02, "sell": 0.02},
strategy_tag="trailing"
)
print(f"\n📊 Initial position:")
print(f" Expected sale: ${float(position.expected_sale_price.price):,.2f}")
# Price goes up - trailing stop should update
high_price = pair.create_price(3500)
updated = position.apply_trailing_stop(high_price, 0.02)
print(f"\n📈 Price rises to: ${float(high_price.price):,.2f}")
print(f" New expected sale: ${float(updated.expected_sale_price.price):,.2f}")
print(f" Difference: ${float(updated.expected_sale_price.price - position.expected_sale_price.price):,.2f}")
# Price goes down - trailing stop should NOT update
low_price = pair.create_price(3200)
same = updated.apply_trailing_stop(low_price, 0.02)
print(f"\n📉 Price drops to: ${float(low_price.price):,.2f}")
print(f" Expected sale unchanged: ${float(same.expected_sale_price.price):,.2f}")
print("\n✅ Test 2 passed!\n")
def test_portfolio_aggregation():
"""Test portfolio-level calculations"""
print("=" * 60)
print("TEST 3: Portfolio Aggregation")
print("=" * 60)
pair = BotPair("BTC/USDT")
# Create multiple positions
positions = [
TradingPosition(
id=f"pos-{i}",
pair=pair,
purchase_price=pair.create_price(50000 + i * 1000),
number_of_tokens=pair.create_token(0.1),
expected_sale_price=pair.create_price(51000 + i * 1000),
next_purchase_price=pair.create_price(49000 + i * 1000),
variations={"buy": 0.02, "sell": 0.02}
)
for i in range(3)
]
print(f"\n📊 Portfolio with {len(positions)} positions:")
for pos in positions:
print(f" - {pos.id}: {float(pos.number_of_tokens.amount)} BTC @ ${float(pos.purchase_price.price):,.2f}")
# Calculate portfolio metrics
current_price = pair.create_price(53000)
total_cost = PositionCalculator.total_cost_basis(positions)
total_value = PositionCalculator.total_value(positions, current_price)
avg_price = PositionCalculator.weighted_average_price(positions)
agg_roi = PositionCalculator.aggregate_roi(positions, current_price)
print(f"\n💰 Current price: ${float(current_price.price):,.2f}")
print(f" Total cost basis: ${float(total_cost.amount):,.2f}")
print(f" Total value: ${float(total_value.amount):,.2f}")
print(f" Weighted avg price: ${float(avg_price.price):,.2f}")
print(f" Aggregate ROI: {agg_roi:.2f}%")
profit = float(total_value.amount) - float(total_cost.amount)
print(f" Total profit: ${profit:,.2f}")
print("\n✅ Test 3 passed!\n")
def test_serialization():
"""Test position serialization/deserialization"""
print("=" * 60)
print("TEST 4: Serialization")
print("=" * 60)
pair = BotPair("SOL/USDT")
# Create position
original = TradingPosition(
id="ser-test-1",
short_id=42,
pair=pair,
purchase_price=pair.create_price(150),
number_of_tokens=pair.create_token(10.0),
expected_sale_price=pair.create_price(155),
next_purchase_price=pair.create_price(145),
variations={"buy": 0.02, "sell": 0.03},
strategy_tag="scalping",
notes="Test position for serialization"
)
print(f"\n📦 Original position: {original.id}")
# Serialize
data = original.to_dict()
print(f" Serialized to dict with {len(data)} fields")
# Deserialize
restored = TradingPosition.from_dict(data, pair)
print(f" Restored position: {restored.id}")
# Verify
assert restored.id == original.id
assert float(restored.purchase_price.price) == float(original.purchase_price.price)
assert float(restored.number_of_tokens.amount) == float(original.number_of_tokens.amount)
assert restored.strategy_tag == original.strategy_tag
assert restored == original # Equality based on ID
print(f" ✅ Serialization verified!")
print("\n✅ Test 4 passed!\n")
def test_price_enhancements():
"""Test new Price methods"""
print("=" * 60)
print("TEST 5: Price Enhancements")
print("=" * 60)
pair = BotPair("BTC/USDT")
price = pair.create_price(50000)
target = pair.create_price(51000)
print(f"\n💵 Price: ${float(price.price):,.2f}")
print(f" Target: ${float(target.price):,.2f}")
# Distance
distance = price.distance_from(target)
print(f" Distance: {distance:.2f}%")
# Within tolerance
within_2pct = price.is_within_percentage(target, 0.02)
within_5pct = price.is_within_percentage(target, 0.05)
print(f" Within 2%: {within_2pct}")
print(f" Within 5%: {within_5pct}")
# Apply percentage
increased = price.apply_percentage(0.10)
print(f" +10%: ${float(increased.price):,.2f}")
decreased = price.apply_percentage(-0.05)
print(f" -5%: ${float(decreased.price):,.2f}")
# Midpoint
buy_price = pair.create_price(49000)
sell_price = pair.create_price(51000)
mid = price.midpoint(buy_price, sell_price)
print(f" Midpoint of ${float(buy_price.price):,.0f}-${float(sell_price.price):,.0f}: ${float(mid.price):,.2f}")
print("\n✅ Test 5 passed!\n")
def test_token_enhancements():
"""Test new Token methods"""
print("=" * 60)
print("TEST 6: Token Enhancements")
print("=" * 60)
pair = BotPair("BTC/USDT")
token = pair.create_token(1.0)
price = pair.create_price(50000)
print(f"\n🪙 Token: {float(token.amount)} BTC")
print(f" Price: ${float(price.price):,.2f}")
# Value at price
value = token.value_at(price)
print(f" Value: ${float(value.amount):,.2f}")
# Split
first, second = token.split(0.6)
print(f"\n✂️ Split 60/40:")
print(f" First: {float(first.amount):.8f} BTC")
print(f" Second: {float(second.amount):.8f} BTC")
print(f" Sum: {float(first.amount + second.amount):.8f} BTC")
# Values
first_value = first.value_at(price)
second_value = second.value_at(price)
print(f" First value: ${float(first_value.amount):,.2f}")
print(f" Second value: ${float(second_value.amount):,.2f}")
print(f" Total value: ${float(first_value.amount + second_value.amount):,.2f}")
print("\n✅ Test 6 passed!\n")
if __name__ == "__main__":
print("\n" + "=" * 60)
print("🚀 PYTHON_TRADING_OBJECTS INTEGRATION TESTS")
print("=" * 60 + "\n")
try:
test_basic_trading_scenario()
test_trailing_stop()
test_portfolio_aggregation()
test_serialization()
test_price_enhancements()
test_token_enhancements()
print("=" * 60)
print("✅ ALL INTEGRATION TESTS PASSED!")
print("=" * 60)
print("\n📦 Migration validated successfully!")
print(" - TradingPosition domain model: ✅")
print(" - Price enhancements: ✅")
print(" - Token enhancements: ✅")
print(" - PositionCalculator: ✅")
print(" - Serialization: ✅")
print(" - Forward annotations: ✅")
print("\n🎉 Ready for production use!\n")
except Exception as e:
print(f"\n❌ TEST FAILED: {e}")
import traceback
traceback.print_exc()
exit(1)