コピペ: OmegaChart Fibonacciリトレースメント Fibonacci.cs 新規追加 ※差し替え その2

形式
Plain text
投稿日時
2018-01-13 16:47
公開期間
無期限
  1. using System;
  2. using System.Collections;
  3. using System.Drawing;
  4. using System.Text;
  5. using Zanetti.Data;
  6. using Travis.Storage;
  7. namespace Zanetti.UI
  8. {
  9. /// <summary>
  10. /// Fibonacci の概要の説明です。
  11. /// </summary>
  12. internal class Fibonacci
  13. {
  14. private PointF _pivot;
  15. private PointF _destination;
  16. private int _id; //SolidFibonacciからの作成時のみセット、それ以外は-1
  17. private LineDrawMode _mode;
  18. public enum LineDrawMode
  19. {
  20. Normal,
  21. Hilight
  22. }
  23. public Fibonacci(PointF p)
  24. {
  25. _pivot = p;
  26. _destination = p;
  27. _id = -1;
  28. }
  29. public Fibonacci(PointF p1, PointF p2)
  30. {
  31. _pivot = p1;
  32. _destination = p2;
  33. _id = -1;
  34. }
  35. //リサイズしたときなどの再計算
  36. public Fibonacci(DataFarm farm, int firstdateindex, SolidFibonacci fl, Trans value_to_y)
  37. {
  38. int p = Env.Layout.DatePitch;
  39. _pivot = new PointF((farm.DateToIndex(fl._date1) - firstdateindex) * p + p / 2, (float)value_to_y.TransValue(fl._value1));
  40. _destination = new PointF((farm.DateToIndex(fl._date2) - firstdateindex) * p + p / 2, (float)value_to_y.TransValue(fl._value2));
  41. _id = fl._id;
  42. }
  43. public PointF Pivot
  44. {
  45. get
  46. {
  47. return _pivot;
  48. }
  49. set
  50. {
  51. _pivot = value;
  52. }
  53. }
  54. public PointF Destination
  55. {
  56. get
  57. {
  58. return _destination;
  59. }
  60. set
  61. {
  62. _destination = value;
  63. }
  64. }
  65. public LineDrawMode DrawMode
  66. {
  67. get
  68. {
  69. return _mode;
  70. }
  71. set
  72. {
  73. _mode = value;
  74. }
  75. }
  76. public int ID
  77. {
  78. get
  79. {
  80. return _id;
  81. }
  82. }
  83. public void SetHighPriceAndLowPrice(DataFarm farm, Trans pricetrans, float x1, float x2, int FirstDateIndex)
  84. {
  85. if (x1 > Env.Layout.ChartAreaWidth || x2 > Env.Layout.ChartAreaWidth)
  86. return;
  87. int ind1 = FirstDateIndex + (int)Math.Floor((double)Math.Min(x1, x2) / Env.Layout.DatePitch);
  88. int ind2 = FirstDateIndex + (int)Math.Floor((double)Math.Max(x1, x2) / Env.Layout.DatePitch);
  89. if (ind1 < 0) ind1 = 0;
  90. if (ind2 < 0) ind2 = 0;
  91. if (ind1 >= farm.TotalLength) ind1 = farm.FilledLength - 1;
  92. if (ind2 >= farm.TotalLength) ind2 = farm.FilledLength - 1;
  93. //最高値と最安値の株価
  94. //double range_max = new IndicatorTimeSeries(farm, Env.CurrentIndicators.GetPrimitive(PrimitiveIndicator.High), ind1, ind2).Max;
  95. //double range_min = new IndicatorTimeSeries(farm, Env.CurrentIndicators.GetPrimitive(PrimitiveIndicator.Low), ind1, ind2).Min;
  96. double range_max = Max(farm, ind1, ind2);
  97. double range_min = Min(farm, ind1, ind2);
  98. //最高値と最安値の画面上の位置
  99. double top = pricetrans.TransValue(range_max);
  100. double bot = pricetrans.TransValue(range_min);
  101. _pivot = new PointF(x1, (float)bot);
  102. _destination = new PointF(x2, (float)top);
  103. }
  104. private double Max(DataFarm farm, int ind1, int ind2)
  105. {
  106. var pos = ind1;
  107. var max = double.MinValue;
  108. var last = ind2 > farm.FilledLength ? farm.FilledLength - 1 : ind2;
  109. while (pos <= last)
  110. {
  111. var a = BitConverter.ToInt32(farm.RawDataImage, pos++ * 32 + 4 * 2) * farm.Brand.PriceScale;
  112. if (max < a) max = a;
  113. }
  114. return max;
  115. }
  116. private double Min(DataFarm farm, int ind1, int ind2)
  117. {
  118. var pos = ind1;
  119. var min = double.MaxValue;
  120. var last = ind2 > farm.FilledLength ? farm.FilledLength - 1 : ind2;
  121. while (pos <= last)
  122. {
  123. var a = BitConverter.ToInt32(farm.RawDataImage, pos++ * 32 + 4 * 3) * farm.Brand.PriceScale;
  124. if (min > a) min = a;
  125. }
  126. return min;
  127. }
  128. public Rectangle GetInclusion(Rectangle rect)
  129. {
  130. if (_pivot == _destination)
  131. return new Rectangle(new Point((int)_pivot.X, (int)_pivot.Y), new Size(0, 0));
  132. Point[] es = GetEdge(rect, new Point((int)_pivot.X, (int)_pivot.Y), new Point((int)_destination.X, (int)_destination.Y));
  133. return new Rectangle(Math.Min(es[0].X, es[1].X), Math.Min(es[0].Y, es[1].Y), Math.Abs(es[0].X - es[1].X), Math.Abs(es[0].Y - es[1].Y));
  134. }
  135. //十分に離した位置でないと線を確定させないようにする
  136. public bool PivotHasEnoughDistanceTo(PointF pt)
  137. {
  138. return Math.Abs(_pivot.X - pt.X) > 10 || Math.Abs(_pivot.Y - pt.Y) > 10;
  139. }
  140. public void Draw(Trans pricetrans, Rectangle rect, IntPtr hdc)
  141. {
  142. Win32.POINT pt = new Win32.POINT();
  143. Win32.SelectObject(hdc, _mode == LineDrawMode.Normal ? Env.Preference.FibonacciPen.Handle : Env.Preference.FibonacciBoldPen.Handle);
  144. PointF p1 = _pivot;
  145. PointF p2 = _destination;
  146. var m = Math.Min(p1.X, p2.X);
  147. var l = Math.Max(p1.X, p2.X);
  148. var y1 = 0F;
  149. var y2 = 0F;
  150. var y3 = 0F;
  151. var y4 = 0F;
  152. var y5 = 0F;
  153. var k1 = .0;
  154. var k2 = .0;
  155. var k3 = .0;
  156. var k4 = .0;
  157. var k5 = .0;
  158. if (Env.Preference.LogScale)
  159. {
  160. k1 = pricetrans.Inverse(Math.Min(p1.Y, p2.Y));
  161. k2 = pricetrans.Inverse(Math.Abs(p1.Y - p2.Y) * 0.382 + Math.Min(p1.Y, p2.Y));
  162. k3 = pricetrans.Inverse(Math.Abs(p1.Y - p2.Y) * 0.500 + Math.Min(p1.Y, p2.Y));
  163. k4 = pricetrans.Inverse(Math.Abs(p1.Y - p2.Y) * 0.618 + Math.Min(p1.Y, p2.Y));
  164. k5 = pricetrans.Inverse(Math.Max(p1.Y, p2.Y));
  165. y1 = Math.Min(p1.Y, p2.Y);
  166. y2 = (float)(Math.Abs(p1.Y - p2.Y) * 0.382 + Math.Min(p1.Y, p2.Y));
  167. y3 = (p1.Y + p2.Y) / 2;
  168. y4 = (float)(Math.Abs(p1.Y - p2.Y) * 0.618 + Math.Min(p1.Y, p2.Y));
  169. y5 = Math.Max(p1.Y, p2.Y);
  170. }
  171. else
  172. {
  173. k1 = pricetrans.Inverse(Math.Min(p1.Y, p2.Y));
  174. k5 = pricetrans.Inverse(Math.Max(p1.Y, p2.Y));
  175. k2 = Math.Abs(k1 - k5) * 0.618 + k5;
  176. k3 = (k5 + k1) / 2;
  177. k4 = Math.Abs(k1 - k5) * 0.382 + k5;
  178. y1 = Math.Min(p1.Y, p2.Y);
  179. y2 = (float)pricetrans.TransValue(k2);
  180. y3 = (float)pricetrans.TransValue(k3);
  181. y4 = (float)pricetrans.TransValue(k4);
  182. y5 = Math.Max(p1.Y, p2.Y);
  183. }
  184. DrawTheLine(hdc, m, l, y1, k1, out pt);
  185. DrawTheLine(hdc, m, l, y2, k2, out pt);
  186. DrawTheLine(hdc, m, l, y3, k3, out pt);
  187. DrawTheLine(hdc, m, l, y4, k4, out pt);
  188. DrawTheLine(hdc, m, l, y5, k5, out pt);
  189. }
  190. private void DrawTheLine(IntPtr hdc, float px1, float px2, float py, double kabuka, out Win32.POINT pt)
  191. {
  192. Win32.MoveToEx(hdc, (int)px1, (int)py, out pt);
  193. Win32.LineTo(hdc, (int)px2, (int)py);
  194. DrawText(hdc, (int)px2, (int)py, kabuka);
  195. }
  196. private void DrawText(IntPtr hdc, int x, int y, double d)
  197. {
  198. Win32.SetTextColor(hdc, Util.ToCOLORREF(Color.White));
  199. ChartUtil.DrawText(hdc, x + 3, y - 6, string.Format("{0:N}", d));
  200. }
  201. //p1,p2がrectに入っているとき、p1からp2に伸ばした線とp2からp1に伸ばした線がそれぞれrectと交差するところを返す
  202. private static Point[] GetEdge(Rectangle rect, Point p1, Point p2)
  203. {
  204. Point[] result = new Point[2];
  205. ArrayList ar = GetAllEdges(rect, p1, p2);
  206. if (ar.Count == 2) return (Point[])ar.ToArray(typeof(Point));
  207. result[0] = (Point)(p1.X > p2.X ? ar[1] : ar[2]);
  208. result[1] = (Point)(p1.X > p2.X ? ar[2] : ar[1]);
  209. return result;
  210. }
  211. //p1とp2を結ぶ線がrectを構成する4直線と交差する点を返す。水平・垂直のときは2個になる
  212. private static ArrayList GetAllEdges(Rectangle rect, Point p1, Point p2)
  213. {
  214. ArrayList ar = new ArrayList();
  215. if (p1.X == p2.X)
  216. {
  217. ar.Add(new Point(p1.X, p1.Y < p2.Y ? rect.Bottom : rect.Top));
  218. ar.Add(new Point(p1.X, p1.Y > p2.Y ? rect.Bottom : rect.Top));
  219. return ar;
  220. }
  221. else if (p1.Y == p2.Y)
  222. {
  223. ar.Add(new Point(p1.X < p2.X ? rect.Right : rect.Left, p1.Y));
  224. ar.Add(new Point(p1.X > p2.X ? rect.Right : rect.Left, p1.Y));
  225. return ar;
  226. }
  227. LinearTrans lt = LinearTrans.Solve(p1.X, p1.Y, p2.X, p2.Y);
  228. ar.Add(new Point(rect.Left, (int)lt.TransValue(rect.Left)));
  229. ar.Add(new Point(rect.Right, (int)lt.TransValue(rect.Right)));
  230. ar.Add(new Point((int)lt.Inverse(rect.Top), rect.Top));
  231. ar.Add(new Point((int)lt.Inverse(rect.Bottom), rect.Bottom));
  232. //この4点をX座標の順に並べ、2番目と3番目が答え
  233. ar.Sort(new PointComparer());
  234. return ar;
  235. }
  236. private class PointComparer : IComparer
  237. {
  238. public int Compare(object x, object y)
  239. {
  240. return ((Point)x).X - ((Point)y).X;
  241. }
  242. }
  243. public SolidFibonacci ToSolid(DataFarm farm, int firstdateindex, Trans value_to_y)
  244. {
  245. SolidFibonacci fl = new SolidFibonacci();
  246. int ind1 = firstdateindex + (int)Math.Floor((double)_pivot.X / Env.Layout.DatePitch);
  247. int ind2 = firstdateindex + (int)Math.Floor((double)_destination.X / Env.Layout.DatePitch);
  248. LinearTrans tr = LinearTrans.Solve(ind1, _pivot.Y, ind2, _destination.Y);
  249. double y1 = _pivot.Y;
  250. if (ind1 >= farm.FilledLength)
  251. {
  252. ind1 = farm.FilledLength - 1;
  253. y1 = tr.TransValue(ind1);
  254. }
  255. else if (ind1 < 0)
  256. {
  257. ind1 = 0;
  258. y1 = tr.TransValue(ind1);
  259. }
  260. fl._date1 = farm.GetByIndex(ind1).Date;
  261. fl._value1 = value_to_y.Inverse(y1);
  262. double y2 = _destination.Y;
  263. if (ind2 >= farm.FilledLength)
  264. {
  265. ind2 = farm.FilledLength - 1;
  266. y2 = tr.TransValue(ind2);
  267. }
  268. else if (ind2 < 0)
  269. {
  270. ind2 = 0;
  271. y2 = tr.TransValue(0);
  272. }
  273. fl._date2 = farm.GetByIndex(ind2).Date;
  274. //fl._value2 = value_to_y.Inverse(y2);
  275. fl._value2 = value_to_y.Inverse(_destination.Y);
  276. if (_id == -1)
  277. {
  278. fl._id = SolidFibonacci.NextID++;
  279. _id = fl._id;
  280. }
  281. else
  282. {
  283. fl._id = _id;
  284. }
  285. return fl;
  286. }
  287. //p1,p2を結ぶ直線とpの距離を返す
  288. public double GetDistance(Point p)
  289. {
  290. double[] par = GetNormalizedParam();
  291. return Math.Abs(par[0] * p.X + par[1] * p.Y + par[2]);
  292. }
  293. //両端を通る直線を ax+by+c=0, a^2+b^2=1 となる形式でa,b,cの配列で返す
  294. private double[] GetNormalizedParam()
  295. {
  296. double a, b, c;
  297. if (_pivot.X == _destination.X)
  298. {
  299. a = 1;
  300. b = 0;
  301. }
  302. else
  303. {
  304. double d = -(_pivot.Y - _destination.Y) / (double)(_pivot.X - _destination.X);
  305. b = Math.Sqrt(1 / (1 + d * d));
  306. a = b * d;
  307. }
  308. c = -(a * _pivot.X + b * _pivot.Y);
  309. return new double[] { a, b, c };
  310. }
  311. }
  312. internal class SolidFibonacci
  313. {
  314. public int _id;
  315. public int _code;
  316. public ChartFormat _targetFormat;
  317. public bool _logScale;
  318. public int _date1;
  319. public double _value1;
  320. public int _date2;
  321. public double _value2;
  322. private static int _nextID;
  323. public static int NextID
  324. {
  325. get
  326. {
  327. return _nextID;
  328. }
  329. set
  330. {
  331. _nextID = value;
  332. }
  333. }
  334. }
  335. internal class FibonacciCollection
  336. {
  337. private ArrayList _data;
  338. public FibonacciCollection()
  339. {
  340. _data = new ArrayList();
  341. }
  342. public int Count
  343. {
  344. get
  345. {
  346. return _data.Count;
  347. }
  348. }
  349. public void Add(AbstractBrand br, ChartFormat format, bool logScale, SolidFibonacci fl)
  350. {
  351. fl._code = br.Code;
  352. fl._targetFormat = format;
  353. fl._logScale = logScale;
  354. _data.Add(fl);
  355. }
  356. public SolidFibonacci[] Find(AbstractBrand br, ChartFormat format, bool logScale)
  357. {
  358. ArrayList t = new ArrayList();
  359. foreach (SolidFibonacci fl in _data)
  360. {
  361. if (fl._code == br.Code && fl._targetFormat == format && fl._logScale == logScale)
  362. t.Add(fl);
  363. }
  364. return (SolidFibonacci[])t.ToArray(typeof(SolidFibonacci));
  365. }
  366. public void ClearAll()
  367. {
  368. _data.Clear();
  369. }
  370. public void Clear(AbstractBrand br, ChartFormat format, bool logScale)
  371. {
  372. ArrayList temp = new ArrayList();
  373. IEnumerator ie = _data.GetEnumerator();
  374. while (ie.MoveNext())
  375. {
  376. SolidFibonacci fl = (SolidFibonacci)ie.Current;
  377. if (!(fl._code == br.Code && fl._targetFormat == format && fl._logScale == logScale))
  378. temp.Add(fl);
  379. }
  380. _data = temp;
  381. }
  382. public void Remove(int id)
  383. {
  384. for (int i = 0; i < _data.Count; i++)
  385. {
  386. SolidFibonacci l = (SolidFibonacci)_data[i];
  387. if (l._id == id)
  388. {
  389. _data.RemoveAt(i);
  390. break;
  391. }
  392. }
  393. }
  394. public void Load(StorageNode parent)
  395. {
  396. string t = parent["fibonacci-lines"];
  397. if (t == null) return;
  398. foreach (string ee in t.Split(','))
  399. {
  400. if (ee == null) continue;
  401. if (ee.Trim() == string.Empty) continue;
  402. SolidFibonacci fl = new SolidFibonacci();
  403. string[] e = ee.Split(':');
  404. fl._code = Int32.Parse(e[0]);
  405. switch (e[1])
  406. {
  407. case "D":
  408. fl._targetFormat = ChartFormat.Daily;
  409. fl._logScale = false;
  410. break;
  411. case "W":
  412. fl._targetFormat = ChartFormat.Weekly;
  413. fl._logScale = false;
  414. break;
  415. case "M":
  416. fl._targetFormat = ChartFormat.Monthly;
  417. fl._logScale = false;
  418. break;
  419. case "Y":
  420. fl._targetFormat = ChartFormat.Yearly;
  421. fl._logScale = false;
  422. break;
  423. case "DL":
  424. fl._targetFormat = ChartFormat.Daily;
  425. fl._logScale = true;
  426. break;
  427. case "WL":
  428. fl._targetFormat = ChartFormat.Weekly;
  429. fl._logScale = true;
  430. break;
  431. case "ML":
  432. fl._targetFormat = ChartFormat.Monthly;
  433. fl._logScale = true;
  434. break;
  435. case "YL":
  436. fl._targetFormat = ChartFormat.Yearly;
  437. fl._logScale = true;
  438. break;
  439. }
  440. fl._date1 = Int32.Parse(e[2]);
  441. fl._value1 = Double.Parse(e[3]);
  442. fl._date2 = Int32.Parse(e[4]);
  443. fl._value2 = Double.Parse(e[5]);
  444. fl._id = SolidFibonacci.NextID++;
  445. _data.Add(fl);
  446. }
  447. }
  448. public void SaveTo(StorageNode parent)
  449. {
  450. StringBuilder bld = new StringBuilder();
  451. foreach (SolidFibonacci sl in _data)
  452. {
  453. if (bld.Length > 0) bld.Append(",");
  454. String format;
  455. switch (sl._targetFormat)
  456. {
  457. case ChartFormat.Daily:
  458. default:
  459. format = "D";
  460. break;
  461. case ChartFormat.Weekly:
  462. format = "W";
  463. break;
  464. case ChartFormat.Monthly:
  465. format = "M";
  466. break;
  467. case ChartFormat.Yearly:
  468. format = "Y";
  469. break;
  470. }
  471. if (sl._logScale)
  472. {
  473. format += "L";
  474. }
  475. bld.Append(String.Format("{0}:{1}:{2}:{3:F2}:{4}:{5:F2}", sl._code, format, sl._date1, sl._value1, sl._date2, sl._value2));
  476. }
  477. parent["fibonacci-lines"] = bld.ToString();
  478. }
  479. }
  480. }
ダウンロード 印刷用表示

このコピペの URL

JavaScript での埋め込み

iframe での埋め込み

元のテキスト