Need help with Amibroker code - Please - Aussie Stock Forums

1. ## Need help with Amibroker code - Please

Hello everybody,

I am going crazy trying to do what I thought would be something simple but whatever I do I end up with weird results.

I have some code below that is basically a linear regression channel. I want to incorporate it into my trading system as a trend indicator.
I simply want the linear regression line to

1. turn red when it slopes down by say a 20% from start to finish
2. turn green when it slopes up by say 20% from start to finish
3. turn white in between

It's driving me crazy why I can't do this so I am hoping someone here can help me out. Thank You

// Linear Regression Line with 2 Standard Deviation Channels Plotted Above and Below
// Written by Patrick Hargus, with critical hints from Marcin Gorzynski, Amibroker.com Technical Support
// Designed for use with AB 4.63 beta and above, using drag and drop feature.
// Permits plotting a linear regression line of any price field available on the chart for a period determined by the user.
// 2 Channels, based on a standard deviation each determined by the user, are plotted above AND below the linear regression line.
// A look back feature is also provided for examining how the indicator would have appeared on a chart X periods in the past.

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 );

// ========================== 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) ); // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET
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", SDColor,SDStyle ); //Inside Regression Lines
Plot( SDL , "Lower Lin Reg", SDColor,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) ); // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET
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", SDColor2,SDStyle2 ); //OutSide Regression Lines
Plot( SDL2 , "Lower Lin Reg", SDColor2,SDStyle2 ); //OutSide Regression Lines

//LRLineDown = IIf(LRLine < Ref(SDU2,-50),colorRed,IIf(LRLine > Ref(SDU2,-50),colorGreen,colorWhite));

Plot( LRLine , "LinReg", LRColor, LRSTYLE ); // styleDots );

// ============================ End Indicator Code ================================================== ============

2. ## Re: Need help with Amibroker code - Please

This is without the SD lines which you can add if you want. I included a Param. for the angle which can be adjusted from the Parameters window. Note the arc tangential are actually radians and below this code I have added a picture that converts radians to degrees. About 0.35 is 20°. You are lucky because I have been playing with a work-around to back test with the lin. reg. line by using a variable as a datum to look back at the data and determine the lin. reg. slope angle. Any ideas?

PHP 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 ================================================== ========LRStyle = ParamStyle("LR Style");Rad = Param("Angle", 0.35, 0, 1.5, 0.01); LRLine = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );Pi = 3.14159265 * atan(1); // PiSlopeAngle = atan(bb)*(180/Pi);LineUp = SlopeAngle > Rad; // 20 degrees = 0.35 LineDn = SlopeAngle < - Rad;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);}  ```

3. ## Re: Need help with Amibroker code - Please

Originally Posted by Wysiwyg
Note the arc tangential are actually radians and below this code I have added a picture that converts radians to degrees. About 0.35 is 20°.
Pi = 3.14159265 * atan(1); // Pi
SlopeAngle = atan(bb)*(180/Pi); // Converts from radians to degrees
Derrr, realised the 180/Pi is the conversion from radians to degrees but it doesn't appear to be so on the chart. Radians "appear" to be closer to the angle than degrees.

4. ## Re: Need help with Amibroker code - Please

Matter of fact I'll fix up my error with the radians/degrees. The SlopeAngle code was from another formula but the plot is mine.

PHP 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 ================================================== ========LRStyle = ParamStyle("LR Style");Angle = Param("Angle", 0.35, 0, 1.5, 0.01); LRLine = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );Pi = 3.14159265 * atan(1); // PiSlopeAngle = 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);}  ```

5. ## Re: Need help with Amibroker code - Please

Wow I cannot thank you enough mate, that is exactly what I was after! My logic skills are pretty good but my maths skills leave much to be desired.

Originally Posted by Wysiwyg
You are lucky because I have been playing with a work-around to back test with the lin. reg. line by using a variable as a datum to look back at the data and determine the lin. reg. slope angle. Any ideas?
I know exactly what you are saying and that is also on my list of objectives. If I work out how to do it it I will post it here.

As it stands I have developed a pivot point system that has been giving me good results but I have tried to filter out some of the losing trades with various trend indicators but with no luck until....

I have now implemented the very simple use of linear regression in the following way -

1. Buy only when the Buy signal is below the LR line AND the LR Line is sloping up (and now thanks to your help, by a specified degree).

2. Sell only when the Sell signal is above the LR line AND the LR Line is sloping down.

The results I am getting now are unbelievable! to the point where I have to double check everything because it looks too good.

6. ## Re: Need help with Amibroker code - Please

Originally Posted by Dracuu
1. Buy only when the Buy signal is below the LR line AND the LR Line is sloping up (and now thanks to your help, by a specified degree).

2. Sell only when the Sell signal is above the LR line AND the LR Line is sloping down.

The results I am getting now are unbelievable! to the point where I have to double check everything because it looks too good.
I can't back test using the lin. reg. line because it uses forward data.

In Ami. go to "Formula Editor" -> "Tools" and then click on "Code Check & Profile". This will tell you the formula references FUTURE quotes.

This what I tried to work-around but need a substitute for "LastValue" .

7. ## Re: Need help with Amibroker code - Please

Originally Posted by Wysiwyg
I can't back test using the lin. reg. line because it uses forward data.

In Ami. go to "Formula Editor" -> "Tools" and then click on "Code Check & Profile". This will tell you the formula references FUTURE quotes.

This what I tried to work-around but need a substitute for "LastValue" .
Yeh mate I know, I am doing the back testing manually using bar replay to see where the LRLine is when my system gives the entry signal.

I would love to be able to use the back tester so if you do happen to work out a way to keep entry signals valid please let me know. I am sure I have done it before but for the life of me I cannot remember how to do it.

8. ## Re: Need help with Amibroker code - Please

I notice some shmacko by the name of "Cyberman" has posted this plot modification on WiseStockTrader and claimed he made the modification. Do not expect any help from me again if you are going to claim someone else's work as your own.

I am here to share what I want and when I want and it is free for all to use but when some stinker claims "they" did the modification when they "did not" will see no help from me again for them.

================
In reference to the code angle; the angle is in degrees but the difference is in the chart scale and X axis is not proportionate to Y axis.

9. ## Re: Need help with Amibroker code - Please

You should have addressed the issue here before flaming me on the comments section of the code.

I am not after recognition of any sort, if I was I would not have mentioned the original coder of the linear regression line and I would have changed the entire layout of the code like some people do to claim it as their own.

If you have a look at line 005 you will see that it is blank. I was meant to write "//Wysiwyg coded the angle in degrees part." and I actually thought I had pasted it in there and it was only until now that you mentioned it that I realised I forgot to put it in.

The idea to change it however is mine and the way I describe how to use it I got from no one.

The only reason I posted anything on that forum is because you need a post count of 1 to view a lot of the code. That was the only indicator I have that looks into the future so I just posted that one.

I will ask the admins to either edit it or remove it.