#region Using declarations using System; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.ComponentModel; using System.Collections; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.Gui.Chart; #endregion #region Global Enums public enum anaPlotAlignVB37 {Left, Right, DoNotPlot} public enum anaSessionTypeVB37 {ETH, RTH} public enum anaSessionCountVB37 {First, Second, Third, Hybrid} public enum anaBandTypeVB37 {Expand, Freeze, Stop} #endregion // This namespace holds all indicators and is required. Do not change it. namespace NinjaTrader.Indicator { /// /// Current Open, High, Low and Fibonacci Levels. /// [Description("anaVolatilityBandsV37.")] public class anaVolatilityBandsV37 : Indicator { #region Variables private SolidBrush[] brushes = { new SolidBrush(Color.Black), new SolidBrush(Color.Black), new SolidBrush(Color.Black), new SolidBrush(Color.Black), new SolidBrush(Color.Black), new SolidBrush(Color.Black), new SolidBrush(Color.Black), new SolidBrush(Color.Black), new SolidBrush(Color.Black), new SolidBrush(Color.Black), new SolidBrush(Color.Black), new SolidBrush(Color.Black)}; private DateTime sessionBegin = Cbi.Globals.MinDate; private DateTime sessionEnd = Cbi.Globals.MinDate; private DateTime plotStart = Cbi.Globals.MaxDate; private DateTime cacheSessionBeginTmp = Cbi.Globals.MinDate; private DateTime cacheSessionEndTmp = Cbi.Globals.MinDate; private DateTime cacheSessionDate = Cbi.Globals.MinDate; private DateTime sessionDateTmp = Cbi.Globals.MinDate; private DateTime currentDate = Cbi.Globals.MinDate; private DateTime highTime = Cbi.Globals.MinDate; private DateTime lowTime = Cbi.Globals.MinDate; private double currentOpen = 0.0; private double currentHigh = double.MinValue; private double currentLow = double.MaxValue; private double priorHigh = double.MinValue; private double priorLow = double.MaxValue; private double currentMidline = 0.0; private double fib786 = 0.0; private double fib618 = 0.0; private double fib382 = 0.0; private double fib236 = 0.0; private double displaySize = 0.0; private double bandFactor = 0.0; private double averageRange1 = 0.0; private double averageRange2 = 0.0; private double adaptedRange1 = 0.0; private double adaptedRange2 = 0.0; private double upperTarget1 = 0.0; private double lowerTarget1 = 0.0; private double upperTarget2 = 0.0; private double lowerTarget2 = 0.0; private SolidBrush textBrush = new SolidBrush(Color.Red); private Font textFont = new Font("Arial", 12); private string errorData1 = "Projection Bands can only be displayed on intraday charts."; private string errorData2 = "Insufficient historical data to calculate Volatility Bands. Please increase chart look back period."; private string errorData3a = "Insufficient historical data. The Volatility Bands Indicator requires a chart lookback period equivalent to "; private string errorData3b = " business days."; private string errorData3c = "The Volatility Bands Indicator did not find a third intraday session."; private float errorTextWidth = 0.0F; private float errorTextHeight = 0.0F; private bool plotOHL = false; private bool periodOpen = false; private bool init = false; private bool extendPublicHoliday = false; private bool rthOHL = false; private bool showCurrentOpen = false; private bool showFibonacci = false; private bool showHiLo = false; private bool showVolatilityLabels = false; private bool showVolatilityBands = true; private bool includeAfterSession = false; private bool isCurrency = false; private bool isGlobex = false; private bool freezeUpper1 = false; private bool freezeLower1 = false; private bool freezeUpper2 = false; private bool freezeLower2 = false; private bool expandedBands = false; private bool totalFreeze = false; private bool target1Hit = false; private bool target2Hit = false; private ArrayList newSessionBarIdxArr1 = new ArrayList(); private anaPlotAlignVB37 plotLabels = anaPlotAlignVB37.Right; private anaPlotAlignVB37 showRangeData = anaPlotAlignVB37.Left; private anaSessionTypeVB37 currentSession = anaSessionTypeVB37.ETH; private anaSessionCountVB37 selectedSession = anaSessionCountVB37.Hybrid; private anaSessionCountVB37 activeSession = anaSessionCountVB37.Hybrid; private anaBandTypeVB37 bandType = anaBandTypeVB37.Freeze; private Data.PivotRange pivotRangeType1 = PivotRange.Daily; private StringFormat stringFormatFar = new StringFormat(); private StringFormat stringFormatNear = new StringFormat(); private StringFormat stringFormatCenter = new StringFormat(); private int countDown = 0; private int numberOfSessions = 1; private int sessionCount = 0; private int rangePeriod = 0; private int rangePeriod1 = 10; private int rangePeriod2 = 20; private int dayCount = 0; private int bandWidth = 100; private int bandWidthExpanded = 127; private int width = 20; private int opacity = 6; private int labelOffset = 20; private int displayFactor = 1; private int startBar = 0; private int targetBar = 0; private Color openColor = Color.PeachPuff; private Color highColor = Color.Lime; private Color lowColor = Color.OrangeRed; private Color midlineColor = Color.MediumSpringGreen; private Color fibColor = Color.Yellow; private Color labelColor = Color.Violet; private Color upperBandColor = Color.Empty; private Color lowerBandColor = Color.Empty; private Color defaultBandColor = Color.DarkViolet; private Color upperTargetColor = Color.Aqua; private Color lowerTargetColor = Color.Magenta; private Color upperExpansionColor = Color.Gold; private Color lowerExpansionColor = Color.Gold; private int plot0Width = 1; private DashStyle dash0Style = DashStyle.Solid; private int plot1Width = 1; private DashStyle dash1Style = DashStyle.Solid; private int plot3Width = 1; private DashStyle dash3Style = DashStyle.Solid; private int plot8Width = 1; private DashStyle dash8Style = DashStyle.Solid; private DateTime publicHoliday0 = new DateTime (2009,01,19); private DateTime publicHoliday1 = new DateTime (2009,02,16); private DateTime publicHoliday2 = new DateTime (2009,05,25); private DateTime publicHoliday3 = new DateTime (2009,07,03); private DateTime publicHoliday4 = new DateTime (2009,09,07); private DateTime publicHoliday5 = new DateTime (2009,11,26); private DateTime publicHoliday6 = new DateTime (2010,01,18); private DateTime publicHoliday7 = new DateTime (2010,02,15); private DateTime publicHoliday8 = new DateTime (2010,05,31); private DateTime publicHoliday9 = new DateTime (2010,07,05); private DateTime publicHoliday10 = new DateTime (2010,09,06); private DateTime publicHoliday11 = new DateTime (2010,11,25); private DateTime publicHoliday12 = new DateTime (2011,01,17); private DateTime publicHoliday13 = new DateTime (2011,02,21); private DateTime publicHoliday14 = new DateTime (2011,05,30); private DateTime publicHoliday15 = new DateTime (2011,07,04); private DateTime publicHoliday16 = new DateTime (2011,09,05); private DateTime publicHoliday17 = new DateTime (2011,11,24); private DateTime[] publicHoliday = new DateTime [18]; private BoolSeries lowIsLast; private double[] rangeArray = new double[100]; private DataSeries ADR0; private DataSeries ADR1; private DataSeries ADR2; private DataSeries upperRegionUpper; private DataSeries upperRegionLower; private DataSeries lowerRegionUpper; private DataSeries lowerRegionLower; #endregion /// /// This method is used to configure the indicator and is called once before any bar data is loaded. /// protected override void Initialize() { Add(new Plot(new Pen(Color.Gray,1), PlotStyle.Line,"D-Open ")); Add(new Plot(new Pen(Color.Gray,1), PlotStyle.Line,"D-High ")); Add(new Plot(new Pen(Color.Gray,1), PlotStyle.Line,"D-Low ")); Add(new Plot(new Pen(Color.Gray,1), PlotStyle.Line,"D-Mid ")); Add(new Plot(new Pen(Color.Gray,1), PlotStyle.Line,"D-78,6 ")); Add(new Plot(new Pen(Color.Gray,1), PlotStyle.Line,"D-61,8 ")); Add(new Plot(new Pen(Color.Gray,1), PlotStyle.Line,"D-38,2 ")); Add(new Plot(new Pen(Color.Gray,1), PlotStyle.Line,"D-23,6 ")); Add(new Plot(new Pen(Color.Gray,1), PlotStyle.Line,"SAR+")); Add(new Plot(new Pen(Color.Gray,1), PlotStyle.Line,"SAR-")); Add(new Plot(new Pen(Color.Gray,1), PlotStyle.Line,"LAR+")); Add(new Plot(new Pen(Color.Gray,1), PlotStyle.Line,"LAR-")); for(int i=0; i<18; i++) publicHoliday[i] = Cbi.Globals.MinDate; for (int i=0; i<100; i++) rangeArray[i] = 0.0; ADR0 = new DataSeries(this); ADR1 = new DataSeries(this); ADR2 = new DataSeries(this); upperRegionUpper = new DataSeries(this); upperRegionLower = new DataSeries(this); lowerRegionUpper = new DataSeries(this); lowerRegionLower = new DataSeries(this); AutoScale = false; Overlay = true; PlotsConfigurable = false; ZOrder = 1; MaximumBarsLookBack = MaximumBarsLookBack.Infinite; stringFormatNear.Alignment = StringAlignment.Near; stringFormatCenter.Alignment = StringAlignment.Center; stringFormatFar.Alignment = StringAlignment.Far; lowIsLast = new BoolSeries(this); } /// /// protected override void OnStartUp() { if (Instrument.MasterInstrument.InstrumentType == InstrumentType.Future && (Instrument.MasterInstrument.Name == "EMD" ||Instrument.MasterInstrument.Name == "ES" || Instrument.MasterInstrument.Name == "NQ" ||Instrument.MasterInstrument.Name == "YM"||Instrument.MasterInstrument.Name == "GE" ||Instrument.MasterInstrument.Name == "SR" ||Instrument.MasterInstrument.Name == "UB"||Instrument.MasterInstrument.Name == "ZB" ||Instrument.MasterInstrument.Name == "ZF" ||Instrument.MasterInstrument.Name == "ZN"||Instrument.MasterInstrument.Name == "ZQ" ||Instrument.MasterInstrument.Name == "ZT" ||Instrument.MasterInstrument.Name == "6A"||Instrument.MasterInstrument.Name == "6B" ||Instrument.MasterInstrument.Name == "6C" ||Instrument.MasterInstrument.Name == "6E"||Instrument.MasterInstrument.Name == "6J" ||Instrument.MasterInstrument.Name == "6M" ||Instrument.MasterInstrument.Name == "6N"||Instrument.MasterInstrument.Name == "6S" ||Instrument.MasterInstrument.Name == "E7" ||Instrument.MasterInstrument.Name == "J7"||Instrument.MasterInstrument.Name == "M6A" ||Instrument.MasterInstrument.Name == "M6B" ||Instrument.MasterInstrument.Name == "M6C"||Instrument.MasterInstrument.Name == "M6E" ||Instrument.MasterInstrument.Name == "M6J" ||Instrument.MasterInstrument.Name == "M6S"||Instrument.MasterInstrument.Name == "CL" ||Instrument.MasterInstrument.Name == "EH" ||Instrument.MasterInstrument.Name == "GC"||Instrument.MasterInstrument.Name == "HG" ||Instrument.MasterInstrument.Name == "HO" ||Instrument.MasterInstrument.Name == "NG"||Instrument.MasterInstrument.Name == "QG" ||Instrument.MasterInstrument.Name == "QM" ||Instrument.MasterInstrument.Name == "RB"||Instrument.MasterInstrument.Name == "SI" ||Instrument.MasterInstrument.Name == "YG" ||Instrument.MasterInstrument.Name == "YI"||Instrument.MasterInstrument.Name == "GF" ||Instrument.MasterInstrument.Name == "GPB" ||Instrument.MasterInstrument.Name == "HE"||Instrument.MasterInstrument.Name == "LE" ||Instrument.MasterInstrument.Name == "YC" ||Instrument.MasterInstrument.Name == "YK"||Instrument.MasterInstrument.Name == "YW" ||Instrument.MasterInstrument.Name == "ZC" ||Instrument.MasterInstrument.Name == "ZE"||Instrument.MasterInstrument.Name == "ZL" ||Instrument.MasterInstrument.Name == "ZM" ||Instrument.MasterInstrument.Name == "ZO"||Instrument.MasterInstrument.Name == "ZR" ||Instrument.MasterInstrument.Name == "ZS" ||Instrument.MasterInstrument.Name == "ZW")) isGlobex = true; if (isGlobex) { publicHoliday[0] = publicHoliday0; publicHoliday[1] = publicHoliday1; publicHoliday[2] = publicHoliday2; publicHoliday[3] = publicHoliday3; publicHoliday[4] = publicHoliday4; publicHoliday[5] = publicHoliday5; publicHoliday[6] = publicHoliday6; publicHoliday[7] = publicHoliday7; publicHoliday[8] = publicHoliday8; publicHoliday[9] = publicHoliday9; publicHoliday[10] = publicHoliday10; publicHoliday[11] = publicHoliday11; publicHoliday[12] = publicHoliday12; publicHoliday[13] = publicHoliday13; publicHoliday[14] = publicHoliday14; publicHoliday[15] = publicHoliday15; publicHoliday[16] = publicHoliday16; publicHoliday[17] = publicHoliday17; } displayFactor = 1; if (Instrument.MasterInstrument.InstrumentType == Cbi.InstrumentType.Currency || Instrument.MasterInstrument.Name == "DX"|| Instrument.MasterInstrument.Name == "6A" || Instrument.MasterInstrument.Name == "6B" || Instrument.MasterInstrument.Name == "6C" ||Instrument.MasterInstrument.Name == "6E" || Instrument.MasterInstrument.Name == "6J" || Instrument.MasterInstrument.Name == "6M" || Instrument.MasterInstrument.Name == "6S" || Instrument.MasterInstrument.Name == "6N" || Instrument.MasterInstrument.Name == "E7" || Instrument.MasterInstrument.Name == "J7" || Instrument.MasterInstrument.Name == "M6A" || Instrument.MasterInstrument.Name == "M6B" || Instrument.MasterInstrument.Name == "M6C" || Instrument.MasterInstrument.Name == "M6E" || Instrument.MasterInstrument.Name == "M6J" || Instrument.MasterInstrument.Name == "M6S") { isCurrency = true; if (Instrument.MasterInstrument.Name == "6J") displayFactor = 1000000; else if (Instrument.MasterInstrument.Name == "USDJPY" || Instrument.MasterInstrument.Name == "EURJPY") displayFactor = 1; else displayFactor = 10000; } if (selectedSession == anaSessionCountVB37.Hybrid) { if(isCurrency) activeSession = anaSessionCountVB37.Third; else activeSession = anaSessionCountVB37.Second; } else activeSession = selectedSession; if (currentSession == anaSessionTypeVB37.ETH) rthOHL = false; else rthOHL = true; if (ShowCurrentOpen) Plots[0].Pen.Color = openColor; else Plots[0].Pen.Color = Color.Transparent; if (ShowHiLo) { Plots[1].Pen.Color = highColor; Plots[2].Pen.Color = lowColor; } else { Plots[1].Pen.Color = Color.Transparent; Plots[2].Pen.Color = Color.Transparent; } if (ShowFibonacci) { Plots[3].Pen.Color = midlineColor; Plots[4].Pen.Color = fibColor; Plots[5].Pen.Color = fibColor; Plots[6].Pen.Color = fibColor; Plots[7].Pen.Color = fibColor; } else { Plots[3].Pen.Color = Color.Transparent; Plots[4].Pen.Color = Color.Transparent; Plots[5].Pen.Color = Color.Transparent; Plots[6].Pen.Color = Color.Transparent; Plots[7].Pen.Color = Color.Transparent; } Plots[8].Name = rangePeriod1 + " Days"; Plots[9].Name = rangePeriod1 + " Days"; Plots[10].Name = rangePeriod2 + " Days"; Plots[11].Name = rangePeriod2 + " Days"; if (ShowVolatilityLabels) { Plots[8].Pen.Color = labelColor; Plots[9].Pen.Color = labelColor; Plots[10].Pen.Color = labelColor; Plots[11].Pen.Color = labelColor; } else { Plots[8].Pen.Color = Color.Transparent; Plots[9].Pen.Color = Color.Transparent; Plots[10].Pen.Color = Color.Transparent; Plots[11].Pen.Color = Color.Transparent; } Plots[0].Pen.Width = plot0Width; Plots[0].Pen.DashStyle = dash0Style; for (int i = 1; i < 3; i++) { Plots[i].Pen.Width = plot1Width; Plots[i].Pen.DashStyle = dash1Style; } for (int i = 3; i < 8; i++) { Plots[i].Pen.Width = plot3Width; Plots[i].Pen.DashStyle = dash3Style; } for (int i = 8; i< 12; i++) { Plots[i].Pen.Width = plot8Width; Plots[i].Pen.DashStyle = dash8Style; } if (Instrument.MasterInstrument.InstrumentType == Cbi.InstrumentType.Currency && (TickSize == 0.00001 || TickSize == 0.001)) displaySize = 5* TickSize; else displaySize = TickSize; if (AutoScale) AutoScale = false; rangePeriod = Math.Max(rangePeriod1, rangePeriod2); countDown = 0; dayCount = 0; } /// /// Called on each bar update event (incoming tick) /// protected override void OnBarUpdate() { if (Bars == null) return; if (!Data.BarsType.GetInstance(Bars.Period.Id).IsIntraday) { DrawTextFixed("errortag1", errorData1, TextPosition.Center, ChartControl.AxisColor, textFont, Color.Transparent,Color.Transparent,0); return; } if (CurrentBar == 0) { currentDate = GetLastBarSessionDate(Time[0], Bars, 0, pivotRangeType1); sessionCount = 1; return; } if(FirstTickOfBar) { if (periodOpen) { priorHigh = currentHigh; priorLow = currentLow; } if (bandType == anaBandTypeVB37.Stop && totalFreeze) { startBar = targetBar; upperBandColor = Color.Transparent; lowerBandColor = Color.Transparent; } else if (bandType == anaBandTypeVB37.Expand && totalFreeze) { startBar = targetBar; if(freezeLower1 && freezeLower2) upperBandColor = upperExpansionColor; if(freezeUpper1 && freezeUpper2) lowerBandColor = lowerExpansionColor; totalFreeze = false; } } DateTime lastBarTimeStamp1 = GetLastBarSessionDate(Time[0], Bars, 0, pivotRangeType1); if (lastBarTimeStamp1 != currentDate) { if (countDown == 0) { Bars.Session.GetNextBeginEnd(Bars, 0, out sessionBegin, out sessionEnd); plotStart = sessionBegin; countDown = -1; } sessionCount = 1; periodOpen = false; init = false; bandFactor = (double)bandWidth; upperBandColor = defaultBandColor; lowerBandColor = defaultBandColor; freezeUpper1 = false; freezeLower1 = false; freezeUpper2 = false; freezeLower2 = false; expandedBands = false; totalFreeze = false; target1Hit = false; target2Hit = false; if (!extendPublicHoliday) { dayCount = dayCount +1; for (int i = rangePeriod - 1; i>0; i--) { rangeArray[i] = rangeArray[i-1]; } if (dayCount > 1) rangeArray [0] = currentHigh - currentLow; if (dayCount > rangePeriod) { averageRange1 = 0.0; for (int i=0; i= currentHigh) { currentHigh = High[0]; highTime = Time[0]; } if (Low[0] <= currentLow) { currentLow = Low[0]; lowTime = Time[0]; } } } currentDate = lastBarTimeStamp1; plotOHL = true; } else if (Bars.FirstBarOfSession) { if (FirstTickOfBar) { sessionCount = sessionCount +1; numberOfSessions = Math.Min(3, Math.Max(sessionCount, numberOfSessions)); } if (!rthOHL) { if (High[0] >= currentHigh) { currentHigh = High[0]; highTime = Time[0]; } if (Low[0] <= currentLow) { currentLow = Low[0]; lowTime = Time[0]; } } else if (rthOHL && ((sessionCount == 1 && activeSession == anaSessionCountVB37.First) || (sessionCount == 2 && activeSession == anaSessionCountVB37.Second) || (sessionCount == 3 && activeSession == anaSessionCountVB37.Third))) { currentOpen = Open[0]; currentHigh = High[0]; currentLow = Low[0]; highTime = Time[0]; lowTime = Time[0]; startBar = CurrentBar - 1; periodOpen = true; init = true; } else periodOpen = false; } else if (periodOpen) { if (High[0] >= currentHigh) { currentHigh = High[0]; highTime = Time[0]; } if (Low[0] <= currentLow) { currentLow = Low[0]; lowTime = Time[0]; } } if (periodOpen && dayCount > rangePeriod && !totalFreeze) { if (adaptedRange1 > currentHigh - currentLow) { if (!freezeLower1) lowerTarget1 = currentHigh - adaptedRange1; if (!freezeUpper1) upperTarget1 = currentLow + adaptedRange1; } else if (!freezeLower1 || !freezeUpper1) { target1Hit = true; if(currentHigh == High[0] && currentLow == Low[0]) { lowerTarget1 = 0.5*(High[0]+ Low[0] - adaptedRange1); upperTarget1 = 0.5*(High[0]+ Low[0] + adaptedRange1); freezeUpper1 = true; freezeLower1 = true; } else if (currentHigh == High[0]) { lowerTarget1 = priorLow; if (!freezeUpper1) upperTarget1 = priorLow + adaptedRange1; freezeLower1 = true; } else if (currentLow == Low[0]) { if (!freezeLower1) lowerTarget1 = priorHigh - adaptedRange1; upperTarget1 = priorHigh; freezeUpper1 = true; } } if (adaptedRange2 > currentHigh - currentLow) { if (!freezeLower2) lowerTarget2 = currentHigh - adaptedRange2; if (!freezeUpper2) upperTarget2 = currentLow + adaptedRange2; } else if (!freezeLower2 || !freezeUpper2) { target2Hit = true; if(currentHigh == High[0] && currentLow == Low[0]) { lowerTarget2 = 0.5*(High[0]+ Low[0] - adaptedRange2); upperTarget2 = 0.5*(High[0]+ Low[0] + adaptedRange2); freezeUpper2 = true; freezeLower2 = true; } else if (currentHigh == High[0]) { lowerTarget2 = priorLow; if (!freezeUpper2) upperTarget2 = priorLow + adaptedRange2; freezeLower2 = true; } else if (currentLow == Low[0]) { if (!freezeLower2) lowerTarget2 = priorHigh - adaptedRange2; upperTarget2 = priorHigh; freezeUpper2 = true; } } } if (plotOHL && init && !(rthOHL && activeSession == anaSessionCountVB37.Third && numberOfSessions == 2) && !(rthOHL && !includeAfterSession && activeSession == anaSessionCountVB37.Second && sessionCount>2) && !(rthOHL && !includeAfterSession && activeSession == anaSessionCountVB37.First && sessionCount>1)) { CurrentOpen.Set(currentOpen); CurrentHigh.Set(currentHigh); CurrentLow.Set(currentLow); CurrentMidline.Set(Math.Round((currentLow + 0.5*(currentHigh-currentLow))/displaySize)* displaySize); if (lowTime >= highTime) { lowIsLast.Set(true); Fib786.Set(Math.Round((currentLow + 0.786*(currentHigh-currentLow))/displaySize)*displaySize); Fib618.Set(Math.Round((currentLow + 0.618*(currentHigh-currentLow))/displaySize)*displaySize); Fib382.Set(Math.Round((currentLow + 0.382*(currentHigh-currentLow))/displaySize)*displaySize); Fib236.Set(Math.Round((currentLow + 0.236*(currentHigh-currentLow))/displaySize)*displaySize); } else { lowIsLast.Set(false); Fib236.Set(Math.Round((currentHigh - 0.236*(currentHigh-currentLow))/displaySize)*displaySize); Fib382.Set(Math.Round((currentHigh - 0.382*(currentHigh-currentLow))/displaySize)*displaySize); Fib618.Set(Math.Round((currentHigh - 0.618*(currentHigh-currentLow))/displaySize)*displaySize); Fib786.Set(Math.Round((currentHigh - 0.786*(currentHigh-currentLow))/displaySize)*displaySize); } if (dayCount > rangePeriod) { DrawTextFixed("errortag3", "", TextPosition.Center, textBrush.Color, textFont, Color.Transparent,Color.Transparent,0); int k = Math.Max (1,(int) Math.Round (ATR(200)[0]/(6*displaySize))); if (Math.Abs(upperTarget1 - upperTarget2) < 2*k * displaySize && ChartControl != null) { double upperMidTarget = 0.5*(upperTarget1 + upperTarget2); if (upperTarget1 > upperTarget2) { upperTarget1 = Math.Round(upperMidTarget/displaySize + k)* displaySize; upperTarget2 = upperTarget1 - 2*k * displaySize; } else { upperTarget1 = Math.Round(upperMidTarget/displaySize - k)* displaySize; upperTarget2 = upperTarget1 + 2*k * displaySize; } } else { upperTarget1 = Math.Round(upperTarget1/displaySize)* displaySize; upperTarget2 = Math.Round(upperTarget2/displaySize)* displaySize; } if (Math.Abs(lowerTarget1 - lowerTarget2) < 2*k * displaySize && ChartControl != null) { double lowerMidTarget = 0.5*(lowerTarget1 + lowerTarget2); if (lowerTarget1 < lowerTarget2) { lowerTarget1 = Math.Round(lowerMidTarget/displaySize - k)* displaySize; lowerTarget2 = lowerTarget1 + 2*k * displaySize; } else { lowerTarget1 = Math.Round(lowerMidTarget/displaySize + k)* displaySize; lowerTarget2 = lowerTarget1 - 2*k * displaySize; } } else { lowerTarget1 = Math.Round(lowerTarget1/displaySize)* displaySize; lowerTarget2 = Math.Round(lowerTarget2/displaySize)* displaySize; } SARUp.Set (upperTarget1); SARDown.Set (lowerTarget1); LARUp.Set (upperTarget2); LARDown.Set (lowerTarget2); ADR0.Set (Math.Round((currentHigh - currentLow)/displaySize)* displaySize * displayFactor); ADR1.Set (Math.Round(averageRange1/displaySize)* displaySize * displayFactor); ADR2.Set (Math.Round(averageRange2/displaySize)* displaySize * displayFactor); upperRegionUpper.Set(Math.Max(upperTarget1, upperTarget2)); upperRegionLower.Set(Math.Min(upperTarget1, upperTarget2)); lowerRegionUpper.Set(Math.Max(lowerTarget1, lowerTarget2)); lowerRegionLower.Set(Math.Min(lowerTarget1, lowerTarget2)); if (target1Hit && target2Hit && !expandedBands) { targetBar = CurrentBar; if(freezeLower1 && freezeLower2) upperBandColor = upperTargetColor; if(freezeUpper1 && freezeUpper2) lowerBandColor = lowerTargetColor; if (bandType == anaBandTypeVB37.Expand) { expandedBands = true; bandFactor = bandWidthExpanded; adaptedRange1 = bandFactor * averageRange1 / 100.0; adaptedRange2 = bandFactor * averageRange2 / 100.0 ; } totalFreeze = true; target1Hit = false; target2Hit = false; } if (ShowVolatilityBands) { if (upperBandColor == Color.Transparent) DrawRegion("upper" + startBar, CurrentBar - startBar, 0, upperRegionUpper, upperRegionLower, Color.Transparent, upperBandColor, 0); else DrawRegion("upper" + startBar, CurrentBar - startBar, 0, upperRegionUpper, upperRegionLower, Color.Transparent, upperBandColor, Opacity); if (lowerBandColor == Color.Transparent) DrawRegion("lower" + startBar, CurrentBar - startBar, 0, lowerRegionUpper, lowerRegionLower,Color.Transparent, lowerBandColor, 0); else DrawRegion("lower" + startBar, CurrentBar - startBar, 0, lowerRegionUpper, lowerRegionLower,Color.Transparent, lowerBandColor, Opacity); } } else { string errorData3 = errorData3a + (rangePeriod+1) + errorData3b; DrawTextFixed("errortag3", errorData3, TextPosition.Center, ChartControl.AxisColor, textFont, Color.Transparent,Color.Transparent,0); ADR0.Set(0); ADR1.Set(0); ADR2.Set(0); SARUp.Set(0); SARDown.Set(0); LARUp.Set(0); LARDown.Set(0); } } else { CurrentHigh.Set(0); CurrentLow.Set(0); CurrentMidline.Set(0); CurrentOpen.Set(0); Fib786.Set(0); Fib618.Set(0); Fib382.Set(0); Fib236.Set(0); ADR0.Set(0); if (dayCount > rangePeriod && !(rthOHL && activeSession == anaSessionCountVB37.Third && numberOfSessions == 2)) { ADR1.Set (Math.Round(averageRange1/displaySize)* displaySize * displayFactor); ADR2.Set (Math.Round(averageRange2/displaySize)* displaySize * displayFactor); } else { if (rthOHL && activeSession == anaSessionCountVB37.Third && numberOfSessions == 2) DrawTextFixed("errortag3", errorData3c, TextPosition.Center, ChartControl.AxisColor, textFont, Color.Transparent,Color.Transparent,0); ADR1.Set(0); ADR2.Set(0); } SARUp.Set(0); SARDown.Set(0); LARUp.Set(0); LARDown.Set(0); } } #region Properties /// /// [Description("Session used for calculating OHL")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Session")] public anaSessionTypeVB37 CurrentSession { get { return currentSession; } set { currentSession = value; } } /// /// [Description("Session used for calculating OHL")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Session # for RTH")] public anaSessionCountVB37 SelectedSession { get { return selectedSession; } set { selectedSession = value; } } /// /// [Description("Option to show CurrentOpen")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Show Current Open")] public bool ShowCurrentOpen { get { return showCurrentOpen; } set { showCurrentOpen = value; } } [Description("Option to show Fibonacci Lines")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Show Dynamic Fibs")] public bool ShowFibonacci { get { return showFibonacci; } set { showFibonacci = value; } } [Description("Option to show current day's High and Low")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Show High/Low")] public bool ShowHiLo { get { return showHiLo; } set { showHiLo = value; } } [Description("Option to show volatility bands")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Show Projection Bands")] public bool ShowVolatilityBands { get { return showVolatilityBands; } set { showVolatilityBands = value; } } [Description("Option to show volatility band labels")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Show Projection Labels")] public bool ShowVolatilityLabels { get { return showVolatilityLabels; } set { showVolatilityLabels = value; } } [Description("Option to show current day's range and average ranges")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Show Range Data")] public anaPlotAlignVB37 ShowRangeData { get { return showRangeData; } set { showRangeData = value; } } [Description("Option to display indicators in RTH mode for the following sessions")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Include After Session")] public bool IncludeAfterSession { get { return includeAfterSession; } set { includeAfterSession = value; } } /// /// [Description("# of days used to calculate average daily range.")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Period(1) for Daily Range")] public int RangePeriod1 { get { return rangePeriod1; } set { rangePeriod1 = Math.Min(100,Math.Max(1, value)); } } /// /// [Description("# of days used to calculate average daily range.")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Period(2) for Daily Range")] public int RangePeriod2 { get { return rangePeriod2; } set { rangePeriod2 = Math.Min(100,Math.Max(1, value)); } } /// /// [Description("Percent of average daily range used to calculate the bands")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Defaut Band Range %")] public int BandWidth { get { return bandWidth; } set { bandWidth = Math.Min(400,Math.Max(10, value)); } } /// /// [Description("Percent of average daily range used to calculate the expanded bands after the target was hit")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Expanded Band Range %")] public int BandWidthExpanded { get { return bandWidthExpanded; } set { bandWidthExpanded = Math.Min(400,Math.Max(10, value)); } } /// /// [Description("Option to expand, freeze or stop plotting the volatility bands after they have been hit")] [GridCategory("Options")] [Gui.Design.DisplayNameAttribute("Band Type")] public anaBandTypeVB37 BandType { get { return bandType; } set { bandType = value; } } /// /// [Browsable(false)] [XmlIgnore] public DataSeries CurrentOpen { get { return Values[0]; } } /// /// [Browsable(false)] [XmlIgnore] public DataSeries CurrentHigh { get { return Values[1]; } } /// /// [Browsable(false)] [XmlIgnore] public DataSeries CurrentLow { get { return Values[2]; } } /// /// [Browsable(false)] [XmlIgnore] public DataSeries CurrentMidline { get { return Values[3]; } } /// /// [Browsable(false)] [XmlIgnore] public DataSeries Fib786 { get { return Values[4]; } } /// /// [Browsable(false)] [XmlIgnore] public DataSeries Fib618 { get { return Values[5]; } } /// /// [Browsable(false)] [XmlIgnore] public DataSeries Fib382 { get { return Values[6]; } } /// /// [Browsable(false)] [XmlIgnore] public DataSeries Fib236 { get { return Values[7]; } } /// /// [Browsable(false)] [XmlIgnore] public DataSeries SARUp { get { return Values[8]; } } /// /// [Browsable(false)] [XmlIgnore] public DataSeries SARDown { get { return Values[9]; } } /// /// [Browsable(false)] [XmlIgnore] public DataSeries LARUp { get { return Values[10]; } } /// /// [Browsable(false)] [XmlIgnore] public DataSeries LARDown { get { return Values[11]; } } /// /// [Description("Option where to plot labels")] [Category("Plot Parameters")] [Gui.Design.DisplayNameAttribute("Label Position")] public anaPlotAlignVB37 PlotLabels { get { return plotLabels; } set { plotLabels = value; } } /// /// [Description("Label distance from line.")] [Category("Plot Parameters")] [Gui.Design.DisplayNameAttribute("Label Offset")] public int LabelOffset { get { return labelOffset; } set { labelOffset = Math.Max(1, value); } } /// /// [Description("Length of the plots as # of bars.")] [Category("Plot Parameters")] [Gui.Design.DisplayNameAttribute("Max. Plot Length # Bars")] public int Width { get { return width; } set { width = Math.Max(0, value); } } /// /// [Description("Opacity of Ranges ")] [Category("Plot Parameters")] [Gui.Design.DisplayNameAttribute("Opacity Volatility Bands")] public int Opacity { get { return opacity; } set { opacity = Math.Min(10, Math.Max(0, value)); } } [XmlIgnore] [Description("Select Color for Current Open")] [Category("Plot Colors")] [Gui.Design.DisplayName("Color Current Open")] public Color OpenColor { get { return openColor; } set { openColor = value; } } // Serialize Color object [Browsable(false)] public string OpenColorSerialize { get { return NinjaTrader.Gui.Design.SerializableColor.ToString(openColor); } set { openColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); } } [XmlIgnore] [Description("Select Color for Current High")] [Category("Plot Colors")] [Gui.Design.DisplayName("Color Current High")] public Color HighColor { get { return highColor; } set { highColor = value; } } // Serialize Color object [Browsable(false)] public string HighColorSerialize { get { return NinjaTrader.Gui.Design.SerializableColor.ToString(highColor); } set { highColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); } } [XmlIgnore] [Description("Select Color for Current Low")] [Category("Plot Colors")] [Gui.Design.DisplayName("Color Current Low")] public Color LowColor { get { return lowColor; } set { lowColor = value; } } // Serialize Color object [Browsable(false)] public string LowColorSerialize { get { return NinjaTrader.Gui.Design.SerializableColor.ToString(lowColor); } set { lowColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); } } [XmlIgnore] [Description("Select Color for Current Midline")] [Category("Plot Colors")] [Gui.Design.DisplayName("Color Current Midline")] public Color MidlineColor { get { return midlineColor; } set { midlineColor = value; } } // Serialize Color object [Browsable(false)] public string MidlineColorSerialize { get { return NinjaTrader.Gui.Design.SerializableColor.ToString(midlineColor); } set { midlineColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); } } [XmlIgnore] [Description("Select Color for Dynamic Fibonacci Lines")] [Category("Plot Colors")] [Gui.Design.DisplayName("Color Fib Lines")] public Color FibColor { get { return fibColor; } set { fibColor = value; } } // Serialize Color object [Browsable(false)] public string FibColorSerialize { get { return NinjaTrader.Gui.Design.SerializableColor.ToString(fibColor); } set { fibColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); } } [XmlIgnore] [Description("Select Color for Volatility Labels")] [Category("Plot Colors")] [Gui.Design.DisplayName("Color Volatility Labels")] public Color LabelColor { get { return labelColor; } set { labelColor = value; } } // Serialize Color object [Browsable(false)] public string LabelColorSerialize { get { return NinjaTrader.Gui.Design.SerializableColor.ToString(labelColor); } set { labelColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); } } [XmlIgnore] [Description("Select Default Color for Volatility Bands")] [Category("Plot Colors")] [Gui.Design.DisplayName("Default Color for Bands")] public Color DefaultBandColor { get { return defaultBandColor; } set { defaultBandColor = value; } } // Serialize Color object [Browsable(false)] public string DefaultBandColorSerialize { get { return NinjaTrader.Gui.Design.SerializableColor.ToString(defaultBandColor); } set { defaultBandColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); } } [XmlIgnore] [Description("Select Target Color for Upper Band")] [Category("Plot Colors")] [Gui.Design.DisplayName("Target Color Upper Band")] public Color UpperTargetColor { get { return upperTargetColor; } set { upperTargetColor = value; } } // Serialize Color object [Browsable(false)] public string UpperTargetColorSerialize { get { return NinjaTrader.Gui.Design.SerializableColor.ToString(upperTargetColor); } set { upperTargetColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); } } [XmlIgnore] [Description("Select Target Color for Lower Band")] [Category("Plot Colors")] [Gui.Design.DisplayName("Target Color Lower Band")] public Color LowerTargetColor { get { return lowerTargetColor; } set { lowerTargetColor = value; } } // Serialize Color object [Browsable(false)] public string LowerTargetColorSerialize { get { return NinjaTrader.Gui.Design.SerializableColor.ToString(lowerTargetColor); } set { lowerTargetColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); } } [XmlIgnore] [Description("Select Expansion Color for Upper Band")] [Category("Plot Colors")] [Gui.Design.DisplayName("Expansion Color Upper Band")] public Color UpperExpansionColor { get { return upperExpansionColor; } set { upperExpansionColor = value; } } // Serialize Color object [Browsable(false)] public string UpperExpansionColorSerialize { get { return NinjaTrader.Gui.Design.SerializableColor.ToString(upperExpansionColor); } set { upperExpansionColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); } } [XmlIgnore] [Description("Select Expansion Color for Lower Band")] [Category("Plot Colors")] [Gui.Design.DisplayName("Expansion Color Lower Band")] public Color LowerExpansionColor { get { return lowerExpansionColor; } set { lowerExpansionColor = value; } } // Serialize Color object [Browsable(false)] public string LowerExpansionColorSerialize { get { return NinjaTrader.Gui.Design.SerializableColor.ToString(lowerExpansionColor); } set { lowerExpansionColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); } } /// /// [Description("Line Width for Current Open.")] [Category("Plot Parameters")] [Gui.Design.DisplayNameAttribute("Line Width for Current Open")] public int Plot0Width { get { return plot0Width; } set { plot0Width = Math.Max(1, value); } } /// /// [Description("DashStyle for Current Open.")] [Category("Plot Parameters")] [Gui.Design.DisplayNameAttribute("Dash Style Current Open")] public DashStyle Dash0Style { get { return dash0Style; } set { dash0Style = value; } } /// /// [Description("Line Width for High and Low.")] [Category("Plot Parameters")] [Gui.Design.DisplayNameAttribute("Line Width High & Low")] public int Plot1Width { get { return plot1Width; } set { plot1Width = Math.Max(1, value); } } /// /// [Description("DashStyle for High and Low.")] [Category("Plot Parameters")] [Gui.Design.DisplayNameAttribute("Dash Style High & Low")] public DashStyle Dash1Style { get { return dash1Style; } set { dash1Style = value; } } /// /// [Description("Width for Fib Lines.")] [Category("Plot Parameters")] [Gui.Design.DisplayNameAttribute("Line Width Fib Lines")] public int Plot3Width { get { return plot3Width; } set { plot3Width = Math.Max(1, value); } } /// /// [Description("DashStyle for Fib Lines.")] [Category("Plot Parameters")] [Gui.Design.DisplayNameAttribute("Dash Style Fib Lines")] public DashStyle Dash3Style { get { return dash3Style; } set { dash3Style = value; } } /// /// [Description("Line Width for Volatility Bands.")] [Category("Plot Parameters")] [Gui.Design.DisplayNameAttribute("Line Width Volatility Labels")] public int Plot8Width { get { return plot8Width; } set { plot8Width = Math.Max(1, value); } } /// /// [Description("DashStyle for Volatility Bands.")] [Category("Plot Parameters")] [Gui.Design.DisplayNameAttribute("Dash Style Volatility Labels")] public DashStyle Dash8Style { get { return dash8Style; } set { dash8Style = value; } } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 01 /No Trade Date")] public DateTime PublicHoliday0 { get { return publicHoliday0;} set { publicHoliday0 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 02 /No Trade Date")] public DateTime PublicHoliday1 { get { return publicHoliday1;} set { publicHoliday1 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 03 /No Trade Date")] public DateTime PublicHoliday2 { get { return publicHoliday2;} set { publicHoliday2 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 04 /No Trade Date")] public DateTime PublicHoliday3 { get { return publicHoliday3;} set { publicHoliday3 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 05 /No Trade Date")] public DateTime PublicHoliday4 { get { return publicHoliday4;} set { publicHoliday4 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 06 /No Trade Date")] public DateTime PublicHoliday5 { get { return publicHoliday5;} set { publicHoliday5 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 07 /No Trade Date")] public DateTime PublicHoliday6 { get { return publicHoliday6;} set { publicHoliday6 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 08 /No Trade Date")] public DateTime PublicHoliday7 { get { return publicHoliday7;} set { publicHoliday7 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 09 /No Trade Date")] public DateTime PublicHoliday8 { get { return publicHoliday8;} set { publicHoliday8 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 10 /No Trade Date")] public DateTime PublicHoliday9 { get { return publicHoliday9;} set { publicHoliday9 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 11 /No Trade Date")] public DateTime PublicHoliday10 { get { return publicHoliday10;} set { publicHoliday10 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 12 /No Trade Date")] public DateTime PublicHoliday11 { get { return publicHoliday11;} set { publicHoliday11 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 13 /No Trade Date")] public DateTime PublicHoliday12 { get { return publicHoliday12;} set { publicHoliday12 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 14 /No Trade Date")] public DateTime PublicHoliday13 { get { return publicHoliday13;} set { publicHoliday13 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 15 /No Trade Date")] public DateTime PublicHoliday14 { get { return publicHoliday14;} set { publicHoliday14 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 16 /No Trade Date")] public DateTime PublicHoliday15 { get { return publicHoliday15;} set { publicHoliday15 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 17 /No Trade Date")] public DateTime PublicHoliday16 { get { return publicHoliday16;} set { publicHoliday16 = value;} } /// [Description("Enter dates for public holidays with no session close.")] [Category("Settlement Next Day For Globex")] [Gui.Design.DisplayNameAttribute("Holiday 18 /No Trade Date")] public DateTime PublicHoliday17 { get { return publicHoliday17;} set { publicHoliday17 = value;} } #endregion #region Miscellaneous public override string FormatPriceMarker(double price) { double trunc = Math.Truncate(price); int fraction = Convert.ToInt32(320 * Math.Abs(price - trunc) - 0.0001); // rounding down for ZF and ZT string priceMarker = ""; if (TickSize == 0.03125) { fraction = fraction/10; if (fraction < 10) priceMarker = trunc.ToString() + "'0" + fraction.ToString(); else priceMarker = trunc.ToString() + "'" + fraction.ToString(); } else if (TickSize == 0.015625 || TickSize == 0.0078125) { if (fraction < 10) priceMarker = trunc.ToString() + "'00" + fraction.ToString(); else if (fraction < 100) priceMarker = trunc.ToString() + "'0" + fraction.ToString(); else priceMarker = trunc.ToString() + "'" + fraction.ToString(); } else priceMarker = price.ToString(Gui.Globals.GetTickFormatString(TickSize)); return priceMarker; } private DateTime GetLastBarSessionDate(DateTime time, Data.Bars bars, int barsAgo, PivotRange pivotRange) { if (time > cacheSessionEndTmp) { extendPublicHoliday = false; for (int i =0; i<18; i++) { if (publicHoliday[i].Date == sessionDateTmp) extendPublicHoliday = true; } if (Bars.BarsType.IsIntraday) sessionDateTmp = Bars.GetTradingDayFromLocal(time); else sessionDateTmp = time.Date; if(cacheSessionDate != sessionDateTmp || (rthOHL && sessionCount == 1 && activeSession == anaSessionCountVB37.Second) || (rthOHL && sessionCount ==2 && activeSession == anaSessionCountVB37.Third)) { cacheSessionDate = sessionDateTmp; if ((!extendPublicHoliday || rthOHL) && (newSessionBarIdxArr1.Count == 0 || (newSessionBarIdxArr1.Count > 0 && CurrentBar > (int) newSessionBarIdxArr1[newSessionBarIdxArr1.Count - 1]))) newSessionBarIdxArr1.Add(CurrentBar); } Bars.Session.GetNextBeginEnd(bars, barsAgo, out cacheSessionBeginTmp, out cacheSessionEndTmp); } return sessionDateTmp; } internal void InvalidateNow() { if (Disposed || ChartControl == null) return; ChartControl.Invalidate(true); } /// /// Overload this method to handle the termination of an indicator. Use this method to dispose of any resources vs overloading the Dispose() method. /// protected override void OnTermination() { textBrush.Dispose(); foreach (SolidBrush solidBrush in brushes) solidBrush.Dispose(); stringFormatCenter.Dispose(); stringFormatFar.Dispose(); stringFormatNear.Dispose(); } /// /// /// /// /// /// public override void Plot(Graphics graphics, Rectangle bounds, double min, double max) { if (Bars == null || ChartControl == null) return; // plot error if data not complete DateTime lastBarSessionBegin = Cbi.Globals.MinDate; DateTime lastBarSessionEnd = Cbi.Globals.MinDate; if (ChartControl.EquidistantBars) Bars.Session.GetNextBeginEnd(Bars.Get(Math.Min(Bars.Count - 1, ChartControl.LastBarPainted)).Time, out lastBarSessionBegin, out lastBarSessionEnd); else Bars.Session.GetNextBeginEnd(Bars.Get(Math.Min(Bars.Count - 1, Bars.GetBar(ChartControl.LastBarTimePainted))).Time, out lastBarSessionBegin, out lastBarSessionEnd); textBrush.Color = ChartControl.AxisColor; SizeF errorSize = graphics.MeasureString(errorData2, textFont); errorTextWidth = errorSize.Width + 5; errorTextHeight = errorSize.Height + 5; if (Bars.Count > 0 && plotStart > lastBarSessionEnd) graphics.DrawString(errorData2, textFont, textBrush, bounds.X + bounds.Width - errorTextWidth, bounds.Y + bounds.Height - errorTextHeight, stringFormatNear); bool retraceFromLow = lowIsLast.Get(this.LastBarIndexPainted); if (ShowFibonacci) { if (retraceFromLow) { Plots[4].Name = "D-78.6"; Plots[5].Name = "D-61.8"; Plots[6].Name = "D-38.2"; Plots[7].Name = "D-23.6"; } else { Plots[4].Name = "D-23.6"; Plots[5].Name = "D-38.2"; Plots[6].Name = "D-61.8"; Plots[7].Name = "D-78.6"; } } double averageDR0 = ADR0.Get(this.LastBarIndexPainted); double averageDR1 = ADR1.Get(this.LastBarIndexPainted); double averageDR2 = ADR2.Get(this.LastBarIndexPainted); string rangeData0 = "Current Day Range = "; string rangeData1 = "ADR [" + rangePeriod1 + "] = "; string rangeData2 = "ADR [" + rangePeriod2 + "] = "; if (TickSize == 0.03125) { double trunc0 = Math.Truncate(averageDR0); double trunc1 = Math.Truncate(averageDR1); double trunc2 = Math.Truncate(averageDR2); int fraction0 = Convert.ToInt32(32 * Math.Abs(averageDR0 - trunc0) -0.0001); int fraction1 = Convert.ToInt32(32 * Math.Abs(averageDR1 - trunc1) -0.0001); int fraction2 = Convert.ToInt32(32 * Math.Abs(averageDR2 - trunc2) -0.0001); if (fraction0 < 10) rangeData0 = rangeData0 + trunc0.ToString() + "'0" + fraction0.ToString(); else rangeData0 = rangeData0 + trunc0.ToString() + "'" + fraction0.ToString(); if (fraction1 < 10) rangeData1 = rangeData1 + trunc1.ToString() + "'0" + fraction1.ToString(); else rangeData1 = rangeData1 + trunc1.ToString() + "'" + fraction1.ToString(); if (fraction2 < 10) rangeData2 = rangeData2 + trunc2.ToString() + "'0" + fraction2.ToString(); else rangeData2 = rangeData2 + trunc2.ToString() + "'" + fraction2.ToString(); } else if (TickSize == 0.015625 || TickSize == 0.0078125) { double trunc0 = Math.Truncate(averageDR0); double trunc1 = Math.Truncate(averageDR1); double trunc2 = Math.Truncate(averageDR2); int fraction0 = Convert.ToInt32(320 * Math.Abs(averageDR0 - trunc0) -0.0001); int fraction1 = Convert.ToInt32(320 * Math.Abs(averageDR1 - trunc1) -0.0001); int fraction2 = Convert.ToInt32(320 * Math.Abs(averageDR2 - trunc2) -0.0001); if (fraction0 < 10) rangeData0 = rangeData0 + trunc0.ToString() + "'00" + fraction0.ToString(); else if (fraction0 < 100) rangeData0 = rangeData0 + trunc0.ToString() + "'0" + fraction0.ToString(); else rangeData0 = rangeData0 + trunc0.ToString() + "'" + fraction0.ToString(); if (fraction1 < 10) rangeData1 = rangeData1 + trunc1.ToString() + "'00" + fraction1.ToString(); else if (fraction1 < 100) rangeData1 = rangeData1 + trunc1.ToString() + "'0" + fraction1.ToString(); else rangeData1 = rangeData1 + trunc1.ToString() + "'" + fraction1.ToString(); if (fraction2 < 10) rangeData2 = rangeData2 + trunc2.ToString() + "'00" + fraction2.ToString(); else if (fraction2 < 100) rangeData2 = rangeData2 + trunc2.ToString() + "'0" + fraction2.ToString(); else rangeData2 = rangeData2 + trunc2.ToString() + "'" + fraction2.ToString(); } else if (isCurrency) { rangeData0 = rangeData0 + averageDR0.ToString(); rangeData1 = rangeData1 + averageDR1.ToString(); rangeData2 = rangeData2 + averageDR2.ToString(); } else { rangeData0 = rangeData0 + averageDR0.ToString(Gui.Globals.GetTickFormatString(TickSize)); rangeData1 = rangeData1 + averageDR1.ToString(Gui.Globals.GetTickFormatString(TickSize)); rangeData2 = rangeData2 + averageDR2.ToString(Gui.Globals.GetTickFormatString(TickSize)); } if (showRangeData == anaPlotAlignVB37.Left) { graphics.DrawString(rangeData0, textFont, textBrush, bounds.X + 25, bounds.Y + textFont.Height + 20, stringFormatNear); graphics.DrawString(rangeData1, textFont, textBrush, bounds.X + 25, bounds.Y + 2*(textFont.Height) + 25, stringFormatNear); graphics.DrawString(rangeData2, textFont, textBrush, bounds.X + 25, bounds.Y + 3*(textFont.Height) + 30, stringFormatNear); } else if (showRangeData == anaPlotAlignVB37.Right) { SizeF rangeSize = graphics.MeasureString(rangeData0, textFont); graphics.DrawString(rangeData0, textFont, textBrush, bounds.X + bounds.Width - rangeSize.Width - 25, bounds.Y + textFont.Height + 20, stringFormatNear); graphics.DrawString(rangeData1, textFont, textBrush, bounds.X + bounds.Width - rangeSize.Width - 25, bounds.Y + 2*(textFont.Height) + 25, stringFormatNear); graphics.DrawString(rangeData2, textFont, textBrush, bounds.X + bounds.Width - rangeSize.Width - 25, bounds.Y + 3*(textFont.Height) + 30, stringFormatNear); } int firstBarIdxToPaint = -1; for (int i = newSessionBarIdxArr1.Count - 1; i >= 0; i--) { int prevSessionBreakIdx = (int) newSessionBarIdxArr1[i]; if (prevSessionBreakIdx <= this.LastBarIndexPainted) { firstBarIdxToPaint = prevSessionBreakIdx; break; } } for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++) { SolidBrush brush = brushes[seriesCount]; int firstX = -1; int lastX = -1; int lastY = -1; SmoothingMode oldSmoothingMode = graphics.SmoothingMode; Gui.Chart.Plot plot = Plots[seriesCount]; DataSeries series = (DataSeries) Values[seriesCount]; using (GraphicsPath path = new GraphicsPath()) { if (brush.Color != plot.Pen.Color) brush = new SolidBrush(plot.Pen.Color); for (int idx = this.LastBarIndexPainted; idx >= Math.Max(this.FirstBarIndexPainted, this.LastBarIndexPainted - Width); idx--) { if (idx - Displacement < 0 || idx - Displacement >= Bars.Count || (!ChartControl.ShowBarsRequired && idx - Displacement < BarsRequired)) continue; else if (!series.IsValidPlot(idx)) continue; if (idx < firstBarIdxToPaint) break; double val = series.Get(idx); int x = ChartControl.GetXByBarIdx(BarsArray[0], idx); int y = ChartControl.GetYByValue(this, val); if (lastX >= 0) { if (y != lastY) // Problem here is, that last bar of old day has date of new day y = lastY; path.AddLine(lastX - plot.Pen.Width / 2, lastY, x - plot.Pen.Width / 2, y); } lastX = x; lastY = y; if (idx == this.LastBarIndexPainted) firstX = x; } graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.DrawPath(plot.Pen, path); graphics.SmoothingMode = oldSmoothingMode; if (PlotLabels == anaPlotAlignVB37.Right) graphics.DrawString(plot.Name, ChartControl.Font, brush, firstX + LabelOffset, lastY - ChartControl.Font.GetHeight() / 2, stringFormatNear); if (PlotLabels == anaPlotAlignVB37.Left) graphics.DrawString(plot.Name, ChartControl.Font, brush, lastX - LabelOffset, lastY - ChartControl.Font.GetHeight() / 2, stringFormatFar); } } } private DateTime RoundUpTimeToPeriodTime(DateTime time, PivotRange pivotRange) { if (pivotRange == PivotRange.Weekly) { DateTime periodStart = time.AddDays((6 - (((int) time.DayOfWeek) + 1) % 7)); return periodStart.Date.AddDays(System.Math.Ceiling(System.Math.Ceiling(time.Date.Subtract(periodStart.Date).TotalDays) / 7) * 7).Date; } else if (pivotRange == PivotRange.Monthly) { DateTime result = new DateTime(time.Year, time.Month, 1); return result.AddMonths(1).AddDays(-1); } else return time; } #endregion } } #region NinjaScript generated code. Neither change nor remove. // This namespace holds all indicators and is required. Do not change it. namespace NinjaTrader.Indicator { public partial class Indicator : IndicatorBase { private anaVolatilityBandsV37[] cacheanaVolatilityBandsV37 = null; private static anaVolatilityBandsV37 checkanaVolatilityBandsV37 = new anaVolatilityBandsV37(); /// /// anaVolatilityBandsV37. /// /// public anaVolatilityBandsV37 anaVolatilityBandsV37(anaBandTypeVB37 bandType, int bandWidth, int bandWidthExpanded, anaSessionTypeVB37 currentSession, bool includeAfterSession, int rangePeriod1, int rangePeriod2, anaSessionCountVB37 selectedSession, bool showCurrentOpen, bool showFibonacci, bool showHiLo, anaPlotAlignVB37 showRangeData, bool showVolatilityBands, bool showVolatilityLabels) { return anaVolatilityBandsV37(Input, bandType, bandWidth, bandWidthExpanded, currentSession, includeAfterSession, rangePeriod1, rangePeriod2, selectedSession, showCurrentOpen, showFibonacci, showHiLo, showRangeData, showVolatilityBands, showVolatilityLabels); } /// /// anaVolatilityBandsV37. /// /// public anaVolatilityBandsV37 anaVolatilityBandsV37(Data.IDataSeries input, anaBandTypeVB37 bandType, int bandWidth, int bandWidthExpanded, anaSessionTypeVB37 currentSession, bool includeAfterSession, int rangePeriod1, int rangePeriod2, anaSessionCountVB37 selectedSession, bool showCurrentOpen, bool showFibonacci, bool showHiLo, anaPlotAlignVB37 showRangeData, bool showVolatilityBands, bool showVolatilityLabels) { if (cacheanaVolatilityBandsV37 != null) for (int idx = 0; idx < cacheanaVolatilityBandsV37.Length; idx++) if (cacheanaVolatilityBandsV37[idx].BandType == bandType && cacheanaVolatilityBandsV37[idx].BandWidth == bandWidth && cacheanaVolatilityBandsV37[idx].BandWidthExpanded == bandWidthExpanded && cacheanaVolatilityBandsV37[idx].CurrentSession == currentSession && cacheanaVolatilityBandsV37[idx].IncludeAfterSession == includeAfterSession && cacheanaVolatilityBandsV37[idx].RangePeriod1 == rangePeriod1 && cacheanaVolatilityBandsV37[idx].RangePeriod2 == rangePeriod2 && cacheanaVolatilityBandsV37[idx].SelectedSession == selectedSession && cacheanaVolatilityBandsV37[idx].ShowCurrentOpen == showCurrentOpen && cacheanaVolatilityBandsV37[idx].ShowFibonacci == showFibonacci && cacheanaVolatilityBandsV37[idx].ShowHiLo == showHiLo && cacheanaVolatilityBandsV37[idx].ShowRangeData == showRangeData && cacheanaVolatilityBandsV37[idx].ShowVolatilityBands == showVolatilityBands && cacheanaVolatilityBandsV37[idx].ShowVolatilityLabels == showVolatilityLabels && cacheanaVolatilityBandsV37[idx].EqualsInput(input)) return cacheanaVolatilityBandsV37[idx]; lock (checkanaVolatilityBandsV37) { checkanaVolatilityBandsV37.BandType = bandType; bandType = checkanaVolatilityBandsV37.BandType; checkanaVolatilityBandsV37.BandWidth = bandWidth; bandWidth = checkanaVolatilityBandsV37.BandWidth; checkanaVolatilityBandsV37.BandWidthExpanded = bandWidthExpanded; bandWidthExpanded = checkanaVolatilityBandsV37.BandWidthExpanded; checkanaVolatilityBandsV37.CurrentSession = currentSession; currentSession = checkanaVolatilityBandsV37.CurrentSession; checkanaVolatilityBandsV37.IncludeAfterSession = includeAfterSession; includeAfterSession = checkanaVolatilityBandsV37.IncludeAfterSession; checkanaVolatilityBandsV37.RangePeriod1 = rangePeriod1; rangePeriod1 = checkanaVolatilityBandsV37.RangePeriod1; checkanaVolatilityBandsV37.RangePeriod2 = rangePeriod2; rangePeriod2 = checkanaVolatilityBandsV37.RangePeriod2; checkanaVolatilityBandsV37.SelectedSession = selectedSession; selectedSession = checkanaVolatilityBandsV37.SelectedSession; checkanaVolatilityBandsV37.ShowCurrentOpen = showCurrentOpen; showCurrentOpen = checkanaVolatilityBandsV37.ShowCurrentOpen; checkanaVolatilityBandsV37.ShowFibonacci = showFibonacci; showFibonacci = checkanaVolatilityBandsV37.ShowFibonacci; checkanaVolatilityBandsV37.ShowHiLo = showHiLo; showHiLo = checkanaVolatilityBandsV37.ShowHiLo; checkanaVolatilityBandsV37.ShowRangeData = showRangeData; showRangeData = checkanaVolatilityBandsV37.ShowRangeData; checkanaVolatilityBandsV37.ShowVolatilityBands = showVolatilityBands; showVolatilityBands = checkanaVolatilityBandsV37.ShowVolatilityBands; checkanaVolatilityBandsV37.ShowVolatilityLabels = showVolatilityLabels; showVolatilityLabels = checkanaVolatilityBandsV37.ShowVolatilityLabels; if (cacheanaVolatilityBandsV37 != null) for (int idx = 0; idx < cacheanaVolatilityBandsV37.Length; idx++) if (cacheanaVolatilityBandsV37[idx].BandType == bandType && cacheanaVolatilityBandsV37[idx].BandWidth == bandWidth && cacheanaVolatilityBandsV37[idx].BandWidthExpanded == bandWidthExpanded && cacheanaVolatilityBandsV37[idx].CurrentSession == currentSession && cacheanaVolatilityBandsV37[idx].IncludeAfterSession == includeAfterSession && cacheanaVolatilityBandsV37[idx].RangePeriod1 == rangePeriod1 && cacheanaVolatilityBandsV37[idx].RangePeriod2 == rangePeriod2 && cacheanaVolatilityBandsV37[idx].SelectedSession == selectedSession && cacheanaVolatilityBandsV37[idx].ShowCurrentOpen == showCurrentOpen && cacheanaVolatilityBandsV37[idx].ShowFibonacci == showFibonacci && cacheanaVolatilityBandsV37[idx].ShowHiLo == showHiLo && cacheanaVolatilityBandsV37[idx].ShowRangeData == showRangeData && cacheanaVolatilityBandsV37[idx].ShowVolatilityBands == showVolatilityBands && cacheanaVolatilityBandsV37[idx].ShowVolatilityLabels == showVolatilityLabels && cacheanaVolatilityBandsV37[idx].EqualsInput(input)) return cacheanaVolatilityBandsV37[idx]; anaVolatilityBandsV37 indicator = new anaVolatilityBandsV37(); indicator.BarsRequired = BarsRequired; indicator.CalculateOnBarClose = CalculateOnBarClose; #if NT7 indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256; indicator.MaximumBarsLookBack = MaximumBarsLookBack; #endif indicator.Input = input; indicator.BandType = bandType; indicator.BandWidth = bandWidth; indicator.BandWidthExpanded = bandWidthExpanded; indicator.CurrentSession = currentSession; indicator.IncludeAfterSession = includeAfterSession; indicator.RangePeriod1 = rangePeriod1; indicator.RangePeriod2 = rangePeriod2; indicator.SelectedSession = selectedSession; indicator.ShowCurrentOpen = showCurrentOpen; indicator.ShowFibonacci = showFibonacci; indicator.ShowHiLo = showHiLo; indicator.ShowRangeData = showRangeData; indicator.ShowVolatilityBands = showVolatilityBands; indicator.ShowVolatilityLabels = showVolatilityLabels; Indicators.Add(indicator); indicator.SetUp(); anaVolatilityBandsV37[] tmp = new anaVolatilityBandsV37[cacheanaVolatilityBandsV37 == null ? 1 : cacheanaVolatilityBandsV37.Length + 1]; if (cacheanaVolatilityBandsV37 != null) cacheanaVolatilityBandsV37.CopyTo(tmp, 0); tmp[tmp.Length - 1] = indicator; cacheanaVolatilityBandsV37 = tmp; return indicator; } } } } // This namespace holds all market analyzer column definitions and is required. Do not change it. namespace NinjaTrader.MarketAnalyzer { public partial class Column : ColumnBase { /// /// anaVolatilityBandsV37. /// /// [Gui.Design.WizardCondition("Indicator")] public Indicator.anaVolatilityBandsV37 anaVolatilityBandsV37(anaBandTypeVB37 bandType, int bandWidth, int bandWidthExpanded, anaSessionTypeVB37 currentSession, bool includeAfterSession, int rangePeriod1, int rangePeriod2, anaSessionCountVB37 selectedSession, bool showCurrentOpen, bool showFibonacci, bool showHiLo, anaPlotAlignVB37 showRangeData, bool showVolatilityBands, bool showVolatilityLabels) { return _indicator.anaVolatilityBandsV37(Input, bandType, bandWidth, bandWidthExpanded, currentSession, includeAfterSession, rangePeriod1, rangePeriod2, selectedSession, showCurrentOpen, showFibonacci, showHiLo, showRangeData, showVolatilityBands, showVolatilityLabels); } /// /// anaVolatilityBandsV37. /// /// public Indicator.anaVolatilityBandsV37 anaVolatilityBandsV37(Data.IDataSeries input, anaBandTypeVB37 bandType, int bandWidth, int bandWidthExpanded, anaSessionTypeVB37 currentSession, bool includeAfterSession, int rangePeriod1, int rangePeriod2, anaSessionCountVB37 selectedSession, bool showCurrentOpen, bool showFibonacci, bool showHiLo, anaPlotAlignVB37 showRangeData, bool showVolatilityBands, bool showVolatilityLabels) { return _indicator.anaVolatilityBandsV37(input, bandType, bandWidth, bandWidthExpanded, currentSession, includeAfterSession, rangePeriod1, rangePeriod2, selectedSession, showCurrentOpen, showFibonacci, showHiLo, showRangeData, showVolatilityBands, showVolatilityLabels); } } } // This namespace holds all strategies and is required. Do not change it. namespace NinjaTrader.Strategy { public partial class Strategy : StrategyBase { /// /// anaVolatilityBandsV37. /// /// [Gui.Design.WizardCondition("Indicator")] public Indicator.anaVolatilityBandsV37 anaVolatilityBandsV37(anaBandTypeVB37 bandType, int bandWidth, int bandWidthExpanded, anaSessionTypeVB37 currentSession, bool includeAfterSession, int rangePeriod1, int rangePeriod2, anaSessionCountVB37 selectedSession, bool showCurrentOpen, bool showFibonacci, bool showHiLo, anaPlotAlignVB37 showRangeData, bool showVolatilityBands, bool showVolatilityLabels) { return _indicator.anaVolatilityBandsV37(Input, bandType, bandWidth, bandWidthExpanded, currentSession, includeAfterSession, rangePeriod1, rangePeriod2, selectedSession, showCurrentOpen, showFibonacci, showHiLo, showRangeData, showVolatilityBands, showVolatilityLabels); } /// /// anaVolatilityBandsV37. /// /// public Indicator.anaVolatilityBandsV37 anaVolatilityBandsV37(Data.IDataSeries input, anaBandTypeVB37 bandType, int bandWidth, int bandWidthExpanded, anaSessionTypeVB37 currentSession, bool includeAfterSession, int rangePeriod1, int rangePeriod2, anaSessionCountVB37 selectedSession, bool showCurrentOpen, bool showFibonacci, bool showHiLo, anaPlotAlignVB37 showRangeData, bool showVolatilityBands, bool showVolatilityLabels) { if (InInitialize && input == null) throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method"); return _indicator.anaVolatilityBandsV37(input, bandType, bandWidth, bandWidthExpanded, currentSession, includeAfterSession, rangePeriod1, rangePeriod2, selectedSession, showCurrentOpen, showFibonacci, showHiLo, showRangeData, showVolatilityBands, showVolatilityLabels); } } } #endregion