FormulaManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. namespace Easychart.Finance.Win
  2. {
  3. using Easychart.Finance;
  4. using System;
  5. using System.Collections;
  6. using System.ComponentModel;
  7. using System.Drawing;
  8. using System.Resources;
  9. using System.Runtime.InteropServices;
  10. using System.Windows.Forms;
  11. [ToolboxItem(false)]
  12. public class FormulaManager : Form
  13. {
  14. private System.Windows.Forms.Button btnCancel;
  15. private System.Windows.Forms.Button btnDelete;
  16. private System.Windows.Forms.Button btnDown;
  17. private System.Windows.Forms.Button btnMore;
  18. private System.Windows.Forms.Button btnOK;
  19. private System.Windows.Forms.Button btnSource;
  20. private System.Windows.Forms.Button btnUp;
  21. private System.Windows.Forms.CheckBox cbSecondYAxis;
  22. private IContainer components;
  23. private System.Windows.Forms.ComboBox ddlFavoriteFormula;
  24. private bool DisableChangeEvent;
  25. private System.Windows.Forms.GroupBox gbCurrent;
  26. private System.Windows.Forms.GroupBox gbParam;
  27. public System.Windows.Forms.ImageList ilFormula;
  28. private System.Windows.Forms.ImageList ilIcon;
  29. private System.Windows.Forms.ListBox lbFormulaList;
  30. private string[] ListedFormulas = new string[] {
  31. "MAIN", "MainArea", "VOLMA", "MACD", "RSI", "CMF", "TRIX", "CCI", "FastSTO", "SlowSTO", "ATR", "OBV", "ULT", "DPO", "WR", "PPO",
  32. "PVO", "StochRSI", "AD"
  33. };
  34. private string[] OverlayFormulas = new string[] {
  35. "MAIN", "MainArea", "HL", "MA", "EMA", "BB", "AreaBB", "SAR", "ZIGLABEL", "ZIG", "ZIGICON", "ZIGW", "ZIGSR", "SR", "SRAxisY", "COMPARE",
  36. "COMPARE2", "Fibonnaci", "LinRegr", "TradingIcon"
  37. };
  38. private System.Windows.Forms.ToolTip tpBtnDown;
  39. private System.Windows.Forms.ToolTip tpBtnUp;
  40. private System.Windows.Forms.ToolTip tpDelete;
  41. private System.Windows.Forms.ToolTip tpSource;
  42. public FormulaManager()
  43. {
  44. this.InitializeComponent();
  45. this.tpBtnUp.SetToolTip(this.btnUp, "Move Up");
  46. this.tpBtnDown.SetToolTip(this.btnDown, "Move Down");
  47. this.tpDelete.SetToolTip(this.btnDelete, "Delete");
  48. this.tpSource.SetToolTip(this.btnSource, "Source Code");
  49. }
  50. private void AddFavorite(string[] Formulas)
  51. {
  52. this.ddlFavoriteFormula.Items.Clear();
  53. this.ddlFavoriteFormula.Items.Add("Select to add new formula");
  54. foreach (string str in Formulas)
  55. {
  56. FormulaBase formulaByName = FormulaBase.GetFormulaByName(str);
  57. foreach (object obj2 in this.ddlFavoriteFormula.Items)
  58. {
  59. if (obj2.GetType() == formulaByName.GetType())
  60. {
  61. continue;
  62. }
  63. }
  64. this.ddlFavoriteFormula.Items.Add(formulaByName);
  65. }
  66. this.ddlFavoriteFormula.SelectedIndex = 0;
  67. }
  68. private void AddFormulas()
  69. {
  70. FormulaBase selectedItem = (FormulaBase) this.ddlFavoriteFormula.SelectedItem;
  71. if (selectedItem != null)
  72. {
  73. this.AddFormulas(selectedItem.CreateName);
  74. }
  75. }
  76. private void AddFormulas(string Name)
  77. {
  78. this.lbFormulaList.Items.Add(Name);
  79. this.SelectLastFormula();
  80. }
  81. private void btnDelete_Click(object sender, EventArgs e)
  82. {
  83. this.DeleteFormulas();
  84. }
  85. private void btnDown_Click(object sender, EventArgs e)
  86. {
  87. this.MoveFormula(1);
  88. }
  89. private void btnMore_Click(object sender, EventArgs e)
  90. {
  91. string name = ChartWinControl.DoSelectFormula(null, null, false);
  92. if (name != null)
  93. {
  94. this.AddFormulas(name);
  95. }
  96. }
  97. private void btnSource_Click(object sender, EventArgs e)
  98. {
  99. int selectedIndex = this.lbFormulaList.SelectedIndex;
  100. if (selectedIndex >= 0)
  101. {
  102. ChartWinControl.EditFormula(FormulaBase.GetFormulaByName((string) this.lbFormulaList.Items[selectedIndex]));
  103. }
  104. }
  105. private void btnUp_Click(object sender, EventArgs e)
  106. {
  107. this.MoveFormula(-1);
  108. }
  109. private void cbSecondYAxis_CheckedChanged(object sender, EventArgs e)
  110. {
  111. this.DisableChangeEvent = true;
  112. try
  113. {
  114. if (this.lbFormulaList.SelectedItem != null)
  115. {
  116. string str = this.lbFormulaList.SelectedItem.ToString();
  117. this.lbFormulaList.Items[this.lbFormulaList.SelectedIndex] = str.TrimEnd(new char[] { '!' }) + (this.cbSecondYAxis.Checked ? "!" : "");
  118. }
  119. }
  120. finally
  121. {
  122. this.DisableChangeEvent = false;
  123. }
  124. }
  125. private void cbSecondYAxis_Click(object sender, EventArgs e)
  126. {
  127. }
  128. private void ddlFavoriteFormula_SelectedIndexChanged(object sender, EventArgs e)
  129. {
  130. if (this.ddlFavoriteFormula.SelectedIndex > 0)
  131. {
  132. this.AddFormulas();
  133. this.ddlFavoriteFormula.SelectedIndex = 0;
  134. }
  135. }
  136. private void DeleteFormulas()
  137. {
  138. this.DisableChangeEvent = true;
  139. int selectedIndex = this.lbFormulaList.SelectedIndex;
  140. if (selectedIndex >= 0)
  141. {
  142. try
  143. {
  144. this.lbFormulaList.Items.RemoveAt(selectedIndex);
  145. }
  146. finally
  147. {
  148. this.DisableChangeEvent = false;
  149. }
  150. this.SetSelectFormula(selectedIndex);
  151. }
  152. }
  153. protected override void Dispose(bool disposing)
  154. {
  155. if (disposing && (this.components != null))
  156. {
  157. this.components.Dispose();
  158. }
  159. base.Dispose(disposing);
  160. }
  161. private void InitializeComponent()
  162. {
  163. this.components = new Container();
  164. ResourceManager manager = new ResourceManager(typeof(FormulaManager));
  165. this.ddlFavoriteFormula = new ComboBox();
  166. this.btnCancel = new Button();
  167. this.gbCurrent = new GroupBox();
  168. this.btnDown = new Button();
  169. this.ilIcon = new ImageList(this.components);
  170. this.btnUp = new Button();
  171. this.btnSource = new Button();
  172. this.lbFormulaList = new ListBox();
  173. this.gbParam = new GroupBox();
  174. this.btnDelete = new Button();
  175. this.btnOK = new Button();
  176. this.btnMore = new Button();
  177. this.ilFormula = new ImageList(this.components);
  178. this.tpBtnUp = new ToolTip(this.components);
  179. this.tpBtnDown = new ToolTip(this.components);
  180. this.tpDelete = new ToolTip(this.components);
  181. this.tpSource = new ToolTip(this.components);
  182. this.cbSecondYAxis = new CheckBox();
  183. this.gbCurrent.SuspendLayout();
  184. base.SuspendLayout();
  185. this.ddlFavoriteFormula.DisplayMember = "CombineName";
  186. this.ddlFavoriteFormula.DropDownStyle = ComboBoxStyle.DropDownList;
  187. this.ddlFavoriteFormula.Location = new Point(0x10, 0x10);
  188. this.ddlFavoriteFormula.MaxDropDownItems = 12;
  189. this.ddlFavoriteFormula.Name = "ddlFavoriteFormula";
  190. this.ddlFavoriteFormula.Size = new Size(0x103, 0x15);
  191. this.ddlFavoriteFormula.TabIndex = 0;
  192. this.ddlFavoriteFormula.SelectedIndexChanged += new EventHandler(this.ddlFavoriteFormula_SelectedIndexChanged);
  193. this.btnCancel.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
  194. this.btnCancel.DialogResult =System.Windows.Forms.DialogResult.Cancel;
  195. this.btnCancel.Location = new Point(0x68, 400);
  196. this.btnCancel.Name = "btnCancel";
  197. this.btnCancel.TabIndex = 9;
  198. this.btnCancel.Text = "&Cancel";
  199. this.gbCurrent.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
  200. this.gbCurrent.Controls.Add(this.cbSecondYAxis);
  201. this.gbCurrent.Controls.Add(this.btnDown);
  202. this.gbCurrent.Controls.Add(this.btnUp);
  203. this.gbCurrent.Controls.Add(this.btnSource);
  204. this.gbCurrent.Controls.Add(this.lbFormulaList);
  205. this.gbCurrent.Controls.Add(this.gbParam);
  206. this.gbCurrent.Controls.Add(this.btnDelete);
  207. this.gbCurrent.Location = new Point(0x10, 0x30);
  208. this.gbCurrent.Name = "gbCurrent";
  209. this.gbCurrent.Size = new Size(0x250, 0x158);
  210. this.gbCurrent.TabIndex = 12;
  211. this.gbCurrent.TabStop = false;
  212. this.gbCurrent.Text = "Current Formulas";
  213. this.btnDown.Anchor = AnchorStyles.Right | AnchorStyles.Top;
  214. this.btnDown.FlatStyle = FlatStyle.Popup;
  215. this.btnDown.ImageAlign = ContentAlignment.BottomCenter;
  216. this.btnDown.ImageIndex = 1;
  217. this.btnDown.ImageList = this.ilIcon;
  218. this.btnDown.Location = new Point(280, 0x40);
  219. this.btnDown.Name = "btnDown";
  220. this.btnDown.Size = new Size(0x18, 0x18);
  221. this.btnDown.TabIndex = 4;
  222. this.btnDown.Click += new EventHandler(this.btnDown_Click);
  223. this.ilIcon.ImageSize = new Size(20, 20);
  224. this.ilIcon.ImageStream = (System.Windows.Forms.ImageListStreamer)manager.GetObject("ilIcon.ImageStream");
  225. this.ilIcon.TransparentColor = Color.Transparent;
  226. this.btnUp.Anchor = AnchorStyles.Right | AnchorStyles.Top;
  227. this.btnUp.FlatStyle = FlatStyle.Popup;
  228. this.btnUp.ImageAlign = ContentAlignment.BottomCenter;
  229. this.btnUp.ImageIndex = 0;
  230. this.btnUp.ImageList = this.ilIcon;
  231. this.btnUp.Location = new Point(280, 0x20);
  232. this.btnUp.Name = "btnUp";
  233. this.btnUp.Size = new Size(0x18, 0x18);
  234. this.btnUp.TabIndex = 3;
  235. this.btnUp.Click += new EventHandler(this.btnUp_Click);
  236. this.btnSource.Anchor = AnchorStyles.Right | AnchorStyles.Top;
  237. this.btnSource.FlatStyle = FlatStyle.Popup;
  238. this.btnSource.ImageIndex = 3;
  239. this.btnSource.ImageList = this.ilIcon;
  240. this.btnSource.Location = new Point(280, 0x81);
  241. this.btnSource.Name = "btnSource";
  242. this.btnSource.Size = new Size(0x18, 0x18);
  243. this.btnSource.TabIndex = 6;
  244. this.btnSource.Click += new EventHandler(this.btnSource_Click);
  245. this.lbFormulaList.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
  246. this.lbFormulaList.DisplayMember = "Description";
  247. this.lbFormulaList.Location = new Point(8, 0x18);
  248. this.lbFormulaList.Name = "lbFormulaList";
  249. this.lbFormulaList.Size = new Size(0x108, 0x12f);
  250. this.lbFormulaList.TabIndex = 2;
  251. this.lbFormulaList.ValueMember = "Name";
  252. this.lbFormulaList.SelectedIndexChanged += new EventHandler(this.lbOverlayList_SelectedIndexChanged);
  253. this.gbParam.Anchor = AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Top;
  254. this.gbParam.Location = new Point(0x138, 0x12);
  255. this.gbParam.Name = "gbParam";
  256. this.gbParam.Size = new Size(0x110, 0x11f);
  257. this.gbParam.TabIndex = 7;
  258. this.gbParam.TabStop = false;
  259. this.gbParam.Text = "Parameters";
  260. this.btnDelete.Anchor = AnchorStyles.Right | AnchorStyles.Top;
  261. this.btnDelete.FlatStyle = FlatStyle.Popup;
  262. this.btnDelete.ImageAlign = ContentAlignment.BottomCenter;
  263. this.btnDelete.ImageIndex = 2;
  264. this.btnDelete.ImageList = this.ilIcon;
  265. this.btnDelete.Location = new Point(280, 0x60);
  266. this.btnDelete.Name = "btnDelete";
  267. this.btnDelete.Size = new Size(0x18, 0x18);
  268. this.btnDelete.TabIndex = 5;
  269. this.btnDelete.Click += new EventHandler(this.btnDelete_Click);
  270. this.btnOK.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
  271. this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
  272. this.btnOK.Location = new Point(0x10, 400);
  273. this.btnOK.Name = "btnOK";
  274. this.btnOK.TabIndex = 8;
  275. this.btnOK.Text = "&OK";
  276. this.btnMore.Location = new Point(0x120, 15);
  277. this.btnMore.Name = "btnMore";
  278. this.btnMore.Size = new Size(0x48, 0x17);
  279. this.btnMore.TabIndex = 1;
  280. this.btnMore.Text = "More";
  281. this.btnMore.Click += new EventHandler(this.btnMore_Click);
  282. this.ilFormula.ImageSize = new Size(0x10, 0x10);
  283. this.ilFormula.ImageStream = (System.Windows.Forms.ImageListStreamer)manager.GetObject("ilFormula.ImageStream");
  284. this.ilFormula.TransparentColor = Color.Transparent;
  285. this.cbSecondYAxis.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
  286. this.cbSecondYAxis.FlatStyle = FlatStyle.Flat;
  287. this.cbSecondYAxis.Location = new Point(0x138, 0x139);
  288. this.cbSecondYAxis.Name = "cbSecondYAxis";
  289. this.cbSecondYAxis.Size = new Size(0xd0, 0x13);
  290. this.cbSecondYAxis.TabIndex = 8;
  291. this.cbSecondYAxis.Text = "Show in second Y-axis";
  292. this.cbSecondYAxis.Click += new EventHandler(this.cbSecondYAxis_Click);
  293. this.cbSecondYAxis.CheckedChanged += new EventHandler(this.cbSecondYAxis_CheckedChanged);
  294. base.AcceptButton = this.btnOK;
  295. this.AutoScaleBaseSize = new Size(6, 14);
  296. base.CancelButton = this.btnCancel;
  297. base.ClientSize = new Size(0x268, 0x1b6);
  298. base.Controls.Add(this.btnMore);
  299. base.Controls.Add(this.ddlFavoriteFormula);
  300. base.Controls.Add(this.btnCancel);
  301. base.Controls.Add(this.gbCurrent);
  302. base.Controls.Add(this.btnOK);
  303. this.Font = new Font("Verdana", 8.5f);
  304. base.MinimumSize = new Size(400, 400);
  305. base.Name = "FormulaManager";
  306. base.StartPosition = FormStartPosition.CenterScreen;
  307. this.Text = "Formula Manager";
  308. this.gbCurrent.ResumeLayout(false);
  309. base.ResumeLayout(false);
  310. }
  311. private void lbOverlayList_SelectedIndexChanged(object sender, EventArgs e)
  312. {
  313. if (!this.DisableChangeEvent)
  314. {
  315. string str;
  316. this.ParamToTextBox((string) this.lbFormulaList.SelectedItem, out str);
  317. }
  318. }
  319. private void MoveFormula(int Delta)
  320. {
  321. this.DisableChangeEvent = true;
  322. try
  323. {
  324. int selectedIndex = this.lbFormulaList.SelectedIndex;
  325. int num2 = selectedIndex + Delta;
  326. if (((selectedIndex >= 0) && (num2 >= 0)) && (num2 < this.lbFormulaList.Items.Count))
  327. {
  328. object item = this.lbFormulaList.Items[selectedIndex];
  329. this.lbFormulaList.Items.RemoveAt(selectedIndex);
  330. this.lbFormulaList.Items.Insert(selectedIndex + Delta, item);
  331. this.lbFormulaList.SelectedIndex = num2;
  332. }
  333. }
  334. finally
  335. {
  336. this.DisableChangeEvent = false;
  337. }
  338. }
  339. public void ParamToTextBox(string NameAndParam, out string FormulaName)
  340. {
  341. this.cbSecondYAxis.Checked = NameAndParam.EndsWith("!");
  342. FormulaBase formulaByName = FormulaBase.GetFormulaByName(NameAndParam.TrimEnd(new char[] { '!' }));
  343. FormulaName = formulaByName.FormulaName;
  344. SelectFormula.AddParamToGroupBox(this.gbParam, this.ilFormula, formulaByName, new EventHandler(this.tbP4_Leave));
  345. }
  346. private void SelectFirstFormula()
  347. {
  348. this.SetSelectFormula(0);
  349. }
  350. private void SelectLastFormula()
  351. {
  352. this.SetSelectFormula(this.lbFormulaList.Items.Count - 1);
  353. }
  354. private void SetSelectFormula(int Index)
  355. {
  356. if (Index < 0)
  357. {
  358. Index = 0;
  359. }
  360. if (Index >= this.lbFormulaList.Items.Count)
  361. {
  362. Index = this.lbFormulaList.Items.Count - 1;
  363. }
  364. if ((Index >= 0) && (Index < this.lbFormulaList.Items.Count))
  365. {
  366. this.lbFormulaList.SelectedIndex = Index;
  367. }
  368. }
  369. public DialogResult ShowForm()
  370. {
  371. this.AddFavorite(this.ListedFormulas);
  372. return this.ShowForm(null, null);
  373. }
  374. public DialogResult ShowForm(FormulaArea fa, FormulaBase SelectedFormula)
  375. {
  376. if (fa != null)
  377. {
  378. string[] strArray = fa.FormulaToStrings();
  379. if (strArray != null)
  380. {
  381. this.CurrentFormulas = string.Join("#", strArray);
  382. string createName = "";
  383. if (SelectedFormula != null)
  384. {
  385. createName = SelectedFormula.CreateName;
  386. }
  387. this.SelectedFormula = createName;
  388. ArrayList list = new ArrayList();
  389. list.AddRange(strArray);
  390. if (fa.IsMain())
  391. {
  392. list.AddRange(this.OverlayFormulas);
  393. }
  394. else
  395. {
  396. list.AddRange(this.ListedFormulas);
  397. }
  398. this.AddFavorite((string[]) list.ToArray(typeof(string)));
  399. }
  400. }
  401. return base.ShowDialog();
  402. }
  403. private void tbP4_Leave(object sender, EventArgs e)
  404. {
  405. string str = this.TextBoxToParam((string) this.lbFormulaList.SelectedItem);
  406. if (str != "")
  407. {
  408. this.DisableChangeEvent = true;
  409. try
  410. {
  411. this.lbFormulaList.Items[this.lbFormulaList.SelectedIndex] = str;
  412. }
  413. finally
  414. {
  415. this.DisableChangeEvent = false;
  416. }
  417. }
  418. }
  419. private string TextBoxToParam(string FormulaName)
  420. {
  421. int index = FormulaName.IndexOf('(');
  422. if (index >= 0)
  423. {
  424. FormulaName = FormulaName.Substring(0, index);
  425. }
  426. string param = SelectFormula.GetParam(this.gbParam);
  427. if (FormulaBase.GetFormulaByName(FormulaName) != null)
  428. {
  429. return (FormulaName + param);
  430. }
  431. return "";
  432. }
  433. public string CurrentFormulas
  434. {
  435. get
  436. {
  437. string str = "";
  438. foreach (string str2 in this.lbFormulaList.Items)
  439. {
  440. if (str != "")
  441. {
  442. str = str + "#";
  443. }
  444. str = str + str2;
  445. }
  446. return str;
  447. }
  448. set
  449. {
  450. this.lbFormulaList.Items.Clear();
  451. if ((value != "") && (value != null))
  452. {
  453. foreach (string str in value.Split(new char[] { '#' }))
  454. {
  455. this.AddFormulas(str);
  456. }
  457. this.SelectFirstFormula();
  458. }
  459. }
  460. }
  461. public string SelectedFormula
  462. {
  463. get
  464. {
  465. if (this.lbFormulaList.SelectedIndex > 0)
  466. {
  467. return this.lbFormulaList.Items[this.lbFormulaList.SelectedIndex].ToString();
  468. }
  469. return "";
  470. }
  471. set
  472. {
  473. int index = this.lbFormulaList.Items.IndexOf(value);
  474. if (index >= 0)
  475. {
  476. this.lbFormulaList.SelectedIndex = index;
  477. }
  478. }
  479. }
  480. }
  481. }