DataService.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. using IndexFormula.Finance;
  2. using IndexFormula.Finance.DataProvider;
  3. using IndexFormula.Finance.Win;
  4. using MuchInfo.Chart.Data.EnumTypes;
  5. using MuchInfo.Chart.Data.Models;
  6. using MuchInfo.Chart.Utilities.Enums;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading;
  13. using System.Windows;
  14. using System.Windows.Forms;
  15. namespace MuchInfo.Chart.DataAdapter
  16. {
  17. public class DataService : IDataService
  18. {
  19. /// <summary>
  20. /// 使用公式计算图表数据
  21. /// </summary>
  22. /// <param name="source">源数据,需要计算的数据</param>
  23. /// <returns>返回图表数据</returns>
  24. public List<FormulaModel> Calculate(List<Data.Interfaces.IBarDataPoint> source, List<FormulaModel> sourceCharts, Data.EnumTypes.CycleType cycleType, TimeSpan minutes)
  25. {
  26. var commonDataProvider = new CommonDataProvider(null);
  27. commonDataProvider.LoadBinary(ConvertData(source));
  28. commonDataProvider.DataCycle = ConvertCycle(cycleType, minutes);
  29. foreach (var formulaModel in sourceCharts)
  30. {
  31. try
  32. {
  33. var formulaBase = FormulaBase.GetFormulaByName(formulaModel.FormulaName);
  34. var formulaPackage = formulaBase.Run(commonDataProvider);
  35. if (formulaPackage.Count > 0)
  36. {
  37. for (int i = 0; i < formulaPackage.Count - 1; i++)
  38. {
  39. var indicatorModel = formulaModel.IndicatorList.FirstOrDefault((model) => model.LineName == formulaPackage[i].Name);
  40. if (indicatorModel == null) continue;
  41. indicatorModel.CalculateDataPoints = ConvertDataPoint(formulaPackage[i].Data, commonDataProvider["DATE"]);
  42. }
  43. }
  44. }
  45. catch
  46. {
  47. //找到不公式跳过,不影响下一个
  48. }
  49. }
  50. return sourceCharts;
  51. }
  52. /// <summary>
  53. /// 使用公式计算图表数据
  54. /// </summary>
  55. /// <param name="source">源数据,需要计算的数据</param>
  56. /// <param name="formula">公式</param>
  57. /// <param name="cycleType">周期类型</param>
  58. /// <param name="minutes">自定义周期分钟数</param>
  59. /// <returns>返回图表数据</returns>
  60. public FormulaModel Calculate(List<Data.Interfaces.IBarDataPoint> source, FormulaModel formula,
  61. Data.EnumTypes.CycleType cycleType, TimeSpan minutes)
  62. {
  63. var commonDataProvider = new CommonDataProvider(null);
  64. commonDataProvider.LoadBinary(ConvertData(source));
  65. commonDataProvider.DataCycle = ConvertCycle(cycleType, minutes);
  66. try
  67. {
  68. var formulaBase = FormulaBase.GetFormulaByName(formula.FormulaName);
  69. var formulaPackage = formulaBase.Run(commonDataProvider);
  70. if (formulaPackage.Count > 0)
  71. {
  72. if (formula.IndicatorList == null)
  73. {
  74. formula.IndicatorList = new List<IndicatorModel>();
  75. }
  76. formula.IndicatorList.Clear();
  77. for (int i = 0; i < formulaPackage.Count; i++)
  78. {
  79. var indicatorModel = FormulaDataCovnert(formulaPackage[i], formula, commonDataProvider["DATE"]);
  80. formula.IndicatorList.Add(indicatorModel);
  81. }
  82. }
  83. }
  84. catch
  85. {
  86. //找到不公式跳过,不影响下一个
  87. }
  88. return formula;
  89. }
  90. /// <summary>
  91. /// 打开指标编辑器
  92. /// </summary>
  93. /// <param name="source">计算源数据</param>
  94. /// <param name="formulaArray">当前图表的所有计算公式</param>
  95. /// <param name="cycleType">周期类型</param>
  96. /// <param name="minutes">自定义周期分钟数</param>
  97. /// <returns>返回图表数据</returns>
  98. public List<FormulaModel> OpenEditor(List<Data.Interfaces.IBarDataPoint> source, string[] formulaArray, Data.EnumTypes.CycleType cycleType, TimeSpan minutes)
  99. {
  100. string selectFormula = (formulaArray != null && formulaArray.Any())
  101. ? formulaArray[0]
  102. : "";
  103. Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(SetLanguage.languageName);
  104. var manager = new FormulaManager();
  105. if (manager.ShowForm(formulaArray, selectFormula) == DialogResult.OK)
  106. {
  107. //公式之间用#号隔开
  108. string[] currentFormulas = manager.CurrentFormulas.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
  109. var commonDataProvider = new CommonDataProvider(null);
  110. //todo:以下内容考虑用线程实现
  111. commonDataProvider.LoadBinary(ConvertData(source));
  112. commonDataProvider.DataCycle = ConvertCycle(cycleType, minutes);
  113. var formulas = new List<FormulaModel>();
  114. foreach (var formula in currentFormulas)
  115. {
  116. try
  117. {
  118. var formulaModel = new FormulaModel();
  119. var formulaBase = FormulaBase.GetFormulaByName(formula);
  120. var formulaPackage = formulaBase.Run(commonDataProvider);
  121. if (formulaPackage.Count > 0)
  122. {
  123. var indicatorModels = new List<IndicatorModel>();
  124. for (int i = 0; i < formulaPackage.Count; i++)
  125. {
  126. var indicatorModel = FormulaDataCovnert(formulaPackage[i], formulaModel, commonDataProvider["DATE"]);
  127. indicatorModels.Add(indicatorModel);
  128. }
  129. formulaModel.FormulaName = formulaBase.FormulaName;
  130. formulaModel.IndicatorList = indicatorModels;
  131. formulas.Add(formulaModel);
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. //找到不公式跳过,不影响下一个
  137. // throw;
  138. }
  139. }
  140. return formulas;
  141. }
  142. return null;
  143. }
  144. /// <summary>
  145. /// 将编辑器计算的数据生成转换生成图表数据
  146. /// </summary>
  147. /// <param name="formulaData">计算后的数据</param>
  148. /// <returns>生成图表数据</returns>
  149. private IndicatorModel FormulaDataCovnert(FormulaData formulaData, FormulaModel formulaModel, double[] dateTime)
  150. {
  151. var indicator = new IndicatorModel(formulaModel);
  152. indicator.Alpha = formulaData.Alpha;
  153. if (formulaData.AreaBrush is System.Drawing.SolidBrush) //只处理单色
  154. {
  155. SolidBrush solidBrush = formulaData.AreaBrush as System.Drawing.SolidBrush;
  156. indicator.AreaBrush = System.Windows.Media.Color.FromArgb
  157. (solidBrush.Color.A, solidBrush.Color.R, solidBrush.Color.G, solidBrush.Color.B);
  158. }
  159. if (formulaData.FormulaUpColor != System.Drawing.Color.Empty)
  160. {
  161. indicator.UpBrush = System.Windows.Media.Color.FromArgb(formulaData.FormulaUpColor.A,
  162. formulaData.FormulaUpColor.R, formulaData.FormulaUpColor.G, formulaData.FormulaUpColor.B);
  163. }
  164. if (formulaData.FormulaDownColor != System.Drawing.Color.Empty)
  165. {
  166. indicator.DownBrush = System.Windows.Media.Color.FromArgb(formulaData.FormulaDownColor.A,
  167. formulaData.FormulaDownColor.R, formulaData.FormulaDownColor.G, formulaData.FormulaDownColor.B);
  168. }
  169. indicator.CalculateDataPoints = ConvertDataPoint(formulaData.Data, dateTime);
  170. // indicator.SubData = formulaData.SubData;
  171. indicator.DataFormat = formulaData.Format;
  172. indicator.LineName = formulaData.Name;
  173. indicator.LineWidth = formulaData.LineWidth;
  174. #region 还有多种未对应
  175. switch (formulaData.DashStyle)
  176. {
  177. case System.Drawing.Drawing2D.DashStyle.Custom: //未设置
  178. indicator.DashStyleType = DashStyleType.Dashed;
  179. break;
  180. case System.Drawing.Drawing2D.DashStyle.Dash:
  181. indicator.DashStyleType = DashStyleType.Dashed;
  182. break;
  183. case System.Drawing.Drawing2D.DashStyle.DashDot://
  184. indicator.DashStyleType = DashStyleType.Dashed;
  185. break;
  186. case System.Drawing.Drawing2D.DashStyle.DashDotDot:
  187. indicator.DashStyleType = DashStyleType.LongDashes; //
  188. break;
  189. case System.Drawing.Drawing2D.DashStyle.Dot:
  190. indicator.DashStyleType = DashStyleType.Dotted;
  191. break;
  192. case System.Drawing.Drawing2D.DashStyle.Solid:
  193. indicator.DashStyleType = DashStyleType.Solid;
  194. break;
  195. default:
  196. indicator.DashStyleType = DashStyleType.Solid;
  197. break;
  198. }
  199. #endregion
  200. #region 图表类型未一一对应
  201. switch (formulaData.RenderType)
  202. {
  203. case FormulaRenderType.NORMAL:
  204. indicator.LinePlotType = LinePlotType.Line;
  205. break;
  206. case FormulaRenderType.COLORSTICK:
  207. indicator.LinePlotType = LinePlotType.Bar;
  208. indicator.PlotNegValsDiffColor = true;
  209. break;
  210. case FormulaRenderType.VOLSTICK:
  211. indicator.LinePlotType = LinePlotType.Bar;
  212. indicator.VolBar = true;
  213. break;
  214. case FormulaRenderType.STICKLINE:
  215. indicator.LinePlotType = LinePlotType.Bar;
  216. break;
  217. case FormulaRenderType.ICON:
  218. break;
  219. case FormulaRenderType.TEXT:
  220. break;
  221. case FormulaRenderType.POLY:
  222. break;
  223. case FormulaRenderType.FILLRGN:
  224. indicator.LinePlotType = LinePlotType.FillRGN;
  225. var doubleArray = formulaData.SubData["PRICE2"] as double[];
  226. if (doubleArray != null)
  227. {
  228. indicator.SubData[indicator.FillRNGKey] = ConvertDataPoint(doubleArray, dateTime);
  229. }
  230. indicator.SubData[indicator.CondKey] = formulaData.SubData[indicator.CondKey];
  231. break;
  232. case FormulaRenderType.FILLAREA:
  233. indicator.LinePlotType = LinePlotType.Area;
  234. break;
  235. case FormulaRenderType.PARTLINE:
  236. indicator.LinePlotType = LinePlotType.OHLC;
  237. break;
  238. case FormulaRenderType.LINE:
  239. indicator.LinePlotType = LinePlotType.Line;
  240. break;
  241. case FormulaRenderType.VERTLINE:
  242. break;
  243. case FormulaRenderType.AXISY:
  244. break;
  245. case FormulaRenderType.AXISYTEXT:
  246. break;
  247. case FormulaRenderType.STOCK:
  248. indicator.LinePlotType = LinePlotType.Candlestick;
  249. break;
  250. case FormulaRenderType.MONOSTOCK:
  251. indicator.LinePlotType = LinePlotType.Candlestick;
  252. break;
  253. default:
  254. break;
  255. }
  256. #endregion
  257. #region 点样式
  258. switch (formulaData.Dot)
  259. {
  260. case FormulaDot.NORMAL:
  261. indicator.PointDotType = PointDotType.NORMAL;
  262. break;
  263. case FormulaDot.CROSSDOT:
  264. indicator.PointDotType = PointDotType.CROSSDOT;
  265. indicator.LinePlotType = LinePlotType.CROSSDOT;
  266. break;
  267. case FormulaDot.POINTDOT:
  268. indicator.PointDotType = PointDotType.POINTDOT;
  269. indicator.LinePlotType = LinePlotType.Dot;
  270. break;
  271. case FormulaDot.CIRCLEDOT:
  272. indicator.PointDotType = PointDotType.CIRCLEDOT;
  273. indicator.LinePlotType = LinePlotType.CIRCLEDOT;
  274. break;
  275. default:
  276. indicator.PointDotType = PointDotType.NORMAL;
  277. break;
  278. }
  279. #endregion
  280. indicator.LineWidth = formulaData.LineWidth;
  281. indicator.SameColor = formulaData.SameColor;
  282. return indicator;
  283. }
  284. /// <summary>
  285. /// 将使用公式计算数据转换成BarPoint
  286. /// </summary>
  287. /// <param name="dataArray">计算数据</param>
  288. /// <param name="dateArray"> </param>
  289. /// <returns></returns>
  290. private List<Data.Interfaces.ILineDataPoint> ConvertDataPoint(double[] dataArray, double[] dateArray)
  291. {
  292. if (dataArray == null) return null;
  293. int index = 0;
  294. var BarPoints = new List<Data.Interfaces.ILineDataPoint>();
  295. if (dataArray.Length < dateArray.Length)
  296. {
  297. foreach (var d in dateArray)
  298. {
  299. if (index >= dataArray.Length)
  300. {
  301. var barPoint = new BarDataPoint(DateTime.FromOADate(d), BarPoints[index - 1].Value);
  302. BarPoints.Add(barPoint);
  303. }
  304. else
  305. {
  306. if (dataArray.Length == 0)
  307. {
  308. var barPoint = new BarDataPoint(DateTime.FromOADate(d), (float)dataArray[index]);
  309. BarPoints.Add(barPoint);
  310. }
  311. else
  312. {
  313. var barPoint = new BarDataPoint(DateTime.FromOADate(d), (float)dataArray[index]);
  314. BarPoints.Add(barPoint);
  315. }
  316. }
  317. index++;
  318. }
  319. }
  320. else
  321. {
  322. if (dataArray != null && dataArray.Count() > 0)
  323. {
  324. foreach (var d in dataArray)
  325. {
  326. if (!double.IsNaN(d))
  327. {
  328. var barPoint = new BarDataPoint(DateTime.FromOADate(dateArray[index]), (float)d);
  329. BarPoints.Add(barPoint);
  330. index++;
  331. }
  332. }
  333. }
  334. }
  335. return BarPoints;
  336. }
  337. /// <summary>
  338. /// 转换周期
  339. /// </summary>
  340. /// <param name="cycleType">周期类型</param>
  341. /// <param name="minutes">分钟数据</param>
  342. /// <returns>编辑器中的数据周期</returns>
  343. private DataCycle ConvertCycle(Data.EnumTypes.CycleType cycleType, TimeSpan minutes)
  344. {
  345. switch (cycleType)
  346. {
  347. case CycleType.None:
  348. return DataCycle.Day;
  349. break;
  350. case CycleType.Tik:
  351. return DataCycle.Tick;
  352. case CycleType.Minute:
  353. return DataCycle.Minute;
  354. case CycleType.Minute3:
  355. return new DataCycle(DataCycleBase.MINUTE, 3);
  356. case CycleType.Minute5:
  357. return new DataCycle(DataCycleBase.MINUTE, 5);
  358. case CycleType.Minute10:
  359. return new DataCycle(DataCycleBase.MINUTE, 10);
  360. case CycleType.Minute15:
  361. return new DataCycle(DataCycleBase.MINUTE, 15);
  362. case CycleType.Minute30:
  363. return new DataCycle(DataCycleBase.MINUTE, 30);
  364. case CycleType.Minute60:
  365. return new DataCycle(DataCycleBase.MINUTE, 60);
  366. case CycleType.Minute90:
  367. return new DataCycle(DataCycleBase.MINUTE, 90);
  368. case CycleType.Minute120:
  369. return new DataCycle(DataCycleBase.MINUTE, 120);
  370. case CycleType.Minute180:
  371. return new DataCycle(DataCycleBase.MINUTE, 180);
  372. case CycleType.Minute240:
  373. return new DataCycle(DataCycleBase.MINUTE, 240);
  374. case CycleType.Hour:
  375. return DataCycle.Hour;
  376. case CycleType.Day:
  377. return DataCycle.Day;
  378. case CycleType.Week:
  379. return DataCycle.Week;
  380. case CycleType.Month:
  381. return DataCycle.Minute;
  382. case CycleType.Quarter:
  383. return DataCycle.Quarter;
  384. case CycleType.Year:
  385. return DataCycle.Year;
  386. case CycleType.Custom:
  387. return new DataCycle(DataCycleBase.MINUTE, (int)minutes.TotalMinutes);
  388. default:
  389. return DataCycle.Day;
  390. }
  391. }
  392. /// <summary>
  393. /// 将源数据转换成数组
  394. /// </summary>
  395. /// <param name="source">源数据</param>
  396. /// <returns>返回open, high, low, close, volume, date数据组 </returns>
  397. private double[][] ConvertData(List<Data.Interfaces.IBarDataPoint> source)
  398. {
  399. var opens = new double[source.Count];
  400. var highs = new double[source.Count];
  401. var lows = new double[source.Count];
  402. var closes = new double[source.Count];
  403. var volumes = new double[source.Count];
  404. var dates = new double[source.Count];
  405. for (int i = 0; i < source.Count; i++)
  406. {
  407. opens[i] = source[i].Open;
  408. highs[i] = source[i].High;
  409. lows[i] = source[i].Low;
  410. closes[i] = source[i].Close;
  411. volumes[i] = source[i].Volume;
  412. dates[i] = source[i].Date.ToOADate();
  413. }
  414. return new double[][] { opens, highs, lows, closes, volumes, dates };
  415. }
  416. /// <summary>
  417. /// 编辑公式
  418. /// </summary>
  419. /// <param name="formulaName">公式名称</param>
  420. public void EditFormula(string formulaName)
  421. {
  422. MessageBoxManager.Unregister();
  423. switch (SetLanguage.currentLanguage)
  424. {
  425. case ChartLanguageType.SimplifiedChinese:
  426. MessageBoxManager.Yes = "是";
  427. MessageBoxManager.No = "否";
  428. MessageBoxManager.Cancel = "取消";
  429. break;
  430. case ChartLanguageType.TraditionalChinese:
  431. MessageBoxManager.Yes = "是";
  432. MessageBoxManager.No = "否";
  433. MessageBoxManager.Cancel = "取消";
  434. break;
  435. case ChartLanguageType.English:
  436. MessageBoxManager.Yes = "Yes";
  437. MessageBoxManager.No = "No";
  438. MessageBoxManager.Cancel = "Cancel";
  439. break;
  440. }
  441. //注册MessageBoxManager
  442. MessageBoxManager.Register();
  443. var formulaBase = FormulaBase.GetFormulaByName(formulaName);
  444. string formulaFile = PluginManager.GetFormulaFile(formulaBase);
  445. if (formulaFile != null)
  446. {
  447. string directoryName = Path.GetDirectoryName(formulaFile);
  448. formulaFile = Path.GetFileNameWithoutExtension(formulaFile).Replace('_', '.');
  449. formulaFile = directoryName + @"\" + formulaFile;
  450. if (File.Exists(formulaFile))
  451. {
  452. Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(SetLanguage.languageName);
  453. FormulaSourceEditor.Open(formulaFile, formulaBase.GetType().ToString());
  454. }
  455. }
  456. }
  457. public DataService()
  458. {
  459. SetLanguage.SetcurrentLanguage(ChartLanguageType.SimplifiedChinese);
  460. }
  461. /// <summary>
  462. /// 设置编辑器的语言
  463. /// </summary>
  464. /// <param name="language"></param>
  465. public void SetEditorLanguage(ChartLanguageType language)
  466. {
  467. SetLanguage.SetcurrentLanguage(language);
  468. }
  469. /// <summary>
  470. /// 从文件中加载公式
  471. /// </summary>
  472. /// <param name="fileName">文件名称</param>
  473. /// <returns></returns>
  474. public SpaceModel LoadSpaceFromFile(string fileName)
  475. {
  476. if (string.IsNullOrEmpty(fileName)) //如fileName为空取主图指标的路径
  477. {
  478. var formulaBase = FormulaBase.GetFormulaByName("Main");
  479. string formulaFile = PluginManager.GetFormulaFile(formulaBase);
  480. if (formulaFile != null)
  481. {
  482. string directoryName = Path.GetDirectoryName(formulaFile);
  483. formulaFile = Path.GetFileNameWithoutExtension(formulaFile).Replace('_', '.');
  484. fileName = directoryName + @"\" + formulaFile;
  485. }
  486. }
  487. var formulaSpace = FormulaSpace.Read(fileName);
  488. if (formulaSpace != null)
  489. {
  490. var spaceModel = new SpaceModel();
  491. CovertToModel(spaceModel, formulaSpace);
  492. return spaceModel;
  493. }
  494. return null;
  495. }
  496. /// <summary>
  497. /// 转换Xml中公式命名对象
  498. /// </summary>
  499. /// <param name="model"></param>
  500. /// <param name="fs"></param>
  501. private void CovertToModel(SpaceModel model, FormulaSpace fs)
  502. {
  503. model.SpaceModels = new List<SpaceModel>();
  504. model.ProgramModels = new List<ProgramModel>();
  505. foreach (FormulaSpace space in fs.Namespaces) //加载命名空间
  506. {
  507. SpaceModel modelItem = new SpaceModel();
  508. modelItem.SpaceName = space.Name;
  509. model.SpaceModels.Add(modelItem);
  510. this.CovertToModel(modelItem, space);
  511. }
  512. if (fs.Programs != null)
  513. {
  514. foreach (FormulaProgram program in fs.Programs)
  515. {
  516. var programItem = new ProgramModel();
  517. programItem.Name = program.Name;
  518. programItem.IsMainView = program.IsMainView;
  519. model.ProgramModels.Add(programItem);
  520. if (program.Params != null && program.Params.Count > 0)
  521. {
  522. foreach (var paramItem in program.Params)
  523. {
  524. var fParam = paramItem as FormulaParam;
  525. if (fParam != null && !string.IsNullOrEmpty(fParam.Name))
  526. {
  527. programItem.DefaultParams[fParam.Name] = fParam.DefaultValue;
  528. programItem.CustomParams[fParam.Name] = new CustomParamModel()
  529. {
  530. Value = fParam.CustomValue
  531. };
  532. }
  533. }
  534. }
  535. }
  536. }
  537. }
  538. /// <summary>
  539. /// 保存指标参数
  540. /// </summary>
  541. /// <param name="programModels">公式参数</param>
  542. public void SaveFormulaParams(List<ProgramModel> programModels)
  543. {
  544. if (programModels == null || !programModels.Any()) return;
  545. var dicFormulaSpace = new Dictionary<string, FormulaSpace>();
  546. foreach (var programModel in programModels)
  547. {
  548. var formulaBase = FormulaBase.GetFormulaByName(programModel.Name);
  549. if (formulaBase == null) continue;
  550. string formulaFile = PluginManager.GetFormulaFile(formulaBase);
  551. string directoryName = Path.GetDirectoryName(formulaFile);
  552. formulaFile = Path.GetFileNameWithoutExtension(formulaFile);
  553. if (!string.IsNullOrWhiteSpace(formulaFile))
  554. {
  555. formulaFile = formulaFile.Replace('_', '.');
  556. var fileName = directoryName + @"\" + formulaFile;
  557. FormulaProgram formulaProgram = null;
  558. if (!dicFormulaSpace.ContainsKey(fileName))
  559. {
  560. var space = FormulaSpace.Read(fileName);
  561. if (space != null)
  562. {
  563. dicFormulaSpace[fileName] = space;
  564. formulaProgram = space.FindFormulaProgram(formulaBase);
  565. }
  566. }
  567. else
  568. {
  569. formulaProgram = dicFormulaSpace[fileName].FindFormulaProgram(formulaBase);
  570. }
  571. if (formulaProgram != null)
  572. {
  573. if (programModel.CustomParams != null && programModel.CustomParams.Keys.Any())
  574. {
  575. foreach (var key in programModel.CustomParams.Keys)
  576. {
  577. if (formulaProgram.Params[key] != null)
  578. {
  579. formulaProgram.Params[key].CustomValue = programModel.CustomParams[key].Value;
  580. }
  581. }
  582. }
  583. }
  584. }
  585. }
  586. if (dicFormulaSpace.Any()) //保存参数到配置文件
  587. {
  588. foreach (var key in dicFormulaSpace.Keys)
  589. {
  590. dicFormulaSpace[key].Write(key);
  591. }
  592. }
  593. }
  594. public void OpenFormulaEdit(string formulaName)
  595. {
  596. MessageBoxManager.Unregister();
  597. switch (SetLanguage.currentLanguage)
  598. {
  599. case ChartLanguageType.SimplifiedChinese:
  600. MessageBoxManager.Yes = "是";
  601. MessageBoxManager.No = "否";
  602. MessageBoxManager.Cancel = "取消";
  603. break;
  604. case ChartLanguageType.TraditionalChinese:
  605. MessageBoxManager.Yes = "是";
  606. MessageBoxManager.No = "否";
  607. MessageBoxManager.Cancel = "取消";
  608. break;
  609. case ChartLanguageType.English:
  610. MessageBoxManager.Yes = "Yes";
  611. MessageBoxManager.No = "No";
  612. MessageBoxManager.Cancel = "Cancel";
  613. break;
  614. }
  615. //注册MessageBoxManager
  616. MessageBoxManager.Register();
  617. var formulaBase = FormulaBase.GetFormulaByName(formulaName);
  618. string formulaFile = PluginManager.GetFormulaFile(formulaBase);
  619. if (formulaFile != null)
  620. {
  621. string directoryName = Path.GetDirectoryName(formulaFile);
  622. formulaFile = Path.GetFileNameWithoutExtension(formulaFile).Replace('_', '.');
  623. formulaFile = directoryName + @"\" + formulaFile;
  624. if (File.Exists(formulaFile))
  625. {
  626. Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(SetLanguage.languageName);
  627. FormulaSourceEditor.Open(formulaFile, "FML.");
  628. }
  629. }
  630. }
  631. /// <summary>
  632. /// 转换DataPoint数据
  633. /// </summary>
  634. /// <param name="source">数据源</param>
  635. /// <param name="cycleType">周期</param>
  636. /// <param name="minutes">自定义周期时间</param>
  637. /// <returns></returns>
  638. public CommonDataProvider DataPointConvert(List<Data.Interfaces.IBarDataPoint> source, CycleType cycleType, TimeSpan minutes,IDataManager dataManager)
  639. {
  640. var commonDataProvider = new CommonDataProvider(dataManager);
  641. if (source != null)
  642. {
  643. commonDataProvider.LoadBinary(ConvertData(source));
  644. commonDataProvider.DataCycle = ConvertCycle(cycleType, minutes);
  645. }
  646. return commonDataProvider;
  647. }
  648. }
  649. }