トップ回答者
C#で指数移動平均式(EMA)を計算したい

質問
回答
すべての返信
-
何を作っているか(Windows Forms? ASP.NET Web Forms? その他?)と、自分の開発環境(OS, .NET, Visual Studio のバージョンなど)ぐらいは質問の一番最初に書きましょうよ。こういうところで質問するときのイロハのイだと思うのですが。
それから、自分ではどこまで実装できていて、どこで躓いているのか、具体的にコードアップするなどして説明してください。今の質問の仕方では丸投げになってます。
適切に情報が提供されていれば、回答者が質問者さんの状況を的確に把握でき、タイムリーで的を得た回答が得られるということで、質問者さんにもメリットがあります。ガイドラインも出ていますので目を通していただければと思います。
フォーラムのご利用方法(質問の投稿)について
https://social.msdn.microsoft.com/Forums/ja-JP/b2074c04-2e91-414d-8e9e-d634be311e31 -
ChartのFinancialFormulaを実行すると、対象となったSeriesのPointsに計算結果が入るので、動作しているのはわかるはず。
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Forms; using Charting=System.Windows.Forms.DataVisualization.Charting; namespace WindowsFormsApplication1 { public partial class Form1 : Form { Charting.Chart chart1; public Form1() { InitializeComponent(); chart1 = new Charting.Chart(); chart1.Dock = DockStyle.Fill; chart1.ChartAreas.Add(new Charting.ChartArea()); this.Controls.Add(chart1); for (int i = 0; i < 2; i++) { Charting.Series seri = new Charting.Series(); seri.ChartType = Charting.SeriesChartType.Line; seri.XValueMember = "X"; seri.YValueMembers = "Y"; chart1.Series.Add(seri); } //適当にダミーデータ作成 List<PointF> points = new List<PointF>(); Random rnd = new Random(); int y = 100; for (int x = 0; x < 30; x++) { y = y + (int)((rnd.NextDouble() - 0.5) * 50); PointF p = new PointF(x, y); points.Add(p); } this.chart1.DataSource = points; this.chart1.PostPaint+=chart1_PostPaint; } void chart1_PostPaint(object sender, Charting.ChartPaintEventArgs e) { this.chart1.PostPaint -= chart1_PostPaint; this.BeginInvoke(new Action(OnPainted)); } private void OnPainted() { //2個目のSeriesに対して指数移動平均を行う this.chart1.DataManipulator.FinancialFormula(System.Windows.Forms.DataVisualization.Charting.FinancialFormula.ExponentialMovingAverage, chart1.Series[1]); var list=EMA.CalcEMA(this.chart1.Series[0].Points.Select(_ => _.YValues[0])).ToList(); for (int x = 1; x < chart1.Series[0].Points.Count; x++) { //結果を見てみる System.Diagnostics.Debug.WriteLine("{0}\t{1,8:F04}\t{2,8:F04}\t{3,8:F04}", x, chart1.Series[0].Points[x].YValues[0],chart1.Series[1].Points[x-1].YValues[0],list[x]); } } } class EMA { //Chartの結果に近くなるように適当に書いてみたEMA public static IEnumerable<double> CalcEMA(IEnumerable<double> values) { var ie=values.GetEnumerator(); double y; if(ie.MoveNext()) { y= ie.Current; yield return y; while (ie.MoveNext()) { double a = 2.0 / 3;//区間をとりあえず3 y = y + a * (ie.Current - y); yield return y; } } } } }
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答の候補に設定 立花楓Microsoft employee, Moderator 2017年9月1日 7:03