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
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};

#[cfg(not(test))] use chrono::{DateTime, TimeZone};
use lazy_static::lazy_static;
use log::debug;
use regex::Regex;

use crate::config::Config;
use crate::core::GenericResult;
use crate::currency::Cash;
use crate::db;
#[cfg(not(test))] use crate::util;

use self::cache::Cache;
use self::finnhub::Finnhub;
use self::moex::Moex;
use self::twelvedata::TwelveData;

mod alphavantage;
mod cache;
mod finnhub;
mod moex;
mod twelvedata;

pub struct Quotes {
    cache: Cache,
    providers: Vec<Box<dyn QuotesProvider>>,
    batched_symbols: RefCell<HashSet<String>>,
}

impl Quotes {
    pub fn new(config: &Config, database: db::Connection) -> GenericResult<Quotes> {
        let finnhub = config.finnhub.as_ref().ok_or(
            "Finnhub configuration is not set in the configuration file")?;

        let twelvedata = config.twelvedata.as_ref().ok_or(
            "Twelve Data configuration is not set in the configuration file")?;

        Ok(Quotes::new_with(Cache::new(database, config.cache_expire_time), vec![
            Box::new(Finnhub::new(&finnhub.token)),
            Box::new(TwelveData::new(&twelvedata.token)),
            Box::new(Moex::new()),
        ]))
    }

    fn new_with(cache: Cache, providers: Vec<Box<dyn QuotesProvider>>) -> Quotes {
        Quotes {
            cache: cache,
            providers: providers,
            batched_symbols: RefCell::new(HashSet::new()),
        }
    }

    pub fn batch(&self, symbol: &str) {
        self.batched_symbols.borrow_mut().insert(symbol.to_owned());
    }

    pub fn get(&self, symbol: &str) -> GenericResult<Cash> {
        if let Some(price) = self.cache.get(symbol)? {
            return Ok(price);
        }

        self.batch(symbol);
        let mut batched_symbols = self.batched_symbols.borrow_mut();

        let mut price = None;

        for provider in &self.providers {
            let quotes = {
                let symbols: Vec<&str> = batched_symbols.iter().filter_map(|symbol| {
                    let is_currency_pair = is_currency_pair(&symbol);

                    if
                        provider.supports_stocks() && !is_currency_pair ||
                        provider.supports_forex() && is_currency_pair
                    {
                        Some(symbol.as_str())
                    } else {
                        None
                    }
                }).collect();

                if symbols.is_empty() {
                    continue;
                }

                debug!("Getting quotes from {} for the following symbols: {}...",
                       provider.name(), symbols.join(", "));

                provider.get_quotes(&symbols).map_err(|e| format!(
                    "Failed to get quotes from {}: {}", provider.name(), e))?
            };

            for (other_symbol, other_price) in quotes.iter() {
                let mut other_price = *other_price;

                // Some providers return stock quotes with unnecessary very high precision, so add
                // rounding here. But don't round Forex pairs since we always round conversion
                // result + reverse pairs always need high precision.
                if provider.high_precision() && !is_currency_pair(other_symbol) {
                    let rounded_price = other_price.round();
                    let round_precision =
                        (other_price.amount - rounded_price.amount).abs() / other_price.amount;

                    if round_precision < dec!(0.0001) {
                        other_price = rounded_price;
                    }
                };

                if *other_symbol == symbol {
                    price.replace(other_price);
                }

                self.cache.save(&other_symbol, other_price)?;
                batched_symbols.remove(other_symbol);
            }

            if batched_symbols.is_empty() {
                break;
            }
        }

        if !batched_symbols.is_empty() {
            let symbols = batched_symbols.iter().cloned().collect::<Vec<String>>();
            return Err!("Unable to find quotes for following symbols: {}", symbols.join(", "));
        }

        Ok(price.unwrap())
    }
}

type QuotesMap = HashMap<String, Cash>;

trait QuotesProvider {
    fn name(&self) -> &'static str;
    fn supports_stocks(&self) -> bool {true}
    fn supports_forex(&self) -> bool {true}
    fn high_precision(&self) -> bool {false}
    fn get_quotes(&self, symbols: &[&str]) -> GenericResult<QuotesMap>;
}

pub fn get_currency_pair(base: &str, quote: &str) -> String {
    format!("{}/{}", base, quote)
}

fn is_currency_pair(symbol: &str) -> bool {
    parse_currency_pair(symbol).is_ok()
}

fn parse_currency_pair(pair: &str) -> GenericResult<(&str, &str)> {
    lazy_static! {
        static ref REGEX: Regex = Regex::new(r"^(?P<base>[A-Z]{3})/(?P<quote>[A-Z]{3})$").unwrap();
    }

    let captures = REGEX.captures(pair).ok_or_else(|| format!(
        "Invalid currency pair: {:?}", pair))?;

    Ok((
        captures.name("base").unwrap().as_str(),
        captures.name("quote").unwrap().as_str(),
    ))
}

#[cfg(not(test))]
fn is_outdated_quote<T: TimeZone>(date_time: DateTime<T>) -> bool {
    (util::utc_now() - date_time.naive_utc()).num_days() >= 5
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cache() {
        struct FirstProvider {
            request_id: RefCell<usize>,
        }

        impl QuotesProvider for FirstProvider {
            fn name(&self) -> &'static str {
                "first-provider"
            }

            fn get_quotes(&self, symbols: &[&str]) -> GenericResult<QuotesMap> {
                let mut symbols = symbols.to_vec();
                symbols.sort();

                assert_eq!(*self.request_id.borrow(), 0);
                assert_eq!(&symbols, &["BND", "BNDX", "VTI"]);
                *self.request_id.borrow_mut() += 1;

                let mut quotes = HashMap::new();
                quotes.insert(s!("BND"), Cash::new("USD", dec!(12.34)));
                quotes.insert(s!("VTI"), Cash::new("USD", dec!(56.78)));
                Ok(quotes)
            }
        }

        struct SecondProvider {
            request_id: RefCell<usize>,
        }

        impl QuotesProvider for SecondProvider {
            fn name(&self) -> &'static str {
                "second-provider"
            }

            fn get_quotes(&self, symbols: &[&str]) -> GenericResult<QuotesMap> {
                assert_eq!(*self.request_id.borrow(), 0);
                assert_eq!(symbols, ["BNDX"]);
                *self.request_id.borrow_mut() += 1;

                let mut quotes = HashMap::new();
                quotes.insert(s!("BNDX"), Cash::new("USD", dec!(90.12)));
                Ok(quotes)
            }
        }

        let (_database, cache) = Cache::new_temporary();
        let quotes = Quotes::new_with(cache, vec![
            Box::new(FirstProvider {request_id: RefCell::new(0)}),
            Box::new(SecondProvider {request_id: RefCell::new(0)}),
        ]);

        quotes.batch("VTI");
        quotes.batch("BNDX");
        assert_eq!(quotes.get("BND").unwrap(), Cash::new("USD", dec!(12.34)));

        quotes.batch("VXUS");
        assert_eq!(quotes.get("BND").unwrap(), Cash::new("USD", dec!(12.34)));
        assert_eq!(quotes.get("VTI").unwrap(), Cash::new("USD", dec!(56.78)));
        assert_eq!(quotes.get("BNDX").unwrap(), Cash::new("USD", dec!(90.12)));
    }
}