80 lines
3.0 KiB
Python
80 lines
3.0 KiB
Python
from pt_strategy.research_strategy import PtResearchStrategy
|
|
|
|
|
|
def visualize_prices(strategy: PtResearchStrategy, trading_date: str) -> None:
|
|
# Plot raw price data
|
|
import matplotlib.pyplot as plt
|
|
# Set plotting style
|
|
import seaborn as sns
|
|
|
|
pair = strategy.trading_pair_
|
|
SYMBOL_A = pair.symbol_a_
|
|
SYMBOL_B = pair.symbol_b_
|
|
TRD_DATE = f"{trading_date[0:4]}-{trading_date[4:6]}-{trading_date[6:8]}"
|
|
|
|
plt.style.use('seaborn-v0_8')
|
|
sns.set_palette("husl")
|
|
plt.rcParams['figure.figsize'] = (15, 10)
|
|
|
|
# Get column names for the trading pair
|
|
colname_a, colname_b = pair.colnames()
|
|
price_data = strategy.pt_mkt_data_.market_data_df_.copy()
|
|
|
|
# Create separate subplots for better visibility
|
|
fig_price, price_axes = plt.subplots(2, 1, figsize=(18, 10))
|
|
|
|
# Plot SYMBOL_A
|
|
price_axes[0].plot(price_data['tstamp'], price_data[colname_a], alpha=0.7,
|
|
label=f'{SYMBOL_A}', linewidth=1, color='blue')
|
|
price_axes[0].set_title(f'{SYMBOL_A} Price Data ({TRD_DATE})')
|
|
price_axes[0].set_ylabel(f'{SYMBOL_A} Price')
|
|
price_axes[0].legend()
|
|
price_axes[0].grid(True)
|
|
|
|
# Plot SYMBOL_B
|
|
price_axes[1].plot(price_data['tstamp'], price_data[colname_b], alpha=0.7,
|
|
label=f'{SYMBOL_B}', linewidth=1, color='red')
|
|
price_axes[1].set_title(f'{SYMBOL_B} Price Data ({TRD_DATE})')
|
|
price_axes[1].set_ylabel(f'{SYMBOL_B} Price')
|
|
price_axes[1].set_xlabel('Time')
|
|
price_axes[1].legend()
|
|
price_axes[1].grid(True)
|
|
|
|
plt.tight_layout()
|
|
plt.show()
|
|
|
|
|
|
# Plot individual prices
|
|
fig, axes = plt.subplots(2, 1, figsize=(18, 12))
|
|
|
|
# Normalized prices for comparison
|
|
norm_a = price_data[colname_a] / price_data[colname_a].iloc[0]
|
|
norm_b = price_data[colname_b] / price_data[colname_b].iloc[0]
|
|
|
|
axes[0].plot(price_data['tstamp'], norm_a, label=f'{SYMBOL_A} (normalized)', alpha=0.8, linewidth=1)
|
|
axes[0].plot(price_data['tstamp'], norm_b, label=f'{SYMBOL_B} (normalized)', alpha=0.8, linewidth=1)
|
|
axes[0].set_title(f'Normalized Price Comparison (Base = 1.0) ({TRD_DATE})')
|
|
axes[0].set_ylabel('Normalized Price')
|
|
axes[0].legend()
|
|
axes[0].grid(True)
|
|
|
|
# Price ratio
|
|
price_ratio = price_data[colname_a] / price_data[colname_b]
|
|
axes[1].plot(price_data['tstamp'], price_ratio, label=f'{SYMBOL_A}/{SYMBOL_B} Ratio', color='green', alpha=0.8, linewidth=1)
|
|
axes[1].set_title(f'Price Ratio Px({SYMBOL_A})/Px({SYMBOL_B}) ({TRD_DATE})')
|
|
axes[1].set_ylabel('Ratio')
|
|
axes[1].set_xlabel('Time')
|
|
axes[1].legend()
|
|
axes[1].grid(True)
|
|
|
|
plt.tight_layout()
|
|
plt.show()
|
|
|
|
# Print basic statistics
|
|
print(f"\nPrice Statistics:")
|
|
print(f" {SYMBOL_A}: Mean=${price_data[colname_a].mean():.2f}, Std=${price_data[colname_a].std():.2f}")
|
|
print(f" {SYMBOL_B}: Mean=${price_data[colname_b].mean():.2f}, Std=${price_data[colname_b].std():.2f}")
|
|
print(f" Price Ratio: Mean={price_ratio.mean():.2f}, Std={price_ratio.std():.2f}")
|
|
print(f" Correlation: {price_data[colname_a].corr(price_data[colname_b]):.4f}")
|
|
|