Indicator Color

 

I've been trying to find the color for this LSMA so that I can bring that value into my EA with the iCustom function. I've been trying to find which buffer contains the value of the color being painted, but no luck. I've searched this forum and found quite a few others with the same problem, but have yet to find a solution.

.

I've tried several ideas offered on this forum and this is the results I've gotten:

.

8:34:58 LSMA EURUSD,M1: Shift: 20 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 19 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 18 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 17 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 16 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 15 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 14 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 13 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 12 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 11 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 10 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 9 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 8 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 7 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 6 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 5 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 4 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 3 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 2 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 1 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: Shift: 0 FoundColor = White Buf 1 = 0.00000000 Buf 2 = 0.00000000 Buf 3 = 0.00000000
08:34:58 LSMA EURUSD,M1: zero divide

.

Here's is the code and what I've attempted so far:

.

//+------------------------------------------------------------------+
//| |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| https://www.metaquotes.net// |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005"
#property link "https://www.metaquotes.net//"

//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Lime
#property indicator_color3 White

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];

extern int Rperiod = 25;
int Draw4HowLong, shift, i, loopbegin, length, c, width, DNT, XT, cnt = 0;
double sum[], lengthvar, tmp, wt[], CNT[];
string FoundColor, ArrowUpA;


//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 3 additional buffers are used for counting.

IndicatorBuffers(3);

//---- drawing settings


SetIndexBuffer(5,CNT);
SetIndexBuffer(4,wt);
SetIndexBuffer(3,sum);
SetIndexBuffer(2,ExtMapBuffer1);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexBuffer(0,ExtMapBuffer3);


SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,3);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,3);
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,3);


SetIndexEmptyValue(5,CNT);
//CNT[1]= 2;

//---- initialization done

return(0);
}

int start()

{

Draw4HowLong = Bars-Rperiod - 5; //---- Bars - 25 - 5
length = Rperiod; //---- 25
loopbegin = Draw4HowLong - length - 1; //---Draw4HowLong - 25 - 1



for(shift = loopbegin; shift >= 0; shift--)

{
sum[1] = 0;


for(i = length; i >= 1 ; i--) //----- 25 Down to 1 reverse step

{
lengthvar = length + 1; //----- 25 + 1 = 26
lengthvar /= 3; //Dividing the y variable(lengthvar) by x(3) y /= x; 8.66666
tmp = 0; //---- Set to 0
tmp = ( i - lengthvar)*Close[length-i+shift]; // (25 - 8.66666)*(Close[25-25+loopbegin]
sum[1]+=tmp; //Adding x(0) to the y variable(tmp) y += x; 0 + tmp
}

wt[shift] = sum[1]*6/(length*(length+1)); //-----wt[shift] = sum[1]*6/(25*(25+1));



//========== COLOR CODING ===========================================

ExtMapBuffer3[shift] = wt[shift]; //White

ExtMapBuffer2[shift] = wt[shift]; //Lime

ExtMapBuffer1[shift] = wt[shift]; //Red


if (wt[shift+1] > wt[shift])

{

//CNT[0] = sum[1];
ExtMapBuffer2[shift+1] = EMPTY_VALUE;

//Alert(" 1 = " + CNT[1]);
//Print("shift: " + shift + ", Color Lime");

}

if (wt[shift+1] < wt[shift])

{

//CNT[0] = sum[1];
ExtMapBuffer1[shift+1] = EMPTY_VALUE;
//Alert(" 2 = " + CNT[1]);
//Print("shift: " + shift + ", Color Red");

}

else

{
//CNT[0] = sum[1];
ExtMapBuffer1[shift+1]=CLR_NONE; //EMPTY_VALUE;
ExtMapBuffer2[shift+1]=CLR_NONE; //EMPTY_VALUE;
//Alert(" 3 = " + CNT[1]);
//Print("shift: " + shift + ", Color White");
}

//if(XT != DNT)
//{cnt = 0;}

//if(cnt == 0)
//{Alert("DNT = ", DNT); cnt = cnt+1;}

//Alert(Bars);


if(ExtMapBuffer1[shift] != EMPTY_VALUE)
{FoundColor = "Red";}

if(ExtMapBuffer2[shift] != EMPTY_VALUE)
{FoundColor = "Lime";}

if(ExtMapBuffer3[shift] != EMPTY_VALUE)
{FoundColor = "White";}


if(FoundColor == "Red")
{CNT[1] = 1;}

if(FoundColor == "Lime")
{CNT[1] = 2;}

if(FoundColor == "White")
{CNT[1] = 3;}


if(shift <= 20)
{Print("Shift: " + shift + " FoundColor = " + FoundColor + " Buf 1 = " + ExtMapBuffer1[shift] + " Buf 2 = " + ExtMapBuffer2[shift] + " Buf 3 = " + ExtMapBuffer3[shift]);}

}

return(0);
}
//+------------------------------------------------------------------+

