Example of SMA in ASP.NET Core Charts Control
This Simple Moving Average example draws the average of the last closing prices over the candles, to show the trend without the daily noise.
@* 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 = "smaIndicatorChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "440px",
Title = "Simple moving average (SMA)",
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.Sma, Period = 10, Name = "SMA (10)" }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "smaIndicatorChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "440px",
Title = "Simple moving average (SMA)",
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.Sma, Period = 10, Name = "SMA (10)" }
}
})
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/IndicatorSma.cshtml; it draws what ViewData holds.
public ActionResult IndicatorSma()
{
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;
}
}
}
@* Program.cs *@
builder.Services.AddControllersWithViews().AddLumenUIJson();
builder.Services.AddLumenUI();
// ...
app.UseLumenUI();
@* Views/_ViewImports.cshtml *@
@using Lumen.UI.Mvc
@addTagHelper *, Lumen.UI.Core
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@await RenderSectionAsync("scripts", required: false)
</body>
@* Charts with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
A simple moving average of the closing prices over 10 candles, drawn on the price axis. The indicator is one item of Indicators and reads the candles of the series it names (SeriesName).