PriceTrend1
Download scriptThis script is a technical analysis tool designed for the MetaTrader 4 platform to help traders identify and visualize market trends in real-time. It simplifies complex price movements into clear visual signals by comparing current price data against two different Moving Averages (MA).
Core Functionality
The logic operates by evaluating the relationship between three primary components:
- The Current Price: The script allows you to choose which specific price point to track (such as the Closing price, Opening price, High, Low, or various averages of these values).
- Main Trend (Moving Average 1): A longer-term average used to establish the primary direction of the market.
- Mini Trend (Moving Average 2): A shorter-term average used to identify immediate, smaller fluctuations in price momentum.
Trend Identification Logic
The script categorizes the market into four distinct states based on where the price sits in relation to these two averages:
- Main UpTrend: Occurs when the current price is above the Main Moving Average AND the Mini Moving Average is also trending above it.
- Main DownTrend: Occurs when the current price falls below the Main Moving Average AND the Mini Moving Average is also trending below it.
- Mini Trends: Independent of the main trend, the script tracks "Mini Up" or "Mini Down" movements by comparing the current price directly to the shorter Mini Moving Average.
- Wait State: If the conditions for a confirmed Main Trend are not met (e.g., the price is caught between the averages), the script identifies this as a "Wait" period, signaling a lack of clear directional momentum.
Visual and Audible Alerts
To ensure traders can react quickly without constantly staring at charts, the script provides several notification layers:
- On-Screen Labels: The tool dynamically places text labels on your chart window. These labels change color (Lime for Up, Red for Down, and Yellow for Wait) to give an instant visual summary of the trend status.
- Pop-up Alerts: The script monitors for specific "state changes." When the market shifts from a neutral state into a confirmed Main Trend, or when a Mini Trend flips, the software generates an audible alert and a pop-up notification on your screen.
- Smart Filtering: To prevent constant noise, the alerts are programmed to trigger only once per bar, ensuring you are notified of significant trend changes rather than every minor price tick.
Summary for the User
Essentially, this tool acts as a trend filter. It filters out market "noise" by requiring multiple confirmations (Price vs. Long Average vs. Short Average) before declaring a trend. This allows you to see at a glance whether the market is currently in a bullish move, a bearish move, or a period of consolidation where it is best to wait.
//+------------------------------------------------------------------+
//| PriceTrender2.mq4 |
//| Kalenzo |
//| bartlomiej.gorski@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Kalenzo"
#property link "bartlomiej.gorski@gmail.com"
#property indicator_buffers 3
#property indicator_color1 LightSkyBlue
#property indicator_color2 Yellow
#property indicator_color3 Red
extern bool Comments=false;
extern int TimeFrame = 60,
Price = 0,
Ma1Type = 0,
Ma1Price = 0,
Ma1Length = 28,
Ma2Type = 0,
Ma2Price = 0,
Ma2Length = 5;
double price[],trend[],trend2[];
double alertBar;
double prev;
double last;
string info;
string infop;
string infoprev;
string infomini;
string ENTRY_price;
string entry_trend2;
string WAIT_price;
string wait_price;
string MINI;
string mini;
extern bool minisounds=true;
#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorShortName("PT");
//---- indicators
SetIndexBuffer(0,price);
SetIndexBuffer(1,trend);
SetIndexBuffer(2,trend2);
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1);
SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit, bigshift;
int counted_bars=IndicatorCounted();
//----
if (counted_bars<0) return(-1);
if (counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for (int i=limit; i>=0; i--)
{
bigshift = iBarShift(Symbol(),TimeFrame,Time[i]);
price[i] = getPrice(bigshift);
trend[i] = iMA(Symbol(),TimeFrame,Ma1Length,0,Ma1Type,Ma1Price,bigshift);
trend2[i] = iMA(Symbol(),TimeFrame,Ma2Length,0,Ma2Type,Ma2Price,bigshift);
{ENTRY_price= "";entry_trend2= "";WAIT_price= "";wait_price= "";MINI= "";mini= "";info = "";infop = "";}
//Trend UP/DOWN
if ((price[i]> trend[i])&& (trend2[i]> trend[i])) {entry_trend2= "MAIN/UP";info = entry_trend2;}
if ((price[i]< trend[i])&& (trend2[i]< trend[i])) {ENTRY_price= "MAIN/DN";info = ENTRY_price;}
if ((price[i]>= trend[i] || trend2[i]> trend[i]) && info == "") {WAIT_price= "WAIT";info = WAIT_price;}
if ((price[i]< trend[i] || trend2[i]< trend[i]) && info == "") {WAIT_price= "WAIT";info = WAIT_price;}
if (price[i]> trend2[i]) {MINI= "MINI/UP";infop=MINI;}
if (price[i]< trend2[i]) {mini= "MINI/DN";infop=mini;}
// Alerts on
if(infoprev == "") infoprev = info;
if(infomini == "") infomini = infop;
if(info != infoprev && info == "MAIN/UP" && Bars>alertBar) {Alert("Price Trend MAIN changing to UpTrend on ",Symbol()," Period ",Period());alertBar = Bars;infoprev = info;}
if(info != infoprev && info == "MAIN/DN" && Bars>alertBar) {Alert("Price Trend MAIN changing to DownTrend on ",Symbol()," Period ",Period());alertBar = Bars;infoprev = info;}
if(minisounds == true && infomini != infop && infop == "MINI/UP" && Bars>alertBar) {Alert("Price Trend Mini changing to UpTrend on ",Symbol()," Period ",Period());alertBar = Bars;infomini = infop;}
if(minisounds == true && infomini != infop && infop == "MINI/DN" && Bars>alertBar) {Alert("Price Trend Mini changing to DownTrend on ",Symbol()," Period ",Period());alertBar = Bars;infomini = infop;}
// Alerts done
ObjectCreate("ESIG2", OBJ_LABEL, WindowFind("PT"), 0, 0);//TREND UP
ObjectSetText("ESIG2",StringSubstr(entry_trend2,0),10, "Arial Bold", Lime);
ObjectSet("ESIG2", OBJPROP_CORNER, 0);
ObjectSet("ESIG2", OBJPROP_XDISTANCE, 250);
ObjectSet("ESIG2", OBJPROP_YDISTANCE, 15);
ObjectCreate("ESIG3", OBJ_LABEL, WindowFind("PT"), 0, 0);//TREND DOWN
ObjectSetText("ESIG3",StringSubstr(ENTRY_price,0),10, "Arial Bold", Red);
ObjectSet("ESIG3", OBJPROP_CORNER, 0);
ObjectSet("ESIG3", OBJPROP_XDISTANCE, 250);
ObjectSet("ESIG3", OBJPROP_YDISTANCE, 15);
ObjectCreate("ESIG4", OBJ_LABEL, WindowFind("PT"), 0, 0);//WAIT
ObjectSetText("ESIG4",StringSubstr(WAIT_price,0),10, "Arial Bold", Yellow);
ObjectSet("ESIG4", OBJPROP_CORNER, 0);
ObjectSet("ESIG4", OBJPROP_XDISTANCE, 263);
ObjectSet("ESIG4", OBJPROP_YDISTANCE, 15);
ObjectCreate("ESIG5", OBJ_LABEL, WindowFind("PT"), 0, 0);//WAIT
ObjectSetText("ESIG5",StringSubstr(wait_price,0),10, "Arial Bold", Yellow);
ObjectSet("ESIG5", OBJPROP_CORNER, 0);
ObjectSet("ESIG5", OBJPROP_XDISTANCE, 263);
ObjectSet("ESIG5", OBJPROP_YDISTANCE, 15);
ObjectCreate("ESIG6", OBJ_LABEL, WindowFind("PT"), 0, 0);//Mini UP
ObjectSetText("ESIG6",StringSubstr(MINI,0),10, "Arial Bold", Lime);
ObjectSet("ESIG6", OBJPROP_CORNER, 0);
ObjectSet("ESIG6", OBJPROP_XDISTANCE, 253);
ObjectSet("ESIG6", OBJPROP_YDISTANCE, 35);
ObjectCreate("ESIG7", OBJ_LABEL, WindowFind("PT"), 0, 0);//Mini DOWN
ObjectSetText("ESIG7",StringSubstr(mini,0),10, "Arial Bold", Red);
ObjectSet("ESIG7", OBJPROP_CORNER, 0);
ObjectSet("ESIG7", OBJPROP_XDISTANCE, 253);
ObjectSet("ESIG7", OBJPROP_YDISTANCE, 35);
}
//----
return(0);
}
//+------------------------------------------------------------------+
double getPrice(int shift)
{
switch(Price)
{
case 0 : return ( iClose(Symbol(),TimeFrame,shift) );
case 1 : return (iOpen(Symbol(),TimeFrame,shift));
case 2 : return (iHigh(Symbol(),TimeFrame,shift));
case 3 : return (iLow(Symbol(),TimeFrame,shift));
case 4 : return ((iLow(Symbol(),TimeFrame,shift)+iHigh(Symbol(),TimeFrame,shift))/2);
case 5 : return ((iClose(Symbol(),TimeFrame,shift)+iLow(Symbol(),TimeFrame,shift)+iHigh(Symbol(),TimeFrame,shift))/3);
case 6 : return ((iOpen(Symbol(),TimeFrame,shift)+iClose(Symbol(),TimeFrame,shift)+iLow(Symbol(),TimeFrame,shift)+iHigh(Symbol(),TimeFrame,shift))/4);
}
}
Comments