#region Documentaiton { Indicator: _LRChanArray Description: Draws linear regression channels with user specified Offset and Length. Uses array to avoid problems wtih MaxBarsBack being exceeded when user moves trend line manually. Uses timer to throttle frequency of checking for user moving trend line for improved efficiency. User may drag the center trend line of the LR Channel to a new location and this indicator will redraw the associated deviation lines in the new location. Notes: USE "Update value intra-bar (tick by tick)" when formatting indicator. Code has been designed to work efficiently in this mode and using this mode causes correct positioning of channel for both real time data and historical data. Input parameter FreezeBeginPoint prevents the beginning point of the linear regression channel from moving once the indicator is applied to a chart, or from the time the user moves the indicator using the "user interactive" feature by dragging of the center trend line. Often _LinRegChan is used to monitor price action within a trend. A trend often begins at a pivot high or pivot low in the chart. If a user moves the beginning point of _LinRegChan to the point coinciding with the beginning of a trend, it is often desirable that this beginning point NOT be moved each time a new bar is added to the chart. Setting FreezeBeginPoint = true prevents the beginning point of the regression channel from shifting one bar to the right each time a new bar is added to the chart. The "length" of the channel therefore increases by one bar as each new bar is added to the chart. Setting FreezeBeginPoint = false causes the linear regression channel to shift one bar to the right (both begin and end points) as each new bar is added to the chart. To coordinate results with FxDeviation indicator on same chart, LinRegChan should use same offset but TgtBar should be left = 0. On FxDeviation, TgtBar = - Offset. LinRegChan.OOEL does not need to update each tick. The timer will generate a signal to check if The user has moved any trend lines intrabar. This is different from the non-OOEL version, LinRegChan, which DOES have to be formatted "Update each tick" for The user-interative functions To work. Alerts can be set on any of the deviation bands within the channel. The alert triggered is NOT The alert formatting of the _LRChan indicator. Instead it will be the preference setting Tradestation Message Center. This is because the alert is set on the trend lines that make up the deviation bands, and not by the EL reserved word ALERT. Warning: Maximum bars study will reference must be formatted for at least the number of bars visible in the chart window without scrolling PLUS the length of the center trend line. A variable defined in this routine, ForceMaxBarsBack, will force the maximum bars the study will reference to be at least 300 by default. If this indicator is used on a chart with more than 200 bars visible in the chart window (without scrolling), then the user should manually set "Maximum bars study will reference" to at least the number of bars that are visible in the chart window. The indicator will not perform corectly if used on tick or share bars when more than One bar has the same Date-Time stamp. This is because trend lines are positioned by declaring the Date-Time the TL begins and the Date-Time the TL ends. Author: MarkSanDiego Updated: 07/17/09 original version 08/06/09 move drawing code to function that both strategy and indicator can call 09/06/09 add user interactive feature 10/12/09 fixed bug in user interactive feature 02/05/10 fixed Marker_ID value assignment 04/30/10 allow user to specify if vertical marker will be drawn at offset point 07/30/10 Channel would not always follow center line when moved by user. Corrected. Force maximum bars back to be at least as many bars as visible on chart. 08/07/10 Corrected failure of distance between channel lines to recalculate when user moved center TL 08/15/10 Corrected failure of deviations lines to occasionally draw incorrectly after center line moved by user. 09/05/10 add ability to freeze beginning point of channel to startup default or any position the user moves the starting point to subsequently. 09/07/10 added error trapping with message if MaxBarsBack must be increased 10/13/10 fixed bug causing beginning point of trendline to move one bar to the left with each new bar instead of remaining "frozen" at original location when FreezeBeginPoint = true 03/15/11 Timer section converted to OOEL for improved efficiency. 05/09/11 Bug fixed in function _DrawLRChan 05/12/11 Removed initial drawing of indicator when LastBar was reached. This is unnecessary Using OOEL Timer code. 05/12/11 Bug fix. Some functions called by _DrawLRChan had variables that were not intrabarpersist. This caused erratic calculation of the distance between the parallel deviation lines. 05/12/11 Allow user to extend all trend lines except the center trend line to the right. Not extending the center TL makes it easier for The user to see which bars are included in the linear regression line calculation. 05/13/11 Add alerts to price crossing outer most deviation bands. When this feature is turned on, The alert band color will change to AlertBandColor. 05/18/11 Bug fix. With input parameter FreezeBeginPoint = false, channel would not advance one bar each time a new bar printed on chart. 07/11/11 Bug fix. FreezeBeginPoint was not working reliably. Logic changes corrected the bug. 07/14/11 Non-recursive functions used in deviation calculations so correct values are obtained if length of center trend line changed by user interactively. 07/14/11 Convert function DrawLRChan to a local Method for greater efficiency and to avoid passing large numbers of parameters to function. 09/20/11 Add marker dot at beginning and end of centerline. These markers remind user where beginning And end of analysis period is and also provide easy to see markers for dragging the ends of The center line. 09/28/11 Improve alert band feature to allow user to specify band number that will trigger alert. 12/03/11 Added BeginDate_YYYYMMDD. If user specifies, This over rides the begin date calculated from length and offset input parameters. Useful if user always wants LR channel to start on a specified date, such as the date of a major pivot point in the price action. 12/17/11 Remove input parameter LRTgtBar, since normally will always be zero. 03/02/12 Extend deviation lines to current bar. Keep center line end point at Offset to show user Range over which linear regression data is being calculated. Allow user to format indicator As "Update value intra-Bar (ticks by tick)" so deviation lines extend right up to current bar. This allows user to more easily see exactly where the deviation is for the current bar. A conditional IF In the main program prevents execution of the code with each tick for greater efficiency. 04/12/12 fix calculation so user can move center line endpoint such that EndBar will be less than input parameter Offset. 07/30/12 adjust main program logic to draw channel immediately if real time data and with each new Bar after LastBarOnChart has been reached 08/09/12 fix bug in deviation calculations when user changes length of LR channel. 05/08/13 Explicitly define TL_SetExtRight = false for dots used to highlight beginning and end of bars used in LRChan calculation, To accomodate a change in default behavior of TS 9.1 EL made between Version 18 and version 20. EL default condition of TL extend right was changed from false to true by EL developers. 12/14/15 hold RefPrice and Bar Date and Bar Time in arrays to eliminate exceeding MaxBarsBack when manually moving center line. Use of DateArray and TimeArray allows beginning point of trend line to move to within Length bars of beginning of chart. 10/16/15 define plot thickness of marker dots in TL definition section rather than plotting section for greater efficiency 12/28/15 error checking for reversal of Begin and End points of TL 12/31/15 fixed Linear regression calculation to be based on Length + 1, for a trend line of "Length" since the LR function treats length as number of data points. For example, A trend line with a length of 10 will have 11 data points, and the LR calculation therefore will need use Length = 11. 12/01/15 created array version of LRChan to eliminate problems with MaxBarsBack 01/12/16 converted PriceArray to a circular array for memory conservation. The indicator can be moved by the user up to ArrayLength bars to the left of the CurrentBar. 12/27/16 Add "astype int" to some calculations to eliminate warning messagages about precision truncation. 06/10/17 replace legacy trend line drawing commands with objects. Store band TL's in vector myLines. Keep centerline as TL object. } #endregion using elsystem; using elsystem.collections; using elsystem.drawing; using elsystem.drawingobjects; using charting; inputs: { Input Parameters } RefPrice(close), int Length(80), { Length of trendlines representing LR channel. **** Calculation of LR involves Length + 1 data points! } int Offset(6), { linear regression channel offset } TgtBar(0), { linear regression target bar, normally do not need to use non-zero values } DevID(2), { deviation function identifier } int StdDevLength(0), { standard deviation length. If zero, defaults to Length + 1 } int StdErrLength(0), { standard error length. If zero, defaults to Length + 1 } int ATRLength(15), { average true range length } JATRLength(15), { Jurik moving average ATR - user must have Jurik routines installed } Percent(1), { percentage } Pts(1), { points } int NBands(3), { number of deviation bands drawn on both sides of center trend line } StartMult(1), { number of deviation units of innermost band from center trend line } Increment(1), { number of deviation units of subsequent bands from each other } FreezeBeginPoint(false), { freeze the beginning point of the TL channel to startup position or any position the center TL is moved to by the user } // BeginDate_YYYYMMDD(0), { if specified , this begin date overrides calculated begin date } int AlertBand(0), { If non-zero, trigger alert when price hits this band number } AlertBandColor(red), { color of Alert band to remind user alert option is active } bool ExtendRight(true), { extend all regression channel lines to right } bool ExtendLeft(false), { extend all regression channel lines to left } CLColor(cyan), { center line color } CLStyle(0), { 0: Solid; 1: Dashed; 2: Dotted; 3: Dashed2; 4: Dashed3 } { = StyleType.Solid, StyleType.Dotted, StyleType.Dashed } int CLWeight(0), { center line plotting width } BandColor(darkgray), { color of parallel bands around center line before transparency added } BandStyle(Tool_Solid), { style of band lines } int BandWeight(0), { band lines plotting width } DisplayedValuesColor(yellow), { color of displayed band values } ShowCLDotMarkers(true), { if true, show dot on center trend line to signify Offset point } CLMarkerDOTColor(yellow), { dots on center line to show begin and end points } int CLDotMarkerWeight(4), ShowCLMarkersVL(false), { Show TrendLine end markers with vertical line } int CLMarkersVLColor(darkgray), int CLMarkersVLStyle(0), { 0: Solid; 1: Dashed; 2: Dotted; 3: Dashed2; 4: Dashed3 } { = StyleType.Solid, StyleType.Dotted, StyleType.Dashed } int CLMarkersVLWeight(0), ShowCAGR(false), { show compound annual growth rate (CAGR) if Bar interval 60 minute or greater } PosCAGRColor(yellow), NegCAGRColor(red), CAGRVerticalPct(9), { vertical price scale percentage to display CAGR } // CAGROffset(100), { CAGR value display offset from CurrentBar } CalcSeconds(2), { minimum interval in seconds that center line is check to see if it was moved by user } Timersound(false); vars: { Objects } Timer Timer1( NULL ), Vector myLines(null), Trendline TL(null), TrendLine CL(null), { center line } TrendLine CL2(null), { clone of center line to compare with C to see if user moved CL } TrendLine oneLine(null), VerticalLine VL1 (null), VerticalLine VL2 (null), BNPoint BNP1(null), BNPoint BNP2(null), // DOPoint DOPBegin(null), // DOPoint DOPEnd(null), { PlotVisualElement PVE(null), BarVisualElement BVE(null), } intrabarpersist int StyleSolid(styletype.solid), intrabarpersist int StyleDotted(styletype.dotted), intrabarpersist int StyleDashed(styletype.dashed); vars: { CAGR } intrabarpersist LengthYrs(0), { xLength expressed in terms of years } intrabarpersist CAGR(0), CAGROffset(Offset + Length/2), { horizontal bar offset to display CAGR on chart } Intrabarpersist BarsPerYear(_BarsPerYear); vars: { Misc Variables } intrabarpersist int BS(0), { Bar status } { determine price range } HighRange(0), LowRange(0), Midrange(0), intrabarpersist VerticalPct(0); vars: { _LRArray } intrabarpersist oLRSlope(0), intrabarpersist oLRAngle(0), intrabarpersist oLRIntercept(0), intrabarpersist oLRValueRaw(0); vars: { _SDMeanArray } int SDDataType(2), { SDDataType = 1: SD of population; 2: SD of sample } Intrabarpersist oMean(0), Intrabarpersist oStdDev(0), intrabarpersist int MinLength(1); { minimum length needed to calculate SD function: StdDev = 2, StdErrLength = 3, Otherwise = 1 } vars: { TrendLine } Intrabarpersist int TextID(0), Text1(""), Text2(""), intrabarpersist Dev(0), { deviation } intrabarpersist LastBar(false), { true when LastBarOnChart reached } intrabarpersist int TL_ID(0), { trend line identification number } intrabarpersist Marker_ID(0), { trend line ID for drawing offset marker line } Intrabarpersist int CL_ID(0), { centerline TL ID } Intrabarpersist int CLBeginDot_ID(0), { centerline begin marker ID } Intrabarpersist int CLEndDot_ID(0), { centerline end marker ID } { { TL definitions changed from DTPoints to BNPoints } intrabarpersist BeginDate(0), intrabarpersist BeginDate2(0), intrabarpersist EndDate(0), intrabarpersist EndDate2(0), intrabarpersist EndDate3(0), intrabarpersist int BeginTime(0), intrabarpersist int BeginTime2(0), intrabarpersist int EndTime(0), intrabarpersist int EndTime2(0), intrabarpersist int EndTime3(0), } intrabarpersist int k(0), intrabarpersist int BeginBar (0), intrabarpersist int BeginBar2(0), intrabarpersist int EndBar(0), intrabarpersist int EndBar2(0), intrabarpersist double BeginValue(0), intrabarpersist double BeginValue2(0), intrabarpersist double EndValue(0), intrabarpersist double EndValue2(0), intrabarpersist double EndValue3(0), Intrabarpersist int xLength(0), intrabarpersist int xOffset(0), intrabarpersist int NDraw(0), intrabarpersist bool DrawOK(true), { switch to indicate when reg channel needs to be redrawn } Intrabarpersist bool ChartDataOK(false); { true when //sufficient bars loaded into chart to draw LR regression channel } vars: { array variables } intrabarpersist Index(0), int ArrayLength(2000); arrays: int UBand[20](0), { stores trend line ID#'s of upper channels } int LBand[20](0), { stores trend line ID#'s of lower channels } intrabarpersist RefPriceArray[2000](0); { Array size should include maximun of bars that will be loaded into chart and should be available for moving TL to } Method override void InitializeComponent() begin Timer1 = new elsystem.Timer; Timer1.Interval = (CalcSeconds * 1000) AsType int; Timer1.AutoReset = true; Timer1.Enable = true; Timer1.Name = "Timer1"; Timer1.elapsed += Timer1_Elapsed; { for displaying CAGR (compound annual growth rate) } TextID = Text_New(0, 0, 0, " "); Text_SetColor(TextID, PosCAGRColor); // Text_SetStyle(TextID, 1, 1); myLines = Vector.Create(); CreateTL(); oneLine = myLines[ 0 ] AsType TrendLine; { get center line } BNP1 = BNPoint.Create(0, 0.0); BNP2 = BNPoint.Create(0, 0.0); VL1 = new elsystem.drawingobjects.VerticalLine; If FreezeBeginPoint then VL1.Color = elsystem.drawing.Color.FromARGB(red) Else VL1.Color = elsystem.drawing.Color.FromARGB(CLMarkersVLColor); VL1.Weight = CLMarkersVLWeight; VL1.Style = CLMarkersVLStyle; VL1.Alert.BarsBefore = 0; VL1.Alert.Frequency = elsystem.drawingobjects.AlertFrequency.Once; VL1.Persist = true; VL1.AutoShow = true; VL1.Name = "VL1"; self.DrawingObjects.Add(VL1); VL2 = new elsystem.drawingobjects.VerticalLine; VL2.Color = elsystem.drawing.Color.FromARGB(CLMarkersVLColor); VL2.Weight = CLMarkersVLWeight; VL2.Style = CLMarkersVLStyle; VL2.Persist = true; VL2.AutoShow = true; VL2.Name = "VL2"; self.DrawingObjects.Add(VL2); { PVE = new PlotVisualElement; BVE = new BarVisualElement; } end; Method void Timer1_Elapsed( elsystem.Object sender, elsystem.TimerElapsedEventArgs args ) {The Elapsed event handler is called when the timer1 counts down to zero} begin // Trigger1 = true; // BS = -1; { all timer generated executions associated with BarStatus(1) = -1 (undefined) } { test if center line has been moved by user } { Comparing StartPoints and EndPoints is faster than parsing BNPoint data with EVERY call } If TimerSound then Value1 = _Click; If (CL.StartPoint <> CL2.StartPoint) or (CL.EndPoint <> CL2.EndPoint) then TestTLMoved(); end; {--------------------------------------------------------------------------------------------------------------------------------} Method void Initialize() begin { If BeginDate_YYYYMMDD > 0 then begin BeginDate = _YYYYMMDD.ToELDate(BeginDate_YYYYMMDD, "BeginDate_YYYYMMDD"); BeginBar = _DateOffset(BeginDate); end else begin BeginBar = Length + Offset; end; BeginBar = MaxList(BeginBar , EndBar + 3) astype int; { starting BeginBar must be <= Bars loaded into chart } BeginBar = MinList(BeginBar , CurrentBar) astype int; } { { is there enough data loaded in chart? } If Currentbar < (Offset + 1) then RaiseRunTimeError ("LRChanArray: Insufficient data loaded in chart. Currentbar = " & NumToStr(CurrentBar,0)); { if insufficient data to display requested length, then display maximum length for which there is data avaialble on chart } xLength = MaxList(MinList(CurrentBar - Offset, Length + 1), 0) asType int; } xOffset = Offset; xLength = Length; BeginBar = xLength + xOffset; {Length is TL Length. Number of points in SD or SE calculations = Length + 1 } EndBar = xOffset; xLength = BeginBar - EndBar; { prepare deviation paramters } Switch DevID begin Case 1: { SD requires at least 2 data points, or TL length at least 1 } If xLength < 1 then RaiseRunTimeError("Std Deviation calculation fails. Less than 2 data points."); Text2 = "Dev: StdDev " & NumtoStr(xLength,0); Case 2: { SE calculation requires at least 3 data points, or TL length of at least 2 } If xLength < 2 then RaiseRunTimeError("Std Error calculation fails. Less than 3 data points."); Text2 = "Dev: StdErr " & NumtoStr(xLength,0); Case 3: Text2 = "Dev: ATR " & NumToStr(ATRLength, 0); Case 4: Text2 = "Dev: JURIC ATR " & NumToStr(JATRLength, 0); Case 5: Text2 = "Dev: Percent " & NumToStr(Percent, 1); Case 6: Dev = Pts; Text2 = "Dev: Points " & NumToStr(Pts, 2); end; { determine vertical location of CAGR Text } HighRange = GetAppInfo(aiHighestDispValue); LowRange = GetAppInfo(aiLowestDispValue); MidRange = (HighRange + LowRange)/2; end; Method void CreateTL() vars: int j; Begin { center line } // myLines.push_back( newTrendLine(0, 0, 0, 0, CLStyle, CLWeight, CLColor, ExtendLeft, ExtendRight)); CL = newTrendLine(0, 0, 0, 0, CLStyle, CLWeight, CLColor, ExtendLeft, ExtendRight); { clone centerline for comparison if CL moved by user, but do not add to chart drawing objects } CL2 = CL; { center line DOT markers } If ShowCLDotMarkers then begin myLines.push_back( newTrendLine(0, 0, 0, 0, StyleSolid, CLDotMarkerWeight, iff(FreezeBeginPoint, red, CLMarkerDOTColor), False, False)); myLines.push_back( newTrendLine(0, 0, 0, 0, StyleSolid, CLDotMarkerWeight, CLMarkerDOTColor, False, False)); end; { band lines } For j = 1 to NBands begin myLines.push_back( newTrendLine(0, 0, 0, 0, StyleSolid, BandWeight, BandColor, ExtendLeft, ExtendRight)); end; For j = 1 to NBands begin myLines.push_back( newTrendLine(0, 0, 0, 0, StyleSolid, BandWeight, BandColor, ExtendLeft, ExtendRight)); end; end; Method Trendline newTrendLine( int BeginBar, double BeginPrice, int EndBar, double EndPrice, int TLStyle, int TLWeight, int TLColor, bool ExtendLeft, bool ExtendRight) begin // k = CurrentBar + MaxBarsBack - 1; TL = TrendLine.Create(BNPoint.Create(k - BeginBar, BeginPrice), BNPoint.Create(k - EndBar, EndPrice)); TL.Style = TLStyle; TL.Weight = TLWeight; TL.Color = color.FromARGB(TLColor); TL.Persist = true; TL.ExtLeft = ExtendLeft; TL.ExtRight = ExtendRight; DrawingObjects.Add(TL); Return TL; end; Method void CalcCAGR() vars: string str; Begin { Compound Annualized Growth Rate } If ShowCAGR then begin if BarsPerYear > 0 then begin LengthYrs = xLength / BarsPerYear; if (BeginValue > 0 and EndValue > 0) then begin CAGR = Power(EndValue/BeginValue, 1/LengthYrs) - 1; // debug //print("_CalcCAGR: ", "BeginValue = ", BeginValue, " EndValue = ", EndValue, " CAGR = ", CAGR:0:3); //print("_CalcCAGR: BarsPerYear = ", BarsPerYear, " LengthYrs = ", LengthYrs); str = NumToStr(CAGR*100, 1) + " %"; end else begin CAGR = 0; str = "--------"; end; Value1 = Text_SetString(TextID, "LR.CAGR: " & str); VerticalPct = iff(Close[CAGROffset - 7] > MidRange, CAGRVerticalPct, 100 - CAGRVerticalPct); Value1 = Text_Float(TextID, CAGROffset, VerticalPct); Value1 = Text_SetColor(TextID, iff(CAGR > 0, PosCAGRColor, NegCAGRColor)); end; end; end; Method void CalcLRChan() Begin { k = CurrentBar + MaxBarsBack - 1; NDraw = NDraw + 1; BeginBar = xLength + xOffset; EndBar = xOffset; xLength = BeginBar - Endbar; } { recalculate LR center line. Use functions where length may vary. } { not sure if all Dev functions will work with varying length } { TgtBar is zero because channel projects to right to reach current bar. Its end values are calculated at Offset } { NOTE: LR calculation length is number of points. For "N" bars in calculation, Length would be N + !. Therefore, 1 is added to usual Length } value1 = _LRArray(RefPriceArray, ArrayLength, xLength + 1, xOffset, TgtBar, oLRSlope, oLRAngle, oLRIntercept, oLRValueRaw); BeginValue = oLRIntercept; EndValue = oLRValueRaw; #region Debug 2 //print("_LRChanArray.CalcLRChan: BeginValue = ", BeginValue, " EndValue = ", Endvalue); #endregion switch DevID begin Case 1: Dev = _SDMeanArray(RefPriceArray, ArrayLength, CurrentBar - xOffset - (xLength + 1), CurrentBar - xOffset, SDDataType, oMean, oStdDev); Case 2: { can not use _StdError, since length may not vary in this function } Dev = _SEArray(RefPriceArray, ArrayLength, xLength + 1, xOffset); Case 3: Dev = _AvgTrueRange(ATRLength)[xOffset]; { uncomment if Jurik Moving Average add-on is installed } { Case 4: Dev = _JMATrueRange(JATRLength)[xOffset]; } Case 5: Dev = RefPriceArray[xOffset] * Percent / 100; { Case 6 = Points. Since never changes, no recalculation required } { Dev for points is set in Initialization method. } end; end; Method void DrawLRChan() { Method: DrawLRChan() Description: Draw a linear regression trendline (center line) and deviation channels using trend lines drawn parallel to the center line. The regresion channel is user interactive. It is initially drawn according to the input parameters Length and Offset. Once drawn the user can move either end or both ends of the center trend line and withing CalcSeconds the regression channel will be redrawn with the Length and Offset matching the new location of the center trend line. To function correctly, MaxBarsBack should be at least equal to the number of bars visible within the chart window (without scrolling). Also, the number of bars loaded into the chart must be significantly more than the MaxBarsBack setting. Discussion: Created as a method so both strategy and indicator could use the same code without duplication. Having the stategy draw the LR channel is better than having an indicator and a strategy placed on the same chart since the parameters of the indicator do not have to be copied to the parameters of the strategy to ensure both are referring to the same LR channel. In this case, the parameters defined for the strategy are used to draw the LR channel directly, reducing the possibiliy of user error. Also, if the LR regression channel parameters are optimized, then at the completion of optimization the strategy will be set to the optimal values and the displayed regression channel will match these values without further user action, as would be required if an indicator was used to draw the regression channel. } { k = CurrentBar + MaxBarsBack - 1; NDraw = NDraw + 1; BeginBar = xLength + xOffset; EndBar = xOffset; xLength = BeginBar - Endbar; } { center line } { working directly with Trendline CL, CL2 is faster than working with vectors.} { Also, BNPoints of these vectors are retained rather than lost if cycle though vectors } { oneLine = myLines[ 0 ] AsType TrendLine; oneline.SetStartPoint( BNPoint.Create(k - BeginBar, BeginValue )); oneline.SetEndPoint( BNPoint.Create(k - EndBar, EndValue )); } vars: int j; begin { check if sufficient data to display LR channel } If k - BeginBar < 1 then Return; CL.SetStartPoint( BNPoint.Create(k - BeginBar, BeginValue )); CL.SetEndPoint( BNPoint.Create(k - EndBar, EndValue )); CL2 = CL; { centerline markers with DOTS } { each DOT is Trendline with length = 0 (starting point = ending point)} If ShowCLDotMarkers then begin oneLine = myLines[ 0 ] AsType TrendLine; oneline.SetStartPoint( BNPoint.Create(k - BeginBar, BeginValue )); oneline.SetEndPoint( BNPoint.Create(k - BeginBar, BeginValue )); oneLine = myLines[ 1 ] AsType TrendLine; oneline.SetStartPoint( BNPoint.Create(k - EndBar, EndValue )); oneline.SetEndPoint( BNPoint.Create(k - EndBar, EndValue )); end; { centerline markers with vertical lines } If ShowCLMarkersVL then begin BNP1.Barnumber = k - BeginBar; VL1.SetPosition(BNP1); BNP2.BarNumber = k - EndBar; VL2.SetPosition(BNP2); end; { debug } // print("k = ", k, " BNP1.Barnumber = ", k - BNP1.Barnumber, " BNP2.Barnumber = ", k - BNP1.BarNumber); { band lines } For j = 1 to NBands begin oneLine = myLines[ 2 + j - 1] AsType TrendLine; oneline.SetStartPoint( BNPoint.Create(k - BeginBar, BeginValue + Dev * j)); oneline.SetEndPoint( BNPoint.Create(k - EndBar, EndValue + Dev * j)); end; For j = 1 to NBands begin oneLine = myLines[ NBands + 2 + j - 1 ] AsType TrendLine; oneline.SetStartPoint( BNPoint.Create(k - BeginBar, BeginValue - Dev * j)); oneline.SetEndPoint( BNPoint.Create(k - EndBar, EndValue - Dev * j)); end; If ShowCAGR then CalcCAGR(); { debug } { For j = 0 to 2 + NBands * 2 begin oneLine = myLines[ j ] AsType TrendLine; print("BarNumber = ", j, " TL Value = ", oneline.GetValue(k -j)); end; } end; Method void TestTLMoved() vars: int j, string str, string strBN; Begin // oneLine = myLines[ 0 ] AsType TrendLine; { get center line } // _TLGetBNPtData(oneLine, BeginBar2, BeginValue2, EndBar2, EndValue2); _TLGetBNPtData(CL, BeginBar2, BeginValue2, EndBar2, EndValue2); {debug} { print("BeginBar = ", BeginBar:0:0, " BeginBar2 = ", BeginBar2:0:0, " EndBar = ", EndBar:0:0, " EndBar2 = ", EndBar2:0:0, " xOffset = ", xOffset:0:0, // " BeginValue = ", BeginValue, " BeginValue2 = ", BeginValue2, " EndValue = ", EndValue, " EndValue2 = ", EndValue2, " Length = ", Length:0:0, " xLength = ", xLength:0:0, " CL1 = ", oneline.Startpoint.ToString(), " CL2 = ", oneline.Endpoint.ToString(), " currentBar = ", currentbar:0:0, " MaxBarsBack = ", MaxBarsBack:0:0); } { check for reversed Begin and End Points } If BeginBar2 < EndBar2 then begin j = BeginBar2; BeginBar2 = EndBar2; EndBar2 = j; end; { Check for minimum length. Do not move TL endpoints if length insufficient for LE or SE calculations. } If AbsValue(BeginBar2 - EndBar2) < 3 then begin { revert to original settings if xLength < 3 } BeginBar2 = BeginBar; EndBar2 = EndBar; DrawLRChan(); { recalulation of values unnecessary as no movement is occuring } end; If (BeginBar <> BeginBar2) or (EndBar <> EndBar2) then begin EndBar2 = MaxList(Endbar2, 0) asType int; { do not allow user to move TL right of current bar } // BeginBar2 = MaxList(BeginBar2, CurrentBar); { do not allow user to move TL to left of first bar (not necessary to check ) } BeginBar = BeginBar2; EndBar = EndBar2; { xLength is the number of data points used in SD and SE calculations = Length + 1 } xLength = BeginBar - EndBar; xOffset = EndBar; CalcLRChan(); { recalculate the trend line positions using the new end point positions } DrawLRChan(); end; { debug } { print("LRChanArray3 TestTLMove BeginBar = ", BeginBar:0:0, " BeginBar2 = ", BeginBar2:0:0, " EndBar = ", EndBar:0:0, " EndBar2 = ", EndBar2:0:0, " xLength = ", xLength:0:0); } end; Method void _TLGetBNPtData(elsystem.drawingobjects.trendline TL, Out int BeginBar, Out double BeginValue, Out int EndBar, Out double EndValue ) { get BNPoints Data from trend line TL } vars: string str, elsystem.collections.Vector params; begin // k = CurrentBar + MaxBarsBack - 1; { obtain BNPoint as string, ex.: "BNPoint ( BarNumber: 499 Price: 285.634993 )" } str = TL.StartPoint.ToString(); { split string components using blank separator into vector elements } { In string example "BNPoint ( BarNumber: 499 Price: 285.634993 )", barnumber is element 3 and price is element 5 } params = str.split(" "); { convert Barnumber and Price to integer and double variables } BeginBar = k - elsystem.elInt.Parse(params.items[3].toString()); { convert BarNumber string to integer } BeginValue = elsystem.elDouble.Parse(params.items[5].toString()); { convert Price to double } str = TL.EndPoint.ToString(); { split string components using blank separator into vector elements } { In string example "BNPoint ( BarNumber: 499 Price: 285.634993 )", barnumber is element 3 and price is element 5 } params = str.split(" "); { convert Barnumber and Price to integer and double variables } EndBar = k - elsystem.elInt.Parse(params.items[3].toString()); { convert BarNumber string to integer } EndValue = elsystem.elDouble.Parse(params.items[5].toString()); { convert Price to double } //debug //Print (str, " BeginValue = ", BeginValue:0:7, " EndValue = ", Endvalue:0:7); // Return 0; end; #region Main Program BS = BarStatus(1); #region Initialize When LBOC Reached Once (LastBarOnChartEx) begin LastBar = true; Index = Mod(CurrentBar, ArrayLength); { load last array element here, since LBOC may have status BS = 1 } RefPriceArray[Index] = RefPrice; Initialize(); { initialize variables AFTER all data is loaded in chart } If BS = 1 then begin { if intrabar, draw initial channel, otherwise if BS = 2 will draw below } k = CurrentBar + MaxBarsBack - 1; NDraw = NDraw + 1; BeginBar = xLength + xOffset; EndBar = xOffset; xLength = BeginBar - Endbar; CalcLRChan(); DrawLRChan(); end; end; #endregion #region EOB Processing If BS = 2 then begin Index = Mod(Currentbar, ArrayLength); RefPriceArray[Index] = RefPrice; { call once every new bar after chart loads } if LastBar then begin k = CurrentBar + MaxBarsBack - 1; NDraw = NDraw + 1; if FreezeBeginPoint then xLength = xLength + 1; BeginBar = xLength + xOffset - 1; EndBar = xOffset - 1; CalcLRChan(); DrawLRChan(); end; end; #endregion #endregion