WeightedCross

Author: niva
WeightedCross

The WeightedCross script is a custom trading tool designed to analyze market trends by tracking the relationship between two different moving averages. It simplifies complex price movements into visual cues and automated alerts, helping traders identify potential shifts in momentum.

Core Logic Overview

The script operates by calculating two distinct mathematical "averages" of the market price over different timeframes. By comparing these two averages, the tool determines whether the current market sentiment is leaning upward or downward.

  • The Fast Average: This line reacts more quickly to recent price changes. It is designed to capture immediate momentum.
  • The Slow Average: This line is calculated over a longer duration, providing a smoother look at the broader trend and filtering out minor "noise" in the market.

How it Works

The script continuously monitors the gap between these two averages. Its primary goal is to detect the moment one line crosses over the other:

  1. Trend Calculation: The tool uses a "Linearly Weighted Moving Average" (LWMA) method. This specific method gives more importance to the most recent price data, making it more responsive than a standard average.
  2. The Cross Signal:
    • Upward Cross: When the faster average moves above the slower average, the script recognizes this as a potential bullish signal (an "up cross").
    • Downward Cross: When the faster average drops below the slower average, the script recognizes this as a potential bearish signal (a "down cross").
  3. Alert System: To ensure traders don't miss these moments, the script is programmed to trigger an instant notification (an alert) on the trading platform the very first time a crossover occurs in either direction.

Visual Representation

On your trading chart, the tool generates two colored lines (Red and Blue). These allow you to visually track the "path" of the market trends:

  • When the lines are moving in the same direction, it confirms the current trend.
  • When the lines intersect or cross each other, it highlights a shift in momentum, providing a clear point of reference for potential trading opportunities.
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
4 Views
0 Downloads
0 Favorites
WeightedCross
//+------------------------------------------------------------------+
//|                                                WeightedCross.mq4 |
//|                                                             niva |
//|                                                        forex-tsd |
//+------------------------------------------------------------------+
#property copyright "niva"
#property link      "forex-tsd"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
//---- indicator parameters
extern int Fast_Period=13;
extern int Slow_Period=20;
//---- indicator buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
bool up=false;
bool down=false;
//----

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   string short_name = "WeightedCross";
   IndicatorShortName(short_name);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();

//---- check for possibble errors
   if (counted_bars<0) return(-1);
  //--- last counted bar will be recounted
   if (counted_bars>0) counted_bars--;
   
   int pos=Bars-counted_bars;
//----

   //----main calculation loop
   while(pos>=0)
   {
      ExtMapBuffer1[pos] = iMA(NULL,0,Fast_Period,8,MODE_LWMA,PRICE_CLOSE,pos);
      ExtMapBuffer2[pos] = iMA(NULL,0,Slow_Period,8,MODE_LWMA,PRICE_CLOSE,pos);
      pos--;
      }
      double difference=ExtMapBuffer1[1]-ExtMapBuffer2[2];
      if(difference>0 && up==false){
         Alert ("up cross");
         up=true;
         down=false;
      }
      if (difference<=0 && down==false){
         Alert ("down corss");
         down=true;
         up=false;
      }
      
   return(0);
  }
//+------------------------------------------------------------------+

Comments