Skip to content

Commit 0d787cf

Browse files
committed
Add OHLC chart and refactor Candlestick chart
1 parent d57ed25 commit 0d787cf

File tree

8 files changed

+347
-47
lines changed

8 files changed

+347
-47
lines changed

RELEASE_NOTES.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Chart.SingleStack: Basically Chart.Grid with one Column
99
* Add Chart.withColorBar and Chart.withColorBarStyle to change the appearance of colorbars
1010
* [Add Sunburst Chart]()
11+
* [Add OHLC Chart]()
1112

1213
### 1.2.2 - Apr 9 2020
1314
* [Opening Charts is now more or less OS agnostic](https://github.com/muehlhaus/FSharp.Plotly/commit/f6e3dceade085e43e7e56b478b9cf7b533a4fe55)

docsrc/content/2d-histograms.fsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
(*** hide ***)
22
#r "netstandard"
3+
#r "../../packages/Newtonsoft.Json/lib/netstandard2.0/Newtonsoft.Json.dll"
34
#r "../../bin/FSharp.Plotly/netstandard2.0/FSharp.Plotly.dll"
45

56

7+
68
(**
79
# FSharp.Plotly: Histogram2d
810

src/FSharp.Plotly/CandelstickExtension.fs

+13-47
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ open Trace
44
open System
55
open System.Runtime.InteropServices
66

7+
[<Obsolete("this type will be removed in favor of the 'StockData' type in 2.0.")>]
78
type Candle =
89
{
910
High : float
@@ -23,47 +24,7 @@ type Candle =
2324
[<AutoOpen>]
2425
module CandelstickExtension =
2526

26-
type TraceStyle with
27-
[<CompiledName("Candlestick")>]
28-
static member Candlestick
29-
(
30-
data : (#IConvertible*Candle) seq,
31-
?increasing : Line,
32-
?decreasing : Line,
33-
?line : Line
34-
) =
35-
(fun (trace:('T :> Trace)) ->
36-
DynObj.setValue trace "open" (data |> Seq.map snd |> Seq.map(fun x->x.Open))
37-
DynObj.setValue trace "high" (data |> Seq.map snd |> Seq.map(fun x->x.High))
38-
DynObj.setValue trace "low" (data |> Seq.map snd |> Seq.map(fun x->x.Low))
39-
DynObj.setValue trace "close" (data |> Seq.map snd |> Seq.map(fun x->x.Close))
40-
DynObj.setValue trace "x" (data |> Seq.map fst)
41-
DynObj.setValue trace "xaxis" "x"
42-
DynObj.setValue trace "yaxis" "y"
43-
DynObj.setValueOpt trace "line" line
44-
DynObj.setValueOpt trace "increasing" increasing
45-
DynObj.setValueOpt trace "decreasing" decreasing
46-
trace
47-
)
48-
4927
type Chart with
50-
[<CompiledName("Candlestick")>]
51-
static member Candlestick
52-
(
53-
data : (#IConvertible*Candle) seq,
54-
[<Optional;DefaultParameterValue(null)>] ?increasing : Line,
55-
[<Optional;DefaultParameterValue(null)>] ?decreasing : Line,
56-
[<Optional;DefaultParameterValue(null)>] ?line : Line
57-
) =
58-
Trace.initCandlestick(TraceStyle.Candlestick
59-
(
60-
data,
61-
?increasing=increasing,
62-
?decreasing=decreasing,
63-
?line=line
64-
))
65-
|> GenericChart.ofTraceObject
66-
6728
[<Obsolete("Function Name has a typo and will be removed in 2.0. use Chart.Candlestick")>]
6829
[<CompiledName("Candelstick")>]
6930
static member Candelstick
@@ -73,11 +34,16 @@ module CandelstickExtension =
7334
[<Optional;DefaultParameterValue(null)>] ?decreasing : Line,
7435
[<Optional;DefaultParameterValue(null)>] ?line : Line
7536
) =
76-
Trace.initCandlestick(TraceStyle.Candlestick
77-
(
78-
data,
79-
?increasing=increasing,
80-
?decreasing=decreasing,
81-
?line=line
82-
))
37+
Trace.initCandlestick(
38+
TraceStyle.Candlestick(
39+
``open`` = (data |> Seq.map (snd >> (fun x -> x.Open))) ,
40+
high = (data |> Seq.map (snd >> (fun x -> x.High))) ,
41+
low = (data |> Seq.map (snd >> (fun x -> x.Low))) ,
42+
close = (data |> Seq.map (snd >> (fun x -> x.Close))) ,
43+
x = (data |> Seq.map fst) ,
44+
?Increasing = increasing ,
45+
?Decreasing = decreasing ,
46+
?Line = line
47+
)
48+
)
8349
|> GenericChart.ofTraceObject

src/FSharp.Plotly/Chart.fs

+179
Original file line numberDiff line numberDiff line change
@@ -1392,3 +1392,182 @@ type Chart =
13921392
)
13931393
|> TraceStyle.Marker(?Colors=Colors,?Colorbar=Colorbar)
13941394
|> GenericChart.ofTraceObject
1395+
1396+
/// Creates an OHLC (open-high-low-close) chart. OHLC charts are typically used to illustrate movements in the price of a financial instrument over time.
1397+
///
1398+
/// ``open`` : Sets the open values.
1399+
///
1400+
/// high : Sets the high values.
1401+
///
1402+
/// low : Sets the low values.
1403+
///
1404+
/// close : Sets the close values.
1405+
///
1406+
/// x : Sets the x coordinates. If absent, linear coordinate will be generated.
1407+
///
1408+
/// ?Increasing : Sets the Line style of the Increasing part of the chart
1409+
///
1410+
/// ?Decreasing : Sets the Line style of the Decreasing part of the chart
1411+
///
1412+
/// ?Line : Sets the Line style of both the Decreasing and Increasing part of the chart
1413+
///
1414+
/// ?Tickwidth : Sets the width of the open/close tick marks relative to the "x" minimal interval.
1415+
///
1416+
/// ?XCalendar : Sets the calendar system to use with `x` date data.
1417+
static member OHLC
1418+
(
1419+
``open`` : #IConvertible seq,
1420+
high : #IConvertible seq,
1421+
low : #IConvertible seq,
1422+
close : #IConvertible seq,
1423+
x : #IConvertible seq,
1424+
[<Optional;DefaultParameterValue(null)>]?Increasing : Line,
1425+
[<Optional;DefaultParameterValue(null)>]?Decreasing : Line,
1426+
[<Optional;DefaultParameterValue(null)>]?Tickwidth : float,
1427+
[<Optional;DefaultParameterValue(null)>]?Line : Line,
1428+
[<Optional;DefaultParameterValue(null)>]?XCalendar : StyleParam.Calendar
1429+
) =
1430+
Trace.initOHLC(
1431+
TraceStyle.OHLC(
1432+
``open`` = ``open`` ,
1433+
high = high ,
1434+
low = low ,
1435+
close = close ,
1436+
x = x ,
1437+
?Increasing = Increasing ,
1438+
?Decreasing = Decreasing ,
1439+
?Tickwidth = Tickwidth ,
1440+
?Line = Line ,
1441+
?XCalendar = XCalendar
1442+
)
1443+
)
1444+
|> GenericChart.ofTraceObject
1445+
1446+
/// Creates an OHLC (open-high-low-close) chart. OHLC charts are typically used to illustrate movements in the price of a financial instrument over time.
1447+
///
1448+
/// stockTimeSeries : tuple list of time * stock (OHLC) data
1449+
///
1450+
/// ?Increasing : Sets the Line style of the Increasing part of the chart
1451+
///
1452+
/// ?Decreasing : Sets the Line style of the Decreasing part of the chart
1453+
///
1454+
/// ?Line : Sets the Line style of both the Decreasing and Increasing part of the chart
1455+
///
1456+
/// ?Tickwidth : Sets the width of the open/close tick marks relative to the "x" minimal interval.
1457+
///
1458+
/// ?XCalendar : Sets the calendar system to use with `x` date data.
1459+
static member OHLC
1460+
(
1461+
stockTimeSeries: seq<System.DateTime*StockData>,
1462+
[<Optional;DefaultParameterValue(null)>]?Increasing : Line,
1463+
[<Optional;DefaultParameterValue(null)>]?Decreasing : Line,
1464+
[<Optional;DefaultParameterValue(null)>]?Tickwidth : float,
1465+
[<Optional;DefaultParameterValue(null)>]?Line : Line,
1466+
[<Optional;DefaultParameterValue(null)>]?XCalendar : StyleParam.Calendar
1467+
) =
1468+
Trace.initOHLC(
1469+
TraceStyle.OHLC(
1470+
``open`` = (stockTimeSeries |> Seq.map (snd >> (fun x -> x.Open))) ,
1471+
high = (stockTimeSeries |> Seq.map (snd >> (fun x -> x.High))) ,
1472+
low = (stockTimeSeries |> Seq.map (snd >> (fun x -> x.Low))) ,
1473+
close = (stockTimeSeries |> Seq.map (snd >> (fun x -> x.Close))) ,
1474+
x = (stockTimeSeries |> Seq.map fst) ,
1475+
?Increasing = Increasing ,
1476+
?Decreasing = Decreasing ,
1477+
?Tickwidth = Tickwidth ,
1478+
?Line = Line ,
1479+
?XCalendar = XCalendar
1480+
)
1481+
)
1482+
|> GenericChart.ofTraceObject
1483+
1484+
/// Creates a candlestick chart. A candlestick cart is a style of financial chart used to describe price movements of a
1485+
/// security, derivative, or currency. Each "candlestick" typically shows one day, thus a one-month chart may show the 20
1486+
/// trading days as 20 candlesticks. Candlestick charts can also be built using intervals shorter or longer than one day.
1487+
///
1488+
/// ``open`` : Sets the open values.
1489+
///
1490+
/// high : Sets the high values.
1491+
///
1492+
/// low : Sets the low values.
1493+
///
1494+
/// close : Sets the close values.
1495+
///
1496+
/// x : Sets the x coordinates. If absent, linear coordinate will be generated.
1497+
///
1498+
/// ?Increasing : Sets the Line style of the Increasing part of the chart
1499+
///
1500+
/// ?Decreasing : Sets the Line style of the Decreasing part of the chart
1501+
///
1502+
/// ?Line : Sets the Line style of both the Decreasing and Increasing part of the chart
1503+
///
1504+
/// ?WhiskerWidth : Sets the width of the whiskers relative to the box' width. For example, with 1, the whiskers are as wide as the box(es).
1505+
///
1506+
/// ?XCalendar : Sets the calendar system to use with `x` date data.
1507+
static member Candlestick
1508+
(
1509+
``open`` : #IConvertible seq,
1510+
high : #IConvertible seq,
1511+
low : #IConvertible seq,
1512+
close : #IConvertible seq,
1513+
x : #IConvertible seq,
1514+
[<Optional;DefaultParameterValue(null)>]?Increasing : Line,
1515+
[<Optional;DefaultParameterValue(null)>]?Decreasing : Line,
1516+
[<Optional;DefaultParameterValue(null)>]?WhiskerWidth : float,
1517+
[<Optional;DefaultParameterValue(null)>]?Line : Line,
1518+
[<Optional;DefaultParameterValue(null)>]?XCalendar : StyleParam.Calendar
1519+
) =
1520+
Trace.initCandlestick(
1521+
TraceStyle.Candlestick(
1522+
``open`` = ``open`` ,
1523+
high = high ,
1524+
low = low ,
1525+
close = close ,
1526+
x = x ,
1527+
?Increasing = Increasing ,
1528+
?Decreasing = Decreasing ,
1529+
?WhiskerWidth = WhiskerWidth,
1530+
?Line = Line ,
1531+
?XCalendar = XCalendar
1532+
)
1533+
)
1534+
|> GenericChart.ofTraceObject
1535+
1536+
/// Creates an OHLC (open-high-low-close) chart. OHLC charts are typically used to illustrate movements in the price of a financial instrument over time.
1537+
///
1538+
/// stockTimeSeries : tuple list of time * stock (OHLC) data
1539+
///
1540+
/// ?Increasing : Sets the Line style of the Increasing part of the chart
1541+
///
1542+
/// ?Decreasing : Sets the Line style of the Decreasing part of the chart
1543+
///
1544+
/// ?Line : Sets the Line style of both the Decreasing and Increasing part of the chart
1545+
///
1546+
/// ?Tickwidth : Sets the width of the open/close tick marks relative to the "x" minimal interval.
1547+
///
1548+
/// ?XCalendar : Sets the calendar system to use with `x` date data.
1549+
static member Candlestick
1550+
(
1551+
stockTimeSeries: seq<System.DateTime*StockData>,
1552+
[<Optional;DefaultParameterValue(true)>]?ShowRangeSlider: bool,
1553+
[<Optional;DefaultParameterValue(null)>]?Increasing : Line,
1554+
[<Optional;DefaultParameterValue(null)>]?Decreasing : Line,
1555+
[<Optional;DefaultParameterValue(null)>]?WhiskerWidth : float,
1556+
[<Optional;DefaultParameterValue(null)>]?Line : Line,
1557+
[<Optional;DefaultParameterValue(null)>]?XCalendar : StyleParam.Calendar
1558+
) =
1559+
Trace.initCandlestick(
1560+
TraceStyle.Candlestick(
1561+
``open`` = (stockTimeSeries |> Seq.map (snd >> (fun x -> x.Open))) ,
1562+
high = (stockTimeSeries |> Seq.map (snd >> (fun x -> x.High))) ,
1563+
low = (stockTimeSeries |> Seq.map (snd >> (fun x -> x.Low))) ,
1564+
close = (stockTimeSeries |> Seq.map (snd >> (fun x -> x.Close))) ,
1565+
x = (stockTimeSeries |> Seq.map fst) ,
1566+
?Increasing = Increasing ,
1567+
?Decreasing = Decreasing ,
1568+
?WhiskerWidth = WhiskerWidth,
1569+
?Line = Line ,
1570+
?XCalendar = XCalendar
1571+
)
1572+
)
1573+
|> GenericChart.ofTraceObject

src/FSharp.Plotly/FSharp.Plotly.fsproj

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<Compile Include="StyleParams.fs" />
2828
<Compile Include="DynamicObj.fs" />
2929
<Compile Include="Colors.fs" />
30+
<Compile Include="StockData.fs" />
3031
<Compile Include="Font.fs" />
3132
<Compile Include="Pathbar.fs" />
3233
<Compile Include="TreemapTiling.fs" />

src/FSharp.Plotly/Playground.fsx

+22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#load "StyleParams.fs"
22
#load "DynamicObj.fs"
33
#load "Colors.fs"
4+
#load "StockData.fs"
45
#load "Font.fs"
56
#load "Pathbar.fs"
67
#load "TreemapTiling.fs"
@@ -39,6 +40,27 @@
3940
open FSharp.Plotly
4041
open GenericChart
4142

43+
let stockData =
44+
[|("2020-01-17T13:40:00", 0.68888, 0.68888, 0.68879, 0.6888);
45+
("2020-01-17T13:41:00", 0.68883, 0.68884, 0.68875, 0.68877);
46+
("2020-01-17T13:42:00", 0.68878, 0.68889, 0.68878, 0.68886);
47+
("2020-01-17T13:43:00", 0.68886, 0.68886, 0.68876, 0.68879);
48+
("2020-01-17T13:44:00", 0.68879, 0.68879, 0.68873, 0.68874);
49+
("2020-01-17T13:45:00", 0.68875, 0.68877, 0.68867, 0.68868);
50+
("2020-01-17T13:46:00", 0.68869, 0.68887, 0.68869, 0.68883);
51+
("2020-01-17T13:47:00", 0.68883, 0.68899, 0.68883, 0.68899);
52+
("2020-01-17T13:48:00", 0.68898, 0.689, 0.68885, 0.68889);
53+
("2020-01-17T13:49:00", 0.68889, 0.68893, 0.68881, 0.68893);
54+
("2020-01-17T13:50:00", 0.68891, 0.68896, 0.68886, 0.68891);
55+
|]
56+
|> Array.map (fun (d,o,h,l,c)->System.DateTime.Parse d, StockData.Create(o,h,l,c))
57+
58+
Chart.Candlestick stockData
59+
|> Chart.Show
60+
61+
Chart.OHLC stockData
62+
|> Chart.Show
63+
4264
Chart.Treemap(
4365
["Eve"; "Cain"; "Seth"; "Enos"; "Noam"; "Abel"; "Awan"; "Enoch"; "Azura"],
4466
[""; "Eve"; "Eve"; "Seth"; "Seth"; "Eve"; "Eve"; "Awan"; "Eve" ],

src/FSharp.Plotly/StockData.fs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace FSharp.Plotly
2+
3+
type StockData =
4+
{
5+
Open : float
6+
High : float
7+
Low : float
8+
Close : float
9+
}
10+
with
11+
static member Create(o,h,l,c) =
12+
{
13+
High = h
14+
Low = l
15+
Close = c
16+
Open = o
17+
}
18+
19+
static member create o h l c =
20+
{
21+
High = h
22+
Low = l
23+
Close = c
24+
Open = o
25+
}

0 commit comments

Comments
 (0)