# F# 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:  let prices = [ 26.24,25.80,26.22,25.95; 26.40,26.18,26.26,26.20 26.37,26.04,26.11,26.08; 26.78,26.15,26.60,26.16 26.86,26.51,26.69,26.58; 26.95,26.50,26.91,26.55 27.06,26.50,26.64,26.77; 26.86,26.43,26.53,26.59 27.10,26.52,26.78,26.59; 27.21,26.99,27.13,27.06 27.37,26.91,26.97,27.21; 27.07,26.60,27.05,27.02 27.33,26.95,27.04,26.96; 27.27,26.95,27.21,27.23 27.81,27.07,27.76,27.25; 27.94,27.29,27.93,27.50 28.26,27.91,28.19,27.97; 28.34,28.05,28.10,28.28 28.34,27.79,27.80,28.20; 27.84,27.51,27.70,27.77 ] 

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:  // On Mac OSX use FSharp.Charting.Gtk.fsx #I "packages/FSharp.Charting" #load "FSharp.Charting.fsx" open FSharp.Charting open System 
 1:  Chart.Stock(prices) 

 1: 2: 3:  let pricesWithDates = prices |> List.mapi (fun i (hi,lo,op,cl) -> (DateTime.Today.AddDays(float i).ToShortDateString(), hi, lo, op, cl)) 
 1: 2:  // Candlestick chart price range specified Chart.Candlestick(pricesWithDates).WithYAxis(Max = 29.0, Min = 25.0) 

 1: 2: 3:  // Alternative specification using pipelining Chart.Candlestick(pricesWithDates) |> Chart.WithYAxis(Max = 29.0, Min = 25.0) 

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.

val prices : (float * float * float * float) list

Full name: StockAndCandlestickCharts.prices
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
namespace FSharp.Charting
namespace System
type Chart =
static member Area : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
static member Area : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
static member Bar : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
static member Bar : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> GenericChart
static member BoxPlotFromData : data:seq<#key * #seq<'a2>> * ?Name:string * ?Title:string * ?Color:Color * ?XTitle:string * ?YTitle:string * ?Percentile:int * ?ShowAverage:bool * ?ShowMedian:bool * ?ShowUnusualValues:bool * ?WhiskerPercentile:int -> GenericChart (requires 'a2 :> value)
static member BoxPlotFromStatistics : data:seq<#key * #value * #value * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string * ?Percentile:int * ?ShowAverage:bool * ?ShowMedian:bool * ?ShowUnusualValues:bool * ?WhiskerPercentile:int -> GenericChart
static member Bubble : data:seq<#value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string * ?BubbleMaxSize:int * ?BubbleMinSize:int * ?BubbleScaleMax:float * ?BubbleScaleMin:float * ?UseSizeForLabel:bool -> GenericChart
static member Bubble : data:seq<#key * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string * ?BubbleMaxSize:int * ?BubbleMinSize:int * ?BubbleScaleMax:float * ?BubbleScaleMin:float * ?UseSizeForLabel:bool -> GenericChart
static member Candlestick : data:seq<#value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> CandlestickChart
static member Candlestick : data:seq<#key * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Color * ?XTitle:string * ?YTitle:string -> CandlestickChart
...

Full name: FSharp.Charting.Chart
static member Chart.Stock : data:seq<#value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.StockChart
static member Chart.Stock : data:seq<#key * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.StockChart
val pricesWithDates : (string * float * float * float * float) list

Full name: StockAndCandlestickCharts.pricesWithDates
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member GetSlice : startIndex:int option * endIndex:int option -> 'T list
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val mapi : mapping:(int -> 'T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.mapi
val i : int
val hi : float
val lo : float
val op : float
val cl : float
Multiple items
type DateTime =
struct
new : ticks:int64 -> DateTime + 10 overloads
member Add : value:TimeSpan -> DateTime
member AddDays : value:float -> DateTime
member AddHours : value:float -> DateTime
member AddMilliseconds : value:float -> DateTime
member AddMinutes : value:float -> DateTime
member AddMonths : months:int -> DateTime
member AddSeconds : value:float -> DateTime
member AddTicks : value:int64 -> DateTime
member AddYears : value:int -> DateTime
...
end

Full name: System.DateTime

--------------------
DateTime()
DateTime(ticks: int64) : unit
DateTime(ticks: int64, kind: DateTimeKind) : unit
DateTime(year: int, month: int, day: int) : unit
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
property DateTime.Today: DateTime
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
static member Chart.Candlestick : data:seq<#value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.CandlestickChart
static member Chart.Candlestick : data:seq<#key * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<string> * ?Color:Drawing.Color * ?XTitle:string * ?YTitle:string -> ChartTypes.CandlestickChart
static member Chart.WithYAxis : ?Enabled:bool * ?Title:string * ?Max:float * ?Min:float * ?Log:bool * ?ArrowStyle:ChartTypes.AxisArrowStyle * ?LabelStyle:ChartTypes.LabelStyle * ?IsMarginVisible:bool * ?MajorGrid:ChartTypes.Grid * ?MinorGrid:ChartTypes.Grid * ?MajorTickMark:ChartTypes.TickMark * ?MinorTickMark:ChartTypes.TickMark * ?TitleAlignment:Drawing.StringAlignment * ?TitleFontName:string * ?TitleFontSize:float * ?TitleFontStyle:Drawing.FontStyle * ?TitleColor:Drawing.Color * ?ToolTip:string -> ('a -> 'a) (requires 'a :> ChartTypes.GenericChart)