Least Squares Moving Average Color Coding

 
Hi,
I like the Least Squares Moving Average and it's color coding. I would like to use it on some other (non-forex) markets I trade, as well. Unfortunately, I can't make heads or tails out of MQL4, though I can program indicators in Tradestation. Can someone please look at the code for this and translate it into normal logic statements for me? I assume the LSMA is simply a (moving) linear regression. If that is true, I'm really just looking for the logic of the color coding so I can apply it to my Tradestation platform, as well. I appreciate any help anyone can give me.

Best regards, Scott
 
Hi,

i am very fluent in TS programming. Maybe i can help you. But i am not sure what you want to do. Is it this:

You want the color of your indicator changing acording to certain criteria?


regards, mathias
 
Hi Mathias,

When I compare the MT4 Least Squares Moving Average to Tradestation's Linear Regression Curve, they are certainly the same. However, the MT4 indicator has some nice red/green/yellow color coding which I like a lot.
My Tradestation indicator is just one color. If you look at the MT4 Least Squares MA, the color coding logic is not simple up/down logic, (ie, if MA >= MA[1] then green, if MA < MA[1] then red) it is something else. I like this particular indicator coloring and would like to apply it to the Tradestation Linear Regression Curve. I am reasonably fluent in TS as well. I'm sure I could program the color into the TS LRC indicator if I could understand the coloring logic in the MQL4 code.

So, my question is, what is the indicator color coding logic contained in the following code?

Thanks, Scott

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

//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 3
#property indicator_color1 Yellow      
#property indicator_color2 Green
#property indicator_color3 Red

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

extern int Rperiod = 34;
extern int Draw4HowLongg = 1500;
int Draw4HowLong;
int shift;
int i;
int loopbegin;
double sum[];
int length;
double lengthvar;
double tmp ;
double wt[];
int c;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
   
//---- drawing settings
   SetIndexBuffer(2,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(0,ExtMapBuffer3);
   SetIndexBuffer(3,sum);
   SetIndexBuffer(4,wt);
   
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);

//---- initialization done
   return(0);
  }

int start()

  {   Draw4HowLong = Bars-Rperiod - 5;
      length = Rperiod;
      loopbegin = Draw4HowLong - length - 1;
 
      for(shift = loopbegin; shift >= 0; shift--)
      { 
         sum[1] = 0;
         for(i = length; i >= 1  ; i--)
         {
         lengthvar = length + 1;
         lengthvar /= 3;
         tmp = 0;
         tmp = ( i - lengthvar)*Close[length-i+shift];
         sum[1]+=tmp;
         }
         wt[shift] = sum[1]*6/(length*(length+1));
         
//========== COLOR CODING ===========================================               
        
       ExtMapBuffer3[shift] = wt[shift]; //red 
       ExtMapBuffer2[shift] = wt[shift]; //green
       ExtMapBuffer1[shift] = wt[shift]; //yellow
       
       //  for(c=loopbegin;c==shift;c++)
       // {
        if (wt[shift+1] > wt[shift])
        {
        ExtMapBuffer2[shift+1] = EMPTY_VALUE;
  // ObjectCreate("smiley_face", OBJ_ARROW, 0, Time[shift], Low[shift]-Point*20);
  // Print("time=  ",Time[shift]);
  // ObjectSet("smiley_face", OBJPROP_ARROWCODE, 242);
  // ObjectSet("smiley_face", OBJPROP_COLOR , Red);
  // ObjectSet("smiley_face", OBJPROP_WIDTH  , 1);
  // ObjectsRedraw();

        //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
        //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
        
        }
       else if (wt[shift+1] < wt[shift]) 
        {
        ExtMapBuffer1[shift+1] = EMPTY_VALUE; //-1 red/greem tight
       //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
        
        }
         else 
         {
         
         ExtMapBuffer1[shift+1]=CLR_NONE;//EMPTY_VALUE;
         ExtMapBuffer2[shift+1]=CLR_NONE;//EMPTY_VALUE;
         }
        
      }
    
      return(0);
  }
//+------------------------------------------------------------------+
 
Any help I could get with this would be much appreciated.

Thx,
Scott
 
one color per indicator index line

when it is going up, it draws with index 2
When flat, index 1
When going down, index 3

It puts empty value into the indexes that are not used at any particular bar.
 
Hi Phy,
I appreciate your answer. And I'm still in the dark. Looking at "real" code, like MQL4 and C and so on makes me feel really stupid, because I don't understand it at all. I can do some straightforward stuff in Tradestation with indicators, but I am not a programmer. I am all self-taught. What is probably clear to you is not clear to me :) I know from looking at the chart that the algorithm is a little more subtle. Sometimes the indicator changes green to yellow when price is still going up, sometimes not, etc.

Could you please write in sentences how the green/red/yellow coding algorithm works? For example, what does

for(shift = loopbegin; shift >= 0; shift--)
{
sum[1] = 0;
for(i = length; i >= 1 ; i--)
{
lengthvar = length + 1;
lengthvar /= 3;
tmp = 0;
tmp = ( i - lengthvar)*Close[length-i+shift];
sum[1]+=tmp;
}
wt[shift] = sum[1]*6/(length*(length+1));

mean in relation to the indexes?

I may be asking for too much. I don't expect you to give me a college course in programming in a forum. But if you could give me some logic statements that explain the coloring algorithm I might be able to convert it to Tradestation. Or it might be beyond me right now. In any case, I just downloaded what looks like a very nice MQL4 tutorial (by coder's guru from forex-tsd.com) and I think I will spend the weekend reading that to try to get a handle on this.

Best,
Scott
 
That part is someones interpretation of "how to find the least squares moving average"

It has nothing to do with color.

"I'm really just looking for the logic of the color coding"

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

wt[] is an array of the LSMA values.

For color coding, he compares the value of the LSMA one bar back and the current LSMA as he moves through the chart. If the prior value was higher, draw red, if lower, draw green, else draw yellow.

shift is a counting index for the bars in the chart, shift+1 indexes the prior bar.
 
Thank you Phy, that make sense to me. What doesn't make sense to me is that the indicator doesn't seem to be doing what you say. I enclosed 2 pics of the indicator on the GBPJPY, from an Alpari demo and an ODL demo. You can see they are doing the same thing (with allowances for the slight diff in datafeeds). I put arrows on the Alpari chart to point out a lot of places where the indicator has turned from green to yellow or red to yellow, but the indicator is not at all flat. I maked a place with a big blue arrow where, not only is the indicator not flat, the slope of the indicator is even increasing as it is still painting a color change from red to yellow!

So, I do understand your explanation, but I still don't understand why the indicator looks this way. Do you think this indicator is repainting (ie, using future data to change the color... in effect using the [shift - 1] data?).

I could do what you described in Tradestation easily enough with some IF statements and PLOT statements. But I don't think the yellow transitions will look similar at all. I think there must be something else to the color coding.....

Any thoughts?

Thanks, Scott


Alpari
[img]
http://turboscottomatic.wordpress.com/files/2008/02/lsma-alpari-022908.gif
[/img]


ODL
[img]
http://turboscottomatic.wordpress.com/files/2008/02/lsma-odl-022908.gif
[/img]
 
Hi Phy,
I figured out what is going on here - this indicator DOES repaint. I just watched it for a while on a one minute chart. Now everything is making more sense - for a moment I thought I'd found the Grail!

What it does in real time is this:
(Market has peaked and is now reversing - first bar of reversal)
The indicator shows green all the way up to it's turning point. The current bar LSMA is starting to roll over, the indicator is flashing green to red as the market ticks up and down.
Once the current bar completes, if the LSMA is < LSMA[1] (remember, LSMA[1] > LSMA[2] because the market is in 1st bar of reversal) then the LSMA for the current bar (the one that just completed) goes RED and it REPAINTS THE LSMA color from green to yellow!

You can watch it do this in real time on a 1 minute chart. So, I guess I answered my own question. The color coding logic for the LSMA is this:

"If able to see the future, recolor LSMA accordingly for most (fantasy) profit"

It doesn't make the indicator totally useless, but manually backtesting with an expectation that the yellow coloring can be counted on (for exits, etc) will lead to ruin...

Scott
 
If you are referring to the color changing on the current bar, there is nothiing unexpected about that. If it repaints the prior bar, then that is not good.
 
It re-paints the prior bar and I agree, that is not good. You can see it yourself if you sit with a LSMA(8) on a 1 min chart for 10-15 minutes. When LMSA reverses, for example, from long to short, it paints the current bar red and it repaints the (previously green!) prior bar to yellow.

Scott