-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharting_engine.py
More file actions
45 lines (38 loc) · 1.65 KB
/
Copy pathcharting_engine.py
File metadata and controls
45 lines (38 loc) · 1.65 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
import pandas as pd
import mplfinance as mpf
import os
import logging
from market_data import MarketData
logger = logging.getLogger(__name__)
def generate_chart(symbol: str, timeframe: str, entry_price: float, tp: float, sl: float, ob_top: float=None, ob_bot: float=None) -> str:
"""
Generate candlestick chart with annotations for entry, TP, SL, and OB.
Returns path to the saved image.
"""
try:
md = MarketData()
df = md.get_klines(symbol, interval=timeframe, limit=100)
if df is None or df.empty:
return ""
apdicts = []
apdicts.append(mpf.make_addplot([entry_price]*len(df), color='blue', linestyle='--', label='Entry'))
apdicts.append(mpf.make_addplot([tp]*len(df), color='green', linestyle='-', label='TP'))
apdicts.append(mpf.make_addplot([sl]*len(df), color='red', linestyle='-', label='SL'))
filename = f"/tmp/{symbol}_{timeframe}_chart.png"
plot_kwargs = dict(
type='candle',
style='charles',
addplot=apdicts,
volume=True,
savefig=filename,
title=f"{symbol} - {timeframe} Setup",
)
# Only pass fill_between when we actually have OB levels — mplfinance
# rejects None for this kwarg and will raise a validation error.
if ob_top and ob_bot:
plot_kwargs['fill_between'] = dict(y1=ob_bot, y2=ob_top, color='purple', alpha=0.2)
mpf.plot(df, **plot_kwargs)
return filename
except Exception as e:
logger.error(f"Error generating chart for {symbol}: {e}")
return ""