/// <summary> /// 计算指标 /// </summary> /// <param name="id">指标ID</param> /// <param name="code">代码</param> /// <param name="path">路径</param> /// <param name="type">类型</param> /// <param name="cycle">周期</param> /// <param name="subscription">复权方式</param> /// <param name="date">日期</param> /// <param name="open">开盘价</param> /// <param name="high">最高价</param> /// <param name="low">最低价</param> /// <param name="close">收盘价</param> /// <param name="volume">成交量</param> /// <param name="amount">成交额</param> /// <returns>返回数据</returns> public static double[] calculateIndicatorExtern(int id, String code, ref double result) { if (m_indicators.ContainsKey(id)) { FCScript indicator = m_indicators[id]; List <FCScript> indicators = new List <FCScript>(); indicators.Add(indicator); List <SecurityData> datas = new List <SecurityData>(); if (SecurityService.m_historyDatas.ContainsKey(code)) { datas = SecurityService.m_historyDatas[code]; SecurityLatestData latestData = null; if (SecurityService.m_latestDatas.ContainsKey(code)) { latestData = SecurityService.m_latestDatas[code]; } if (latestData != null) { SecurityData newData = new SecurityData(); getSecurityData(latestData, latestData.m_lastClose, 1440, 0, ref newData); if (datas.Count == 0) { datas.Add(newData); } else { if (newData.m_date > datas[datas.Count - 1].m_date) { datas.Add(newData); } else { datas[datas.Count - 1] = newData; } } } FCDataTable dataSource = indicator.DataSource; int[] fields = new int[] { KeyFields.CLOSE_INDEX, KeyFields.HIGH_INDEX, KeyFields.LOW_INDEX, KeyFields.OPEN_INDEX, KeyFields.VOL_INDEX, KeyFields.AMOUNT_INDEX }; SecurityDataHelper.bindHistoryDatas(m_chart, dataSource, indicators, fields, datas);; int rowsCount = dataSource.RowsCount; int variablesSize = indicator.MainVariables.Count; double[] list = new double[variablesSize]; if (rowsCount > 0) { int pos = 0; foreach (String name in indicator.MainVariables.Keys) { int field = indicator.MainVariables[name]; double value = dataSource.get2(rowsCount - 1, field); list[pos] = value; pos++; } } result = indicator.m_result; dataSource.clear(); return(list); } } return(null); }
/// <summary> /// 创建指标 /// </summary> /// <param name="id">编号</param> /// <param name="script">脚本</param> /// <param name="xml">XML</param> /// <returns>指标</returns> public static FCScript createIndicator(String id, String script, FCUIXml xml) { FCScript indicator = new FCScript(); indicator.Name = id; FCDataTable table = new FCDataTable(); indicator.DataSource = table; NFunctionBase.addFunctions(indicator); NFunctionUI.addFunctions(indicator, xml); NFunctionWin.addFunctions(indicator); int index = STARTINDEX; String[] functions = FUNCTIONS.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); int functionsSize = functions.Length; for (int i = 0; i < functionsSize; i++) { indicator.addFunction(new NFunctionEx(indicator, index + i, functions[i], xml)); } indicator.Script = script; table.addColumn(0); table.set(0, 0, 0); indicator.onCalculate(0); return(indicator); }
/// <summary> /// 创建数据源 /// </summary> /// <param name="chart">股票控件</param> /// <returns>数据源</returns> public static FCDataTable createDataSource(FCChart chart) { FCDataTable dataSource = new FCDataTable(); dataSource.addColumn(KeyFields.CLOSE_INDEX); dataSource.addColumn(KeyFields.HIGH_INDEX); dataSource.addColumn(KeyFields.LOW_INDEX); dataSource.addColumn(KeyFields.OPEN_INDEX); dataSource.addColumn(KeyFields.VOL_INDEX); dataSource.addColumn(KeyFields.AMOUNT_INDEX); return(dataSource); }
/// <summary> /// 添加指标 /// </summary> /// <param name="chart">股票控件</param> /// <param name="dataSource">数据源</param> /// <param name="text">文本</param> /// <param name="parameters">参数</param> public static FCScript createIndicator(FCChart chart, FCDataTable dataSource, String text, String parameters) { FCScript indicator = new FCScript(); indicator.DataSource = dataSource; indicator.Name = ""; //indicator.FullName = ""; if (dataSource != null) { indicator.setSourceField(KeyFields.CLOSE, KeyFields.CLOSE_INDEX); indicator.setSourceField(KeyFields.HIGH, KeyFields.HIGH_INDEX); indicator.setSourceField(KeyFields.LOW, KeyFields.LOW_INDEX); indicator.setSourceField(KeyFields.OPEN, KeyFields.OPEN_INDEX); indicator.setSourceField(KeyFields.VOL, KeyFields.VOL_INDEX); indicator.setSourceField(KeyFields.AMOUNT, KeyFields.AMOUNT_INDEX); indicator.setSourceField(KeyFields.CLOSE.Substring(0, 1), KeyFields.CLOSE_INDEX); indicator.setSourceField(KeyFields.HIGH.Substring(0, 1), KeyFields.HIGH_INDEX); indicator.setSourceField(KeyFields.LOW.Substring(0, 1), KeyFields.LOW_INDEX); indicator.setSourceField(KeyFields.OPEN.Substring(0, 1), KeyFields.OPEN_INDEX); indicator.setSourceField(KeyFields.VOL.Substring(0, 1), KeyFields.VOL_INDEX); indicator.setSourceField(KeyFields.AMOUNT.Substring(0, 1), KeyFields.AMOUNT_INDEX); } IndicatorData indicatorData = new IndicatorData(); indicatorData.m_parameters = parameters; indicatorData.m_script = text; indicator.Tag = indicatorData; String constValue = ""; if (parameters != null && parameters.Length > 0) { String[] strs = parameters.Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries); int strsSize = strs.Length; for (int i = 0; i < strsSize; i++) { String str = strs[i]; String[] strs2 = str.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); constValue += "const " + strs2[0] + ":" + strs2[3] + ";"; } } if (text != null && text.Length > 0) { indicator.Script = constValue + text; } return(indicator); }
/// <summary> /// 插入最新数据 /// </summary> /// <param name="chart">股票控件</param> /// <param name="dataSource">数据源</param> /// <param name="indicators">指标</param> /// <param name="fields">字段</param> /// <param name="historyDatas">最近的历史数据</param> /// <param name="latestData">实时数据</param> /// <returns>索引</returns> public static int insertLatestData(FCChart chart, FCDataTable dataSource, List <FCScript> indicators, int[] fields, SecurityData latestData) { if (latestData.m_close > 0 && latestData.m_volume > 0) { int indicatorsSize = indicators.Count; int index = insertData(chart, dataSource, fields, latestData); for (int i = 0; i < indicatorsSize; i++) { indicators[i].onCalculate(index); } return(index); } else { return(-1); } }
/// <summary> /// 插入数据 /// </summary> /// <param name="chart">证券控件</param> /// <param name="dataSource">数据源</param> /// <param name="fields">字段</param> /// <param name="securityData">证券数据</param> /// <returns>索引</returns> public static int insertData(FCChart chart, FCDataTable dataSource, int[] fields, SecurityData securityData) { double close = securityData.m_close, high = securityData.m_high, low = securityData.m_low, open = securityData.m_open, avgPrice = securityData.m_avgPrice, volume = securityData.m_volume, amount = securityData.m_amount; if (volume > 0 || close > 0) { if (high == 0) { high = close; } if (low == 0) { low = close; } if (open == 0) { open = close; } if (avgPrice == 0) { avgPrice = double.NaN; } } else { close = double.NaN; high = double.NaN; low = double.NaN; open = double.NaN; volume = double.NaN; amount = double.NaN; avgPrice = double.NaN; } double date = securityData.m_date; dataSource.set(date, fields[4], volume); int index = dataSource.getRowIndex(date); dataSource.set2(index, fields[0], close); dataSource.set2(index, fields[1], high); dataSource.set2(index, fields[2], low); dataSource.set2(index, fields[3], open); dataSource.set2(index, fields[5], volume); dataSource.set2(index, fields[6], avgPrice); return(index); }
/// <summary> /// 创建指标 /// </summary> /// <param name="text">脚本</param> /// <param name="parameters">参数</param> /// <returns>指标ID</returns> public static int createIndicatorExtern(String text, String parameters, StringBuilder fields) { try { if (m_native == null) { m_native = new FCNative(); } if (m_chart == null) { m_chart = new FCChart(); m_chart.Native = m_native; } m_serialNumber++; FCDataTable dataSource = new FCDataTable(); dataSource.addColumn(KeyFields.CLOSE_INDEX); dataSource.addColumn(KeyFields.HIGH_INDEX); dataSource.addColumn(KeyFields.LOW_INDEX); dataSource.addColumn(KeyFields.OPEN_INDEX); dataSource.addColumn(KeyFields.VOL_INDEX); dataSource.addColumn(KeyFields.AMOUNT_INDEX); FCScript indicator = SecurityDataHelper.createIndicator(m_chart, dataSource, text, parameters); m_indicators[m_serialNumber] = indicator; indicator.onCalculate(0); int pos = 0; int variablesSize = indicator.MainVariables.Count; foreach (String field in indicator.MainVariables.Keys) { fields.Append(field); if (pos != variablesSize - 1) { fields.Append(","); } pos++; } } catch (Exception ex) { Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace); } return(m_serialNumber); }
/// <summary> /// 绑定历史数据 /// </summary> /// <param name="chart">股票控件</param> /// <param name="dataSource">数据源</param> /// <param name="indicators">指标</param> /// <param name="fields">字段</param> /// <param name="historyDatas">历史数据</param> public static void bindHistoryDatas(FCChart chart, FCDataTable dataSource, List <FCScript> indicators, int[] fields, List <SecurityData> historyDatas) { dataSource.clear(); int size = historyDatas.Count; dataSource.setRowsCapacity(size + 10); dataSource.setRowsGrowStep(100); int columnsCount = dataSource.ColumnsCount; for (int i = 0; i < size; i++) { SecurityData securityData = historyDatas[i]; if (dataSource == chart.DataSource) { insertData(chart, dataSource, fields, securityData); } else { double[] ary = new double[columnsCount]; ary[0] = securityData.m_close; ary[1] = securityData.m_high; ary[2] = securityData.m_low; ary[3] = securityData.m_open; ary[4] = securityData.m_volume; for (int j = 5; j < columnsCount; j++) { ary[j] = double.NaN; } dataSource.AddRow(securityData.m_date, ary, columnsCount); } } int indicatorsSize = indicators.Count; for (int i = 0; i < indicatorsSize; i++) { indicators[i].onCalculate(0); } }
/// <summary> /// 创建指标 /// </summary> /// <param name="native">方法库</param> /// <param name="script">脚本</param> /// <returns>指标</returns> public static FCScript CreateScript(String script, FCNative native) { FCScript indicator = new FCScript(); FCDataTable table = new FCDataTable(); indicator.DataSource = table; CFunctionBase.addFunctions(indicator); CFunctionHttp.addFunctions(indicator); int index = STARTINDEX; string[] functions = FUNCTIONS.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); int functionsSize = functions.Length; for (int i = 0; i < functionsSize; i++) { indicator.addFunction(new CFunctionEx(indicator, index + i, functions[i], native)); } indicator.Script = script; table.addColumn(0); table.set(0, 0, 0); indicator.onCalculate(0); return(indicator); }