//------------------------------------------------------ // // Formula Name: Linear Regression // Author/Uploader: Trading Tuitions // E-mail: support@tradingtuitions.com // Website: www.tradingtuitions.com //------------------------------------------------------ _SECTION_BEGIN("Linear Regression"); SetTradeDelays( 1, 1, 1, 1 ); SetOption( "InitialEquity", 200000); SetOption("FuturesMode" ,True); SetOption("MinShares",1); SetOption("CommissionMode",2); SetOption("CommissionAmount",100); SetOption("AccountMargin",10); SetOption("RefreshWhenCompleted",True); SetPositionSize(100,spsPercentOfEquity); SetOption( "AllowPositionShrinking", True ); BuyPrice=Open; SellPrice=Open; ShortPrice=Open; CoverPrice=Open; SetChartOptions(0,chartShowArrows|chartShowDates); _N(Title = StrFormat("{{NAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) )); Plot( Close, "Price", colorWhite, styleCandle ); P = ParamField("Price field",-1); Periods = Param("Periods", 20, 2, 300, 1, 10 ); Plot( LinearReg( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); _SECTION_END(); NewDay = (Day()!= Ref(Day(), -1)) OR BarIndex() == 0; //Plot(NewDay,"",colorlightGrey,styleHistogram|styleDots|styleNoLabel|styleOwnScale); FirstTradeTime=093000; SquareOffTime = 151500; LR = LinearReg(C,20); SD = StDev(C,20); up = LR + 2*SD; down = LR - 2*SD; Plot(up,"UP",colorRed); Plot(down,"DOWN",colorGreen); Buy = Ref(C,-2) < Ref(down,-2) AND Ref(C,-1)>Ref(down,-1) AND C>Ref(C,-1) AND TimeNum()>= FirstTradeTime AND TimeNum() Ref(up,-2) AND Ref(C,-1)= FirstTradeTime and TimeNum()= SquareOffTime; Cover=Buy OR TimeNum() >= SquareOffTime; Buy=ExRem(Buy,Sell); Sell=ExRem(Sell,Buy); Short=ExRem(Short,Cover); Cover=ExRem(Cover,Short); StopLoss=Optimize("stop",0.5,0.5,3,0.5); ApplyStop(Type=0,Mode=1,Amount=StopLoss); Target=Optimize("Target",0.5,0.5,5,0.5); ApplyStop(Type=1,Mode=1,Amount=Target); printf("\nBuy : " + Buy ); printf("\nSell : " + Sell ); printf("\nShort : " + Short ); printf("\nCover : " + Cover ); /* Plot Buy and Sell Signal Arrows */ PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40); PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40); PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40); PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45); PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40); PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45); _SECTION_END();