owchart证券图形控件学习:如何添加MACD指标?
源码:
public Form1() { InitializeComponent(); Chart chart = new Chart(); //创建K线 chart.Dock = DockStyle.Fill; //设置填充 Controls.Add(chart); //添加控件 //设置显示左轴的宽度 chart.LeftYScaleWidth = 100; //设置K线的间隔 chart.XScalePixel = 20; //添加第一个层 ChartDiv div1 = chart.AddChartDiv(50); //开盘价=0,最高价=1,最低价=2,收盘价=3 int openField = 0; int highField = 1; int lowField = 2; int closeField = 3; //添加K线 CandleShape candleShape = chart.AddCandle("K线", openField, highField, lowField, closeField, div1); //添加第二个层 ChartDiv div2 = chart.AddChartDiv(25); //声明成交量字段 int volField = 4; //添加K线 BarShape barShape = chart.AddBar("成交量", volField, div2); div1.XScale.Visible = false; div2.XScale.Visible = false; //添加第三个层,占25% ChartDiv div3 = chart.AddChartDiv(25); //创建MACD IndicatorMACD indicatorMacd = new IndicatorMACD(); indicatorMacd.Chart = chart; indicatorMacd.SetParam(closeField, 9, 26, 9, div3); //获取数据源 CTableEx dataSource = chart.DataSource; candleShape.StyleField = CTableEx.AutoField; candleShape.ColorField = CTableEx.AutoField; dataSource.AddColumn(candleShape.StyleField); dataSource.AddColumn(candleShape.ColorField); //读取文件,并填充数据 String filePath = Application.StartupPath + "\\SH600000.txt"; String content = File.ReadAllText(filePath, Encoding.Default); String[] strs = content.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); int strsSize = strs.Length; //获取起始索引 int startIndex = dataSource.RowsCount; for (int i = 2; i < strs.Length - 1; i++) { String str = strs[i]; String[] subStrs = str.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); double date = (Convert.ToDateTime(subStrs[0]) - new DateTime(1970, 1, 1)).TotalSeconds; double open = Convert.ToDouble(subStrs[1]); double high = Convert.ToDouble(subStrs[2]); double low = Convert.ToDouble(subStrs[3]); double close = Convert.ToDouble(subStrs[4]); double volume = Convert.ToDouble(subStrs[5]); double amount = Convert.ToDouble(subStrs[6]); dataSource.Set(date, openField, open); dataSource.Set(date, highField, high); dataSource.Set(date, lowField, low); dataSource.Set(date, closeField, close); dataSource.Set(date, volField, volume); } //调用计算方法 indicatorMacd.OnCalculate(startIndex); //刷新图像 chart.RefreshGraph(); }
界面: