Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
343 changes: 343 additions & 0 deletions project-templates/csharp/common/research.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,343 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "f3b00e08",
"source": "This template shows how to use Plotly.NET to create charts, since charting is a common feature in research.",
"metadata": {}
},
{
"cell_type": "markdown",
"id": "17fb20aa",
"metadata": {},
"source": [
"## Preparation\n",
"\n",
"Load assemblies, import the QuantConnect, Plotly.NET, and Accord packages, then get some historical data to plot."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75a10299",
"metadata": {
"vscode": {
"languageId": "csharp"
}
},
"outputs": [],
"source": [
"// Load the assembly files and data types in their own cell.\n",
"#load \"../Initialize.csx\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "079dc2af",
"metadata": {
"vscode": {
"languageId": "csharp"
}
},
"outputs": [],
"source": [
"// Load the necessary assembly files.\n",
"#load \"../QuantConnect.csx\"\n",
"#r \"nuget: Plotly.NET\"\n",
"#r \"nuget: Plotly.NET.Interactive\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1ea581c8",
"metadata": {
"vscode": {
"languageId": "csharp"
}
},
"outputs": [],
"source": [
"// Import the QuantConnect, Plotly.NET, and Accord packages for calculation and plotting.\n",
"using QuantConnect;\n",
"using QuantConnect.Research;\n",
"\n",
"using Plotly.NET;\n",
"using Plotly.NET.Interactive;\n",
"using Plotly.NET.LayoutObjects;\n",
"\n",
"using Accord.Math;\n",
"using Accord.Statistics;"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa6c58a4",
"metadata": {
"vscode": {
"languageId": "csharp"
}
},
"outputs": [],
"source": [
"// Get historical data for a bank sector ETF and some banking companies over 2021.\n",
"var qb = new QuantBook();\n",
"var tickers = new[]\n",
"{\n",
" \"XLF\", // Financial Select Sector SPDR Fund\n",
" \"COF\", // Capital One Financial Corporation\n",
" \"GS\", // Goldman Sachs Group, Inc.\n",
" \"JPM\", // J P Morgan Chase & Co\n",
" \"WFC\" // Wells Fargo & Company\n",
"};\n",
"var symbols = tickers.Select(ticker => qb.AddEquity(ticker, Resolution.Daily).Symbol).ToList();\n",
"var history = qb.History(symbols, new DateTime(2021, 1, 1), new DateTime(2022, 1, 1)).ToList();"
]
},
{
"cell_type": "markdown",
"id": "d9ce2705",
"metadata": {},
"source": [
"## Candlestick Chart"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ca44325",
"metadata": {
"vscode": {
"languageId": "csharp"
}
},
"outputs": [],
"source": [
"// Select a symbol to plot its candlestick plot.\n",
"var symbol = symbols.First();\n",
"\n",
"// Call the Chart2D.Chart.Candlestick constructor with the time and OHLC price IEnumerable to create the candlestick plot.\n",
"var bars = history.Select(slice => slice.Bars[symbol]);\n",
"var candlestick = Chart2D.Chart.Candlestick<decimal, decimal, decimal, decimal, DateTime, string>(\n",
" bars.Select(x => x.Open),\n",
" bars.Select(x => x.High),\n",
" bars.Select(x => x.Low),\n",
" bars.Select(x => x.Close),\n",
" bars.Select(x => x.EndTime)\n",
");\n",
"\n",
"// Create the layout.\n",
"LinearAxis candleX = new LinearAxis();\n",
"candleX.SetValue(\"title\", \"Time\");\n",
"LinearAxis candleY = new LinearAxis();\n",
"candleY.SetValue(\"title\", \"Price ($)\");\n",
"Layout candleLayout = new Layout();\n",
"candleLayout.SetValue(\"xaxis\", candleX);\n",
"candleLayout.SetValue(\"yaxis\", candleY);\n",
"candleLayout.SetValue(\"title\", Title.init($\"{symbol} OHLC\"));\n",
"\n",
"// Assign the Layout and show the plot.\n",
"candlestick.WithLayout(candleLayout);\n",
"display(candlestick);"
]
},
{
"cell_type": "markdown",
"id": "93db8598",
"metadata": {},
"source": [
"## Line Chart"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7be5f8a7",
"metadata": {
"vscode": {
"languageId": "csharp"
}
},
"outputs": [],
"source": [
"// Plot the volume of the first symbol as a line chart.\n",
"var lineBars = history.Select(slice => slice.Bars[symbol]);\n",
"var line = Chart2D.Chart.Line<DateTime, decimal, string>(\n",
" lineBars.Select(x => x.EndTime),\n",
" lineBars.Select(x => x.Volume)\n",
");\n",
"\n",
"LinearAxis lineX = new LinearAxis();\n",
"lineX.SetValue(\"title\", \"Time\");\n",
"LinearAxis lineY = new LinearAxis();\n",
"lineY.SetValue(\"title\", \"Volume\");\n",
"Layout lineLayout = new Layout();\n",
"lineLayout.SetValue(\"xaxis\", lineX);\n",
"lineLayout.SetValue(\"yaxis\", lineY);\n",
"lineLayout.SetValue(\"title\", Title.init($\"{symbol} Volume\"));\n",
"\n",
"line.WithLayout(lineLayout);\n",
"display(line);"
]
},
{
"cell_type": "markdown",
"id": "9c0ff10e",
"metadata": {},
"source": [
"## Scatter Plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18a22de4",
"metadata": {
"vscode": {
"languageId": "csharp"
}
},
"outputs": [],
"source": [
"// Plot the closing-price relationship between two symbols as a scatter plot.\n",
"var symbol1 = symbols.First();\n",
"var symbol2 = symbols.Last();\n",
"\n",
"var scatter = Chart2D.Chart.Point<decimal, decimal, string>(\n",
" history.Select(slice => slice.Bars[symbol1].Close),\n",
" history.Select(slice => slice.Bars[symbol2].Close)\n",
");\n",
"\n",
"LinearAxis scatterX = new LinearAxis();\n",
"scatterX.SetValue(\"title\", $\"{symbol1} Price ($)\");\n",
"LinearAxis scatterY = new LinearAxis();\n",
"scatterY.SetValue(\"title\", $\"{symbol2} Price ($)\");\n",
"Layout scatterLayout = new Layout();\n",
"scatterLayout.SetValue(\"xaxis\", scatterX);\n",
"scatterLayout.SetValue(\"yaxis\", scatterY);\n",
"scatterLayout.SetValue(\"title\", Title.init($\"{symbol1} vs {symbol2}\"));\n",
"\n",
"scatter.WithLayout(scatterLayout);\n",
"display(scatter);"
]
},
{
"cell_type": "markdown",
"id": "fc076c7b",
"metadata": {},
"source": [
"## Heat Map"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0a7c40f9",
"metadata": {
"vscode": {
"languageId": "csharp"
}
},
"outputs": [],
"source": [
"// Compute the daily returns of each stock.\n",
"var data = history.SelectMany(x => x.Bars.Values)\n",
" .GroupBy(x => x.Symbol)\n",
" .Select(x =>\n",
" {\n",
" var prices = x.Select(b => (double)b.Close).ToArray();\n",
" return Enumerable.Range(0, prices.Length - 1)\n",
" .Select(i => prices[i + 1] / prices[i] - 1).ToArray();\n",
" }).ToArray().Transpose();\n",
"\n",
"// Build the correlation matrix.\n",
"var corrMatrix = Measures.Correlation(data).Select(x => x.ToList()).ToList();\n",
"\n",
"// Plot the heat map.\n",
"var X = Enumerable.Range(0, tickers.Length).ToList();\n",
"var heatmap = Plotly.NET.Chart2D.Chart.Heatmap<IEnumerable<double>, double, int, int, string>(\n",
" zData: corrMatrix,\n",
" X: X,\n",
" Y: X,\n",
" ShowScale: true,\n",
" ReverseYAxis: true\n",
");\n",
"\n",
"var heatAxis = new LinearAxis();\n",
"heatAxis.SetValue(\"tickvals\", X);\n",
"heatAxis.SetValue(\"ticktext\", tickers);\n",
"var heatLayout = new Layout();\n",
"heatLayout.SetValue(\"xaxis\", heatAxis);\n",
"heatLayout.SetValue(\"yaxis\", heatAxis);\n",
"heatLayout.SetValue(\"title\", Title.init(\"Banking Stocks and bank sector ETF Correlation Heat Map\"));\n",
"\n",
"heatmap.WithLayout(heatLayout);\n",
"display(heatmap);"
]
},
{
"cell_type": "markdown",
"id": "b403a42e",
"metadata": {},
"source": [
"## 3D Chart"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9e1459b",
"metadata": {
"vscode": {
"languageId": "csharp"
}
},
"outputs": [],
"source": [
"// Plot the closing-price correlation between three symbols in 3D.\n",
"var s1 = symbols[0];\n",
"var s2 = symbols[1];\n",
"var s3 = symbols[2];\n",
"\n",
"var chart3d = Chart3D.Chart.Point3D<decimal, decimal, decimal, string>(\n",
" history.Select(slice => slice.Bars[s1].Close),\n",
" history.Select(slice => slice.Bars[s2].Close),\n",
" history.Select(slice => slice.Bars[s3].Close)\n",
");\n",
"\n",
"LinearAxis xAxis3d = new LinearAxis();\n",
"xAxis3d.SetValue(\"title\", $\"{s1} Price ($)\");\n",
"LinearAxis yAxis3d = new LinearAxis();\n",
"yAxis3d.SetValue(\"title\", $\"{s2} Price ($)\");\n",
"LinearAxis zAxis3d = new LinearAxis();\n",
"zAxis3d.SetValue(\"title\", $\"{s3} Price ($)\");\n",
"Layout layout3d = new Layout();\n",
"layout3d.SetValue(\"xaxis\", xAxis3d);\n",
"layout3d.SetValue(\"yaxis\", yAxis3d);\n",
"layout3d.SetValue(\"zaxis\", zAxis3d);\n",
"layout3d.SetValue(\"title\", Title.init($\"{s1} vs {s2} vs {s3}\"));\n",
"\n",
"chart3d.WithLayout(layout3d);\n",
"display(chart3d);"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"file_extension": ".cs",
"mimetype": "text/x-csharp",
"name": "C#",
"pygments_lexer": "csharp",
"version": "11.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading