Example of EMA in ASP.NET MVC Charts Control
This Exponential Moving Average example draws an average that gives more weight to the latest prices over the candles.
@* 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 = "emaIndicatorChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "440px",
Title = "Exponential moving average (EMA)",
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.Ema, Period = 10, Name = "EMA (10)" }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "emaIndicatorChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "440px",
Title = "Exponential moving average (EMA)",
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.Ema, Period = 10, Name = "EMA (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/IndicatorEma.cshtml; it draws what ViewData holds.
public ActionResult IndicatorEma()
{
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. *@
An exponential moving average, which follows the latest prices more closely than the simple one. The indicator is one item of Indicators and reads the candles of the series it names (SeriesName).