-
-
Notifications
You must be signed in to change notification settings - Fork 681
Expand file tree
/
Copy pathhistory.js
More file actions
184 lines (157 loc) · 5.51 KB
/
history.js
File metadata and controls
184 lines (157 loc) · 5.51 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
const { genSessionID } = require('../utils');
const { parseCompressed } = require('../protocol');
const { getInputs, parseTrades } = require('./study');
/**
* @param {import('../client').ClientBridge} client
*/
module.exports = (client) => class HistorySession {
#historySessionID = genSessionID('hs');
/** Parent client */
#client = client;
#callbacks = {
historyLoaded: [],
event: [],
error: [],
};
/** @type {StrategyReport} */
#strategyReport = {
trades: [],
history: {},
performance: {},
};
/** @return {StrategyReport} Get the strategy report if available */
get strategyReport() {
return this.#strategyReport;
}
/**
* @param {ChartEvent} ev Client event
* @param {...{}} data Packet data
*/
#handleEvent(ev, ...data) {
this.#callbacks[ev].forEach((e) => e(...data));
this.#callbacks.event.forEach((e) => e(ev, ...data));
}
#handleError(...msgs) {
if (this.#callbacks.error.length === 0) console.error(...msgs);
else this.#handleEvent('error', ...msgs);
}
constructor() {
this.#client.sessions[this.#historySessionID] = {
type: 'history',
onData: async (packet) => {
if (global.TW_DEBUG) console.log('§90§30§106 HISTORY SESSION §0 DATA', packet);
if (packet.type === 'request_data') {
const data = packet.data[2];
if (data.ns && data.ns.d) {
const parsed = JSON.parse(data.ns.d);
const changes = await this.updateReport(parsed);
this.#handleEvent('historyLoaded', changes);
}
}
if (['request_error', 'critical_error'].includes(packet.type)) {
const [, name, description] = packet.data;
this.#handleError('Critical error:', name, description);
}
},
};
this.#client.send('history_create_session', [this.#historySessionID]);
}
async updateReport(parsed) {
const changes = [];
const updateStrategyReport = (report) => {
if (report.currency) {
this.#strategyReport.currency = report.currency;
changes.push('report.currency');
}
if (report.settings) {
this.#strategyReport.settings = report.settings;
changes.push('report.settings');
}
if (report.performance) {
this.#strategyReport.performance = report.performance;
changes.push('report.perf');
}
if (report.trades) {
this.#strategyReport.trades = parseTrades(report.trades);
changes.push('report.trades');
}
if (report.equity) {
this.#strategyReport.history = {
buyHold: report.buyHold,
buyHoldPercent: report.buyHoldPercent,
drawDown: report.drawDown,
drawDownPercent: report.drawDownPercent,
equity: report.equity,
equityPercent: report.equityPercent,
};
changes.push('report.history');
}
};
if (parsed.dataCompressed) {
updateStrategyReport((await parseCompressed(parsed.dataCompressed)).report);
}
if (parsed.data && parsed.data.report) updateStrategyReport(parsed.data.report);
return changes;
}
/**
* Sets the history market
* @param {string} symbol Market symbol
* @param {number} from Deep backtest starting point (Timestamp)
* @param {number} to Deep backtest ending point (Timestamp)
* @param {PineIndicator} indicator PineIndicator with options set
* @param {Object} options Chart options
* @param {import('../types').TimeFrame} [options.timeframe] Chart period timeframe (Default is 5)
* @param {number} [options.from] First available timestamp (Default is 2010-01-01)
* @param {number} [options.to] Last candle timestamp (Default is now)
* @param {'splits' | 'dividends'} [options.adjustment] Market adjustment
* @param {'regular' | 'extended'} [options.session] Chart session
* @param {'EUR' | 'USD' | string} [options.currency] Chart currency
*/
requestHistoryData(symbol, indicator, options) {
const symbolInit = {
symbol: symbol || 'BTCEUR',
adjustment: options.adjustment || 'splits',
};
if (options.session) symbolInit.session = options.session;
if (options.currency) symbolInit['currency-id'] = options.currency;
const from = options.from || Math.floor(new Date(2010, 1, 1) / 1000);
const to = options.to || Math.floor(Date.now() / 1000);
this.#client.send('request_history_data', [
this.#historySessionID,
0, // what is this?
`=${JSON.stringify(symbolInit)}`,
options.timeframe || '5',
0, // what is this?
{ from_to: { from, to } },
indicator.type,
getInputs(indicator),
[], // what is this?
]);
}
/**
* When a deep backtest history is loaded
* @param {() => void} cb
* @event
*/
onHistoryLoaded(cb) {
this.#callbacks.historyLoaded.push(cb);
}
/**
* When deep backtest history error happens
* @param {(...any) => void} cb Callback
* @event
*/
onError(cb) {
this.#callbacks.error.push(cb);
}
/** @type {HistorySessionBridge} */
#historySession = {
sessionID: this.#historySessionID,
send: (t, p) => this.#client.send(t, p),
};
/** Delete the chart session */
delete() {
this.#client.send('history_delete_session', [this.#historySessionID]);
delete this.#client.sessions[this.#historySessionID];
}
};