use serde::{Deserialize, Serialize};
pub fn get_screener(exchange: &str) -> String {
let uex = exchange.to_ascii_uppercase();
let uexs = uex.as_str();
match uexs {
"NASDAQ" | "NYSE" | "NYSE ARCA" | "OTC" => "america".to_string(), "ASX" => "australia".to_string(), "TSX" | "TSXV" | "CSE" | "NEO" => "canada".to_string(), "EGX" => "egypt".to_string(), "FWB" | "SWB" | "XETR" => "germany".to_string(), "BSE" | "NSE" => "india".to_string(), "TASE" => "israel".to_string(), "MIL" | "MILSEDEX" => "italy".to_string(), "LUXSE" => "luxembourg".to_string(), "NEWCONNECT" => "poland".to_string(), "NGM" => "sweden".to_string(), "BIST" => "turkey".to_string(), "LSE" | "LSIN" => "uk".to_string(), "HNX" => "vietnam".to_string(), "BINANCE" | "BITSTAMP" | "COINBASE" => "crypto".to_string(), _ => exchange.to_ascii_lowercase(), }
}
#[derive(Deserialize, Serialize, Debug)]
struct Symbol {
symbols: Symbols,
columns: Vec<String>,
}
#[derive(Deserialize, Serialize, Debug)]
struct Symbols {
tickers: Vec<String>,
query: Queries,
}
#[derive(Deserialize, Serialize, Debug)]
struct Queries {
types: Vec<i32>,
}
pub const BASE_INDICATORS: [&str; 1] = ["Recommend.All"];
pub async fn get_ta(symbols: Vec<&str>, interval: &str, indicators: Vec<&str>) -> f64 {
let client = reqwest::Client::new();
let converted_interval = match interval {
"1m" => "|1",
"5m" => "|5",
"15m" => "|15",
"30m" => "|30",
"1h" => "|60",
"2h" => "|120",
"4h" => "|240",
"1w" => "|1W",
"1M" => "|1M",
_ => "",
};
let changed_indicators: Vec<String> = indicators
.clone()
.into_iter()
.map(|x| String::from(x) + converted_interval)
.collect();
let json_data = Symbol {
symbols: Symbols {
tickers: symbols.iter().map(|x| x.to_string()).collect(),
query: Queries { types: vec![] },
},
columns: changed_indicators,
};
let url = format!(
"https://scanner.tradingview.com/{}/scan",
get_screener((symbols[0].split(':').collect::<Vec<&str>>())[0])
);
let data: serde_json::Value = client
.post(url)
.json(&json_data)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
data["data"][0]["d"][0].as_f64().unwrap_or(0.0)
}
#[test]
fn test_get_screener() {
assert_eq!(
get_screener("NYSE"),
"america",
"Input 'NYSE' should return 'america'"
);
assert_eq!(
get_screener("nyse"),
"america",
"Input 'nyse' should return 'america'"
);
assert_eq!(
get_screener("NYSE ARCA"),
"america",
"Input 'NYSE ARCA' should return 'america'"
);
assert_eq!(
get_screener("nyse arca"),
"america",
"Input 'nyse arca' should return 'america'"
);
assert_eq!(
get_screener("NASDAQ"),
"america",
"Input 'NASDAQ' should return 'america'"
);
assert_eq!(
get_screener("nasdaq"),
"america",
"Input 'nasdaq' should return 'america'"
);
assert_eq!(
get_screener("OTC"),
"america",
"Input 'OTC' should return 'america'"
);
assert_eq!(
get_screener("otc"),
"america",
"Input 'otc' should return 'america'"
);
assert_eq!(
get_screener("ASX"),
"australia",
"Input 'ASX' should return 'australia'"
);
assert_eq!(
get_screener("TSX"),
"canada",
"Input 'TSX' should return 'canada'"
);
assert_eq!(
get_screener("TSXV"),
"canada",
"Input 'TSXV' should return 'canada'"
);
assert_eq!(
get_screener("CSE"),
"canada",
"Input 'CSE' should return 'canada'"
);
assert_eq!(
get_screener("NEO"),
"canada",
"Input 'NEO' should return 'canada'"
);
assert_eq!(
get_screener("EGX"),
"egypt",
"Input 'EGX' should return 'egypt'"
);
assert_eq!(
get_screener("FWB"),
"germany",
"Input 'FWB' should return 'germany'"
);
assert_eq!(
get_screener("SWB"),
"germany",
"Input 'SWB' should return 'germany'"
);
assert_eq!(
get_screener("XETR"),
"germany",
"Input 'XETR' should return 'germany'"
);
assert_eq!(
get_screener("BSE"),
"india",
"Input 'BSE' should return 'india'"
);
assert_eq!(
get_screener("NSE"),
"india",
"Input 'NSE' should return 'india'"
);
assert_eq!(
get_screener("TASE"),
"israel",
"Input 'TASE' should return 'israel'"
);
assert_eq!(
get_screener("MIL"),
"italy",
"Input 'MIL' should return 'italy'"
);
assert_eq!(
get_screener("MILSEDEX"),
"italy",
"Input 'MILSEDEX' should return 'italy'"
);
assert_eq!(
get_screener("LUXSE"),
"luxembourg",
"Input 'LUXSE' should return 'luxembourg'"
);
assert_eq!(
get_screener("NEWCONNECT"),
"poland",
"Input 'NEWCONNECT' should return 'poland'"
);
assert_eq!(
get_screener("NGM"),
"sweden",
"Input 'NGM' should return 'sweden'"
);
assert_eq!(
get_screener("BIST"),
"turkey",
"Input 'BIST' should return 'turkey'"
);
assert_eq!(get_screener("LSE"), "uk", "Input 'LSE' should return 'uk'");
assert_eq!(
get_screener("LSIN"),
"uk",
"Input 'LSIN' should return 'uk'"
);
assert_eq!(
get_screener("HNX"),
"vietnam",
"Input 'HNX' should return 'vietnam'"
);
assert_eq!(
get_screener("foo"),
"foo",
"Input 'foo' should return 'foo'"
);
assert_eq!(
get_screener("FOO"),
"foo",
"Input 'FOO' should return 'foo'"
);
}