omniverse1 commited on
Commit
539f8db
·
verified ·
1 Parent(s): 54fff71

Create plotter.py

Browse files
Files changed (1) hide show
  1. plotter.py +98 -0
plotter.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import mplfinance as mpf
3
+ from io import BytesIO
4
+ import base64
5
+ import matplotlib.pyplot as plt
6
+
7
+ def create_mplfinance_chart(df, ticker, predictions=None):
8
+ """
9
+ Creates a custom mplfinance candlestick chart and returns it as a base64 encoded image.
10
+ Implements the exact layout: Candlestick + Volume + MACD + Stochastic.
11
+ """
12
+ if df.empty:
13
+ return None
14
+
15
+ # Define style - Yahoo style as requested
16
+ mc = mpf.make_marketcolors(
17
+ up='#00ff00', down='#ff0000', # Green/Red candles
18
+ wick='black',
19
+ edge='black',
20
+ volume='#00bfff',
21
+ inherit=True
22
+ )
23
+
24
+ s = mpf.make_mpf_style(
25
+ base_mpf_style='yahoo',
26
+ marketcolors=mc,
27
+ facecolor='white',
28
+ edgecolor='black',
29
+ gridcolor='lightgray',
30
+ gridstyle='-',
31
+ figcolor='white',
32
+ rc={'axes.labelcolor': 'black',
33
+ 'xtick.color': 'black',
34
+ 'ytick.color': 'black',
35
+ 'figure.titlesize': 16,
36
+ 'axes.titlesize': 14,
37
+ 'axes.titleweight': 'bold'}
38
+ )
39
+
40
+ # Define panels: [Candlestick, Volume, MACD, Stochastic]
41
+ apds = []
42
+
43
+ # MACD Panel (Panel 2 - index 1 for addplot)
44
+ # MACD Line
45
+ apds.append(
46
+ mpf.make_addplot(df['MACD'], color='#606060', panel=2, ylabel='MACD', secondary_y=False)
47
+ )
48
+ # Signal Line
49
+ apds.append(
50
+ mpf.make_addplot(df['MACD_signal'], color='#1f77b4', panel=2, secondary_y=False)
51
+ )
52
+ # Positive Histogram Bars
53
+ apds.append(
54
+ mpf.make_addplot(df['MACD_bar_positive'], type='bar', color='#4dc790', panel=2, width=0.8)
55
+ )
56
+ # Negative Histogram Bars
57
+ apds.append(
58
+ mpf.make_addplot(df['MACD_bar_negative'], type='bar', color='#fd6b6c', panel=2, width=0.8)
59
+ )
60
+
61
+ # Stochastic Panel (Panel 3 - index 2 for addplot)
62
+ apds.append(
63
+ mpf.make_addplot(df[['%D', '%SD', 'UL', 'DL']], panel=3, ylabel='Stoch (14,3)', ylim=[0, 100])
64
+ )
65
+
66
+ # Prediction overlay on main chart
67
+ if predictions is not None and predictions.any():
68
+ last_date = df.index[-1]
69
+ future_index = pd.date_range(start=last_date, periods=len(predictions) + 1, freq=df.index.freq or 'D')[1:]
70
+ future_series = pd.Series(predictions, index=future_index)
71
+
72
+ apds.append(
73
+ mpf.make_addplot(future_series, color='blue', linestyle='-.', width=2, marker='o', markersize=4)
74
+ )
75
+
76
+ # Plotting
77
+ fig, axes = mpf.plot(
78
+ df,
79
+ type='candle',
80
+ style=s,
81
+ title=f'{ticker} Price Chart and Analysis',
82
+ ylabel='Price (USD)',
83
+ volume=True,
84
+ addplot=apds,
85
+ mav=(5, 20), # Moving averages as requested
86
+ figratio=(16, 9),
87
+ figscale=1.5,
88
+ panel_ratios=(3, 1, 3, 3), # Ratio as requested
89
+ returnfig=True
90
+ )
91
+
92
+ # Convert to Base64
93
+ buf = BytesIO()
94
+ fig.savefig(buf, format='png', bbox_inches='tight', dpi=100)
95
+ plt.close(fig)
96
+ image_base64 = base64.b64encode(buf.getvalue()).decode('utf-8')
97
+
98
+ return f'<img src="data:image/png;base64,{image_base64}" style="width: 100%; height: auto;">'