use std::sync::Once;
use std::fs;
use std::collections::HashMap;
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use super::{Close, High, Low, Open, Volume};
#[derive(Debug, PartialEq)]
pub struct Bar {
open: f64,
high: f64,
low: f64,
close: f64,
volume: f64,
}
impl Bar {
pub fn new() -> Self {
Self {
open: 0.0,
close: 0.0,
low: 0.0,
high: 0.0,
volume: 0.0,
}
}
pub fn high<T: Into<f64>>(mut self, val: T) -> Self {
self.high = val.into();
self
}
pub fn low<T: Into<f64>>(mut self, val: T) -> Self {
self.low = val.into();
self
}
pub fn close<T: Into<f64>>(mut self, val: T) -> Self {
self.close = val.into();
self
}
pub fn volume(mut self, val: f64) -> Self {
self.volume = val;
self
}
}
impl Open for Bar {
fn open(&self) -> f64 {
self.open
}
}
impl Close for Bar {
fn close(&self) -> f64 {
self.close
}
}
impl Low for Bar {
fn low(&self) -> f64 {
self.low
}
}
impl High for Bar {
fn high(&self) -> f64 {
self.high
}
}
impl Volume for Bar {
fn volume(&self) -> f64 {
self.volume
}
}
pub fn round(num: f64) -> f64 {
(num * 1000.0).round() / 1000.00
}
static INIT: Once = Once::new();
#[derive(Debug, Serialize, Deserialize)]
pub struct Slot {
pub input: u32,
pub options: u32,
pub output: u32,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Test {
pub options: Vec<f64>,
pub inputs: Vec<Vec<f64>>,
pub outputs: Vec<Vec<f64>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct TestIndicator {
pub name: String,
pub description: String,
#[serde(rename = "type")]
pub indicatortype: String,
pub slots: Slot,
pub options: String,
pub tests: Vec<Test>,
}
lazy_static! {
pub static ref TESTS: HashMap<String, TestIndicator> = {
let contents = fs::read_to_string("tests/data.json")
.expect("Something went wrong reading the file");
serde_json::from_str(&contents).unwrap()
};
}
pub fn initialize() {
INIT.call_once(|| {
if TESTS.len() == 0 {
println!("Tests uninitialized")
}
});
}