-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalerts.js
75 lines (66 loc) · 2.92 KB
/
alerts.js
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
function showAlertsWindow(chartId) {
const html = `
<div>
<select
name="alertType"
style="padding: 10px;display: block;margin-bottom: 15px;width: 100%;background: inherit;color:inherit;border:1px solid;"
>
<option value="ema">Previous candle low above 5 EMA</option>
</select>
<label For="previousData">Previous candles EMA</label>
<input type="number" name="previousData" />
<br><br>
<label For="timeframe">Timeframe</label>
<input type="number" name="timeframe" />
<br><br>
<button
onclick="
window.postMessage({
type: 'FROM_PAGE',
alert: {
type: document.querySelector('#prokite-popup select').value,
previousData: document.querySelector('#prokite-popup input[name=previousData]').value,
timeframe: document.querySelector('#prokite-popup input[name=timeframe]').value
},
chartId: ${chartId}
}); document.getElementById('prokite-popup').style.display = 'none'"
style="background:royalblue;"
>Create Alert</button>
</div>`;
const activeAlertHtml = `
<h3>Alert is active! refresh the page to reset the alert.</h3>`
showPopup('Create alert', prokiteGlobals.alert.active ? activeAlertHtml : html);
}
// listen to messages coming from webpage
window.addEventListener("message", async (event) => {
// We only accept messages from ourselves
if (event.source != window) {
return;
}
if (event.data.type && (event.data.type == "FROM_PAGE") && event.data.alert) {
if (event.data.alert.type === 'ema') {
prokiteGlobals.alert.active = true;
return alertEMA(event.data);
}
}
}, false);
function alertEMA(edata) {
// insert sound link
const alertSound = insertAfter('', $$('body'));
alertSound.innerHTML = `<a id="prokite-alert" href="https://soc.threej.in/beep.mp3" style="display:none;" target="_blank">`;
setTimeout(async () => {
const date = new Date().getFullYear() + '-' + ("0" + (new Date().getMonth() + 1)).slice(-2) + '-' + ("0" + new Date().getDate()).slice(-2);
const url = `https://kite.zerodha.com/oms/instruments/historical/${edata.chartId}/${edata.alert.timeframe || 5}minute?user_id=${prokiteGlobals.userId}&oi=1&from=${date}&to=${date}`;
const result = await ajax(url, prokiteGlobals.enctoken)
if(result.data){
//calculate ema
ema = (result.data.candles[result.data.candles.length - 2][1] * 0.333) + (edata.alert.previousData * 0.667);
edata.alert.previousData = ema;
if(result.data.candles[result.data.candles.length - 1][3] > ema){
$('#prokite-alert').click();
alert('5EMA alert triggered');
}
}
alertEMA(edata);
}, 240000);
}