LSMA in Color4_Alert

Download script
LSMA in Color4_Alert

This script is a specialized technical indicator designed for the MetaTrader platform to identify potential market trend reversals or momentum shifts. It works by monitoring price action and identifying specific "cross" events where the trend changes direction.

Core Logic Overview

The tool functions by calculating a custom moving average, known as the LSMA (Linearly Weighted Moving Average). Unlike a standard moving average that treats all past data equally, this calculation gives more weight to more recent price information. This makes the indicator more responsive to current market movements while still filtering out minor "noise."

Key Components of the Analysis

To determine when to display a signal, the script evaluates several specific factors:

  • Price Mode Selection: The user can choose which type of price data the calculation should focus on (such as the Closing price, the High/Low range, or various weighted averages of these prices). This allows the tool to be adjusted based on different trading styles.
  • Momentum Detection: The script compares the current value of the moving average against its previous value. It looks for a specific "distance" (a threshold) between these two points. If the gap is large enough, it identifies a significant shift in momentum.
  • Trend Identification:
    • Buy Signals: Triggered when the current moving average rises significantly above the previous one.
    • Sell Signals: Triggered when the moving average drops significantly below the previous one.
  • Dynamic Positioning: Instead of placing a signal exactly on the price line, the script calculates the "Range" of the market (the volatility over the last 10 candles). It then places visual arrows slightly above or below the candles based on this range to ensure they are clearly visible to the trader.

Trading Constraints & Alerts

The script includes several "smart" filters to help manage the trading environment:

  • Time Windows: The indicator can be restricted to specific hours of the day (e.g., only during the London or New York sessions). It recognizes Daylight Savings Time to ensure these windows remain accurate.
  • Signal Filtering: It uses a "flag" system to prevent the script from repeating the same signal multiple times in a row. It ensures that once a Buy or Sell signal is triggered, it won't repeat until the opposite trend occurs.
  • Audio Notifications: When a valid signal is identified during active trading hours, the script can trigger an audible alert and a pop-up notification on the screen, providing the current price, time, and currency symbol.
Price Data Components
Series array that contains open time of each bar
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
12 Views
0 Downloads
0 Favorites
LSMA in Color4_Alert
//+------------------------------------------------------------------+
//|                                                        |
//|                      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 2
#property indicator_color1 LawnGreen
#property indicator_color2 Red
#property indicator_width1  2
#property indicator_width2  2

extern bool SoundON=true;

//---- buffers
double CrossUp[];
double CrossDown[];
int flagval1 = 0;
int flagval2 = 0;

extern int Rperiod = 34;
int FastPriceMode = 0;//0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2, 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4
 int LSMA_Dif = 6;
 int LookBack = 2;
 int SignalCandle = 1;
 
extern bool UseDST = true;
 int StartHourDST = 7;       // Start trades after time
 int StopHourDST = 15;      // Stop trading after time
 int StartHourNormal = 8;
 int StopHourNormal = 16;
 int StartHour;
 int StopHour;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 2 additional buffers are used for counting.
//   IndicatorBuffers(3);
   
//---- drawing settings
   SetIndexBuffer(0, CrossUp);
   SetIndexBuffer(1, CrossDown);
   
   SetIndexStyle(0, DRAW_ARROW, EMPTY);
   SetIndexArrow(0, 233);
   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   SetIndexArrow(1, 234);

   if (UseDST)
   {
     StartHour = StartHourDST;
     StopHour = StopHourDST;
   }
   else
   {
     StartHour = StartHourNormal;
     StopHour = StopHourNormal;
   }
//---- initialization done
   return(0);
  }

//+------------------------------------------------------------------+
//| LSMA with PriceMode                                              |
//| PrMode  0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2,    |
//| 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4  |
//+------------------------------------------------------------------+

double LSMA(int Rperiod, int prMode, int shift)
{
   int i;
   double sum, pr;
   int length;
   double lengthvar;
   double tmp;
   double wt;

   length = Rperiod;
 
   sum = 0;
   for(i = length; i >= 1  ; i--)
   {
     lengthvar = length + 1;
     lengthvar /= 3;
     tmp = 0;
     switch (prMode)
     {
     case 0: pr = Close[length-i+shift];break;
     case 1: pr = Open[length-i+shift];break;
     case 2: pr = High[length-i+shift];break;
     case 3: pr = Low[length-i+shift];break;
     case 4: pr = (High[length-i+shift] + Low[length-i+shift])/2;break;
     case 5: pr = (High[length-i+shift] + Low[length-i+shift] + Close[length-i+shift])/3;break;
     case 6: pr = (High[length-i+shift] + Low[length-i+shift] + Close[length-i+shift] + Close[length-i+shift])/4;break;
     }
     tmp = ( i - lengthvar)*pr;
     sum+=tmp;
    }
    wt = MathFloor(sum*6/(length*(length+1))/Point)*Point;
    
    return(wt);
}

int start()

  {
   int limit, counter, shift;
   double Range, AvgRange;
   double fastMAnow, fastMAprevious;
   int counted_bars=IndicatorCounted();
  
 //---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;

   limit=Bars-counted_bars;

 
   for(shift = 0; shift <= limit; shift++)
   { 
      Range=0;
      AvgRange=0;
      for (counter=shift ;counter<=shift+9;counter++)
      {
         AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
      }
      Range=AvgRange/10;
      fastMAnow = LSMA(Rperiod, FastPriceMode, shift + SignalCandle);
      fastMAprevious = LSMA(Rperiod, FastPriceMode,  shift + SignalCandle + LookBack);
         
//========== COLOR CODING ===========================================               
        
       
   if (Hour() >= StartHour && Hour() <= StopHour)
   {
      CrossUp[shift] = 0;
      CrossDown[shift] = 0;
        if (fastMAnow - fastMAprevious > LSMA_Dif*Point)
        {
         if (flagval1==0)
         {
           flagval1=1;
           flagval2=0;
           if (shift == 1 && SoundON ) Alert("BUY signal at Ask=",Ask,"\n Bid=",Bid,"\n Time=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
           CrossUp[shift] = Low[shift] - Range*0.75;
         }

        }
       else if (fastMAprevious - fastMAnow > LSMA_Dif*Point ) 
        {
         if ( flagval2==0)
         {
          flagval2=1;
          flagval1=0;
          if (shift == 1 && SoundON) Alert("SELL signal at Ask=",Ask,"\n Bid=",Bid,"\n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
          CrossDown[shift] = High[shift] + Range*0.75;
         }
        
        }
      }
    }
    
      return(0);
  }
//+------------------------------------------------------------------+



Comments