Pastebin: OmegaChart 日付固定表示 ChartDrawing.cs 追加修正ポイントには//☆FixedDisplayYMD

Format
Plain text
Post date
2018-04-25 18:15
Publication Period
Unlimited
  1. /*
  2. * Copyright (c) Daisuke OKAJIMA All rights reserved.
  3. *
  4. * $Id$
  5. */
  6. using System;
  7. using System.Collections;
  8. using System.Drawing;
  9. using System.Text;
  10. using System.Diagnostics;
  11. using System.Windows.Forms;
  12. using Travis.Util;
  13. using Zanetti;
  14. using Zanetti.Data;
  15. using Zanetti.Indicators;
  16. using Zanetti.Arithmetic;
  17. using Zanetti.Arithmetic.Series;
  18. #if DOJIMA
  19. using DojimaChart = Zanetti.Dojima.DojimaChart;
  20. #endif
  21. namespace Zanetti.UI
  22. {
  23. internal class RedrawValue {
  24. public int _lastDrawn;
  25. public int _nextToBeDrawn;
  26. }
  27. /// <summary>
  28. /// ChartDrawing の概要の説明です。
  29. /// </summary>
  30. internal class ChartDrawing
  31. {
  32. private ChartCanvas _owner;
  33. private Preference _pref; //描画のいろいろな過程で参照するのでメンバとして保持
  34. private LayoutInfo _layout;
  35. private int _maximumValueWindowItemCount;
  36. private AbstractBrand _brand;
  37. private int _firstDateIndex;
  38. private int _firstDate;
  39. private int _lastDate;
  40. private double[] _relativisedMultipliers;
  41. private RedrawValue _dateLine;
  42. private RedrawValue _priceLine;
  43. private bool _scaleIsInvalidated;
  44. private Trans _priceTrans; //price->y座標変換
  45. private double _highPrice; //表示している値段の範囲
  46. private double _lowPrice;
  47. private int _priceScaleStep; //値段を示す横線の幅 これの整数倍ごとに線を引く
  48. private Trans _volumeTrans; //volume->y座標変換
  49. private double _volumeScaleStep;
  50. private Win32.POINT _dummyPoint;
  51. //キャプションが長すぎて見えないやつを拾うためのデータ
  52. private ArrayList _tipEntries;
  53. private bool _tipBuilding;
  54. //価格帯出来高
  55. private AccumulativeVolume _accumulativeVolume;
  56. //FreeLine
  57. private ArrayList _freeLines;
  58. public ChartDrawing(ChartCanvas owner) {
  59. _owner = owner;
  60. _dateLine = new RedrawValue();
  61. _dateLine._lastDrawn = -1;
  62. _dateLine._nextToBeDrawn = -1;
  63. _priceLine = new RedrawValue();
  64. _freeLines = new ArrayList();
  65. _scaleIsInvalidated = true;
  66. _accumulativeVolume = new AccumulativeVolume();
  67. }
  68. public void SetBrand(AbstractBrand br) {
  69. _brand = br;
  70. #if DOJIMA
  71. _halfDailyDataFarm = null;
  72. #endif
  73. //銘柄がかわるごとにクリア
  74. ClearScale();
  75. _relativisedMultipliers = new double[Env.CurrentIndicators.IndicatorCount];
  76. }
  77. //☆FixedDisplayYMD
  78. public void SetStartDate(int date)
  79. {
  80. if (Env.Frame.ChartCanvas.GetBrand() == null) return;
  81. var farm = Env.Frame.ChartCanvas.GetBrand().ReserveFarm();
  82. if (farm.IsEmpty) return;
  83. var limit = farm.TotalLength;
  84. var w = Env.Layout.DisplayColumnCount;
  85. var pos = limit - 1;
  86. while (pos >= 0)
  87. {
  88. if (farm.GetByIndex(pos).Date <= 0)
  89. {
  90. pos--;
  91. continue;
  92. }
  93. if (farm.GetByIndex(pos).Date <= date)
  94. break;
  95. else
  96. pos--;
  97. }
  98. var n = pos - w + 1;
  99. if (n < 0) n = 0;
  100. Env.Frame.ChartCanvas.SetDateIndex(n, pos);
  101. }
  102. //☆FixedDisplayYMD ここまで
  103. public void ClearScale() {
  104. if(!Env.Preference.ScaleLock) {
  105. _priceTrans = null;
  106. _freeLines.Clear();
  107. }
  108. _scaleIsInvalidated = true;
  109. _tipEntries = null;
  110. }
  111. public ArrayList TipEntries {
  112. get {
  113. return _tipEntries;
  114. }
  115. }
  116. public int FirstDateIndex {
  117. get {
  118. return _firstDateIndex;
  119. }
  120. set {
  121. _firstDateIndex = value;
  122. }
  123. }
  124. public RedrawValue DateLine {
  125. get {
  126. return _dateLine;
  127. }
  128. }
  129. public RedrawValue PriceLine {
  130. get {
  131. return _priceLine;
  132. }
  133. set {
  134. _priceLine = value;
  135. }
  136. }
  137. //次に描画するものを決める
  138. public void UpdateDateLineIndex(int index) {
  139. if(_dateLine._nextToBeDrawn != index) {
  140. if(index != -1) {
  141. DataFarm farm = _owner.GetBrand().ReserveFarm();
  142. if(farm.IsEmpty || _scaleIsInvalidated)
  143. _accumulativeVolume.Available = false;
  144. else {
  145. int price_pitch = Math.Max(1, _priceScaleStep / AccumulativeVolume.DATA_PER_SCALELINE);
  146. _accumulativeVolume.Fill(farm, index, _accumulativeVolume.StartPrice, price_pitch, _accumulativeVolume.EndPrice);
  147. }
  148. }
  149. }
  150. _dateLine._nextToBeDrawn = index;
  151. }
  152. //現在の設定で表示できる日付範囲
  153. public int FirstDate {
  154. get {
  155. DataFarm farm = _owner.GetBrand().ReserveFarm();
  156. return farm.GetByIndex(_firstDateIndex).Date;
  157. }
  158. }
  159. public int LastDate {
  160. get {
  161. DataFarm farm = _owner.GetBrand().ReserveFarm();
  162. return farm.GetByIndex(Math.Min(farm.FilledLength, _firstDateIndex+Env.Layout.DisplayColumnCount)-1).Date;
  163. }
  164. }
  165. public int MaximumValueWindowItemCount {
  166. get {
  167. return _maximumValueWindowItemCount;
  168. }
  169. }
  170. public int FreeLineCount {
  171. get {
  172. return _freeLines.Count;
  173. }
  174. }
  175. public IEnumerable FreeLines {
  176. get {
  177. return _freeLines;
  178. }
  179. }
  180. public void PaintMain(Graphics g, Rectangle clip) {
  181. if(_brand==null) return;
  182. if(!Env.CurrentIndicators.Valid) return;
  183. _pref = Env.Preference;
  184. _layout = Env.Layout;
  185. IntPtr hdc = g.GetHdc();
  186. Win32.SetBkColor(hdc, Util.ToCOLORREF(_pref.BackBrush.Color));
  187. Win32.SetTextColor(hdc, Util.ToCOLORREF(_pref.TextColor));
  188. Win32.SelectObject(hdc, _pref.DefaultHFont);
  189. if(_tipEntries==null) {
  190. _tipEntries = new ArrayList();
  191. _tipBuilding = true;
  192. }
  193. else
  194. _tipBuilding = false;
  195. try {
  196. if(clip.IntersectsWith(_layout.ChartBodyRect))
  197. DrawChartBody(hdc, clip);
  198. if(_pref.ShowPrice) {
  199. if(clip.IntersectsWith(_layout.CurrentValueRect))
  200. DrawCurrentValue(hdc);
  201. if(clip.IntersectsWith(_layout.ExplanationRect))
  202. DrawExplanation(hdc);
  203. }
  204. if(_pref.ShowAccumulativeVolume && clip.IntersectsWith(_layout.AccumulativeVolumeRect))
  205. DrawAccumulativeVolume(hdc);
  206. foreach(FreeLine fl in _freeLines) {
  207. if(fl.GetInclusion(_owner.ClientRectangle).IntersectsWith(clip))
  208. fl.Draw(_layout.ChartBodyRect, hdc);
  209. }
  210. if(clip.IntersectsWith(_layout.BrandInformationRect))
  211. DrawBrandInformation(hdc);
  212. }
  213. catch(Exception ex) {
  214. Util.SilentReportCriticalError(ex);
  215. Util.Warning(Env.Frame, ex.Message + "\n拡張キットを読み込みなおすまでチャートの描画を停止します。");
  216. Env.CurrentIndicators.Valid = false;
  217. }
  218. finally {
  219. g.ReleaseHdc(hdc);
  220. }
  221. }
  222. //各パーツの描画 けっこう大変だよ
  223. private void DrawChartBody(IntPtr hdc, Rectangle clip) {
  224. Indicator[] indicators = Env.CurrentIndicators.GetIndicatorsForChart();
  225. DataFarm farm = _brand.ReserveFarm();
  226. if(farm.IsEmpty) {
  227. RecalcValueWindowFormat();
  228. DrawBrandInformation(hdc);
  229. return;
  230. }
  231. if(_scaleIsInvalidated) {
  232. RecalcFormat();
  233. RecalcValueWindowFormat();
  234. _scaleIsInvalidated = false;
  235. }
  236. DrawScaleLines(hdc);
  237. DrawMouseTrackingLine(hdc); //これはロウソクの前に描いたほうがヒゲなどが見えやすい
  238. Win32.SelectObject(hdc, _pref.DefaultHFont);
  239. Win32.SetTextColor(hdc, Util.ToCOLORREF(_pref.FushiColor));
  240. int index = _firstDateIndex;
  241. int end = Math.Min(farm.TotalLength, _firstDateIndex+_layout.DisplayColumnCount);
  242. _firstDate = this.FirstDate; //farm.GetByIndex(index).Date;
  243. _lastDate = this.LastDate; //farm.GetByIndex(Math.Min(farm.FilledLength, _firstDateIndex+_layout.DisplayColumnCount)-1).Date;
  244. if(_firstDate==0) return; //ケンミレの一部のデータが壊れている件の暫定対応
  245. //特殊処理:一目の雲
  246. if(IchimokuKumoIsAvailable())
  247. DrawIchimokuKumo(hdc, farm, index, end, clip);
  248. int prev_date_part = 0;
  249. string caption = "";
  250. while(index < end) {
  251. int x = (index - _firstDateIndex) * _layout.DatePitch;
  252. TradeData td = farm.GetByIndex(index);
  253. //if(!td.IsFuture) {
  254. int mid = x + _layout.CandleMiddleOffset;
  255. if(clip.Left<=x+_layout.DatePitch && x-_layout.DatePitch<clip.Right) {
  256. #if DOJIMA
  257. ChartFormat fmt = Env.CurrentIndicators.Format;
  258. if(DojimaChart.IsMusousenAvailable(fmt))
  259. DojimaChart.DrawMusousen(hdc, _pref.CandlePen, mid, -_priceScaleStep*3, td, clip, _priceTrans);
  260. if(!td.IsFuture) {
  261. if(fmt==ChartFormat.HalfDaily)
  262. DrawHalfDailyChart(hdc, farm as DailyDataFarm, x, td, clip);
  263. else
  264. DrawBasicChart(hdc, mid, td, clip);
  265. #else
  266. if(!td.IsFuture) {
  267. DrawBasicChart(hdc, mid, td, clip);
  268. #endif
  269. foreach(Indicator ind in indicators)
  270. DrawIndicator(hdc, mid, td, null, ind, clip);
  271. for(int i=0; i<OscillatorPreference.LENGTH; i++) {
  272. OscillatorPreference op = _pref.OscillatorPreferences[i];
  273. if(op.Config!=HeightConfig.None) {
  274. foreach(Indicator ind in op.OscillatorGroup)
  275. DrawIndicator(hdc, mid, td, op, ind, clip);
  276. }
  277. }
  278. }
  279. }
  280. //月/4半期の切り替わり
  281. if(!td.IsFuture) {
  282. int date_part = td.Date % 100;
  283. if(CheckMonthDiv(prev_date_part, date_part, td, ref caption)) {
  284. Win32.SelectObject(hdc, _pref.MonthDivPen.Handle);
  285. Win32.SetTextColor(hdc, Util.ToCOLORREF(_pref.TextColor));
  286. Win32.MoveToEx(hdc, x, _layout.HeaderHeight, out _dummyPoint);
  287. Win32.LineTo(hdc, x, _layout.ChartAreaBottom);
  288. if(ChartUtil.HasIntersectionY(clip, _layout.ChartAreaBottom, _layout.ChartAreaBottom+_layout.FooterHeight))
  289. ChartUtil.DrawText(hdc, x+2, _layout.ChartAreaBottom, caption);
  290. }
  291. prev_date_part = date_part;
  292. DrawFushiOrSplit(hdc, mid, td, clip); //節は最後の描画でないと隠れてしまうことがある
  293. }
  294. //}
  295. index++;
  296. }
  297. }
  298. private bool CheckMonthDiv(int prev_date_part, int date_part, TradeData td, ref string caption) {
  299. if(Util.IsDailyBased(Env.CurrentIndicators.Format)) {
  300. if(prev_date_part > date_part) { //月の値が減少したら
  301. caption = String.Format("{0}年{1}月", td.Date / 10000, (td.Date % 10000) / 100);
  302. return true;
  303. }
  304. }
  305. else if(Env.CurrentIndicators.Format==ChartFormat.Weekly) {
  306. int t = td.Date;
  307. DateTime d = new DateTime(t / 10000, (t % 10000) / 100, (t % 100));
  308. d = d.AddDays(5); //金曜日の位置で決めるのが自然
  309. int m = d.Month;
  310. if(d.Day<=7 && (m==1 || m==4 || m==7 || m==10)) {
  311. caption = String.Format("{0}年{1}Q", d.Year, (m-1)/3+1);
  312. return true;
  313. }
  314. }
  315. else if (Env.CurrentIndicators.Format == ChartFormat.Yearly)
  316. {
  317. int t = td.Date;
  318. int y = t / 10000;
  319. if (y % 5 == 0)
  320. {
  321. caption = String.Format("{0}年", y);
  322. return true;
  323. }
  324. }
  325. else
  326. { // Monthly
  327. int t = td.Date;
  328. int y = t/10000;
  329. int m = (t % 10000) / 100;
  330. if(m==1) {
  331. caption = String.Format("{0}年", y);
  332. return true;
  333. }
  334. }
  335. return false;
  336. }
  337. private void DrawBasicChart(IntPtr hdc, int mid, TradeData td, Rectangle clip) {
  338. DrawCandle(hdc, mid, td, clip);
  339. //volume
  340. if(_pref.ShowVolume!=HeightConfig.None && _brand.IsVolumeAvailable)
  341. DrawVolume(hdc, mid, td, clip);
  342. }
  343. private void DrawCurrentValue(IntPtr hdc) {
  344. DataFarm farm = _brand.ReserveFarm();
  345. int ni = _dateLine._nextToBeDrawn;
  346. if(farm.IsEmpty || farm.FilledLength<=ni) ni = -1;
  347. TradeData td = ni==-1? null : farm.GetByIndex(ni);
  348. DrawValueWindow(hdc, td);
  349. }
  350. private void DrawScaleLines(IntPtr hdc) {
  351. int right = _layout.ChartBodyRect.Right;
  352. //price
  353. Win32.SelectObject(hdc, _pref.PriceScalePen.Handle);
  354. double y;
  355. double top = _highPrice;
  356. int price = (int)(Math.Floor(top / _priceScaleStep) * _priceScaleStep);
  357. while(price >= _lowPrice) {
  358. y = _priceTrans.TransValue((double)price);
  359. Win32.MoveToEx(hdc, 0, (int)y, out _dummyPoint);
  360. Win32.LineTo(hdc, right, (int)y);
  361. ChartUtil.DrawText(hdc, right, (int)y-5, price.ToString());
  362. price -= _priceScaleStep;
  363. }
  364. //volume
  365. if(_pref.ShowVolume!=HeightConfig.None && _brand.IsVolumeAvailable) {
  366. Win32.SelectObject(hdc, _pref.VolumeScalePen.Handle);
  367. top = _volumeTrans.Inverse(GetPricePaneBottom());
  368. double volume = (Math.Floor(top / _volumeScaleStep) * _volumeScaleStep);
  369. y = _volumeTrans.TransValue(volume);
  370. while(volume >= 0) {
  371. Win32.MoveToEx(hdc, 0, (int)y, out _dummyPoint);
  372. Win32.LineTo(hdc, right, (int)y);
  373. ChartUtil.DrawText(hdc, right, (int)y-6, volume.ToString());
  374. volume -= _volumeScaleStep;
  375. y = _volumeTrans.TransValue((double)volume);
  376. }
  377. }
  378. //oscillator
  379. Win32.SelectObject(hdc, _pref.OscillatorScalePen.Handle);
  380. for(int i=0; i<OscillatorPreference.LENGTH; i++) {
  381. OscillatorPreference op = _pref.OscillatorPreferences[i];
  382. if(op.Config!=HeightConfig.None) {
  383. foreach(double osc in op.ScaleValues) {
  384. y = op.Trans.TransValue(osc);
  385. Win32.MoveToEx(hdc, 0, (int)y, out _dummyPoint);
  386. Win32.LineTo(hdc, right, (int)y);
  387. ChartUtil.DrawText(hdc, right, (int)y-6, FormatOscillatorValue(op, osc));
  388. }
  389. }
  390. }
  391. }
  392. private string FormatOscillatorValue(OscillatorPreference op, double v) {
  393. switch(op.OscillatorGroup.Type) {
  394. case ValueRange.Percent0_1:
  395. case ValueRange.Percent1_1:
  396. return (v*100).ToString();
  397. case ValueRange.Origin0:
  398. return (v*100).ToString("F2");
  399. default:
  400. return v.ToString();
  401. }
  402. }
  403. private void DrawCandle(IntPtr hdc, int x, TradeData td, Rectangle clip) {
  404. DrawCandle(hdc, x, td.Open, td.High, td.Low, td.Close, clip);
  405. }
  406. private void DrawCandle(IntPtr hdc, int x, double open, double high, double low, double close, Rectangle clip) {
  407. if(!ChartUtil.HasIntersectionY(clip, (int)_priceTrans.TransValue(high), (int)_priceTrans.TransValue(low))) return;
  408. Win32.SelectObject(hdc, _pref.CandlePen.Handle);
  409. int halfcw = _pref.HalfCandleWidth;
  410. //正規の枠は[x-HALF_CW, x+HALF_CW+1)
  411. if(open<=close) { //陽線
  412. int top = (int)_priceTrans.TransValue(close);
  413. int bottom = (int)_priceTrans.TransValue(open);
  414. if(_pref.InverseChart) Util.Swap(ref top, ref bottom);
  415. Win32.SelectObject(hdc, _pref.BackBrush.Handle);
  416. if(_pref.UseCandleEffect) {
  417. Win32.Rectangle(hdc, x-halfcw, top, x+halfcw+1, bottom+1);
  418. //上と左
  419. Win32.SelectObject(hdc, _pref.CandlePen.DarkDarkPen);
  420. Win32.MoveToEx(hdc, x+halfcw, top-1, out _dummyPoint);
  421. Win32.LineTo(hdc, x-halfcw-1, top-1);
  422. Win32.LineTo(hdc, x-halfcw-1, bottom+1);
  423. //下と右
  424. Win32.SelectObject(hdc, _pref.CandlePen.DarkPen);
  425. Win32.MoveToEx(hdc, x+halfcw-1, top+1, out _dummyPoint);
  426. Win32.LineTo(hdc, x+halfcw-1, bottom-1);
  427. Win32.LineTo(hdc, x-halfcw , bottom-1);
  428. }
  429. else
  430. Win32.Rectangle(hdc, x-halfcw, top, x+halfcw+1, bottom+1);
  431. }
  432. else { //陰線
  433. int top = (int)_priceTrans.TransValue(open);
  434. int bottom = (int)_priceTrans.TransValue(close);
  435. if(_pref.InverseChart) Util.Swap(ref top, ref bottom);
  436. Win32.SelectObject(hdc, _pref.InsenBrush.Handle);
  437. if(_pref.UseCandleEffect) {
  438. Win32.Rectangle(hdc, x-halfcw+1, top+1, x+halfcw, bottom);
  439. //上と左
  440. Win32.SelectObject(hdc, _pref.CandlePen.LightPen);
  441. Win32.MoveToEx(hdc, x+halfcw, top, out _dummyPoint);
  442. Win32.LineTo(hdc, x-halfcw, top);
  443. Win32.LineTo(hdc, x-halfcw, bottom+1);
  444. //下と右
  445. Win32.SelectObject(hdc, _pref.CandlePen.DarkPen);
  446. Win32.MoveToEx(hdc, x+halfcw, top+1, out _dummyPoint);
  447. Win32.LineTo(hdc, x+halfcw, bottom);
  448. Win32.LineTo(hdc, x-halfcw, bottom);
  449. }
  450. else
  451. Win32.Rectangle(hdc, x-halfcw, top, x+halfcw+1, bottom+1);
  452. }
  453. Win32.SelectObject(hdc, _pref.CandlePen.Handle);
  454. //ひげ
  455. if(high > Math.Max(open, close)) {
  456. Win32.MoveToEx(hdc, x, (int)_priceTrans.TransValue(high), out _dummyPoint);
  457. Win32.LineTo(hdc, x, (int)_priceTrans.TransValue(Math.Max(open, close)));
  458. }
  459. if(low < Math.Min(open, close)) {
  460. Win32.MoveToEx(hdc, x, (int)_priceTrans.TransValue(Math.Min(open, close)), out _dummyPoint);
  461. Win32.LineTo(hdc, x, (int)_priceTrans.TransValue(low));
  462. }
  463. }
  464. private void DrawFushiOrSplit(IntPtr hdc, int x, TradeData td, Rectangle clip) {
  465. bool low = false;
  466. //節
  467. int upmargin = _pref.InverseChart ? 5 : -5-_layout.DefaultTextHeight;
  468. int downmargin = _pref.InverseChart ? -5-_layout.DefaultTextHeight : 5;
  469. if(td.Fushi==Fushi.High)
  470. {
  471. int y = (int)_priceTrans.TransValue(td.High)+upmargin;
  472. if(ChartUtil.HasIntersectionY(clip, y, y + _layout.DefaultTextHeight)) {
  473. Win32.SetTextColor(hdc, Util.ToCOLORREF(_pref.FushiColor));
  474. var bp = _brand.IsBuiltIn || td.High <= 5000 ? _brand.PriceFormatString : "F0";
  475. string t = td.High.ToString(bp);
  476. ChartUtil.DrawText(hdc, x-t.Length*_layout.DefaultTextWidth/2, y, t);
  477. }
  478. }
  479. else if(td.Fushi==Fushi.Low) {
  480. int y = (int)_priceTrans.TransValue(td.Low)+downmargin;
  481. if(ChartUtil.HasIntersectionY(clip, y, y + _layout.DefaultTextHeight)) {
  482. Win32.SetTextColor(hdc, Util.ToCOLORREF(_pref.FushiColor));
  483. var bp = _brand.IsBuiltIn || td.High <= 5000 ? _brand.PriceFormatString : "F0";
  484. string t = td.Low.ToString(bp);
  485. ChartUtil.DrawText(hdc, x-t.Length*_layout.DefaultTextWidth/2, y, t);
  486. }
  487. low = true;
  488. }
  489. //分割
  490. if(_brand.SplitInfo!=null) {
  491. foreach(SplitInfo si in _brand.SplitInfo) {
  492. if(td.CoversDate(si.Date)) {
  493. int y = (int)_priceTrans.TransValue(td.Low)+5+(low? _layout.DefaultTextHeight : 0);
  494. if(ChartUtil.HasIntersectionY(clip, y, y + _layout.DefaultTextHeight)) {
  495. Win32.SetTextColor(hdc, Util.ToCOLORREF(_pref.FushiColor));
  496. string t = "▲分"+si.Ratio.ToString();
  497. ChartUtil.DrawText(hdc, x-_layout.DefaultTextWidth/2-2, y, t);
  498. }
  499. break;
  500. }
  501. }
  502. }
  503. }
  504. private void DrawVolume(IntPtr hdc, int x, TradeData td, Rectangle clip) {
  505. DrawVolume(hdc, x, td.Volume, clip);
  506. }
  507. private void DrawVolume(IntPtr hdc, int x, double volume, Rectangle clip) {
  508. Win32.RECT r = new Win32.RECT();
  509. r.left = x-_pref.HalfCandleWidth;
  510. r.top = (int)_volumeTrans.TransValue(volume);
  511. r.right = x+_pref.HalfCandleWidth;
  512. r.bottom = (int)_volumeTrans.TransValue(0)+1;
  513. if(ChartUtil.HasIntersectionY(clip, r.top, r.bottom)) {
  514. Win32.FillRect(hdc, ref r, _pref.VolumeBrush.Handle);
  515. Win32.POINT pt;
  516. Win32.SelectObject(hdc, _pref.VolumeBrush.LightPen);
  517. Win32.MoveToEx(hdc, r.left, r.top, out pt);
  518. Win32.LineTo(hdc, r.right+1, r.top);
  519. Win32.MoveToEx(hdc, r.left, r.top, out pt);
  520. Win32.LineTo(hdc, r.left, r.bottom);
  521. Win32.SelectObject(hdc, _pref.VolumeBrush.DarkPen);
  522. Win32.MoveToEx(hdc, r.right, r.top+1, out pt);
  523. Win32.LineTo(hdc, r.right, r.bottom);
  524. }
  525. }
  526. //indicatorの描画:opはオシレータ以外ではnull
  527. private void DrawIndicator(IntPtr hdc, int x, TradeData td, OscillatorPreference op, Indicator ind, Rectangle clip) {
  528. if(ind==null) return; //!!_currentOscillatorGroupがおかしいのか、nullが入っているケースがあった。原因究明したい
  529. if(ind.Target==IndicatorTarget.Volume && _pref.ShowVolume==HeightConfig.None) return;
  530. TradeData pr = td.Prev;
  531. if(pr==null) return;
  532. if(ind.TargetBrand!=null && !ind.TargetBrand.Applicable(_brand.Code)) return; //適用不可
  533. double v1 = pr.GetValue(ind);
  534. double v2 = td.GetValue(ind);
  535. if(!Double.IsNaN(v1) && !Double.IsNaN(v2)) {
  536. if(ind.Target==IndicatorTarget.Volume && (v1==0 || v2==0)) return; //存在しない信用残を回避
  537. //相対化表示パラメータがついていれば調整
  538. if(ind.RelativiseParam!=null) {
  539. double m = _relativisedMultipliers[ind.LaneID];
  540. if(m==0) {
  541. m = ind.RelativiseParam.CalcMultiplier(ind, td.Farm);
  542. _relativisedMultipliers[ind.LaneID] = m;
  543. }
  544. v1 *= m;
  545. v2 *= m;
  546. //基準日だったらさらに■を書く
  547. if(td.Date==ind.RelativiseParam.Date) {
  548. Win32.RECT rect = new Win32.RECT();
  549. rect.left = x-2; rect.right = x+2;
  550. rect.top = ConvertToY(ind.Target, null, v2)-2; rect.bottom = rect.top+4;
  551. Win32.FillRect(hdc, ref rect, new ZBrush(ind.Appearance.Pen.Color).Handle);
  552. }
  553. }
  554. int y1 = ConvertToY(ind.Target, op, v1);
  555. int y2 = ConvertToY(ind.Target, op, v2);
  556. if(ChartUtil.HasIntersectionY(clip, y1, y2)) {
  557. Win32.SelectObject(hdc, ind.Appearance.Pen.Handle);
  558. Win32.MoveToEx(hdc, x-_layout.DatePitch, y1, out _dummyPoint);
  559. Win32.LineTo(hdc, x, y2);
  560. }
  561. }
  562. }
  563. private void DrawMouseTrackingLine(IntPtr hdc) {
  564. if(Env.Preference.MouseTrackingLineMode!=MouseTrackingLineMode.None){
  565. if(_dateLine._nextToBeDrawn!=-1) {
  566. int x = (_dateLine._nextToBeDrawn-_firstDateIndex)*_layout.DatePitch + _layout.DatePitch/2;
  567. Win32.SelectObject(hdc, _pref.MouseTrackingLinePen.Handle);
  568. Win32.MoveToEx(hdc, x, _layout.HeaderHeight, out _dummyPoint);
  569. Win32.LineTo(hdc, x, _owner.Height-_layout.FooterHeight);
  570. _dateLine._lastDrawn = _dateLine._nextToBeDrawn;
  571. }
  572. }
  573. if(Env.Preference.MouseTrackingLineMode==MouseTrackingLineMode.Full) { //!!Flagにしたほうがいいか
  574. int y = _priceLine._nextToBeDrawn;
  575. if(y>=_layout.HeaderHeight && y<GetPricePaneBottom()) {
  576. Win32.SelectObject(hdc, _pref.MouseTrackingLinePen.Handle);
  577. Win32.MoveToEx(hdc, 0, y, out _dummyPoint);
  578. Win32.LineTo(hdc, _layout.ChartAreaWidth, y);
  579. double price = _priceTrans.Inverse((double)y);
  580. double yobine = Util.Yobine(_brand.Market, price);
  581. price = Math.Floor(price / yobine + 0.5) * yobine; //呼値の整数倍に修正
  582. Win32.SetTextColor(hdc, Util.ToCOLORREF(_pref.MouseTrackingLinePen.Color));
  583. ChartUtil.DrawText(hdc, _layout.ChartAreaWidth, y-_layout.DefaultTextHeight/2, price.ToString("F0"));
  584. _priceLine._lastDrawn = y;
  585. }
  586. }
  587. }
  588. private void DrawBrandInformation(IntPtr hdc) {
  589. Win32.SelectObject(hdc, _pref.HeaderHFont);
  590. Win32.SetTextColor(hdc, Util.ToCOLORREF(_pref.TextColor));
  591. AbstractBrand br = _brand;
  592. StringBuilder bld = new StringBuilder();
  593. bld.Append(EnumDescAttribute.For(typeof(MarketType)).GetDescription(br.Market));
  594. bld.Append(" コード:");
  595. bld.Append(br.CodeAsString);
  596. BasicBrand bb = br as BasicBrand;
  597. if(bb!=null) {
  598. if(bb.Nikkei225) bld.Append(" 日経225採用");
  599. if(bb.Unit!=0) {
  600. bld.Append(" 単元株数:");
  601. bld.Append(bb.Unit);
  602. }
  603. }
  604. if(_brand.ReserveFarm().IsEmpty) {
  605. bld.Append(" (データが取得できません)");
  606. }
  607. else {
  608. ChartFormat format = Env.CurrentIndicators.Format;
  609. int fd = Env.Frame.ChartCanvas.DrawingEngine.FirstDate;
  610. int ld = Env.Frame.ChartCanvas.DrawingEngine.LastDate;
  611. string h = String.Format(" 表示期間:{0} - {1}", Util.FormatFullDate(fd, format), Util.FormatFullDate(ld, format));
  612. bld.Append(h);
  613. }
  614. int offset = 7; //少し右へ描画
  615. ChartUtil.DrawTextLimitedWidth(hdc, bld.ToString(), offset, _layout.HeaderHeight+1, _layout.ChartAreaWidth-offset, _layout.DefaultTextHeight);
  616. Win32.SelectObject(hdc, _pref.DefaultHFont);
  617. }
  618. private void DrawValueWindow(IntPtr hdc, TradeData td) {
  619. Win32.SelectObject(hdc, _pref.DefaultHFont);
  620. Win32.SetTextColor(hdc, Util.ToCOLORREF(_pref.TextColor));
  621. int ix = _layout.CurrentValueRect.Left + 5;
  622. int vx = ix + _layout.DefaultTextWidth*11;
  623. Indicator[] inds = Env.CurrentIndicators.GetIndicatorsForValueWindow();
  624. int y = _layout.HeaderHeight;
  625. if(td!=null) ChartUtil.DrawText(hdc, ix, y, Util.FormatFullDate(td.Date, Env.CurrentIndicators.Format));
  626. y += _layout.DefaultTextHeight;
  627. foreach(Indicator ind in inds) {
  628. if(ind.Target==IndicatorTarget.Oscillator && !IsVisibleOscillator(ind)) continue;
  629. if(ind.TargetBrand!=null && !ind.TargetBrand.Applicable(_brand.Code)) continue; //適用不可
  630. if(ind.Appearance!=null)
  631. Win32.SetTextColor(hdc, Util.ToCOLORREF(ind.Appearance.Color));
  632. else
  633. Win32.SetTextColor(hdc, Util.ToCOLORREF(_pref.TextColor));
  634. ChartUtil.DrawText(hdc, ix, y, ind.Name);
  635. if(_tipBuilding) {
  636. int w = MeasureString(ind.Name);
  637. if(w > vx-ix)
  638. _tipEntries.Add(new ComplementaryTextEntry(new Rectangle(ix, y, w, _layout.DefaultTextHeight), ind.Name));
  639. }
  640. double val = Double.NaN;
  641. if(td!=null) {
  642. val = td.GetValue(ind);
  643. }
  644. ChartUtil.DrawText(hdc, vx, y, Util.FormatFixedLenValue(val, 9, RetFormatString(_brand, ind, val), ind.FormatModifier));
  645. y += _layout.DefaultTextHeight;
  646. }
  647. }
  648. private string RetFormatString(AbstractBrand br, Indicator ind, double val)
  649. {
  650. switch ((PrimitiveIndicator)ind.LaneID)
  651. {
  652. case PrimitiveIndicator.Open:
  653. case PrimitiveIndicator.High:
  654. case PrimitiveIndicator.Low:
  655. case PrimitiveIndicator.Close:
  656. if (!br.IsBuiltIn && val > 5000)
  657. return "F0";
  658. break;
  659. }
  660. return ind.GetFormatString(br);
  661. }
  662. private void DrawExplanation(IntPtr hdc) {
  663. Win32.SelectObject(hdc, _pref.DefaultHFont);
  664. Win32.SetTextColor(hdc, Util.ToCOLORREF(_pref.TextColor));
  665. int ix = _layout.CurrentValueRect.Left + 5;
  666. int lx = ix + _layout.DefaultTextWidth*14;
  667. Indicator[] inds = Env.CurrentIndicators.GetIndicatorsForExplanationWindow();
  668. int y = _layout.HeaderHeight + _layout.DefaultTextHeight * _maximumValueWindowItemCount + 30;
  669. Point pt = _owner.PointToClient(Control.MousePosition);
  670. foreach(Indicator ind in inds) {
  671. if(ind.Target==IndicatorTarget.Oscillator && !IsVisibleOscillator(ind)) continue;
  672. IndicatorAppearance ia = ind.Appearance;
  673. ZPen pen = ia==null? _pref.DefaultPen : ia.Pen;
  674. ChartUtil.DrawText(hdc, ix, y, ind.Name);
  675. if(_tipBuilding) {
  676. int w = MeasureString(ind.Name);
  677. if(w > lx-ix)
  678. _tipEntries.Add(new ComplementaryTextEntry(new Rectangle(ix, y, w, _layout.DefaultTextHeight), ind.Name));
  679. }
  680. Win32.MoveToEx(hdc, lx, y+_layout.DefaultTextHeight/2-1, out _dummyPoint);
  681. Win32.SelectObject(hdc, pen.Handle);
  682. Win32.LineTo(hdc, lx+30, y+_layout.DefaultTextHeight/2-1);
  683. y += _layout.DefaultTextHeight;
  684. }
  685. }
  686. private void DrawAccumulativeVolume(IntPtr hdc) {
  687. if(!_brand.IsVolumeAvailable || !_accumulativeVolume.Available || _priceTrans==null) return;
  688. double price = _accumulativeVolume.StartPrice;
  689. Rectangle acc_rect = _layout.AccumulativeVolumeRect;
  690. Win32.SelectObject(hdc, _pref.DefaultHFont);
  691. Win32.SetTextColor(hdc, Util.ToCOLORREF(_pref.TextColor));
  692. int char_height = (int)_pref.DefaultCharPitch.Height;
  693. //棒の半分の幅。EndPrice付近を使うのは対数グラフでも困らないようにするため
  694. int half_height = (int)Math.Abs(_priceTrans.TransValue(_accumulativeVolume.EndPrice) - _priceTrans.TransValue(_accumulativeVolume.EndPrice - _accumulativeVolume.Pitch)) / 2 - 2;
  695. if(half_height < 2) half_height = 2;
  696. ChartUtil.DrawText(hdc, acc_rect.Left, acc_rect.Top, String.Format("価格帯別出来高"));
  697. ChartUtil.DrawText(hdc, acc_rect.Left, acc_rect.Top + char_height, String.Format("{0}~", Util.FormatShortDate(_accumulativeVolume.BeginDate)));
  698. ChartUtil.DrawText(hdc, acc_rect.Left, acc_rect.Top + char_height*2, String.Format("   {0}", Util.FormatShortDate(_accumulativeVolume.EndDate)));
  699. double scale = ChartUtil.SelectGoodValue(_accumulativeVolume.MaxVolume);
  700. if(scale < _accumulativeVolume.MaxVolume) scale = ChartUtil.SelectGoodValue(_accumulativeVolume.MaxVolume*2); //はみ出ることがないように
  701. Trans volume_trans = Trans.Solve(0, acc_rect.Left, scale, acc_rect.Right, false, false);
  702. for(int index = 0; index < _accumulativeVolume.DataLength; index++) {
  703. int y = (int)_priceTrans.TransValue(price + _accumulativeVolume.Pitch/2);
  704. double value = _accumulativeVolume[index];
  705. if(value != 0) {
  706. Win32.RECT r = new Win32.RECT();
  707. r.left = acc_rect.Left;
  708. r.top = y - half_height;
  709. r.right = (int)volume_trans.TransValue(value);
  710. r.bottom = y + half_height;
  711. Win32.FillRect(hdc, ref r, _pref.VolumeBrush.Handle);
  712. Win32.POINT pt;
  713. Win32.SelectObject(hdc, _pref.VolumeBrush.LightPen);
  714. Win32.MoveToEx(hdc, r.left, r.top, out pt);
  715. Win32.LineTo(hdc, r.right + 1, r.top);
  716. Win32.MoveToEx(hdc, r.right, r.top, out pt);
  717. Win32.LineTo(hdc, r.right, r.bottom);
  718. Win32.SelectObject(hdc, _pref.VolumeBrush.DarkPen);
  719. Win32.MoveToEx(hdc, r.left, r.bottom + 1, out pt);
  720. Win32.LineTo(hdc, r.right, r.bottom + 1);
  721. }
  722. price += _accumulativeVolume.Pitch;
  723. }
  724. //縦線
  725. Win32.SelectObject(hdc, _pref.VolumeScalePen.Handle);
  726. const int SCALE_COUNT = 4;
  727. for(int i = 0; i < SCALE_COUNT; i++) {
  728. int x = acc_rect.Left + acc_rect.Width / SCALE_COUNT * i;
  729. Win32.MoveToEx(hdc, x, acc_rect.Top + char_height * 3, out _dummyPoint);
  730. int y = acc_rect.Bottom - char_height * (i % 2 == 0 ? 1 : 2); //数字が大きくなりがちなので互い違いにする
  731. Win32.LineTo(hdc, x, y);
  732. string scale_string = (scale/SCALE_COUNT*i).ToString();
  733. ChartUtil.DrawText(hdc, x - (int)_pref.DefaultCharPitch.Width * scale_string.Length / 2, y, scale_string);
  734. }
  735. }
  736. //Conversions
  737. private int ConvertToY(IndicatorTarget target, OscillatorPreference op, double value) {
  738. switch(target) {
  739. case IndicatorTarget.Price:
  740. return (int)_priceTrans.TransValue(value);
  741. case IndicatorTarget.Volume:
  742. return (int)_volumeTrans.TransValue(value);
  743. case IndicatorTarget.Oscillator:
  744. return (int)op.Trans.TransValue(value);
  745. }
  746. return 0;
  747. }
  748. private int DateOffsetToX(int offset) {
  749. return offset*_layout.DatePitch + _layout.CandleMiddleOffset;
  750. }
  751. //Formats
  752. private void RecalcFormat() {
  753. int width = _owner.Width;
  754. _pref = Env.Preference;
  755. DataFarm farm = _brand.ReserveFarm();
  756. int half_height = _layout.DefaultTextHeight / 2;
  757. int end = Math.Min(farm.FilledLength, _firstDateIndex + _layout.DisplayColumnCount);
  758. double chart_bottom = (double)GetPricePaneBottom();
  759. if(chart_bottom < 0) chart_bottom = 0;
  760. //値段ゾーン
  761. if(!_pref.ScaleLock) { //Brandをリセットするとロックが外れるのでこれでよい
  762. double range_min = new IndicatorTimeSeries(farm, Env.CurrentIndicators.GetPrimitive(PrimitiveIndicator.Low), _firstDateIndex, end).Min;
  763. double range_max = new IndicatorTimeSeries(farm, Env.CurrentIndicators.GetPrimitive(PrimitiveIndicator.High), _firstDateIndex, end).Max;
  764. //ボリンジャーバンドなど、上下に広がるチャートのことを考えてちょっと広めにとる
  765. //double t = (range_max - range_min) * 0.1;
  766. //_lowPrice = range_min - t;
  767. //_highPrice = range_max + t;
  768. if (_pref.LogScale)
  769. {
  770. double t = (Math.Log10(range_max) - Math.Log10(range_min)) * 0.1;
  771. _highPrice = Math.Pow(10, Math.Log10(range_max) + t);
  772. _lowPrice = Math.Pow(10, Math.Log10(range_min) - t);
  773. }
  774. else
  775. {
  776. double t = (range_max - range_min) * 0.1;
  777. _lowPrice = range_min - t;
  778. _highPrice = range_max + t;
  779. }
  780. #if DOJIMA
  781. _priceScaleStep = ChartUtil.CalcScaleStep((_highPrice - _lowPrice)*1.2, chart_bottom - (double)_layout.HeaderHeight, 60);
  782. if(Dojima.DojimaChart.IsMusousenAvailable(Env.CurrentIndicators.Format))
  783. _lowPrice -= _priceScaleStep * 3; //下の値段に3段分余裕を作る
  784. #else
  785. _priceScaleStep = (int)ChartUtil.CalcScaleStep(_highPrice - _lowPrice, chart_bottom - (double)_layout.HeaderHeight, 60);
  786. #endif
  787. _priceTrans = Trans.Solve(_highPrice, (double)_layout.HeaderHeight, _lowPrice, chart_bottom, _pref.LogScale, _pref.InverseChart);
  788. if(_pref.ShowAccumulativeVolume) {
  789. int first_index = _dateLine._nextToBeDrawn == -1 ? farm.FilledLength - 1 : Math.Min(_dateLine._nextToBeDrawn, farm.FilledLength - 1);
  790. int price_pitch = Math.Max(1, _priceScaleStep / AccumulativeVolume.DATA_PER_SCALELINE);
  791. _accumulativeVolume.Fill(farm, first_index, range_min, price_pitch, range_max);
  792. }
  793. }
  794. //出来高ゾーン
  795. if(_pref.ShowVolume!=HeightConfig.None) {
  796. double max_volume = new IndicatorTimeSeries(farm, Env.CurrentIndicators.GetPrimitive(PrimitiveIndicator.Volume), _firstDateIndex, end).Max;
  797. max_volume = Math.Max(max_volume, new IndicatorTimeSeries(farm, Env.CurrentIndicators.GetPrimitive(PrimitiveIndicator.CreditShort), _firstDateIndex, end).Max);
  798. max_volume = Math.Max(max_volume, new IndicatorTimeSeries(farm, Env.CurrentIndicators.GetPrimitive(PrimitiveIndicator.CreditLong), _firstDateIndex, end).Max);
  799. double volume_bottom = (double)GetVolumePaneBottom() - half_height;
  800. if(volume_bottom<0) volume_bottom=0;
  801. _volumeTrans = LinearTrans.Solve(max_volume, chart_bottom, 0, volume_bottom);
  802. _volumeScaleStep = ChartUtil.CalcScaleStep(max_volume, volume_bottom-chart_bottom, 30);
  803. }
  804. //オシレータゾーン
  805. int top = this.GetVolumePaneBottom();
  806. for(int i=0; i<OscillatorPreference.LENGTH; i++) {
  807. OscillatorPreference op = _pref.OscillatorPreferences[i];
  808. if(op.Config!=HeightConfig.None) {
  809. if(op.OscillatorGroup.Type==ValueRange.Percent0_1) {
  810. op.SetScaleValues(1, 0.5, 0);
  811. }
  812. else if(op.OscillatorGroup.Type==ValueRange.Percent1_1) {
  813. op.SetScaleValues(1, 0, -1);
  814. }
  815. else if(op.OscillatorGroup.Type==ValueRange.Origin0) {
  816. double m = 0;
  817. foreach(Indicator ind in op.OscillatorGroup) {
  818. TimeSeries ts = new IndicatorTimeSeries(farm, ind, _firstDateIndex, end);
  819. m = Math.Max(m, Math.Max(Math.Abs(ts.Max), Math.Abs(ts.Min)));
  820. }
  821. m = ChartUtil.SelectGoodValue(m);
  822. op.SetScaleValues(m, 0, -m);
  823. }
  824. else {
  825. double min = Double.MaxValue;
  826. double max = Double.MinValue;
  827. foreach(Indicator ind in op.OscillatorGroup) {
  828. TimeSeries ts = new IndicatorTimeSeries(farm, ind, _firstDateIndex, end);
  829. max = Math.Max(max, ts.Max);
  830. min = Math.Min(min, ts.Min);
  831. }
  832. double mid = (max+min)/2;
  833. double pitch = ChartUtil.SelectGoodValue((max-min)/2);
  834. mid = pitch * (int)(mid / pitch); //四捨五入しての整数倍
  835. op.SetScaleValues(mid + pitch, mid, mid - pitch);
  836. }
  837. op.Trans = LinearTrans.Solve(op.ScaleValues[0], top+half_height, op.ScaleValues[2], top + _layout.OscillatorPaneHeights[i] -half_height);
  838. }
  839. top += _layout.OscillatorPaneHeights[i];
  840. }
  841. //FreeLine
  842. SolidFreeLine[] sls = Env.FreeLines.Find(_brand, Env.CurrentIndicators.Format, _pref.LogScale);
  843. _freeLines.Clear();
  844. foreach(SolidFreeLine sl in sls)
  845. _freeLines.Add(new FreeLine(farm, _firstDateIndex, sl, _priceTrans));
  846. }
  847. private void RecalcValueWindowFormat() {
  848. Indicator[] inds = Env.CurrentIndicators.GetIndicatorsForValueWindow();
  849. _maximumValueWindowItemCount = 0;
  850. foreach(Indicator ind in inds)
  851. if(ind.Target!=IndicatorTarget.Oscillator) _maximumValueWindowItemCount++;
  852. //これにOscillatorGroupを考慮
  853. int im = 0;
  854. foreach(OscillatorPreference op in _pref.OscillatorPreferences) {
  855. if(op.OscillatorGroup!=null)
  856. im += op.OscillatorGroup.Count;
  857. }
  858. _maximumValueWindowItemCount += im;
  859. }
  860. private int GetPricePaneBottom() {
  861. return _layout.ChartAreaBottom-_layout.OscillatorPaneHeightTotal-_layout.VolumePaneHeight;
  862. }
  863. private int GetVolumePaneBottom() {
  864. return _layout.ChartAreaBottom-_layout.OscillatorPaneHeightTotal;
  865. }
  866. private bool IsVisibleOscillator(Indicator ind) {
  867. for(int i=0; i<OscillatorPreference.LENGTH; i++) {
  868. OscillatorPreference op = _pref.OscillatorPreferences[i];
  869. if(op.Config!=HeightConfig.None) {
  870. OscillatorGroup g = op.OscillatorGroup;
  871. if(g.Contains(ind)) return true;
  872. }
  873. }
  874. return false;
  875. }
  876. //FreeLineの追加
  877. public void FixFreeLine(FreeLine fl) {
  878. Env.FreeLines.Add(_brand, Env.CurrentIndicators.Format,_pref.LogScale, fl.ToSolid(_brand.ReserveFarm(), _firstDateIndex, _priceTrans));
  879. _freeLines.Add(fl);
  880. }
  881. public bool RemoveHighlitedFreeLines() {
  882. ArrayList remain = new ArrayList();
  883. bool f = false;
  884. foreach(FreeLine line in _freeLines) {
  885. if(line.DrawMode==FreeLine.LineDrawMode.Normal) {
  886. remain.Add(line);
  887. }
  888. else {
  889. if(line.ID!=-1) Env.FreeLines.Remove(line.ID);
  890. f = true;
  891. }
  892. }
  893. _freeLines = remain;
  894. return f; //1つでも消去したらtrueを返す
  895. }
  896. //特殊なやつの描画
  897. //一目均衡表の雲
  898. private bool IchimokuKumoIsAvailable() {
  899. IList l = Env.CurrentIndicators.IchimokuKumo;
  900. if(l.Count<2) return false;
  901. Indicator indA = (Indicator)l[0];
  902. Indicator indB = (Indicator)l[1];
  903. //両方が描画されるときのみ雲を描く
  904. return ((indA.Display & IndicatorDisplay.Chart)!=IndicatorDisplay.None && indA.Appearance.Style!=IndicatorStyle.None) &&
  905. ((indB.Display & IndicatorDisplay.Chart)!=IndicatorDisplay.None && indB.Appearance.Style!=IndicatorStyle.None);
  906. }
  907. private void DrawIchimokuKumo(IntPtr hdc, DataFarm farm, int index, int end, Rectangle clip) {
  908. while(index < end) {
  909. int x = (index - _firstDateIndex) * _layout.DatePitch;
  910. int mid = x + _layout.CandleMiddleOffset;
  911. TradeData td = farm.GetByIndex(index);
  912. if(clip.Left<=x+_pref.DatePitch && x-_layout.DatePitch<clip.Right) {
  913. DrawIchimokuKumoParts(hdc, mid, td);
  914. }
  915. index++;
  916. }
  917. }
  918. private void DrawIchimokuKumoParts(IntPtr hdc, int x, TradeData td) {
  919. IList l = Env.CurrentIndicators.IchimokuKumo;
  920. Indicator indA = (Indicator)l[0];
  921. Indicator indB = (Indicator)l[1];
  922. double vA = td.GetValue(indA);
  923. double vB = td.GetValue(indB);
  924. if(Double.IsNaN(vA) || Double.IsNaN(vB)) return;
  925. int y1A = (int)_priceTrans.TransValue(vA);
  926. int y1B = (int)_priceTrans.TransValue(vB);
  927. TradeData pr = td.Prev;
  928. if(pr==null) return;
  929. double pA = pr.GetValue(indA);
  930. double pB = pr.GetValue(indB);
  931. if(Double.IsNaN(pA) || Double.IsNaN(pB)) return;
  932. int y0A = (int)_priceTrans.TransValue(pA);
  933. int y0B = (int)_priceTrans.TransValue(pB);
  934. //ここまでで値が出揃った
  935. int x0 = x-_layout.DatePitch;
  936. if(pA>=pB && vA>=vB)
  937. PaintKumo(hdc, indA.Appearance.Color, x0, y0A, x, y1A, x, y1B, x0, y0B);
  938. else if(pA<=pB && vA<=vB)
  939. PaintKumo(hdc, indB.Appearance.Color, x0, y0B, x, y1B, x, y1A, x0, y0A);
  940. else { //交差。めんどうくさいなあ
  941. double r = (double)(Math.Abs(y0A-y0B)) / (double)(Math.Abs(y0A-y0B)+Math.Abs(y1A-y1B));
  942. int cx = x - (int)((double)_layout.DatePitch*(1-r));
  943. int cy = (int)(y1A*r + y0A*(1-r));
  944. if(pA>=pB) {
  945. PaintKumo(hdc, indA.Appearance.Color, cx, cy, x0, y0A, x0, y0B);
  946. PaintKumo(hdc, indB.Appearance.Color, cx, cy, x , y1A, x , y1B);
  947. }
  948. else {
  949. PaintKumo(hdc, indB.Appearance.Color, cx, cy, x0, y0A, x0, y0B);
  950. PaintKumo(hdc, indA.Appearance.Color, cx, cy, x , y1A, x , y1B);
  951. }
  952. }
  953. }
  954. private void PaintKumo(IntPtr hdc, Color col, int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3) {
  955. Win32.POINT[] points = new Win32.POINT[4];
  956. points[0].x = x0;
  957. points[0].y = y0;
  958. points[1].x = x1;
  959. points[1].y = y1;
  960. points[2].x = x2;
  961. points[2].y = y2;
  962. points[3].x = x3;
  963. points[3].y = y3;
  964. IntPtr rgn = Win32.CreatePolygonRgn(points, 4, 2); //2はWINDING
  965. IntPtr brush = Win32.CreateSolidBrush(Util.ToCOLORREF(col));
  966. Win32.FillRgn(hdc, rgn, brush);
  967. Win32.DeleteObject(brush);
  968. Win32.DeleteObject(rgn);
  969. }
  970. private void PaintKumo(IntPtr hdc, Color col, int x0, int y0, int x1, int y1, int x2, int y2) {
  971. Win32.POINT[] points = new Win32.POINT[3];
  972. points[0].x = x0;
  973. points[0].y = y0;
  974. points[1].x = x1;
  975. points[1].y = y1;
  976. points[2].x = x2;
  977. points[2].y = y2;
  978. IntPtr rgn = Win32.CreatePolygonRgn(points, 3, 2); //2はWINDING
  979. IntPtr brush = Win32.CreateSolidBrush(Util.ToCOLORREF(col));
  980. Win32.FillRgn(hdc, rgn, brush);
  981. Win32.DeleteObject(brush);
  982. Win32.DeleteObject(rgn);
  983. }
  984. //y座標から、そこに最も近い呼値に対応したy座標に変換する
  985. public int NormalizeByYobine(int y) {
  986. if(_priceTrans==null || _brand==null) return y;
  987. double price = _priceTrans.Inverse(y);
  988. double yobine = Util.Yobine(_brand.Market, price);
  989. return (int)_priceTrans.TransValue(yobine * Math.Round(price / yobine));
  990. }
  991. //utils
  992. private int MeasureString(string text) {
  993. int n = 0;
  994. for(int i=0; i<text.Length; i++) {
  995. char ch = text[i];
  996. if(ch<=0xFF)
  997. n++;
  998. else
  999. n += 2;
  1000. }
  1001. return n * _layout.DefaultTextWidth;
  1002. }
  1003. #if DOJIMA
  1004. private HalfDailyDataFarm _halfDailyDataFarm;
  1005. private void DrawHalfDailyChart(IntPtr hdc, DailyDataFarm farm, int x, TradeData td, Rectangle clip) {
  1006. if(_halfDailyDataFarm==null)
  1007. _halfDailyDataFarm = Dojima.DojimaUtil.HalfDailyDataFarmCache.Get(farm);
  1008. int width = _layout.CandleMiddleOffset / 2;
  1009. bool vol = _pref.ShowVolume!=HeightConfig.None && _brand.IsVolumeAvailable;
  1010. HalfDailyDataUnit unit = _halfDailyDataFarm.GetData(td.Index, HalfDay.Zenba);
  1011. DrawCandle(hdc, x + width, unit.open, unit.high, unit.low, unit.close, clip);
  1012. if(vol) DrawVolume(hdc, x + width, unit.volume, clip);
  1013. unit = _halfDailyDataFarm.GetData(td.Index, HalfDay.Goba);
  1014. DrawCandle(hdc, x + width*3, unit.open, unit.high, unit.low, unit.close, clip);
  1015. if(vol) DrawVolume(hdc, x + width*3, unit.volume, clip);
  1016. //さらに週末の後場にマーク
  1017. bool flag = true;
  1018. DateTime mark = Util.IntToDate(td.Date);
  1019. mark = mark.AddDays(1);
  1020. while(mark.DayOfWeek!=DayOfWeek.Saturday) {
  1021. if(Util.IsMarketOpenDate(mark)) {
  1022. flag = false; //翌日以降土曜日以前に市場の開いている日を見つけたら描画しない
  1023. break;
  1024. }
  1025. mark = mark.AddDays(1);
  1026. }
  1027. if(flag) {
  1028. double p = td.Low + _priceScaleStep / 5 * (_pref.InverseChart? 1 : -1);
  1029. ZBrush cb = new ZBrush(Color.Red);
  1030. ZPen cp = new ZPen(Color.Red, ZPen.PenStyle.Normal);
  1031. IntPtr ob = Win32.SelectObject(hdc, cb.Handle);
  1032. IntPtr op = Win32.SelectObject(hdc, cp.Handle);
  1033. int t = _layout.CandleMiddleOffset / 3; //半径
  1034. int y = (int)_priceTrans.TransValue(p);
  1035. Win32.Ellipse(hdc, x+width*3-t, y-t, x+width*3+t, y+t);
  1036. Win32.SelectObject(hdc, ob);
  1037. Win32.SelectObject(hdc, op);
  1038. cb.Dispose();
  1039. cp.Dispose();
  1040. }
  1041. }
  1042. #endif
  1043. }
  1044. internal class ChartUtil {
  1045. public static double CalcScaleStep(double range, double pane_height, double standard_pitch_in_pixel) {
  1046. if(pane_height<=0) return 1;
  1047. double t = range / (pane_height / standard_pitch_in_pixel);
  1048. return SelectGoodValue(t);
  1049. }
  1050. //srcに最も近い、切りの良い数を返す。切りがよいとは、(1 or 2.5 or 5) * 10^n
  1051. public static double SelectGoodValue(double src) {
  1052. if(src<0)
  1053. return -SelectGoodValue(-src);
  1054. double log = Math.Log10(src);
  1055. int n = (int)Math.Floor(log);
  1056. double a = log - n;
  1057. double b;
  1058. if(a < 0.16)
  1059. b = 1;
  1060. else if(a < 0.5)
  1061. b = 2.5;
  1062. else if(a < 0.83)
  1063. b = 5;
  1064. else {
  1065. b = 1;
  1066. n++;
  1067. }
  1068. if(n>0)
  1069. for(int i=0; i<n; i++) b*=10;
  1070. else
  1071. for(int i=0; i<-n; i++) b/=10;
  1072. return b;
  1073. }
  1074. public static void DrawText(IntPtr hdc, int x, int y, string text) {
  1075. unsafe {
  1076. int len = text.Length;
  1077. fixed(char* p = text) {
  1078. Win32.TextOut(hdc, x, y, p, len);
  1079. }
  1080. }
  1081. }
  1082. private static Win32.RECT _tempRect = new Win32.RECT();
  1083. public static void DrawTextLimitedWidth(IntPtr hdc, string text, int x, int y, int width, int height) {
  1084. _tempRect.left = x;
  1085. _tempRect.top = y;
  1086. _tempRect.right = x + width;
  1087. _tempRect.bottom = y + height;
  1088. Win32.DrawText(hdc, text, text.Length, ref _tempRect, 0);
  1089. }
  1090. public static bool HasIntersectionY(Rectangle clip, int y1, int y2) {
  1091. if(y1 > y2) {
  1092. int t = y1;
  1093. y1 = y2;
  1094. y2 = t;
  1095. }
  1096. return !(y1 > clip.Bottom || y2 < clip.Top);
  1097. }
  1098. }
  1099. internal class ComplementaryTextEntry {
  1100. public Rectangle rect;
  1101. public string text;
  1102. public ComplementaryTextEntry(Rectangle r, string t) {
  1103. rect = r;
  1104. text = t;
  1105. }
  1106. }
  1107. }
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text