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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//! # Xylex spread tracker
//!
//! `spread_tracker` is a library for tracking the spread of various symbols in the forex market.
//!
//! Every forex broker has a different spread for each symbol. This library is used to track the spread of various symbols in the forex market.
//! Retrieve & scrape the spread of various symbols from different brokers.
//!
//!
//!
//! ### Features
//! - Track the spread of various symbols in the forex market.
//! - Retrieve & scrape the spread of various symbols from different brokers.
//! - Store the spread data in a database.
//! - Cache the spread data to minimize the number of requests to the broker.
//!
//! ### Granularity
//! - Query the spread of all symbols from all listed brokers.
//! - Query the spread of a specific symbol from all listed brokers.
//! - Query the spread of all symbols from a specific broker.
//! - Query the spread of a specific symbol from a specific broker.
//!
//!
//! ### Usage
//! Add this to your `Cargo.toml`:
//! ```toml
//! [dependencies]
//! spread_tracker = "0.1.0"
//! ```
//!
//! ### How to assign the correct Broker type
//! ```rust
//! use spread_tracker::config::{
//! SpreadBrokerUrl,
//! Brokers
//! };
//! ```
//!
//! ### How to get their spreads
//!
//!
//! <div class="warning">
//! This is currently strictly available for asynchronous context!
//! </div>
//!
//!
//! ```rust
//! use spread_tracker::config::SpreadBrokerUrl;
//! use spread_tracker::model::{
//! Symbol,
//! SymbolSpread
//! };
//!
//! let config: SpreadBrokerUrl = SpreadBrokerUrl::new();
//!
//! // In this example, we are tracking the spread of the symbols from the Vantage and EightCap brokers.
//! let brokers: Vec<Brokers> = vec![
//! Brokers::Vantage,
//! Brokers::EightCap,
//! ];
//!
//! // Get the spread of the symbols from the brokers
//! let spread: Value = SpreadTracker::get_spread(
//! config,
//! brokers
//! ).await.unwrap();
//!
//! println!("Spread: {:#?}", spread);
//!
//! Result:
//!
//! {
//! "spread": {
//! "eightcap": [
//! {
//! "ask": 2197.92,
//! "bid": 2209.92,
//! "spread": 12.0,
//! "symbol": "XAUUSD"
//! },
//! {
//! "ask": 2031.57,
//! "bid": 2060.8973,
//! "spread": 29.3273,
//! "symbol": "XAUEUR"
//! },
//! {
//! ---------------- cut for brevity ----------------
//! ```
//! ### Return type
//! The return type is a `serde_json::Value` object, which is a JSON object.
//!
//! #### Structure
//! Object[spread]Vector[broker] -> Object[symbol, ask, bid, spread]
//!
//! * `spread` is the key for the spread data.
//! * `broker` is the key for the broker name, which will differ based on the broker.
//!
//! - Notes:
//! A Vector of objects is sometimes referred to as a list of objects. It is a collection of objects that are stored in no particular order.
//!
//! ### Configuration
//! The `config.yaml` is used to store the broker URLs for the spread tracking, and the `model.rs` is used to store the data model for the spread tracking. The `config.yaml` file should be in the following format:
//! ```yaml
//! BrokerSpreadUrls:
//! Vantage: "https://www.vantagefx.com/trading-info/forex-market-hours/"
//! MyFxBook: "https://www.myfxbook.com/forex-broker-quotes/vantage/6052"
//! ```
//!
//! Where `Vantage` and `MyFxBook` are the broker names and the URLs are the URLs of the brokers for spread tracking, derived from the MyFxBook website.
//!
//!
//!
//! ### Caching
//! wip
//! You can use the `db` module to store the spread data in a database.
//! Currently, the library relies on `supabase_rs` to store the data in a Supabase database.
//!
//! If you want to use a different database, you can implement the `Db` trait for your database.
//! Or simply call the `store_spread` method in the `model.rs` file to store the spread data in your database via a manual file like `.json`
//!
//!
//! ### Asynchronous vs Synchronous API
//! The main library is already asynchronous, so you can use it in an asynchronous context.
//! Sync API is **not** due to more testing being needed
//!
//!
//!
//! ### Database
//! wip
//! You can use the `db` module to store the spread data in a database.
//! Currently, the library relies on `supabase_rs` to store the data in a Supabase database.
//!
//! If you want to use a different database, you can implement the `Db` trait for your database.
//! Or simply call the `store_spread` method in the `model.rs` file to store the spread data in your database via a manual file like `.json`
//!
//! ### Errors
//!
//! <div class="warning">
//! Custom type errors are not yet implemented!
//! </div>
//!
//! * `url_invalid` will be returned if the URL is invalid.
//! * `url_not_reachable` will be returned if the URL is not reachable.
//! * `url_not_found` will be returned if the URL is not found.
//! * `invalid_body` will be returned if the body is invalid.
//! * `internal_error` will be returned if there is an internal error.
//! * `empty_body` will be returned if the body is empty.
//! * `could_not_extract_broker_name` will be returned if the broker name could not be extracted from the URL.
//! * `failed_to_open_config` will be returned if the config.yaml file is not found.
//! * `failed_to_read_yaml` will be returned if the file is not in the correct format.
//! * `failed_to_parse_symbol_spread` will be returned if the Vec<String> could not be parsed into a Vec<SymbolSpread>.
//! * `failed_to_parse_symbol` will be returned if the Symbol could not be parsed from the string.
//! * `failed_to_parse_ask_price` will be returned if the ask price could not be parsed from the string.
//! * `failed_to_parse_bid_price` will be returned if the bid price could not be parsed from the string.
//! * `failed_to_parse_spread` will be returned if the spread could not be parsed from the string.
//! * `could_not_retrieve_spread_data` will be returned if the spread data could not be retrieved from the broker URL.
//! * `failed_to_bind_json_object_to_key` will be returned if the JSON object could not be bound to the key.
//!
//!
//!
//! ### Modules
//! - `db`: This module is used to store the spread data in a database.
//! - `caching`: This module is used to cache the spread data to minimize the number of requests to the broker.
//! - `utils`: This module is used to save the spread data to a `.json` file.
//! - `config`: This module is used to load the configuration from the `config.yaml` file.
//! - `model`: This module is used to store the data model for the spread tracking.
//! - `errors`: This module is used to handle the errors in the library.
//!
// import the necessary modules into the hierarchy
// import the necessary external crates into the hierarchy
use File;
use Write;
use Error as StdError;
use get;
use ;
// import the necessary modules into the hierarchy
use cratefind_symbol_spread;
use crate;
use crate;
use crate;
/// ### The `SpreadTracker` struct is used to track the spread of various symbols in the forex market.
///
/// This struct is used to track the spread of various symbols in the forex market.
///