FSharp.Charting: Stock and Candlestick Charts
Summary: This example shows how to create stock and candlestick charts to visualize financial data in F#. It looks at how to draw charts, use dates as labels, and specify the range of a chart.
Stock and candlestick charts are designed to visualize stock prices. The data recorded about stock prices typically consists of four values representing High, Low, Open, and Close prices. The first two are the maximum and minimum price of the stock reached during the day and the last two are prices at the start and the end of the day. The examples use the following data set (price of MSFT stocks over 10 days):
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: |
|
A stock or a candlestick chart can be created using the FSharpChart.Stock
and FSharpChart.Candlestick
methods. Financial charts for
visualizing stocks require four values for drawing each data point (High, Low, Open, and Close price).
When calling the methods, it is possible to specify the values as a collection
containing four-element tuples, or five-element tuples (Date, High, Low, Open, and Close price).
1: 2: 3: 4: 5: 6: |
|
1:
|
|
1: 2: 3: |
|
1: 2: |
|
1: 2: 3: |
|
When using F# Interactive, each of these examples needs to be evaluated separately. This way, F# Interactive invokes a handler that automatically shows the created chart.
The first snippet calls the Chart.Stock
method with a list containing prices as
four-element tuples.
The second example adds dates as the labels for the chart.
This is done using the List.mapi
function. The lambda function used as an argument returns a tuple
containing the date and the original tuple of prices.
The example also demonstrates how to set the price range of the Y axis using the method WithYAxis
.
The last example shows an elegant way of configuring the Y axis range by using pipelining (the |>
operator).
The chart specification is passed to a configuration method Chart.WithYAxis
using pipelining.
This method takes named parameters that allow us to specify the range and other properties of the axis.