amv-dev / yata

Yet Another Technical Analysis library [for Rust]
Apache License 2.0
321 stars 49 forks source link

How do I use this with CSV data #23

Closed whyCPPgofast closed 2 years ago

whyCPPgofast commented 2 years ago

Hi,

Looking at docs its really hard to tell how to import CSV data (Date,Open,High,Low,Close,Volume) and which struct to use for it.

I was looking at the RandomCandles struct and had no idea how to make one thats not actually random.

Please show an example of working with a CSV file and constructing candles off of it.

amv-dev commented 2 years ago

Hello! Strictly speaking, you can't load data from CSV using this library. This library is all about what you would do after you load your data. If you need to process a CSV file, you can try https://docs.rs/csv/latest/csv/.

You can use default Candle structure to hold your data. It already implements all required traits like OHLCV. Or you can create your own structure and implement OHLCV for it. You can also hold data in raw types like [f64; 5] or (f64, f64, f64, f64, f64) as it already implement OHLCV https://docs.rs/yata/0.6.0/yata/core/trait.OHLCV.html#foreign-impls.

whyCPPgofast commented 2 years ago

Yeah the problem is that I can load data from a CSV and initialize Candle structs. Its when I try to iterate over them (in a vec) I get like 10 errors.

amv-dev commented 2 years ago

I don't understand your problem. Please, provide some additional information, like errors you get and code you're trying to run.

To iterate over whole vector you can use IndicatorConfig::over or IndicatorInstance::over for indicators and Method::new_over for methods.

whyCPPgofast commented 2 years ago

Sorry for being vague, here is my code.

use yata::prelude::*;
use yata::core::Candle;
use yata::indicators::MACD;

fn main() {

    let mut macd = MACD::default();

    let test: Candle = (10.0, 10.0, 10.0, 10.0, 1.0).into();

    let klines = vec![];

    for x in 0..10 {
        let test: Candle = (1.0, 1.0, 1.0, 1.0, 1.0).into();
        klines.push(test);
    }

    let mut macd = macd.init(&klines.first().unwrap()).unwrap();

    for kline in klines.take(10) {
        let result = macd.next(&kline);
        println!("{:?}", result);
    }
}
line:20
the method `take` exists for struct `std::vec::Vec<yata::core::Candle>`, but its trait bounds were not satisfied

method cannot be called on `std::vec::Vec<yata::core::Candle>` due to unsatisfied trait bounds

note: the following trait bounds were not satisfied:
      `std::vec::Vec<yata::core::Candle>: std::iter::Iterator`
      which is required by `&mut std::vec::Vec<yata::core::Candle>: std::iter::Iterator`
      `[yata::core::Candle]: std::iter::Iterator`
      which is required by `&mut [yata::core::Candle]: std::iter::Iterator`
amv-dev commented 2 years ago

You run into errors which are not consequences of using this library. Only iterators have method take(usize), so you need to convert your Vec into iterator first. There are several ways to do this, but the simplest is just call iter method. Here is working example.

use yata::indicators::MACD;
use yata::prelude::*;

fn main() {
    let macd = MACD::default();

    let mut klines = vec![];

    for _ in 0..10 {
        let test = (1.0, 1.0, 1.0, 1.0, 1.0);
        klines.push(test);
    }

    let mut macd = macd.init(klines.first().unwrap()).unwrap();

    for kline in klines.iter().take(10) {
        let result = macd.next(kline);
        println!("{:?}", result);
    }
}
whyCPPgofast commented 2 years ago

Thanks!