-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandlestickchart.py
More file actions
260 lines (234 loc) · 5.77 KB
/
candlestickchart.py
File metadata and controls
260 lines (234 loc) · 5.77 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import os
# Load data
folder_path = r'C:\data\equity\NSE500_202509'
df = pd.read_csv(os.path.join(folder_path, 'DLF.csv'))
# Convert date to datetime
df['date'] = pd.to_datetime(df['date'])
df = df.sort_values('date')
# Calculate RSI
def calculate_rsi(data, period=14):
delta = data.diff()
gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
rs = gain / loss
rsi = 100 - (100 / (1 + rs))
return rsi
# Calculate Moving Averages
df['SMA_20'] = df['close'].rolling(window=20).mean()
df['SMA_50'] = df['close'].rolling(window=50).mean()
df['RSI'] = calculate_rsi(df['close'])
# TradingView color scheme
tv_bg = '#131722' # Dark background
tv_grid = '#1e222d' # Grid lines
tv_text = '#d1d4dc' # Text color
tv_green = '#089981' # Bull candle (TradingView green)
tv_red = '#f23645' # Bear candle (TradingView red)
tv_blue = '#2962ff' # Blue for indicators
tv_purple = '#9c27b0' # Purple for indicators
tv_orange = '#ff6d00' # Orange for indicators
# Create subplots with 3 rows
fig = make_subplots(
rows=3, cols=1,
shared_xaxes=True,
vertical_spacing=0.02,
row_heights=[0.6, 0.2, 0.2],
subplot_titles=('DLF', 'Volume', 'RSI')
)
# Candlestick chart (TradingView style)
fig.add_trace(
go.Candlestick(
x=df['date'],
open=df['open'],
high=df['high'],
low=df['low'],
close=df['close'],
name='DLF',
increasing=dict(line=dict(color=tv_green, width=1), fillcolor=tv_green),
decreasing=dict(line=dict(color=tv_red, width=1), fillcolor=tv_red)
),
row=1, col=1
)
# Add SMA 20
fig.add_trace(
go.Scatter(
x=df['date'],
y=df['SMA_20'],
mode='lines',
name='SMA 20',
line=dict(color=tv_blue, width=2),
opacity=0.8
),
row=1, col=1
)
# Add SMA 50
fig.add_trace(
go.Scatter(
x=df['date'],
y=df['SMA_50'],
mode='lines',
name='SMA 50',
line=dict(color=tv_orange, width=2),
opacity=0.8
),
row=1, col=1
)
# Volume bars with TradingView colors
colors = [tv_green if row['close'] >= row['open'] else tv_red
for idx, row in df.iterrows()]
fig.add_trace(
go.Bar(
x=df['date'],
y=df['volume'],
name='Volume',
marker=dict(
color=colors,
line=dict(width=0)
),
showlegend=False,
opacity=0.5
),
row=2, col=1
)
# RSI line
fig.add_trace(
go.Scatter(
x=df['date'],
y=df['RSI'],
mode='lines',
name='RSI',
line=dict(color=tv_purple, width=2),
fill='tozeroy',
fillcolor='rgba(156, 39, 176, 0.1)'
),
row=3, col=1
)
# RSI overbought line (70)
fig.add_trace(
go.Scatter(
x=df['date'],
y=[70] * len(df),
mode='lines',
line=dict(color='rgba(255, 82, 82, 0.5)', width=1, dash='dash'),
showlegend=False,
hoverinfo='skip'
),
row=3, col=1
)
# RSI oversold line (30)
fig.add_trace(
go.Scatter(
x=df['date'],
y=[30] * len(df),
mode='lines',
line=dict(color='rgba(38, 166, 154, 0.5)', width=1, dash='dash'),
showlegend=False,
hoverinfo='skip'
),
row=3, col=1
)
# RSI middle line (50)
fig.add_trace(
go.Scatter(
x=df['date'],
y=[50] * len(df),
mode='lines',
line=dict(color='rgba(209, 212, 220, 0.2)', width=1, dash='dot'),
showlegend=False,
hoverinfo='skip'
),
row=3, col=1
)
# Update layout with TradingView styling
fig.update_layout(
title=dict(
text='DLF - Technical Analysis',
font=dict(color=tv_text, size=20),
x=0.5,
xanchor='center'
),
plot_bgcolor=tv_bg,
paper_bgcolor=tv_bg,
font=dict(color=tv_text, family='Arial, sans-serif', size=12),
xaxis_rangeslider_visible=False,
height=900,
showlegend=True,
legend=dict(
orientation="h",
yanchor="top",
y=0.99,
xanchor="left",
x=0.01,
bgcolor='rgba(19, 23, 34, 0.8)',
bordercolor=tv_grid,
borderwidth=1,
font=dict(color=tv_text, size=11)
),
hovermode='x unified',
hoverlabel=dict(
bgcolor=tv_grid,
font_size=12,
font_family="Arial, sans-serif",
font_color=tv_text
),
margin=dict(l=60, r=30, t=80, b=40)
)
# Update all xaxes
for i in [1, 2, 3]:
fig.update_xaxes(
gridcolor=tv_grid,
showgrid=True,
gridwidth=1,
zeroline=False,
showline=True,
linewidth=1,
linecolor=tv_grid,
row=i, col=1
)
# Update all yaxes
for i in [1, 2, 3]:
fig.update_yaxes(
gridcolor=tv_grid,
showgrid=True,
gridwidth=1,
zeroline=False,
showline=True,
linewidth=1,
linecolor=tv_grid,
row=i, col=1
)
# Update y-axes labels
fig.update_yaxes(
title_text="Price (₹)",
title_font=dict(color=tv_text, size=12),
row=1, col=1
)
fig.update_yaxes(
title_text="Volume",
title_font=dict(color=tv_text, size=12),
row=2, col=1
)
fig.update_yaxes(
title_text="RSI",
title_font=dict(color=tv_text, size=12),
range=[0, 100],
row=3, col=1
)
# Update x-axis label
fig.update_xaxes(
title_text="Date",
title_font=dict(color=tv_text, size=12),
row=3, col=1
)
# Update subplot titles styling
for annotation in fig['layout']['annotations']:
annotation['font'] = dict(color=tv_text, size=14)
annotation['xanchor'] = 'left'
annotation['x'] = 0.01
# Show the plot
fig.show()
# Optional: Save to HTML file
# fig.write_html("DLF_tradingview_style.html")