int BarFromX(int x) { int thisBar = CurrentBar - (int)ChartBars.GetBarIdxByX(ChartControl, x); //Limit to existing right bar # if (thisBar < rightMaxBarNum) { thisBar = 0; } return(thisBar); }
private void UpdateTextLayout(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale) { if (isTextCreated && textLayout != null && !textLayout.IsDisposed) { return; } if (textFormat != null && !textFormat.IsDisposed) { textFormat.Dispose(); } if (textLayout != null && !textLayout.IsDisposed) { textLayout.Dispose(); } ChartBars chartBars = GetAttachedToChartBars(); // bars can be null while chart is initializing if (chartBars == null) { return; } double yDiffPrice = AttachedTo.Instrument.MasterInstrument.RoundToTickSize(EndAnchor.Price - StartAnchor.Price); double yDiffTicks = yDiffPrice / AttachedTo.Instrument.MasterInstrument.TickSize; switch (YValueDisplayUnit) { case ValueUnit.Price: yValueString = chartBars.Bars.Instrument.MasterInstrument.FormatPrice(yDiffPrice); break; case ValueUnit.Currency: yValueString = AttachedTo.Instrument.MasterInstrument.InstrumentType == InstrumentType.Forex ? Core.Globals.FormatCurrency((int)Math.Abs(yDiffTicks) * Account.All[0].ForexLotSize * (AttachedTo.Instrument.MasterInstrument.TickSize * AttachedTo.Instrument.MasterInstrument.PointValue)) : Core.Globals.FormatCurrency((int)Math.Abs(yDiffTicks) * (AttachedTo.Instrument.MasterInstrument.TickSize * AttachedTo.Instrument.MasterInstrument.PointValue)); break; case ValueUnit.Percent: yValueString = (yDiffPrice / AttachedTo.Instrument.MasterInstrument.RoundToTickSize(StartAnchor.Price)).ToString("P", Core.Globals.GeneralOptions.CurrentCulture); break; case ValueUnit.Ticks: yValueString = yDiffTicks.ToString("F0"); break; case ValueUnit.Pips: // show tenth pips (if available) double pips = Math.Abs(yDiffTicks / 10); char decimalChar = Char.Parse(Core.Globals.GeneralOptions.CurrentCulture.NumberFormat.NumberDecimalSeparator); yValueString = Int32.Parse(pips.ToString("F1").Split(decimalChar)[1]) > 0 ? pips.ToString("F1").Replace(decimalChar, '\'') : pips.ToString("F0"); break; } TimeSpan timeDiff = EndAnchor.Time - StartAnchor.Time; // trim off millis/ticks, match NT7 time formatting timeDiff = new TimeSpan(timeDiff.Days, timeDiff.Hours, timeDiff.Minutes, timeDiff.Seconds); bool isMultiDay = Math.Abs(timeDiff.TotalHours) >= 24; if (chartBars.Bars.BarsPeriod.BarsPeriodType == BarsPeriodType.Day) { int timeDiffDay = Math.Abs(timeDiff.Days); timeText = timeDiffDay > 1 ? Math.Abs(timeDiff.Days) + " " + Custom.Resource.Days : Math.Abs(timeDiff.Days) + " " + Custom.Resource.Day; } else { timeText = isMultiDay ? string.Format("{0}\n{1,25}", string.Format(Custom.Resource.NinjaScriptDrawingToolRulerDaysFormat, Math.Abs(timeDiff.Days)), timeDiff.Subtract(new TimeSpan(timeDiff.Days, 0, 0, 0)).Duration().ToString()) : timeDiff.Duration().ToString(); } Point startPoint = StartAnchor.GetPoint(chartControl, chartPanel, chartScale); Point endPoint = EndAnchor.GetPoint(chartControl, chartPanel, chartScale); int startIdx = chartBars.GetBarIdxByX(chartControl, (int)startPoint.X); int endIdx = chartBars.GetBarIdxByX(chartControl, (int)endPoint.X); int numBars = endIdx - startIdx; SimpleFont wpfFont = chartControl.Properties.LabelFont ?? new SimpleFont(); textFormat = wpfFont.ToDirectWriteTextFormat(); textFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading; textFormat.WordWrapping = SharpDX.DirectWrite.WordWrapping.NoWrap; // format text to our text rectangle bounds (it will wrap to these constraints), nt7 format // NOTE: Environment.NewLine doesnt work right here string text = string.Format("{0}\n{1,-11}{2,-11}\n{3,-11}{4,-11}\n{5,-10}{6,-10}", AttachedTo.DisplayName, Custom.Resource.NinjaScriptDrawingToolRulerNumberBarsText, numBars, Custom.Resource.NinjaScriptDrawingToolRulerTimeText, timeText, Custom.Resource.NinjaScriptDrawingToolRulerYValueText, yValueString); // give big values for max width/height, we will trim to actual used textLayout = new SharpDX.DirectWrite.TextLayout(Core.Globals.DirectWriteFactory, text, textFormat, 600, 600); // use measured max width/height textLayout.MaxWidth = textLayout.Metrics.Width; textLayout.MaxHeight = textLayout.Metrics.Height; isTextCreated = true; }