#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_imports)]
use crate::indicators::SimpleMovingAverage;
use crate::{Next};
use std::collections::VecDeque;
use std::collections::HashMap;
use std::rc::Rc;
use std::cell::RefCell;
use crate::errors::Error;
#[derive(Debug, Clone)]
pub enum DataPoint {
Ohlcv(Ohlcv),
BidAsk(BidAsk),
Frame(Frame),
}
#[derive(Debug, Clone)]
pub struct Ohlcv {
timestamp: u64,
open: f64,
high: f64,
low: f64,
close: f64,
volume: f64,
}
impl Ohlcv {
pub fn new() -> Self {
Self {
timestamp: 0,
open: 0.0,
high: 0.0,
low: 0.0,
close: 0.0,
volume: 0.0,
}
}
}
#[derive(Debug, Clone)]
pub struct BidAsk {
timestamp: u64,
price: f64,
amount: f64,
}
impl BidAsk {
pub fn new() -> Self {
Self {
timestamp: 0,
price: 0.0,
amount: 0.0,
}
}
}
#[derive(Debug, Clone)]
pub struct Frame {
timestamp: u64,
data: f64,
}
impl Frame {
pub fn new() -> Self {
Self {
timestamp: 0,
data: 0.0,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum SlotType {
Input,
Output,
}
#[derive(Debug, Clone)]
pub struct Slot {
pub name: String,
slot_type: SlotType,
state: f64,
pub changed: bool,
connected: bool,
connections: Vec<Rc<RefCell<Slot>>>,
}
impl Slot {
pub fn new(slot_type: SlotType) -> Self {
Self {
name: String::from("slot"),
slot_type: slot_type,
state: 0.0,
changed: false,
connected: false,
connections: vec![],
}
}
pub fn connect(&mut self, wire: Rc<RefCell<Slot>>) -> Result<(), Error> {
if self.slot_type == SlotType::Output {
return Ok(())
}
Err("The error message".into())
}
pub fn put(&mut self, val: f64) {
if self.state != val {
self.changed = true;
}
self.state = val;
}
pub fn get(&mut self) -> f64 {
self.changed = false;
self.state
}
}
pub type SlotPtr = *mut Slot;
#[derive(Debug, Clone)]
pub struct Indicator {
inputs: HashMap<String, Slot>,
outputs: HashMap<String, Slot>,
}
impl Indicator {
pub fn new() -> Self {
Self {
inputs: HashMap::new(),
outputs: HashMap::new(),
}
}
pub fn slot(&self, name: &str) -> Option<Rc<RefCell<Slot>>> {
None
}
pub fn stuff(&self) {
}
}
#[derive(Debug, Clone)]
pub enum InputType {
Ohlcv,
BidAsk,
Frame,
}
#[derive(Debug, Clone)]
pub struct Input {
input_type: InputType,
slots: Vec<Slot>,
timeseries: VecDeque<DataPoint>,
}
impl Input {
pub fn new(input_type: InputType) -> Self {
Self {
input_type: input_type,
slots: vec![],
timeseries: VecDeque::new(),
}
}
pub fn slot(&self, name: &str) -> Option<Rc<RefCell<Slot>>> {
None
}
pub fn push(&mut self, item: DataPoint) {
match item {
DataPoint::Ohlcv(_) => {
self.timeseries.push_back(item);
},
_ => {
print!("unhandled node type")
},
}
}
}
#[derive(Debug, Clone)]
pub struct View {
plots: Vec<Rc<Plot>>,
}
impl View {
pub fn new() -> Self {
Self {
plots: vec![],
}
}
pub fn attach(&mut self, plot: Rc<Plot>){
self.plots.push(plot);
}
}
#[derive(Debug, Clone)]
pub struct Plot {
view: Option<View>,
timeseries: VecDeque<Frame>,
}
impl Plot {
pub fn new() -> Self {
Self {
view: None,
timeseries: VecDeque::new(),
}
}
pub fn draw(&self){
}
pub fn slot(&self, name: &str) -> Option<Rc<RefCell<Slot>>> {
None
}
}
enum Node {
Input(Rc<Input>),
Indicator(Rc<Indicator>),
Plot(Rc<Plot>),
View(Rc<View>),
Text(String),
}
pub fn example() {
let mut input: Input = Input::new(InputType::Ohlcv);
let indicator: Indicator = Indicator::new();
let plot: Plot = Plot::new();
let mut view: View = View::new();
let input_close = input.slot("close").unwrap();
let mut input_close = input_close.borrow_mut();
{
let indicator_close = indicator.slot("close").unwrap();
let _ = input_close.connect(Rc::clone(&indicator_close));
}
let indicator_output = indicator.slot("output").unwrap();
let mut indicator_output = indicator_output.borrow_mut();
{
let plot_close = plot.slot("close").unwrap();
let _ = indicator_output.connect(Rc::clone(&plot_close));
}
let rcplot = Rc::new(plot);
view.attach(Rc::clone(&rcplot));
let a: &Plot = rcplot.as_ref();
a.draw();
input.push(DataPoint::Ohlcv(Ohlcv{
timestamp: 1,
open: 0.1,
high: 0.1,
low: 0.1,
close: 0.1,
volume: 0.1,
}));
let workspace: Vec<Node> = vec![
Node::Input(Rc::new(input)),
Node::Indicator(Rc::new(indicator)),
Node::Plot(rcplot),
Node::View(Rc::new(view)),
];
for item in workspace.iter() {
match item {
Node::View(val) => {
let a: &View = val;
},
Node::Plot(val) => {
let a: &Plot = val;
},
Node::Text(val) => {
let a: String = val.to_string();
},
_ => {
print!("unhandled node type")
},
}
}
}