// Downloaded From https://www.WiseStockTrader.com _SECTION_BEGIN("H K CHART"); //---- TREND TRADE SetChartOptions(1,chartShowArrows|chartShowDates); GraphXSpace=5; HaClose = (O+H+L+C)/4; HaOpen = AMA( Ref( HaClose, -1 ), 0.8); HaHigh = Max( H, Max( HaClose, HaOpen ) ); HaLow = Min( L, Min( HaClose, HaOpen ) ); xDiff = (HaHigh - Halow) * IIf(StrFind(Name(),"JPY"),100,10000); barcolor = IIf(HaClose >= HaOpen,colorWhite,colorWhite); if( ParamToggle("Plot Heikin-Ashi", "No,Yes", 1 ) ) PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "Heikin Ashi " + Name(), colorWhite, styleCandle ); else Plot( C, "Regular candles " + Name(), colorWhite, styleCandle ); j=Haclose; //PRICE LINE PriceLineColor = PriceLineColor= colorCustom4; PriceLevel = C; Daysback = 200; FirstBar = BarCount - DaysBack; YY = IIf(BarIndex() >= Firstbar,EndValue(PriceLevel),Null); Plot(YY,"Current Price",PriceLineColor,ParamStyle("LineStyle",styleLine|styleDashed|styleNoTitle|styleThick,maskAll)); side = 1; dist = 0; for( i = 0; i < BarCount; i++ ) { if(i+side== BarCount) PlotText( "\n " + PriceLevel[ i ], i, YY[ i ]-dist[i], colorCustom2 ); } _SECTION_END(); _SECTION_BEGIN("Trend Lines"); p1 = Param("TL 1 Periods", 13, 1, 50, 1); p2 = Param("TL 2 Periods", 3, 1, 25, 1); TL1 = LinearReg(C, p1); TL2 = EMA(TL1, p2); Col1 = IIf(TL1 > TL2, ParamColor("TL Up Colour", colorGreen), ParamColor("TL Dn Colour", colorRed)); Plot(TL1, "TriggerLine 1", Col1, styleLine|styleThick|styleNoLabel); Plot(TL2, "TriggerLine 2", Col1, styleLine|styleThick|styleNoLabel); _SECTION_END(); //================================================End Chart Configuration=============================================================================== //====================================Start of Linear Regression Code================================================================================== P = ParamField("Price field",-1); Length = 150; Daysback = Param("Period for Liner Regression Line",Length,1,240,1); shift = Param("Look back period",0,0,240,1); //=============================== Math Formula ======================================================================================================== x = Cum(1); lastx = LastValue( x ) - shift; aa = LastValue( Ref(LinRegIntercept( p, Daysback), -shift) ); bb = LastValue( Ref(LinRegSlope( p, Daysback ), -shift) ); y = Aa + bb * ( x - (Lastx - DaysBack +1 ) ); //==================Plot the Linear Regression Line ==================================================================================================== LRColor = ParamColor("LR Color", colorCycle ); LRStyle = ParamStyle("LR Style"); LRLine = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null ); LRStyle = ParamStyle("LR Style"); Angle = Param("Angle", 0.05, 0, 1.5, 0.01);// A slope higher than 0.05 radians will turn green, less than -0.05 will turn red and anything in between will be white. LRLine = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null ); Pi = 3.14159265 * atan(1); // Pi SlopeAngle = atan(bb)*(180/Pi); LineUp = SlopeAngle > Angle; LineDn = SlopeAngle < - Angle; if(LineUp) { Plot(LRLine, "Lin. Reg. Line Up", IIf(LineUp, colorBrightGreen, colorWhite), LRStyle); } else { Plot(LRLine, "Lin. Reg. Line Down", IIf(LineDn, colorDarkRed, colorWhite), LRStyle); } //========================== Plot 1st SD Channel ====================================================================================================== SDP = Param("Standard Deviation", 1.5, 0, 6, 0.1); SD = SDP/2; width = LastValue( Ref(SD*StDev(p, Daysback),-shift) ); //Set width of inside chanels here. SDU = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width , Null ) ; SDL = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width , Null ) ; SDColor = ParamColor("SD Color", colorCycle ); SDStyle = ParamStyle("SD Style"); Plot( SDU , "Upper Lin Reg", colorRed,SDStyle ); //Inside Regression Lines Plot( SDL , "Lower Lin Reg", colorLime,SDStyle ); //Inside Regression Lines //========================== Plot 2d SD Channel ======================================================================================================== SDP2 = Param("2d Standard Deviation", 2.0, 0, 6, 0.1); SD2 = SDP2/2; width2 = LastValue( Ref(SD2*StDev(p, Daysback),-shift) ); //Set width of outside chanels here. SDU2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width2 , Null ) ; SDL2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width2 , Null ) ; SDColor2 = ParamColor("2 SD Color", colorCycle ); SDStyle2 = ParamStyle("2 SD Style"); Plot( SDU2 , "Upper Lin Reg", colorRed,SDStyle2 ); //OutSide Regression Lines Plot( SDL2 , "Lower Lin Reg", colorLime,SDStyle2 ); //OutSide Regression Lines Trend = IIf(LRLine > Ref(LRLine,-1),colorGreen,colorRed);//Changes LR line to green if sloping up and red if sloping down. Plot( LRLine , "LinReg", Trend, LRSTYLE ); //============================ End Indicator Code ========= /* ********************************** Code to automatically identify pivots ********************************** */ // -- what will be our lookback range for the hh and ll? farback=Param("How Far back to go",300,50,5000,10); nBars = Param("Number of bars", 13, 1, 40); // -- Title. Title = Name() ; // -- Plot basic candle chart PlotOHLC(Open, High, Low, Close, "BIdx = " + BarIndex() + "\n" + "O = " + O + "\n"+"H = "+ H + "\n"+"L = " + L + "\n"+"C ", colorBlack, styleBar); GraphXSpace=7; // -- Create 0-initialized arrays the size of barcount aHPivs = H - H; aLPivs = L - L; // -- More for future use, not necessary for basic plotting aHPivHighs = H - H; aLPivLows = L - L; aHPivIdxs = H - H; aLPivIdxs = L - L; nHPivs = 0; nLPivs = 0; lastHPIdx = 0; lastLPIdx = 0; lastHPH = 0; lastLPL = 0; curPivBarIdx = 0; // -- looking back from the current bar, how many bars // back were the hhv and llv values of the previous // n bars, etc.? aHHVBars = HHVBars(H, nBars); aLLVBars = LLVBars(C, nBars); aHHV = HHV(H, nBars); aLLV = LLV(L, nBars); // -- Would like to set this up so pivots are calculated back from // last visible bar to make it easy to "go back" and see the pivots // this code would find. However, the first instance of // _Trace output will show a value of 0 aVisBars = Status("barvisible"); nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0))); _TRACE("Last visible bar: " + nLastVisBar); // -- Initialize value of curTrend curBar = (BarCount-1); curTrend = ""; if (aLLVBars[curBar] < aHHVBars[curBar]) { curTrend = "D"; } else { curTrend = "U"; } // -- Loop through bars. Search for // entirely array-based approach // in future version for (i=0; i lastHPIdx) { // -- Bar and price info for candidate pivot candIdx = curBar - aHHVBars[curBar]; candPrc = aHHV[curBar]; if ( lastHPH < candPrc AND candIdx > lastLPIdx AND candIdx < curBar) { // -- OK, we'll add this as a pivot... aHPivs[candIdx] = 1; // ...and then rearrange elements in the // pivot information arrays for (j=0; j candPrc AND candIdx > lastHPIdx AND candIdx < curBar) { // -- OK, we'll add this as a pivot... aLPivs[candIdx] = 1; // ...and then rearrange elements in the // pivot information arrays for (j=0; j