backend redesign
This commit is contained in:
154
backend.old/src/agent/tools/chart_utils_example.py
Normal file
154
backend.old/src/agent/tools/chart_utils_example.py
Normal file
@@ -0,0 +1,154 @@
|
||||
"""
|
||||
Example usage of chart_utils.py plotting functions.
|
||||
|
||||
This demonstrates how the LLM can use the plot_ohlc() convenience function
|
||||
in analyze_chart_data scripts to create beautiful, standard OHLC charts.
|
||||
"""
|
||||
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
|
||||
def create_sample_data(days=30):
|
||||
"""Create sample OHLCV data for testing."""
|
||||
dates = pd.date_range(end=datetime.now(), periods=days * 24, freq='1H')
|
||||
|
||||
# Simulate price movement
|
||||
np.random.seed(42)
|
||||
close = 50000 + np.cumsum(np.random.randn(len(dates)) * 100)
|
||||
|
||||
data = {
|
||||
'open': close + np.random.randn(len(dates)) * 50,
|
||||
'high': close + np.abs(np.random.randn(len(dates))) * 100,
|
||||
'low': close - np.abs(np.random.randn(len(dates))) * 100,
|
||||
'close': close,
|
||||
'volume': np.abs(np.random.randn(len(dates))) * 1000000
|
||||
}
|
||||
|
||||
df = pd.DataFrame(data, index=dates)
|
||||
|
||||
# Ensure high is highest and low is lowest
|
||||
df['high'] = df[['open', 'high', 'low', 'close']].max(axis=1)
|
||||
df['low'] = df[['open', 'high', 'low', 'close']].min(axis=1)
|
||||
|
||||
return df
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from chart_utils import plot_ohlc, add_indicators_to_plot, plot_price_volume
|
||||
|
||||
# Create sample data
|
||||
df = create_sample_data(days=30)
|
||||
|
||||
print("=" * 60)
|
||||
print("Example 1: Basic OHLC chart with volume")
|
||||
print("=" * 60)
|
||||
print("\nScript the LLM would generate:")
|
||||
print("""
|
||||
fig = plot_ohlc(df, title='BTC/USDT 1H', volume=True)
|
||||
df.tail(5)
|
||||
""")
|
||||
|
||||
# Execute it
|
||||
fig = plot_ohlc(df, title='BTC/USDT 1H', volume=True)
|
||||
print("\n✓ Chart created successfully!")
|
||||
print(f" Figure size: {fig.get_size_inches()}")
|
||||
print(f" Number of axes: {len(fig.axes)}")
|
||||
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("Example 2: OHLC chart with indicators")
|
||||
print("=" * 60)
|
||||
print("\nScript the LLM would generate:")
|
||||
print("""
|
||||
# Calculate indicators
|
||||
df['SMA_20'] = df['close'].rolling(20).mean()
|
||||
df['SMA_50'] = df['close'].rolling(50).mean()
|
||||
df['EMA_12'] = df['close'].ewm(span=12, adjust=False).mean()
|
||||
|
||||
# Plot with indicators
|
||||
fig = add_indicators_to_plot(
|
||||
df,
|
||||
indicators={
|
||||
'SMA_20': {'color': 'blue', 'width': 1.5},
|
||||
'SMA_50': {'color': 'red', 'width': 1.5},
|
||||
'EMA_12': {'color': 'green', 'width': 1.0}
|
||||
},
|
||||
title='BTC/USDT with Moving Averages',
|
||||
volume=True
|
||||
)
|
||||
|
||||
df[['close', 'SMA_20', 'SMA_50', 'EMA_12']].tail(5)
|
||||
""")
|
||||
|
||||
# Execute it
|
||||
df['SMA_20'] = df['close'].rolling(20).mean()
|
||||
df['SMA_50'] = df['close'].rolling(50).mean()
|
||||
df['EMA_12'] = df['close'].ewm(span=12, adjust=False).mean()
|
||||
|
||||
fig = add_indicators_to_plot(
|
||||
df,
|
||||
indicators={
|
||||
'SMA_20': {'color': 'blue', 'width': 1.5},
|
||||
'SMA_50': {'color': 'red', 'width': 1.5},
|
||||
'EMA_12': {'color': 'green', 'width': 1.0}
|
||||
},
|
||||
title='BTC/USDT with Moving Averages',
|
||||
volume=True
|
||||
)
|
||||
|
||||
print("\n✓ Chart with indicators created successfully!")
|
||||
print(f" Last close: ${df['close'].iloc[-1]:,.2f}")
|
||||
print(f" SMA 20: ${df['SMA_20'].iloc[-1]:,.2f}")
|
||||
print(f" SMA 50: ${df['SMA_50'].iloc[-1]:,.2f}")
|
||||
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("Example 3: Price-only chart (no volume)")
|
||||
print("=" * 60)
|
||||
print("\nScript the LLM would generate:")
|
||||
print("""
|
||||
from chart_utils import plot_price_only
|
||||
|
||||
fig = plot_price_only(df, title='Clean Price Action')
|
||||
""")
|
||||
|
||||
# Execute it
|
||||
from chart_utils import plot_price_only
|
||||
fig = plot_price_only(df, title='Clean Price Action')
|
||||
|
||||
print("\n✓ Price-only chart created successfully!")
|
||||
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("Summary")
|
||||
print("=" * 60)
|
||||
print("""
|
||||
The chart_utils module provides:
|
||||
|
||||
1. plot_ohlc() - Main function for beautiful candlestick charts
|
||||
- Professional seaborn-inspired styling
|
||||
- Consistent color scheme (teal up, coral down)
|
||||
- Optional volume subplot
|
||||
- Customizable figure size
|
||||
|
||||
2. add_indicators_to_plot() - OHLC charts with technical indicators
|
||||
- Overlay multiple indicators
|
||||
- Customizable colors and line widths
|
||||
- Proper integration with mplfinance
|
||||
|
||||
3. Preset functions for common chart types:
|
||||
- plot_price_volume() - Standard price + volume
|
||||
- plot_price_only() - Candlesticks without volume
|
||||
|
||||
Benefits:
|
||||
✓ Consistent look and feel across all charts
|
||||
✓ Less code for the LLM to generate
|
||||
✓ Professional appearance out of the box
|
||||
✓ Easy to customize when needed
|
||||
✓ Works seamlessly with analyze_chart_data tool
|
||||
|
||||
The LLM can now simply call plot_ohlc(df) instead of writing
|
||||
custom matplotlib code for every chart request!
|
||||
""")
|
||||
Reference in New Issue
Block a user