Charting

Plotly NET

Introduction

Plotly.NET provides functions for generating and rendering plotly.js charts in .NET programming languages. Our .NET interactive notebooks support its C# implementation.

Import Libraries

Follow these steps to import the libraries that you need:

  1. Load the necessary assembly files.
  2. #r "../Plotly.NET.dll"
  3. Import the Plotly.NET and Plotly.NET.LayoutObjects packages.
  4. using Plotly.NET;
    using Plotly.NET.LayoutObjects;

Get Historical Data

Get some historical market data to produce the plots. For example, to get data for a bank sector ETF and some banking companies over 2021, run:

var qb = new QuantBook();
var tickers = new[] 
{
    "XLF",  // Financial Select Sector SPDR Fund
    "COF",  // Capital One Financial Corporation
    "GS",   // Goldman Sachs Group, Inc.
    "JPM",  // J P Morgan Chase & Co
    "WFC"   // Wells Fargo & Company   
}
var symbols = tickers.Select(ticker => qb.AddEquity(ticker, Resolution.Daily).Symbol);
var history = qb.History(symbols, new DateTime(2021, 1, 1), new DateTime(2022, 1, 1));

Create Candlestick Chart

You must import the plotting libraries and get some historical data to create candlestick charts.

In this example, you create a candlestick chart that shows the open, high, low, and close prices of one of the banking securities. Follow these steps to create the candlestick chart:

  1. Select a Symbol.
  2. var symbol = symbols.First();
  3. Call the Chart2D.Chart.Candlestick constructor with the time and open, high, low, and close price IEnumerable.
  4. var bars = history.Select(slice => slice.Bars[symbol]);
    var chart = Chart2D.Chart.Candlestick<decimal, decimal, decimal, decimal, DateTime, string>(
        bars.Select(x => x.Open),
        bars.Select(x => x.High),
        bars.Select(x => x.Low),
        bars.Select(x => x.Close),
        bars.Select(x => x.EndTime)
    );
  5. Call the Layout constructor and set the title, xaxis, and yaxis properties as the title and axes label objects.
  6. LinearAxis xAxis = new LinearAxis();
    xAxis.SetValue("title", "Time");
    LinearAxis yAxis = new LinearAxis();
    yAxis.SetValue("title", "Price ($)");
    Title title = Title.init($"{symbol} OHLC");
    
    Layout layout = new Layout();
    layout.SetValue("xaxis", xAxis);
    layout.SetValue("yaxis", yAxis);
    layout.SetValue("title", title);
  7. Assign the Layout to the chart.
  8. chart.WithLayout(layout);
  9. Show the plot.
  10. HTML(GenericChart.toChartHTML(chart));

    The Jupyter Notebook displays the candlestick chart.

Candlestick plot of XLF OHLC

Create Line Chart

You must import the plotting libraries and get some historical data to create line charts.

In this example, you create a line chart that shows the volume of a security. Follow these steps to create the chart:

  1. Select a Symbol.
  2. var symbol = symbols.First();
  3. Call the Chart2D.Chart.Line constructor with the timestamps and volumes.
  4. var bars = history.Select(slice => slice.Bars[symbol]);
    var chart = Chart2D.Chart.Line<DateTime, decimal, string>(
        bars.Select(x => x.EndTime),
        bars.Select(x => x.Volume)
    );
  5. Create a Layout.
  6. LinearAxis xAxis = new LinearAxis();
    xAxis.SetValue("title", "Time");
    LinearAxis yAxis = new LinearAxis();
    yAxis.SetValue("title", "Volume");
    Title title = Title.init($"{symbol} Volume");
    
    Layout layout = new Layout();
    layout.SetValue("xaxis", xAxis);
    layout.SetValue("yaxis", yAxis);
    layout.SetValue("title", title);
  7. Assign the Layout to the chart.
  8. chart.WithLayout(layout);
  9. Show the plot.
  10. HTML(GenericChart.toChartHTML(chart));

    The Jupyter Notebook displays the line chart.

Create Scatter Plot

You must import the plotting libraries and get some historical data to create scatter plots.

In this example, you create a scatter plot that shows the relationship between the daily price of two securities. Follow these steps to create the scatter plot:

  1. Select two Symbol objects.
  2. var symbol1 = symbols.First();
    var symbol2 = symbols.Last();
  3. Call the Chart2D.Chart.Point constructor with the closing prices of both securities.
  4. var chart = Chart2D.Chart.Point<decimal, decimal, string>(
        history.Select(slice => slice.Bars[symbol1].Close),
        history.Select(slice => slice.Bars[symbol2].Close)
    );
  5. Create a Layout.
  6. LinearAxis xAxis = new LinearAxis();
    xAxis.SetValue("title", $"{symbol1} Price ($)");
    LinearAxis yAxis = new LinearAxis();
    yAxis.SetValue("title", $"{symbol2} Price ($)");
    Title title = Title.init($"{symbol1} vs {symbol2}");
    
    Layout layout = new Layout();
    layout.SetValue("xaxis", xAxis);
    layout.SetValue("yaxis", yAxis);
    layout.SetValue("title", title);
  7. Assign the Layout to the chart.
  8. chart.WithLayout(layout);
  9. Show the plot.
  10. HTML(GenericChart.toChartHTML(chart));

    The Jupyter Notebook displays the scatter plot.

    Plotly.NET scatter plot

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: