HTMLcopy
1
<div id="container"></div>
CSScopy
8
1
html,
2
body,
3
#container {
4
width: 100%;
5
height: 100%;
6
margin: 0;
7
padding: 0;
8
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// Load CSV data from the Gist URL
4
anychart.data.loadCsvFile(
5
'https://gist.githubusercontent.com/awanshrestha/a396693b2f812d3fd89f5e9db4178b6d/raw/7f91976f7c5694fb56ed6955538634b770daff94/SndP500.csv',
6
function (data) {
7
8
// Create a data table
9
var dataTable = anychart.data.table();
10
dataTable.addData(data);
11
12
// Map the columns from the data table to the OHLC format
13
var mapping = dataTable.mapAs({
14
date: 0, // Date column
15
open: 1, // Open column
16
high: 2, // High column
17
low: 3, // Low column
18
close: 4 // Close column
19
});
20
21
// Create a stock chart
22
var chart = anychart.stock();
23
24
// Create the chart plot
25
var plot = chart.plot(0);
26
27
// Create an OHLC series and bind it to the mapped data
28
var ohlcSeries = plot.ohlc(mapping);
29
30
// set the grid settings
31
plot.yGrid(true).xGrid(true).yMinorGrid(true).xMinorGrid(true);
32
33
// Customize the color of the candlesticks
34
ohlcSeries.fallingFill("#FF0D0D");
35
ohlcSeries.fallingStroke("#FF0D0D");
36
ohlcSeries.risingFill("#43FF43");
37
ohlcSeries.risingStroke("#43FF43");
38
39
// Additional customization for OHLC series
40
ohlcSeries.normal().risingStroke("#33ccff");
41
ohlcSeries.hovered().risingStroke("#33ccff", 1.5);
42
ohlcSeries.selected().risingStroke("#33ccff", 3);
43
ohlcSeries.normal().fallingStroke("#ff33cc");
44
ohlcSeries.hovered().fallingStroke("#ff33cc", 1.5);
45
ohlcSeries.selected().fallingStroke("#ff33cc", 3);
46
47
// Name the series
48
ohlcSeries.name('S&P');
49
50
// Customize the legend item for the OHLC series to display a rising-falling icon
51
ohlcSeries.legendItem().iconType('rising-falling');
52
53
// Create a range for selecting a date
54
var rangePicker = anychart.ui.rangePicker();
55
rangePicker.render(chart);
56
57
// Create a range selector
58
var rangeSelector = anychart.ui.rangeSelector();
59
rangeSelector.render(chart);
60
61
// add event markers
62
plot.eventMarkers({
63
"groups": [
64
{
65
"data": [
66
{
67
"symbol": "1",
68
"date": "2020-03-11",
69
"description": "WHO declares COVID-19 a global pandemic",
70
"normal": {
71
"type": "rect", "width": 40,
72
"fill": "#ead9d1", "stroke": "2 #990033",
73
"fontColor": "#990033", "fontWeight": 600,
74
"connector": { "stroke": "2 #990033" }
75
}
76
},
77
{
78
"symbol": "2",
79
"date": "2021-03-08",
80
"description": "The first COVID-19 vaccine is approved for use in the United States",
81
"normal": {
82
"type": "circle",
83
"fill": "#d1ead9", "stroke": "2 #009933",
84
"fontColor": "#009933", "fontWeight": 600,
85
"connector": { "stroke": "2 #009933" }
86
}
87
},
88
{
89
"symbol": "3",
90
"date": "2022-02-24",
91
"description": "Russia invades Ukraine, sparking a global sell-off in stocks and other assets",
92
"normal": {
93
"type": "rect", "width": 40,
94
"fill": "#ead9d1", "stroke": "2 #990033",
95
"fontColor": "#990033", "fontWeight": 600,
96
"connector": { "stroke": "2 #990033" }
97
}
98
},
99
{
100
"symbol": "4",
101
"date": "2023-05-03",
102
"description": "The Federal Reserve announces raise interest rates to more than 5% to combat inflation",
103
"normal": {
104
"type": "circle",
105
"fill": "#d1ead9", "stroke": "2 #009933",
106
"fontColor": "#009933", "fontWeight": 600,
107
"connector": { "stroke": "2 #009933" }
108
},
109
"hovered": {
110
"fill": "white", "stroke": "2 #990033",
111
"fontColor": "#990033",
112
"connector": { "stroke": "2 #990033" }
113
},
114
"selected": {
115
"fill": "white", "stroke": "2 #4d1a00",
116
"fontColor": "#4d1a00",
117
"connector": { "stroke": "2 #4d1a00" }
118
}
119
},
120
]
121
}
122
]
123
});
124
125
// set the symbol of event markers
126
plot.eventMarkers().format(function () {
127
return this.getData("symbol");
128
});
129
130
// Set chart background color
131
chart.background().fill('#edf4f5'); // Background
132
133
// create an annotation
134
var annotation = plot.annotations();
135
136
// create a rectangle
137
annotation.rectangle({
138
// X - part of the first anchor
139
xAnchor: '2022-02-24',
140
// Y - part of the first anchor
141
valueAnchor: 3491,
142
// X - part of the second anchor
143
secondXAnchor: '2022-06-04',
144
// Y - part of the second anchor
145
secondValueAnchor: 4637,
146
// set the stroke settings
147
stroke: '3 #e65a37',
148
// set the fill settings
149
fill: '#e65a37 0.25'
150
});
151
152
// create a text label
153
annotation
154
.label()
155
.xAnchor('2022-02-24')
156
.valueAnchor(4900)
157
.anchor('left-top')
158
.offsetY(5)
159
.padding(6)
160
.text('The first 100 days of Russia Ukraine War')
161
.fontColor('#fff')
162
.background({
163
fill: '#e65a37 0.75',
164
stroke: '0.5 #2d2d2d',
165
corners: 2
166
});
167
168
// Create a KDJ indicator plot
169
var kdjPlot = chart.plot(1); // New plot for KDJ
170
171
// Map the necessary data for KDJ calculation
172
var kdjMapping = dataTable.mapAs({
173
high: 2,
174
low: 3,
175
close: 4
176
});
177
178
// Create the KDJ indicator
179
var kdj = kdjPlot.kdj(kdjMapping, 14, 3, 3); // Default values: 14 periods for %K, 3 periods for %D, 3 periods for %J
180
kdj.kSeries().stroke('purple'); // Customize the %K line color
181
kdj.dSeries().stroke('green'); // Customize the %D line color
182
kdj.jSeries().stroke('orange'); // Customize the %J line color
183
184
// Set the plot's height
185
kdjPlot.height('20%');
186
187
188
189
// set the title of the chart
190
chart.title('S & P 500 OHLC Chart');
191
192
// set the container id for the chart
193
chart.container('container');
194
195
// initiate the chart drawing
196
chart.draw();
197
}
198
);
199
});
200