-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcs_forex.js
303 lines (274 loc) · 9.46 KB
/
fcs_forex.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
class FCSForex {
constructor(api_key = '') {
this.api_key = api_key ? api_key : "api_key";
this.output = ''; // default is json
this.output_type = 'JSON'; // Default output type JSON
this.basic_url = "https://fcsapi.com/api-v3/forex";
this.api_message = "API Key is empty, please set your API Key.";
}
returnError(msg) {
return { status: false, msg: msg, Error: "Code or input data error" };
}
checkApiKey() {
return this.api_key !== null && this.api_key !== 'api_key';
}
checkSymbolId(txt) {
const cleanedTxt = txt.replace(/,/g, "");
return /^\d+$/.test(cleanedTxt) ? 'id' : 'symbol';
}
async response(url, params) {
if (!this.checkApiKey()) {
return this.returnError(this.api_message);
}
if (this.api_key) {
params['access_key'] = this.api_key;
}
if (this.output) {
params['output'] = this.output;
}
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams(params)
});
const responseData = await response.json();
return responseData;
} catch (error) {
return this.returnError(error.message);
}
}
/**
* use to get all supporting symbols
* @param {*} type set to forex or crypto
* @returns
*/
async getSymbolsList(type = 'forex') {
if (type !== 'forex' && type !== 'crypto') {
type = 'forex';
}
const params = { type: type };
const link = `${this.basic_url}/list`;
return await this.response(link, params);
}
/**
* get symbol profile details
* @param {*} symbol set currency symbol name e.g: EUR/USD
* @returns
*/
async getProfile(symbol) {
symbol = Array.isArray(symbol) ? symbol.join(',') : symbol;
if (!symbol) {
return this.returnError("Symbol or Id not defined");
}
const symbolId = this.checkSymbolId(symbol);
const params = { [symbolId]: symbol };
const link = `${this.basic_url}/profile`;
return await this.response(link, params);
}
/**
* get convertion of currency
* @param {*} amount set amunt of currency
* @param {*} pairOne set currency symbol name e.g: EUR
* @param {*} pairTwo set currency symbol name e.g: USD
* @returns
*/
async getConverter(amount = '200', pairOne = '', pairTwo = '') {
if (!pairOne) {
return this.returnError("Symbol not defined");
}
const params = { amount: amount };
if (pairTwo) {
params['pair1'] = pairOne;
params['pair2'] = pairTwo;
} else {
params["symbol"] = pairOne;
}
const link = `${this.basic_url}/converter`;
return await this.response(link, params);
}
/**
* Use to get latest price of currency
* @param {*} symbol set currency symbol name e.g: EUR/USD
* @returns
*/
async getLatestPrice(symbol) {
symbol = Array.isArray(symbol) ? symbol.join(',') : symbol;
if (!symbol) {
return this.returnError("Symbol or Id not defined");
}
const symbolId = this.checkSymbolId(symbol);
const params = { [symbolId]: symbol };
const link = `${this.basic_url}/latest`;
return await this.response(link, params);
}
/**
* get Base Price
* @param {*} symbol set currency symbol name e.g: EUR/USD
* @param {*} type set to forex or crypto
* @param {*} time set to true or false
* @returns
*/
async getBasePrices(symbol, type = "forex", time = false) {
symbol = Array.isArray(symbol) ? symbol.join(',') : symbol;
if (!symbol) {
return this.returnError("Symbol or Id not defined");
}
const params = { symbol: symbol, type: type };
if (time) {
params['time'] = 1;
}
const link = `${this.basic_url}/base_latest`;
return await this.response(link, params);
}
/**
* get last candle
* @param {*} symbol set currency symbol name e.g: EUR/USD
* @param {*} period 1m, 5m, 15m, 30m,1h, 2h, 4h, 5h, 1d, 1w, month (all except 1m and 2h)
* @param {*} candle set to active, close and both, default: both
* @returns
*/
async getLastCandle(symbol, period = '1h', candle = 'both') {
symbol = Array.isArray(symbol) ? symbol.join(',') : symbol;
// Check if symbol is defined
if (!symbol) {
return this.returnError("Symbol or Id not defined");
}
const symbolId = this.checkSymbolId(symbol);
// Set up the parameters for the API call
const params = { [symbolId]: symbol, period: period, candle: candle };
const link = `${this.basic_url}/candle`;
return await this.response(link, params);
}
/**
* use to get history of currency
* @param {*} data set all params data
* @returns
*/
async getHistory(data) {
const id = data.id || '';
const symbol = data.symbol || '';
const period = data.period || '1h';
const level = (data.level > 3) ? 1 : Math.min(Math.max(data.level || 1, 1), 3);
const fromDate = data.from || '';
const toDate = data.to || '';
if (!symbol && !id) {
return this.returnError("Symbol or Id not defined");
}
const params = {
period: period,
limit: level
};
if (id) {
params['id'] = id;
}
if (symbol) {
params['symbol'] = symbol;
}
if (fromDate && toDate) {
params['from'] = fromDate;
params['to'] = toDate;
}
const link = `${this.basic_url}/history`;
return await this.response(link, params);
}
/**
* use to get signal
* @param {*} symbol pass currency name or id
* @param {*} period pass period
* @param {*} url pass to retrieve the signal
* @returns
*/
async getSignal(symbol, period, url) {
if (!symbol) {
return this.returnError("Symbol or Id not defined");
}
const result = !Array.isArray(symbol) && !symbol.includes(',');
if (!result) {
return this.returnError("Symbol only accept single id");
}
const symbolId = this.checkSymbolId(symbol);
const params = { [symbolId]: symbol, period: period };
const link = `${this.basic_url}/${url}`;
return await this.response(link, params);
}
/**
* get pivot points
* @param {*} symbol set currency symbol name e.g: EUR/USD
* @param {*} period set period (1m, 5m, 15m, 30m,1h, 2h, 4h, 5h, 1d, 1w, month)
* @returns
*/
async getPivotPoints(symbol, period = '1h') {
return await this.getSignal(symbol, period, "pivot_points");
}
/**
* get moving averages
* @param {*} symbol set currency symbol name e.g: EUR/USD
* @param {*} period set period (1m, 5m, 15m, 30m,1h, 2h, 4h, 5h, 1d, 1w, month)
* @returns
* */
async getMovingAverages(symbol, period = '1h') {
return await this.getSignal(symbol, period, "ma_avg");
}
/**
* get technical indicators
* @param {*} symbol set currency symbol name e.g: EUR/USD
* @param {*} period set period (1m, 5m, 15m, 30m,1h, 2h, 4h, 5h, 1d, 1w, month)
* @returns
*/
async getTechnicalIndicator(symbol, period = '1h') {
return await this.getSignal(symbol, period, "indicators");
}
/**
* get economic calendar
* @param {*} symbol set currency symbol name e.g: EUR/USD
* @param {*} country set country name e.g: US,JP,GB
* @param {*} fromDate set from date e.g: 2024-11-13
* @param {*} toDate set to date e.g: 2024-11-14
* @param {*} event set event name To get all history of event
* @returns
*/
async getEconomyCalendar(symbol = '', country = '', fromDate = '', toDate = '', event = '') {
if (!this.checkApiKey()) {
return this.returnError(this.api_message);
}
const params = { access_key: this.api_key };
if (fromDate) {
params['from'] = fromDate;
}
if (toDate) {
params['to'] = toDate;
}
if (symbol) {
params['symbol'] = symbol;
}
if (country) {
params['country'] = country;
}
if (event) {
params['event'] = event;
}
if (!symbol && !country && !event) {
return this.returnError("At least a symbol, country, or event must be defined");
}
const link = `${this.basic_url}/economy_cal`;
return await this.response(link, params);
}
/**
* get search query
* @param {*} search set search value e.g: EURO Dollar
* @param {*} strict set strict value to 0 AND 1 {0: search if any word exist, 1: search if all words exist}
* @returns
* */
async getSearchQuery(search, strict = 0) {
if (!search) {
return this.returnError("Search value is empty");
}
const params = { s: search, strict: strict };
const link = `${this.basic_url}/search`;
return await this.response(link, params);
}
}
module.exports = FCSForex;