Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Candlestick Example #15

Closed
pferreira8 opened this issue Jul 26, 2023 · 4 comments
Closed

Candlestick Example #15

pferreira8 opened this issue Jul 26, 2023 · 4 comments

Comments

@pferreira8
Copy link

if I recall correctly, I remember at one point seeing an example for how to load data for candlesticks, but the example is now gone. Adding this back would be great since there's only the svg examples. I have a Polars Dataframe as my source.

@yuankunzhang
Copy link
Owner

Hi, the candlestick examples are still there. Please check this section in readme - https://github.com/yuankunzhang/charming/#candlestick-charts

@pferreira8
Copy link
Author

thanks! I will close the issue, was checking out the project on docs.rs and that might be why I didn't see it

@wangjiawen2013
Copy link

@pferreira8
Hi,
Do you know how to use polars dataframe in charming ? Is it compatible with charming dataframe ?

@pferreira8
Copy link
Author

pferreira8 commented Dec 14, 2023

@pferreira8 Hi, Do you know how to use polars dataframe in charming ? Is it compatible with charming dataframe ?

It is compatible once you convert the Dataframe into a Vec<Vec>
I have the logic split between two functions, it could be cleaned up for sure but this was my hacky method that worked.

// pass df into this function, assuming column names below or edit them to match properly
fn parse_ohlc_data(data: DataFrame) -> Result< (Vec<Vec<f64>>, Vec<String> ), Box<dyn std::error::Error> > {
       let d_open: Vec<f64> = data.column("open")?.f64()?.into_iter().map(|x| x.unwrap()).collect();
       let d_high: Vec<f64> = data.column("high")?.f64()?.into_iter().map(|x| x.unwrap()).collect();
       let d_low: Vec<f64> = data.column("low")?.f64()?.into_iter().map(|x| x.unwrap()).collect();
       let d_close: Vec<f64> = data.column("close")?.f64()?.into_iter().map(|x| x.unwrap()).collect();
       let dt: Vec<String> = data.column("date")?.iter().map(|x| x.to_string()).collect(); 
       let mut ohlc_vec: Vec<Vec<f64>> = Vec::new();
       for i in 0..d_open.len() {
            let array: [f64;4] = [d_open[i], d_high[i], d_low[i], d_close[i]]; 
            ohlc_vec.push(array.to_vec());
        }
        Ok((ohlc_vec, dt)) 
}
pub fn build_symbol_chart(symbol_name: &str, ohlc: (Vec<Vec<f64>>, Vec<String>)) -> Result<(), EchartsError> {
    let chart = Chart::new()
        .x_axis(Axis::new().show(true).data(ohlc.1))
        .y_axis(Axis::new().show(true).scale(true))
        .data_zoom(
            DataZoom::new()
                .type_(DataZoomType::Slider)
                .min_value_span(5),
        )
        .series(Candlestick::new().data(
            ohlc.0
        )).background_color("6E7468");

    let mut rend = HtmlRenderer::new("OHLCV", 1000, 800).theme(Westeros);
    let dir_file = format!("./html/charts/{}.html", symbol_name);
    HtmlRenderer::render(&rend, &chart)?;
    rend.save(&chart, std::path::Path::new(&dir_file))?;
    // println!("saving chart to html");
    
    Ok(())
}
// EXAMPLE
//converts polars df to work with charming
let ohlc = parse_ohlc_data(data).unwrap();

// creates an html file for candlestick charts
tools::chart::build_symbol_chart(YOUR_FILE_NAME_HERE, ohlc)?;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants