14 releases

0.4.0 Apr 8, 2024
0.3.6 Jan 15, 2024
0.3.0 Dec 30, 2023
0.2.9 Nov 26, 2023
0.1.9 Oct 26, 2023

#52 in Visualization

Download history 30/week @ 2024-02-15 77/week @ 2024-02-22 5/week @ 2024-02-29 9/week @ 2024-03-07 40/week @ 2024-03-14 6/week @ 2024-03-28 118/week @ 2024-04-04 22/week @ 2024-04-11

1,669 downloads per month

MIT license

8.5MB
3.5K SLoC

Finalytics

Crates.io Docs.rs License Homepage CodeFactor

Finalytics is a Rust library designed for retrieving financial data and performing security analysis and portfolio optimization.

Installation

Add the following to your Cargo.toml file:

[dependencies]
finalytics = {version = "0.4.0", features = ["kaleido"]}

Or run the following command:

cargo install finalytics --features kaleido

Documentation

View the library documentation on docs.rs or visit the homepage

Security Analysis

use std::error::Error;
use finalytics::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {

    // Instantiate the Ticker Object
    let ticker = TickerBuilder::new().ticker("AAPL")?
        .start_date("2023-01-01")
        .end_date("2023-02-01")
        .interval(Interval::OneDay)
        .benchmark_symbol("^GSPC")
        .confidence_level(0.95)
        .risk_free_rate(0.02)
        .build()?;

    // Fetch Ticker Data
    let quote = ticker.get_quote().await?;
    let stats = ticker.get_ticker_stats().await?;
    let chart = ticker.get_chart().await?;
    let options = ticker.get_options().await?;

    println!("{:?}", quote);
    println!("{:?}", stats);
    println!("{:?}", chart);
    println!("{:?}", options);

    // Fundamental Analysis
    let income_statement = ticker.income_statement().await?;
    let balance_sheet = ticker.balance_sheet().await?;
    let cash_flow = ticker.cashflow_statement().await?;
    let financial_ratios = ticker.financial_ratios().await?;
    let performance_stats = ticker.performance_stats().await?;
    let volatility_surface = ticker.volatility_surface().await?;

    println!("{:?}", income_statement);
    println!("{:?}", balance_sheet);
    println!("{:?}", cash_flow);
    println!("{:?}", financial_ratios);
    println!("{:?}", performance_stats);
    println!("{:?}", volatility_surface);

    // Technical Analysis
    let sma = ticker.sma(50).await?;
    let ema = ticker.ema(3).await?;
    let macd = ticker.macd(12, 26, 9).await?;
    let rsi = ticker.rsi(14).await?;
    let fs = ticker.fs(14).await?;
    let ss = ticker.ss(7, 3).await?;
    let ppo = ticker.ppo(12, 26, 9).await?;
    let roc = ticker.roc(1).await?;
    let mfi = ticker.mfi(14).await?;
    let bb = ticker.bb(20, 2.0).await?;
    let sd = ticker.sd(20).await?;
    let mad = ticker.mad(20).await?;
    let atr = ticker.atr(14).await?;
    let max = ticker.max(20).await?;
    let min = ticker.min(20).await?;
    let obv = ticker.obv().await?;

    println!("SMA:{:?}\nEMA:{:?}\nMACD:{:?}\nRSI:{:?}\nFS:{:?}\nSS:{:?}\nPPO:{:?}\nROC:{:?}\nMFI:{:?}\
            \nBB:{:?}\nSD:{:?}\nMAD:{:?}\nATR:{:?}\nMAX:{:?}\nMIN:{:?}\nOBV:{:?}\n",
             sma, ema, macd, rsi, fs, ss, ppo, roc, mfi, bb, sd, mad, atr, max, min, obv);

    // News Sentiment Analysis
    let news_sentiment = ticker.get_news(true).await?;
    println!("{:?}", news_sentiment);

    // Display Ticker Charts
    let candlestick_chart = ticker.candlestick_chart().await?;
    let performance_chart = ticker.performance_chart().await?;
    let volatility_charts = ticker.volatility_charts().await?;
    let summary_stats_table = ticker.summary_stats_table().await?;
    let performance_stats_table = ticker.performance_stats_table().await?;
    let financials_tables = ticker.financials_tables().await?;

    candlestick_chart.show();
    performance_chart.show();
    summary_stats_table.show();
    performance_stats_table.show();
    volatility_charts["Volatility Surface"].show();
    volatility_charts["Volatility Smile"].show();
    volatility_charts["Volatility Term Structure"].show();
    financials_tables["Income Statement"].show();
    financials_tables["Balance Sheet"].show();
    financials_tables["Cashflow Statement"].show();
    financials_tables["Financial Ratios"].show();

    Ok(())

}

