I want to play, experiment with linear regresion line but I ecountered the problem that I can not solve. It is about backtesting linear regresion line. I understand that I should use loop in my code so that the backest will be conducted bar by bar but it does seem to work at least not for me. If you guys guide me which direction I should proceed I would be very greatfull. Although my coding skills are very basic I fully understand array coding and looping, so any help from you will be appreciated.
LinearReg() is just another array! Treat that the way you would backtest any other array.
This is my code, I have tried to make it as simple as possible
SetBarsRequired( sbrAll, sbrAll ); MarginDeposit=3300; SetOption("MaxOpenPositions", 1 ); SetPositionSize(1,spsShares); RoundLotSize=1; MarginDeposit=3200; PointValue=20; SetTradeDelays(1,1,1,1); BuyPrice=Open; SellPrice=Open; ShortPrice=Open; CoverPrice=Open; //////////////////////////////////////////////////////// _SECTION_BEGIN("Linear Regresion Line"); x = Cum(1); lastx = LastValue( x ); Daysback = 15; aa = LastValue( LinRegIntercept( Close, Daysback) ); bb = LastValue( LinRegSlope( Close, Daysback ) ); y = Aa + bb * ( x - (Lastx - DaysBack) ); Plot( Close, "Close", colorBlack, styleCandle ); Plot( IIf( x >= (lastx - Daysback), y, -1e10 ), "LinReg", colorRed ); _SECTION_END(); ////////////////////////////////////////////////////////// TimeFrameSet(inDaily); _linregline = aa ; //I assume this is an END POINT of linregline TimeFrameRestore(); stsk=StochK(14,3); stsd=StochD(14,3,3); value1=70; value2=30; cond5=Ref(stsd,-1)<=value2 AND Ref(stsk<stsd,-1) AND stsk>stsd; cond12 =Ref(stsd,-1)>=value1 AND Ref(stsd<stsk,-1) AND stsd>stsk; golong=C <TimeFrameExpand( _linregline,inDaily); goshort=C >TimeFrameExpand( _linregline,inDaily); longtrade= cond5 AND golong; shorttrade= cond12 AND goshort; /////////////////////////////////////////////////// Buy= longtrade; Sell= shorttrade ; short=shorttrade; Cover = longtrade; /////////////////////////////////////// Short=ExRem(Short,Cover); Cover=ExRem(Cover,Short); Buy=ExRem(Buy,Sell); Sell=ExRem(Sell,Buy);
I treated it as another array, but I still do not have desired results. Once again. I want the price to be below daily linear regresion line on the particular date (daily bar), when I want to go long and oposite, the price should be above Daily linear regresion line when I want to go short. but it was not always the case. I checked it with the bar reply. I went back to the past trades with the bar reply and I discovered the problem. My question is where is my mistake? Is there problem in my code, or rather in the method I use ?
Simply use LinearReg() - compress it to Higher timeframe and then expand. Moreover, that code is not generating signals. Quoting Tomasz:
And while expanding an array, please understand the difference between
Thank you Cougar for your response. I understand now,.I can backtest linreg line as any other array becouse it is a curve in backtesting.
In the opening post of this thread and the code that you shared, you've clearly depicted:
Quoting from LinearReg( ARRAY, periods ) definition:
Linear regression line end-point value is calculated according to a + b * x (where a and b are intercept and slope of linear regression line) from the ARRAY using periods range.
It is ambiguous (thus incorrect) to state:
LinearReg() in our topic of discussion is not curve-fitting, because it is end point (not forward looking) and has no predictive power. Once again exhibiting from the same link LinearReg( ARRAY, periods ):
So, YES you can treat this LinearReg() like you would treat any other array while backtesting.