File size: 2,878 Bytes
da0ce7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from aureon_baton_link import link_system as _baton_link; _baton_link(__name__)
import sys
import os

# Fix path to allow importing from current directory
sys.path.append(os.getcwd())

from adaptive_prime_profit_gate import AdaptivePrimeProfitGate, ExchangeFeeProfile

def verify_math():
    print("=" * 60)
    print("πŸ§ͺ VERIFYING PROFIT GATE MATH")
    print("=" * 60)

    # 1. Setup Test Case
    V = 100.0  # Trade Notional $100
    G = 0.0    # Fixed cost (Gas)
    P = 1.0    # Target Profit $1
    
    # Fees
    maker_fee = 0.001       # 0.1%
    slippage = 0.0005       # 0.05%
    spread = 0.0005         # 0.05%
    
    # Total cost rate 'c'
    c = maker_fee + slippage + spread # 0.002 (0.2%)
    
    print(f"Parameters:")
    print(f"  Notional (V): ${V}")
    print(f"  Target Profit (P): ${P}")
    print(f"  Fixed Cost (G): ${G}")
    print(f"  Cost Rate (c): {c:.6f} (Fee {maker_fee} + Slip {slippage} + Spread {spread})")
    
    # 2. Manual Calculation
    # r = (V + G + P) / [V * (1-c)^2] - 1
    
    numerator = V + G + P
    denominator = V * ((1 - c) ** 2)
    expected_r = (numerator / denominator) - 1
    
    print("-" * 30)
    print(f"MANUAL CALCULATION:")
    print(f"  Numerator (V+G+P): {numerator}")
    print(f"  Denominator (V*(1-c)^2): {denominator:.6f}")
    print(f"  Expected r: {expected_r:.8f}")
    print(f"  Expected Price Increase: {expected_r*100:.6f}%")
    
    # 3. Code Execution
    gate = AdaptivePrimeProfitGate()
    
    # Verify calculated_required_r method
    calculated_r = gate.calculate_required_r(
        trade_value=V,
        target_profit=P,
        fee_rate=maker_fee,
        slippage=slippage,
        spread=spread,
        fixed_costs=G
    )
    
    print("-" * 30)
    print(f"CODE EXECUTION:")
    print(f"  Calculated r: {calculated_r:.8f}")
    
    # 4. Compare
    diff = abs(expected_r - calculated_r)
    print("-" * 30)
    if diff < 1e-9:
        print("βœ… MATCH: The code accurately implements the profit formula.")
    else:
        print(f"❌ MISMATCH: Diff is {diff}")
        exit(1)

    # 5. Sanity Check - Break Even
    # If P=0, G=0
    # r_be = V / [V * (1-c)^2] - 1 = 1/(1-c)^2 - 1
    P_be = 0
    expected_r_be = (1 / ((1-c)**2)) - 1
    
    calc_r_be = gate.calculate_required_r(
        trade_value=V,
        target_profit=P_be,
        fee_rate=maker_fee,
        slippage=slippage,
        spread=spread,
        fixed_costs=G
    )
    
    print(f"Break-even check (P=0):")
    print(f"  Expected r_be: {expected_r_be:.8f}")
    print(f"  Calculated r_be: {calc_r_be:.8f}")
    
    if abs(expected_r_be - calc_r_be) < 1e-9:
        print("βœ… BE MATCH")
    else:
        print("❌ BE MISMATCH")
        exit(1)

if __name__ == "__main__":
    verify_math()