Any ideas would be greatly appreciated! Sorry to be such a pest! My programing background is BASIC. Remember PEEK and POKE? I've been grinding my way through the MQL and getting a fairly good understanding of how things work.

Thanks again!

 
You don't need to find a colour all you do is look for the buffer that isn't empty. That tells you the colour by reference. If buffer one has a value then the display will be showing the colour assigned to buffer one.
 
Ruptor wrote >>
You don't need to find a colour all you do is look for the buffer that isn't empty. That tells you the colour by reference. If buffer one has a value then the display will be showing the colour assigned to buffer one.

O.K. Maybe a little closer. I realized a had a problem with the count of buffers being 3 when it should have been 6.

.

.

Now I have:

.

09:40:20 LSMA EURUSD,M1: Shift: 20 FoundColor = White Buf 1 = 1.44855818 Buf 2 = 1.44855818 Buf 3 = 1.44855818 sum = 156.92713667 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 19 FoundColor = White Buf 1 = 1.44868225 Buf 2 = 1.44868225 Buf 3 = 1.44868225 sum = 156.94057667 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 18 FoundColor = White Buf 1 = 1.44873280 Buf 2 = 1.44873280 Buf 3 = 1.44873280 sum = 156.94605333 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 17 FoundColor = White Buf 1 = 1.44880809 Buf 2 = 1.44880809 Buf 3 = 1.44880809 sum = 156.95421000 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 16 FoundColor = White Buf 1 = 1.44891809 Buf 2 = 1.44891809 Buf 3 = 1.44891809 sum = 156.96612667 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 15 FoundColor = White Buf 1 = 1.44897194 Buf 2 = 1.44897194 Buf 3 = 1.44897194 sum = 156.97196000 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 14 FoundColor = White Buf 1 = 1.44894665 Buf 2 = 1.44894665 Buf 3 = 1.44894665 sum = 156.96922000 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 13 FoundColor = White Buf 1 = 1.44892911 Buf 2 = 1.44892911 Buf 3 = 1.44892911 sum = 156.96732000 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 12 FoundColor = White Buf 1 = 1.44898394 Buf 2 = 1.44898394 Buf 3 = 1.44898394 sum = 156.97326000 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 11 FoundColor = White Buf 1 = 1.44906523 Buf 2 = 1.44906523 Buf 3 = 1.44906523 sum = 156.98206667 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 10 FoundColor = White Buf 1 = 1.44911283 Buf 2 = 1.44911283 Buf 3 = 1.44911283 sum = 156.98722333 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 9 FoundColor = White Buf 1 = 1.44914283 Buf 2 = 1.44914283 Buf 3 = 1.44914283 sum = 156.99047333 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 8 FoundColor = White Buf 1 = 1.44919354 Buf 2 = 1.44919354 Buf 3 = 1.44919354 sum = 156.99596667 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 7 FoundColor = White Buf 1 = 1.44921135 Buf 2 = 1.44921135 Buf 3 = 1.44921135 sum = 156.99789667 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 6 FoundColor = White Buf 1 = 1.44923431 Buf 2 = 1.44923431 Buf 3 = 1.44923431 sum = 157.00038333 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 5 FoundColor = White Buf 1 = 1.44921446 Buf 2 = 1.44921446 Buf 3 = 1.44921446 sum = 156.99823333 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 4 FoundColor = White Buf 1 = 1.44917160 Buf 2 = 1.44917160 Buf 3 = 1.44917160 sum = 156.99359000 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 3 FoundColor = White Buf 1 = 1.44919157 Buf 2 = 1.44919157 Buf 3 = 1.44919157 sum = 156.99575333 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 2 FoundColor = White Buf 1 = 1.44931086 Buf 2 = 1.44931086 Buf 3 = 1.44931086 sum = 157.00867667 wt = 2147483647.00000000 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 1 FoundColor = White Buf 1 = 1.44950889 Buf 2 = 1.44950889 Buf 3 = 1.44950889 sum = 157.03013000 wt = 1.44950889 CNT = 3.00000000
09:40:20 LSMA EURUSD,M1: Shift: 0 FoundColor = White Buf 1 = 1.44967477 Buf 2 = 1.44967477 Buf 3 = 1.44967477 sum = 157.04810000 wt = 1.44950889 CNT = 3.00000000

