-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_skew_ratio.py
More file actions
155 lines (126 loc) · 5.21 KB
/
test_skew_ratio.py
File metadata and controls
155 lines (126 loc) · 5.21 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
"""Test script to verify skew ratio calculation logic"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def calculate_skew_ratio(buy_qty: float, sell_qty: float, epsilon: float = 0.001) -> tuple:
"""Calculate skew ratio with detailed output
Args:
buy_qty: Buy volume
sell_qty: Sell volume
epsilon: Minimum threshold
Returns:
Tuple of (ratio, dominant_side, calculation_details)
"""
max_side = max(buy_qty, sell_qty)
min_side = min(buy_qty, sell_qty)
# Check if both are too small
if max_side < epsilon:
return None, None, "Both sides below threshold"
# Adjust min_side if too small
original_min = min_side
if min_side < epsilon:
min_side = epsilon
adjusted = True
else:
adjusted = False
# Calculate ratio
ratio = max_side / min_side
# Determine dominant side
dominant_side = "BUY" if buy_qty > sell_qty else "SELL"
# Build details
details = {
'max_side': max_side,
'min_side_original': original_min,
'min_side_used': min_side,
'adjusted': adjusted,
'ratio': ratio,
'dominant_side': dominant_side,
'buy_qty': buy_qty,
'sell_qty': sell_qty
}
return ratio, dominant_side, details
def test_scenarios():
"""Test various scenarios for skew ratio calculation"""
print("=" * 60)
print("SKEW RATIO CALCULATION TESTS")
print("=" * 60)
test_cases = [
# (buy_qty, sell_qty, description)
(10.0, 2.0, "Normal case - buy dominant"),
(2.0, 10.0, "Normal case - sell dominant"),
(10.0, 0.0, "One side zero - buy only"),
(0.0, 10.0, "One side zero - sell only"),
(0.0005, 10.0, "One side very small"),
(10.0, 0.0005, "Other side very small"),
(0.0005, 0.0005, "Both sides below threshold"),
(100.0, 0.01, "Large imbalance"),
(1000.0, 0.001, "Extreme imbalance"),
(1000.0, 0.0001, "Ultra extreme imbalance"),
(5.0, 5.0, "Perfect balance"),
(5.0, 2.5, "2:1 ratio"),
(7.5, 2.5, "3:1 ratio"),
]
for buy_qty, sell_qty, description in test_cases:
print(f"\nTest: {description}")
print(f"Buy: {buy_qty:.4f}, Sell: {sell_qty:.4f}")
ratio, side, details = calculate_skew_ratio(buy_qty, sell_qty)
if ratio is None:
print(f"Result: {details}")
else:
print(f"Ratio: {ratio:.2f}")
print(f"Dominant side: {side}")
if details['adjusted']:
print(f"Note: Min side adjusted from {details['min_side_original']:.4f} to {details['min_side_used']:.4f}")
# Check if ratio seems reasonable
if ratio > 10000:
print(f"WARNING: Very high ratio detected!")
print(f" Max side: {details['max_side']:.4f}")
print(f" Min side used: {details['min_side_used']:.4f}")
print(f" Calculation: {details['max_side']:.4f} / {details['min_side_used']:.4f} = {ratio:.2f}")
def test_real_scenario():
"""Test with real-world values that might occur"""
print("\n" + "=" * 60)
print("REAL-WORLD SCENARIO TESTS")
print("=" * 60)
# Simulate a large market buy
print("\n1. Large Market Buy Event (100 BTC buy vs 0.1 BTC sell)")
ratio, side, details = calculate_skew_ratio(100.0, 0.1)
print(f" Ratio: {ratio:.2f} - {side}")
print(f" This is a {ratio:.0f}:1 imbalance")
# Simulate extreme one-sided action
print("\n2. Extreme One-Sided Action (500 BTC buy vs 0 sell)")
ratio, side, details = calculate_skew_ratio(500.0, 0.0)
print(f" Ratio: {ratio:.2f} - {side}")
print(f" Note: {details['adjusted']} adjustment applied")
# Normal volatile period
print("\n3. Normal Volatile Period (50 BTC buy vs 20 BTC sell)")
ratio, side, details = calculate_skew_ratio(50.0, 20.0)
print(f" Ratio: {ratio:.2f} - {side}")
print(f" This is a {ratio:.1f}:1 imbalance")
def analyze_epsilon_impact():
"""Analyze impact of different epsilon values"""
print("\n" + "=" * 60)
print("EPSILON VALUE IMPACT ANALYSIS")
print("=" * 60)
epsilons = [1e-12, 1e-6, 0.0001, 0.001, 0.01, 0.1]
test_case = (100.0, 0.0) # 100 BTC buy, 0 sell
print(f"\nTest case: Buy={test_case[0]}, Sell={test_case[1]}")
print("\nEpsilon Value | Resulting Ratio")
print("-" * 35)
for eps in epsilons:
ratio, _, _ = calculate_skew_ratio(test_case[0], test_case[1], epsilon=eps)
if ratio:
print(f"{eps:12.2e} | {ratio:15,.2f}")
else:
print(f"{eps:12.2e} | None")
if __name__ == "__main__":
test_scenarios()
test_real_scenario()
analyze_epsilon_impact()
print("\n" + "=" * 60)
print("CONCLUSION:")
print("- Epsilon=0.001 provides reasonable protection against division issues")
print("- High ratios (>1000) can occur naturally with one-sided markets")
print("- Current logic appears mathematically correct")
print("- No artificial cap needed if we accept extreme market conditions")
print("=" * 60)