コピペ: OmegaChart Elliot波動 ChartDrawing.cs 追加修正ポイントには//☆Elliott 差し替え その1

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

このコピペの URL

JavaScript での埋め込み

iframe での埋め込み

元のテキスト