.

.

.

Buf 1,2 & 3 are the ExtMapBuffers. sum & wt are for calculations and CNT is for assigning a value to the color ( Red, Lime or White) which can then be brought into the EA.

.

.

Buf 1,2,& 3 are the same, though change from bar to bar. wt seems to always be at a state of EMPTY_VALUE. CNT is functioning in the indicator as expected, though when called into EA with iCustom function,

.

LSMA5 = iCustom(NULL,0,"LSMA",CNT,5,0); //---- LSMA Buffer 5

.

The result is: 0.00000000

.

.

Can you tell from this what I need to do?

.

.

Thanks!

 

Your list shows wt = 1.4495 on bottom 2 lines of output. Try getting values of the closed candle ie 5,1); to see. I usually make all buffers visible on the chart to debug things because a picture speaks a thousand words. It usually requires scaling but is always worth it.

 
Ruptor wrote >>

Your list shows wt = 1.4495 on bottom 2 lines of output. Try getting values of the closed candle ie 5,1); to see. I usually make all buffers visible on the chart to debug things because a picture speaks a thousand words. It usually requires scaling but is always worth it.

Thanks Ruptor!

Your right. Something is going on with wt. I've noticed that every time shift = 1, Buf 1, 2, 3 and wt all equal. Also, the change in the value of wt seems to occur either on the shift before 0 or the shift after 0.

The value of wt stays the same for a period of time. ( Which seems to correspond to the length of the line on the chart before change in color) And the increase or decrease of the value of wt, seems to correspond to the direction of the line on the chart.

 
Yellowbeard:

Thanks Ruptor!

Your right. Something is going on with wt. I've noticed that every time shift = 1, Buf 1, 2, 3 and wt all equal. Also, the change in the value of wt seems to occur either on the shift before 0 or the shift after 0.

The value of wt stays the same for a period of time. ( Which seems to correspond to the length of the line on the chart before change in color) And the increase or decrease of the value of wt, seems to correspond to the direction of the line on the chart.


Hello everyone, nice to see others interested in the LSMA. BTW, I think this is my first post here (long time lurker) but I have been into FX in my spare time for several years. I have been on MoneyTec mostly but decided to finally register here.

.

Anyways, I have been studying this indicator quite a bit myself. I see that you are trying to figure out the exact point when the color changes. I have figured out that the LSMA corresponds the right-most endpoint of a linear regression line. If you were to draw a LR line starting from the exact number of periods back from the most completed period that you are using in the calculation, the LSMA will always be equal to the LR line at right-most point, or most recent completed time period. I believe this corresponds to the calculated value of wt[shift] which is the LSMA value. Hence the name "Least Squares Moving Average" which is also called an "Endpoint Moving Average" or EPMA.

.

The formula used in the LSMA indicator you posted is a simplified version of a Pivot Point Moving Average (PPMA). The PPMA is uses reverse linear weights that begin with the most positive value for the most recent time period and continue downward until they become negative for the oldest time period. This makes its divisor smaller than a usual linear wieghted MA. At a certain point, there will be a zero weight assigned to a period which is the "pivot point." The purpose of this is to reduce the lag.

.

