LSMAinColor
Download scriptThis script is designed as a visual momentum and trend indicator for the MetaTrader trading platform. Its primary goal is to analyze price movement over a specific period to help traders identify the direction and strength of the current market trend.
Core Logic and Methodology
The tool operates by calculating a weighted average of the closing prices over a user-defined period (defaulted to 25 bars). Unlike a simple moving average, which treats all previous prices as equally important, this script applies a specific mathematical weighting system. It prioritizes certain price points within that window to create a smoother, more reactive line that tracks the "flow" of the market.
How it Works on the Chart
The indicator processes historical data and generates three distinct visual layers (colored in DarkKhaki, SeaGreen, and Maroon). Here is the step-by-step logic the script follows:
- Data Processing: The script looks back at a set number of previous candles. It performs a calculation where it multiplies the price of each candle by a specific factor based on its position in time.
- Trend Detection: By calculating these weighted values, the script determines if the current momentum is moving upward or downward compared to the previous data point.
- Dynamic Coloring: The most important feature for the user is the visual feedback. The script compares the current calculated value against the previous one:
- If the trend is moving in one direction (e.g., rising), it highlights a specific color to signify strength.
- If the trend reverses or flattens out, the script automatically "clears" the opposite color from the chart.
- This creates a clean visual effect where only the active trend color remains visible, allowing the trader to see the current market direction at a glance without unnecessary visual clutter.
Summary for the User
Essentially, this tool acts as a visual guide. It filters out "market noise" by calculating a weighted trend line and then uses dynamic coloring to show you exactly where the momentum is heading. Instead of looking at raw price fluctuations, you are viewing a smoothed representation of market movement that highlights whether the current push is bullish or bearish.
//+------------------------------------------------------------------+
//| |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005"
#property link "http://www.metaquotes.net/"
//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 DarkKhaki
#property indicator_color2 SeaGreen
#property indicator_color3 Maroon
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
int width;
extern int Rperiod = 25;
extern int Draw4HowLongg = 5500;
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);
}
//+------------------------------------------------------------------+
Comments