Here is my Linear Regression 2 colors code - How can I anticipate the color switch?  [SOLVED]

Questions about MultiCharts and user contributed studies.
dvdkite
Posts: 65
Joined: 16 May 2018
Has thanked: 10 times
Been thanked: 1 time

Here is my Linear Regression 2 colors code - How can I anticipate the color switch?

Postby dvdkite » 29 Aug 2019

Hello Everyone,

I'm using the following code for the Linear Regression but it change its color a bar late for...

Code: Select all

inputs:
Price( Close),
Length( 89),
Displace( 0),
UpColor( green),
DnColor (red),
PaintInBarLRCLine ("Yes");

variables:
LinReg( 0 ),Trend (0),Transition (0) ;

if Displace >= 0 or CurrentBar > AbsValue( Displace ) then
begin
LinReg = LinearRegValue( Price, Length, 0 ) ;
If LinReg > LinReg [1] then
Begin
If Trend = -1 then Transition = 1;
If PaintInBarLRCLine = "Yes" then Plot1[Displace]( LinReg, "LinReg", UpColor );
Trend = 1;
End
Else
Begin
If Trend = 1 then Transition = 1;
If PaintInBarLRCLine = "Yes" then Plot1 [Displace] ( LinReg, "LinReg",DnColor ) ;
Trend = -1;
End;
End;
Let me explane with an example over the FDAX...
Let's suppose we have an uptrend linear regression (LR) line yellow and then it is approching to switch direction and we came to the point where LR[4] is 12349,70, LR[3] is 12350,80 , LR[2] is 12350,68 and then LR[1] is 12350,49.

Ok so this is the situation that I actually see on my chart:

I have the UpColor between LR4 and LR3, // correct because LR4 12349,70 < LR3 12350,80
I have the UpColor between LR3 and LR2, // wrong because LR3 12350,80 > LR2 12350,68
I have the DownColor between LR2 and LR1, //correct because LR2 12350,68 > LR1 12350,49

So basically the indicator is switching color a bar late and I honestly can't find a way to change it!
... Also I would like to see the last piece of line to change color (if the condition are met) on every tick untill the bar end and its value is definitive so I know that I'm approching the switch...
How can I achive that?

HERE you have a grafic example of what I mean (There's a continuous error during the attachement upload now using Chrome and Images cannot be uploaded) so please visit the following link Image


Any help would be appreciated.

Thanks

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Here is my Linear Regression 2 colors code - How can I anticipate the color switch?

Postby TJ » 29 Aug 2019

You will need to add this line of code to the respective plot:

Code: Select all

SetPlotColor[Displace+1]( 1, UpColor );

SetPlotColor[Displace+1]( 1, DnColor );

dvdkite
Posts: 65
Joined: 16 May 2018
Has thanked: 10 times
Been thanked: 1 time

Re: Here is my Linear Regression 2 colors code - How can I anticipate the color switch?  [SOLVED]

Postby dvdkite » 29 Aug 2019

You will need to add this line of code to the respective plot:

Code: Select all

SetPlotColor[Displace+1]( 1, UpColor );

SetPlotColor[Displace+1]( 1, DnColor );
Amazing Tj! Thanks you, it's working perfectly now


Return to “MultiCharts”