Now here is where it gets interesting. Suppose you were to calculate a linear regression line for the same number of periods in the PPMA (LSMA) calculation. As we already know, a LR line can be found by the lowest possible sum of squares of all the residuals. If we were to find the equation of that same LR line (by using the LR formulas to find the slope and y-int), we can calculate the values of each residual (the y value or price in our case). If you calculate the the right-most residual value of the LR line, it is equal to the PPMA (the LSMA)! I know this positively certain because I did this experiment on a spreadsheet and it is exactly the same number each time. Again, this is our wt[shift] value in the LSMA indicator above.

.

To answer your question, after experimenting with the indicator's code and observing, I figured out that the colors change based on the calculated LSMA in relation to the previous LSMA.

.

In a nutshell, if the most recent LSMA is currently:

Green: and the most recent completed LSMA, wt[shift], is calculated higher than the previous period's LSMA, wt[shift+1], = it stays green = wt[shift] > wt[shift+1]

Green: and wt[shift] is calculated lower than wt[shift+1] = it turns yellow ..... = wt[shift] < wt[shift+1]

Yellow: and wt[shift] is calculated lower than wt[shift+1] twice in a row = it turns red ..... = wt[shift] < wt[shift+1] for one period and same for next period

Yellow: and wt[shift] is calculated higher than wt[shift+1] = it stays yellow ......... = wt[shift] > wt[shift+1]

Yellow: and wt[shift] is calculated higher than wt[shift+1] twice in a row = it turns green ..... = wt[shift] > wt[shift+1] for one period and same for next period

Red: and wt[shift] is calculated lower than wt[shift+1] = it stays red ........ = wt[shift] < wt[shift+1]

Red: and wt[shift] is calculated higher than wt[shift+1] = it turns yellow ......... = wt[shift] > wt[shift+1]

.

Yellow is used as a "transition" from green to red or red to green. The one thing different about yellow, as opposed to red or green, is that you must 2 consecutive LSMAs of either higher + higher (to change yellow back to green) or lower + lower (to change yellow back to red). Also, the when the color changes, the LSMA is crossng the LR line when compared to the prior periods in the calculation. For example, when the LSMA is crossing LR line from below = red to yellow OR yellow to green depending on whether it was red or yellow to start with. Vice versa when it crosses LR from above. The exact value of LSMA is not a factor in the color change for this version of this indicator because the rules used in the indicator only depend on the calculated value of the LSMA for wt[shift] vs. wt[shift+1] and whether they are higher or lower in relation to each other.

.

I have attached chart below. I changed "Rperiod" to 5 to simplify things. On the chart, I used LR channels that come stock w/MT4 platform so you can see how the LSMA lines up with the right-most point of the LR line which. The color changes I drew red arrows to a few of them that seem to illustrate the 'color change' rules above.

.

I am very interested in working with this indicator to develop an EA and I have researched it quite a bit. However, I am not sure it is really all that much better at identifying a trend change than a double smoothed LWMA, for instance, or other MAs with a reduced lag. I guess only backtesting will answer that question. In Perry Kaufman's book, "New Trading Systems and Methods" he mentions that the PPMA is most useful for cyclical markets and in long-term time periods where the trend change can be identified and lined up with the PPMA "pivot point" (the point with 0 weighting in the PPMA formula).

Hope this clears a few things up.

- outofdebt

.

Notes on chart:

1st red arrow (from left) red to yellow because LSMA value at 6:35 is greater than the LSMA at 6:30 (this is wt[shift] > wt[shift+1]). It was yellow between first and second arrow because the LSMA at 6:40 was higher than at 6:35. On the next LSMA (the on the next time period after the 2nd arrow), it stayed yellow because when it is yellow, it must have 2 consecutive higher or lower LSMAs to change it back to red or green. It went red after that because the LSMA was lower at 6:45 than at 6:40. This is the 2nd consecutive lower LSMA changing yellow back to red.

.

It stayed red at 4th arrow, because the LSMA was lower than at the 3rd arrow. (this is the wt[shift] < wt[shift+1] scenario).

.

It went yellow between the 4th and 5th because the LSMA at the 5th arrow was higher than at the 4th.

.

It went green at the 5th arrow because the next LSMA was higher than at the 5th arrow. (Again, rememeber that there must be 2 consecutive highs or lows to change yellow to either red or green).

.

LSMA on a 5 min chart with Rperiod = 5 and LR channels:

Reason: