LSMA_ind_12xxx
Download scriptThis script is designed as a visual trading tool that tracks the Linear Weighted Moving Average (LSMA)—a technical indicator used to determine the general trend of a market. Unlike standard moving averages, the LSMA gives more importance to recent price action, making it more responsive to current market shifts.
Core Functionality
The primary purpose of this tool is to provide a dynamic, color-coded visual representation of the market's momentum. Instead of just showing a static line, it changes appearance based on whether the trend is moving upward or downward:
- Trend Detection: The script constantly calculates the difference between the current LSMA value and its previous value.
- Dynamic Coloring:
- When the trend is rising, the visual display highlights in green.
- When the trend is falling, the visual display highlights in red.
- When the price movement is neutral or within a specific "flat" range (defined by the user), it remains yellow.
Key Features for Traders
1. Visual Momentum Cues The script utilizes "buffer magic," which is a technical way of layering multiple lines on top of each other. By strategically hiding certain colors based on price action, it ensures that you only see the color relevant to the current trend, providing a clean and intuitive view of market direction.
2. Customizable Sensitivity Users can adjust how the indicator behaves through several settings:
- Period: Determines how many bars of data are analyzed to calculate the average.
- Slope: Allows users to set a specific threshold for what qualifies as a "rise" or "fall," filtering out minor market noise.
3. Optional Signal Markers (Arrows) The script includes an optional signaling system. If enabled, it can plot specific icons on the chart:
- Green Arrows: Appear during confirmed upward momentum.
- Red Arrows: Appear during confirmed downward momentum.
- Yellow Markers: Indicate periods where the price is moving sideways or staying within a neutral zone.
4. Precise Data Tracking For advanced analysis, the script can also display the exact numerical value of the difference (the "velocity" of the move) directly on the chart as text labels, allowing traders to see exactly how much the trend is shifting in real-time.
Summary
In short, this tool transforms a standard mathematical calculation into a clear, color-coded roadmap. It helps traders quickly identify whether the market is gaining strength, losing steam, or consolidating by using visual color shifts and optional directional arrows.
//+-----------+
//| LSMA |
//+-----------+
#property copyright "Copyright 2005 Ron Thompson"
#property link "http://www.forexmt4.com/"
/*
This indicator uses LSMA and colors it
red when falling and green when rising.
There is some buffer magic that makes this work.
The order of the buffers and colors is important.
Removing green exposes red
removing red exposes green
removing neither shows yellow (!?!)
*/
//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Yellow
#property indicator_color2 Green
#property indicator_color3 Red
#property indicator_color4 Green
#property indicator_color5 Red
#property indicator_color6 LawnGreen
#property indicator_color7 Red
#property indicator_color8 Yellow
//---- buffers
double ExtMapBuffer1[]; //Yellow
double ExtMapBuffer2[]; //Green
double ExtMapBuffer3[]; //Red
double XBuffer[]; //White
double ZBuffer[]; //White
double ExtMapBuffer6[]; //Green
double ExtMapBuffer7[]; //Red
double ExtMapBuffer8[]; //Yellow
extern int extRperiod = 14;
extern double extSlope = 0.0;
extern int extDraw4HowLong = 200;
extern bool showarrows=false;
extern int open1_close2=1;
extern bool showarrowvalues=false;
// bar open handling
int bartime;
//Objects
int uniq=0;
//+-----------+
//| Init |
//+-----------+
int init()
{
ObjectsDeleteAll();
SetIndexBuffer(0,ExtMapBuffer1); //Yellow
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
SetIndexBuffer(1,ExtMapBuffer2); //Green
SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
SetIndexBuffer(2,ExtMapBuffer3); //Red
SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, 2);
// 233 up arrow
// 234 down arrow
// 158 little dot
// 159 big dot
// 168 open square
// 120 box with X
SetIndexStyle(3,DRAW_ARROW);
SetIndexBuffer(3, XBuffer); //Green
SetIndexArrow(3,233);
SetIndexStyle(4,DRAW_ARROW);
SetIndexBuffer(4, ZBuffer); //Red
SetIndexArrow(4,234);
SetIndexBuffer(5,ExtMapBuffer6); //Green
SetIndexStyle(5, DRAW_ARROW);
SetIndexArrow(5,233);
SetIndexBuffer(6,ExtMapBuffer7); //Red
SetIndexStyle(6, DRAW_ARROW);
SetIndexArrow(6,234);
SetIndexBuffer(7,ExtMapBuffer8); //Yellow
SetIndexStyle(7, DRAW_ARROW);
SetIndexArrow(7,120);
Print("Init complete");
}
//+-----------+
//| DE-Init |
//+-----------+
int deinit()
{
int i;
for( i=0; i<Bars; i++ )
{
ExtMapBuffer1[i]=0;
ExtMapBuffer2[i]=0;
ExtMapBuffer3[i]=0;
XBuffer[i]=0;
ZBuffer[i]=0;
ExtMapBuffer6[i]=0;
ExtMapBuffer7[i]=0;
ExtMapBuffer8[i]=0;
}
ObjectsDeleteAll();
Print("DE-Init complete");
}
//+-----------+
//| Each Tick |
//+-----------+
int start()
{
int pos;
double wtp; //LSMA previous value
double wt; //LSMA current value
double wd; //difference
string sColor;
double pSlope=extSlope*Point;
// draw once at open of bar
if(bartime==Time[0]) return(0);
bartime=Time[0];
for(pos = extDraw4HowLong; pos >= 0; pos--)
{
//save previous value
wtp = wt;
//get next bar LSMA
wt = LSMA(pos);
//calculate difference
wd = (wt-wtp)/Point;
// draw actual LSMA line
ExtMapBuffer1[pos] = wt; //yellow
ExtMapBuffer2[pos] = wt; //green
ExtMapBuffer3[pos] = wt; //red
if (wtp < wt+(pSlope)) //rising, remove RED
{
ExtMapBuffer3[pos] = EMPTY_VALUE;
}
if (wtp > wt-(pSlope)) //falling, remove GREEN
{
ExtMapBuffer2[pos] = EMPTY_VALUE;
}
if(showarrows)
{
// Draw arrows that reflect the REAL difference levels
sColor="YELLOW";
if (wd > 0.0 && wd > extSlope ) //rising
{
sColor="GREEN";
ExtMapBuffer6[pos]=wt;//+(10*Point);
}
if (wd < 0.0 && wd < (extSlope*(-1)) ) //falling
{
sColor="RED";
ExtMapBuffer7[pos]=wt;//+(10*Point);
}
if (sColor=="YELLOW") //static - within slope
{
ExtMapBuffer8[pos]=wt;//+(10*Point);
}
}
if(showarrowvalues)
{
ObjectCreate("myx"+DoubleToStr(uniq,0), OBJ_TEXT, 0, Time[pos], wt );
ObjectSetText("myx"+DoubleToStr(uniq,0),DoubleToStr(wd,0),15,"Arial",White);
uniq++;
}
}//for
}//start
double LSMA(int mybar)
{
int i;
double sum=0;
double lengthvar = ( extRperiod + 1.0 ) / 3.0;
for(i = extRperiod; i >= 1 ; i--)
{
//open
if(open1_close2==1) sum += ( i - lengthvar)*Open[extRperiod - i + mybar];
//close
if(open1_close2==2) sum += ( i - lengthvar)*Close[extRperiod - i + mybar];
}
return(sum * 6 / (extRperiod*(extRperiod + 1)) );
}
Comments