Portfolio Optimization

use std::error::Error;
use finalytics::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
   // Construct the Portfolio Object
    let ticker_symbols = Vec::from(["NVDA", "BRK-A", "AAPL", "ZN=F"]);
    let portfolio = PortfolioBuilder::new().ticker_symbols(ticker_symbols)
                                            .benchmark_symbol("^GSPC")
                                            .start_date("2017-01-01")
                                            .end_date("2023-01-01")
                                            .interval(Interval::OneDay)
                                            .confidence_level(0.95)
                                            .risk_free_rate(0.02)
                                            .max_iterations(1000)
                                            .objective_function(ObjectiveFunction::MaxSharpe)
                                            .build().await?;

    // Display Portfolio Optimization Results
    println!("{:?}", portfolio.performance_stats);

   // Display Portfolio Analytics Charts
    portfolio.optimization_chart()?.show();
    portfolio.performance_chart()?.show();
    portfolio.asset_returns_chart()?.show();
    portfolio.performance_stats_table()?.show();

    Ok(())
}

Sample Applications

Ticker Charts Viewer

This sample application allows you to perform security analysis based on the Finalytics Library.

Portfolio Charts Viewer

This sample application enables you to perform portfolio optimization based on the Finalytics Library.

Telegram Bot

The Finalytics Telegram Bot allows you to perform security analysis, portfolio optimization and news sentiment analysis directly from Telegram.

Python Binding

pypi License Homepage Documentation Status Platform Python Version PePy CodeFactor

Installation

pip install finalytics

Documentation

View Library documentation on readthedocs here

Security Analysis

from finalytics import Ticker

ticker = Ticker(symbol="AAPL")
print(ticker.get_current_price())
print(ticker.get_summary_stats())
print(ticker.get_price_history(start="2023-01-01", end="2023-10-31", interval="1d"))
print(ticker.get_options_chain())
print(ticker.get_news(compute_sentiment=True))
print(ticker.get_income_statement())
print(ticker.get_balance_sheet())
print(ticker.get_cashflow_statement())
print(ticker.get_financial_ratios())
print(ticker.compute_performance_stats(start="2023-01-01", end="2023-10-31", interval="1d", benchmark="^GSPC", 
                                       confidence_level=0.95, risk_free_rate=0.02))
ticker.display_performance_chart(start="2023-01-01", end="2023-10-31", interval="1d", benchmark="^GSPC", 
                                 confidence_level=0.95, risk_free_rate=0.02, display_format="notebook")
ticker.display_candlestick_chart(start="2023-01-01", end="2023-10-31", interval="1d", display_format="html")
ticker.display_options_chart(risk_free_rate=0.02, chart_type="surface", display_format="png")

Portfolio Optimization

from finalytics import Portfolio

portfolio = Portfolio(ticker_symbols=["AAPL", "GOOG", "MSFT", "BTC-USD"], 
                      benchmark_symbol="^GSPC", start_date="2020-01-01", end_date="2022-01-01", interval="1d", 
                      confidence_level=0.95, risk_free_rate=0.02, max_iterations=1000, 
                      objective_function="max_sharpe")
print(portfolio.get_optimization_results())
portfolio.display_portfolio_charts("performance", "html")

Dependencies

~82MB
~1.5M SLoC