Example of Bollinger in ASP.NET MVC Charts Control
This Bollinger Bands example draws a moving average with a band above and below it that follows the volatility of the price.
@* The chart's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiChart(new UiChartOptions
{
Id = "bollingerIndicatorChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "440px",
Title = "Bollinger bands",
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "dd MMM" },
PrimaryYAxis = new UiChartAxis { LabelFormat = "C0", Minimum = 90, Maximum = 130 },
Series = new List<UiChartSeries>
{
new UiChartSeries
{
Type = UiChartSeriesType.Candle, XName = "Date", High = "High", Low = "Low", Open = "Open", Close = "Close",
Volume = "Volume", Name = "Lumen", EnableSolidCandles = true
}
},
Indicators = new List<UiChartIndicator>
{
new UiChartIndicator { Type = UiChartIndicatorType.Bollinger, Period = 20, Standard = 2 }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "bollingerIndicatorChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "440px",
Title = "Bollinger bands",
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "dd MMM" },
PrimaryYAxis = new UiChartAxis { LabelFormat = "C0", Minimum = 90, Maximum = 130 },
Series = new List<UiChartSeries>
{
new UiChartSeries
{
Type = UiChartSeriesType.Candle, XName = "Date", High = "High", Low = "Low", Open = "Open", Close = "Close",
Volume = "Volume", Name = "Lumen", EnableSolidCandles = true
}
},
Indicators = new List<UiChartIndicator>
{
new UiChartIndicator { Type = UiChartIndicatorType.Bollinger, Period = 20, Standard = 2 }
}
})
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ChartController : Controller
{
// The page is Views/Chart/IndicatorBollinger.cshtml; it draws what ViewData holds.
public ActionResult IndicatorBollinger()
{
ViewData["ChartData"] = StockHistory();
return View();
}
/// <summary>One trading day with its volume: the rows of the technical indicator samples.</summary>
public class StockHistoryData
{
public DateTime Date { get; set; }
public decimal Open { get; set; }
public decimal High { get; set; }
public decimal Low { get; set; }
public decimal Close { get; set; }
public decimal Volume { get; set; }
}
/// <summary>Sixty trading days (the same prices on every run), for the indicators: they need some history.</summary>
private static List<StockHistoryData> StockHistory()
{
var list = new List<StockHistoryData>();
decimal close = 100;
uint seed = 12345;
Func<int, int> next = range => { seed = seed * 1103515245 + 12345; return (int)((seed >> 16) % (uint)range); };
for (int i = 0; i < 60; i++)
{
DateTime day = new DateTime(2026, 1, 5).AddDays(i + (i / 5) * 2); // weekdays only
decimal open = close + (next(5) - 2) * 0.4m;
close = Math.Max(20, open + (next(13) - 6) * 0.7m + (i < 30 ? 0.3m : -0.2m));
list.Add(new StockHistoryData
{
Date = day,
Open = Math.Round(open, 2),
Close = Math.Round(close, 2),
High = Math.Round(Math.Max(open, close) + next(30) / 10m + 0.3m, 2),
Low = Math.Round(Math.Min(open, close) - next(30) / 10m - 0.3m, 2),
Volume = 40000 + next(60000)
});
}
return list;
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Charts with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
Bollinger bands: a 20-candle average with lines two standard deviations above and below it. The indicator is one item of Indicators and reads the candles of the series it names (SeriesName).