128 lines
5.1 KiB
Python
128 lines
5.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script to demonstrate multi-timeframe velocity calculation (Option 3B)
|
|
Shows how the new approach reduces false triggers while maintaining emergency response
|
|
"""
|
|
|
|
import time
|
|
import random
|
|
|
|
def simulate_velocity_calculation():
|
|
"""Simulate the multi-timeframe velocity calculation"""
|
|
print("=== Multi-Timeframe Velocity Calculation Demo ===\n")
|
|
|
|
# Simulate price data with noise and occasional real moves
|
|
base_price = 3000.0
|
|
price_history = []
|
|
velocity_history = []
|
|
|
|
scenarios = [
|
|
("Normal Trading", 10, 0.0002), # 0.02% noise
|
|
("Noisy Market", 10, 0.0008), # 0.08% noise
|
|
("Sharp Move", 5, 0.0025), # 0.25% move
|
|
("Sustained Move", 10, 0.0010), # 0.1% sustained
|
|
]
|
|
|
|
for scenario_name, duration, max_change_pct in scenarios:
|
|
print(f"Scenario: {scenario_name}")
|
|
print(f"Duration: {duration}s, Max change per interval: {max_change_pct*100:.2f}%")
|
|
print("-" * 50)
|
|
|
|
current_price = base_price
|
|
last_price = current_price
|
|
price_history = [current_price]
|
|
|
|
for i in range(duration):
|
|
# Simulate price change
|
|
change_pct = random.uniform(-max_change_pct, max_change_pct)
|
|
current_price = current_price * (1 + change_pct)
|
|
|
|
# Calculate velocities (same as implemented in clp_scalper_hedger.py)
|
|
# 1-second velocity
|
|
velocity_1s = (current_price - last_price) / last_price
|
|
|
|
# 5-second average velocity
|
|
velocity_5s = 0.0
|
|
if len(price_history) >= 5:
|
|
price_5s_ago = price_history[-5]
|
|
velocity_5s = (current_price - price_5s_ago) / price_5s_ago / 5
|
|
|
|
# Choose velocity (Option 3B logic)
|
|
if abs(velocity_1s) > 0.002: # Extreme 1s move
|
|
price_velocity = velocity_1s
|
|
velocity_type = "1S_EXTREME"
|
|
else: # Use smoothed 5s average
|
|
price_velocity = velocity_5s
|
|
velocity_type = "5S_SMOOTHED"
|
|
|
|
# Current threshold (0.05% = 0.0005)
|
|
VELOCITY_THRESHOLD_PCT = 0.0005
|
|
trigger_emergency = abs(price_velocity) > VELOCITY_THRESHOLD_PCT
|
|
|
|
print(f" Second {i+1:2d}: ${current_price:7.2f} | "
|
|
f"Vel: {price_velocity*100:+6.3f}% ({velocity_type}) | "
|
|
f"{'EMERGENCY' if trigger_emergency else 'Normal'}")
|
|
|
|
# Update history
|
|
price_history.append(current_price)
|
|
last_price = current_price
|
|
|
|
time.sleep(0.1) # Small delay for readability
|
|
|
|
print(f"\nResults for {scenario_name}:")
|
|
print(f" Emergency triggers: {sum(1 for i in range(len(price_history)) if abs(price_history[i]/price_history[max(0,i-1)] - 1) > 0.0005 and i > 0)}")
|
|
print(f" Final price: ${current_price:.2f} ({((current_price/base_price)-1)*100:+.2f}%)")
|
|
print("\n" + "="*60 + "\n")
|
|
|
|
def compare_approaches():
|
|
"""Compare old vs new velocity approach"""
|
|
print("=== Approach Comparison ===\n")
|
|
|
|
# Noisy price series that would trigger old approach falsely
|
|
prices = [3000, 3001.5, 2998.5, 3002.0, 2999.0, 3003.0, 2997.0, 3001.0]
|
|
|
|
print("Price series with 0.05% noise:", [f"${p:.2f}" for p in prices])
|
|
print("\nOld Approach (1-second velocity only):")
|
|
|
|
old_triggers = 0
|
|
for i in range(1, len(prices)):
|
|
old_velocity = (prices[i] - prices[i-1]) / prices[i-1]
|
|
trigger = abs(old_velocity) > 0.0005
|
|
if trigger:
|
|
old_triggers += 1
|
|
print(f" {i}: {old_velocity*100:+.3f}% {'EMERGENCY' if trigger else 'Normal'}")
|
|
|
|
print(f"\nOld approach triggers: {old_triggers}")
|
|
|
|
print("\nNew Approach (Multi-timeframe):")
|
|
|
|
new_triggers = 0
|
|
for i in range(1, len(prices)):
|
|
if i >= 5:
|
|
velocity_5s = (prices[i] - prices[i-5]) / prices[i-5] / 5
|
|
final_velocity = velocity_5s
|
|
velocity_type = "5S_SMOOTHED"
|
|
else:
|
|
final_velocity = (prices[i] - prices[i-1]) / prices[i-1]
|
|
velocity_type = "1S_NORMAL"
|
|
|
|
trigger = abs(final_velocity) > 0.0005
|
|
if trigger:
|
|
new_triggers += 1
|
|
print(f" {i}: {final_velocity*100:+.3f}% ({velocity_type}) {'EMERGENCY' if trigger else 'Normal'}")
|
|
|
|
print(f"\nNew approach triggers: {new_triggers}")
|
|
print(f"\nReduction in false triggers: {old_triggers - new_triggers} ({((old_triggers-new_triggers)/old_triggers*100):.0f}%)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("Testing Multi-Timeframe Velocity Calculation for CLP Scalper Hedger\n")
|
|
simulate_velocity_calculation()
|
|
compare_approaches()
|
|
|
|
print("\nKEY Benefits of Option 3B:")
|
|
print(" • Reduces false triggers from normal 1-second noise")
|
|
print(" • Maintains fast response to genuine sharp moves")
|
|
print(" • Uses 5-second smoothing for sustained directional detection")
|
|
print(" • Context-aware: distinguishes noise from real emergencies")
|
|
print(" • Better suited for $8k position with lower risk appetite") |