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
#![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),
}

// used for input node
#[derive(Debug, Clone)]
pub struct Ohlcv {
    // for syncing
    timestamp: u64,
    // values
    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 {
    // for syncing
    timestamp: u64,
    // price
    price: f64,
    // value
    amount: f64,
}

impl BidAsk {
    pub fn new() -> Self {
        Self {
            timestamp: 0,
            price: 0.0,
            amount: 0.0,
        }
    }
}

// is dataframe
#[derive(Debug, Clone)]
pub struct Frame {
    // for syncing
    timestamp: u64,
    // value
    data: f64,
}

impl Frame {
    pub fn new() -> Self {
        Self {
            timestamp: 0,
            data: 0.0,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum SlotType {
    Input,
    Output,
}

// define slot
#[derive(Debug, Clone)]
pub struct Slot {
    pub name: String,
    slot_type: SlotType,
    state: f64, // used to store value when it input type
    pub changed: bool, // when it input type,
    connected: bool, // when in input type,
    connections: Vec<Rc<RefCell<Slot>>>, // used to keep references when it output type
}

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![],
        }
    }
    // output is multiple, input is single
    pub fn connect(&mut self, wire: Rc<RefCell<Slot>>) -> Result<(), Error> {
        if self.slot_type == SlotType::Output {
            return Ok(())
        }
        // self.wire = Some(wire)
        Err("The error message".into())
    }
    // pub fn wire(&self) -> Option<Rc<RefCell<Wire>>> {
    //     // &self.wire
    //     None
    // }

    // put data into buffer
    pub fn put(&mut self, val: f64) {
        if self.state != val {
            self.changed = true;
        }
        self.state = val;
    }

    // fetch data from buffer 
    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(_) => {
                // let a: Ohlcv = val;
                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(),
        }
    }
    // draw to view
    pub fn draw(&self){

    }

    pub fn slot(&self, name: &str) -> Option<Rc<RefCell<Slot>>> {
        None
    }
}

enum Node {
    // Wire(Rc<RefCell<Wire>>),
    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 input_wire: Rc<RefCell<Wire>> = Rc::new(RefCell::new(Wire::new()));
    // let output_wire: Rc<RefCell<Wire>> = Rc::new(RefCell::new(Wire::new()));
    let mut view: View = View::new();

    // input -> indicator
    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));
    }
        
    // // indicator -> plot
    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);
    // put plot into view
    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,
    }));

    // collect all to workspace
    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")
            },
        }
    } 
}

// pub struct Inticator {
//     _impl: Box<dyn Next<f64, Output = Box<[f64]>>>,
// }

// impl Inticator {
//     pub fn new(name: &str) -> Result<Inticator, JsValue> {
//         match name {
//             // 0 => Err(Error::from_kind(ErrorKind::InvalidParameter)),
//             "ichimoku" => {
//                 Ok(Inticator {
//                     _impl: Box::new(SimpleMovingAverage::default()),
//                 })
//             }
//             _ => {
//                 Ok(Inticator {
//                     _impl: Box::new(SimpleMovingAverage::default()),
//                 })
//             }
//         }
//     }

//     pub fn get_contents(&self) -> u32 {
//         0
//     }
// }