Overview
This script is a technical analysis tool designed to provide visual feedback on market trends by comparing current price action against two different moving averages. It simplifies complex market data into clear, color-coded labels and audible alerts, helping users identify whether the momentum is moving upward, downward, or in a state of uncertainty.
Core Logic & Functionality
1. Data Multi-Layering The tool analyzes the market by looking at three distinct components simultaneously:
- Price Action: The script can be configured to look at various price points (such as the closing price, opening price, highs, lows, or various averages of these).
- Major Trend (Long-term): A primary moving average that tracks the broader direction of the market.
- Minor Trend (Short-term): A secondary moving average that tracks immediate, faster price fluctuations.
2. Trend Identification System The script evaluates the relationship between these three components to determine the current market "state." It categorizes the environment into four specific conditions:
- Main Uptrend: Triggered when both the current price and the short-term trend are consistently above the long-term trend. This suggests strong bullish momentum.
- Main Downtrend: Triggered when both the current price and the short-term trend are consistently below the long-term trend. This suggests strong bearish momentum.
- Wait State (Neutrality): If the price is on one side of the major trend but the short-term movement is on the opposite side, the script identifies this as a "WAIT" signal. This indicates conflicting data or a period of transition where the market direction is unclear.
- Mini Trends: In addition to the main status, the tool provides a secondary check on the immediate price relative to the short-term trend, labeling it as a "Mini Up" or "Mini Down" to help users see rapid shifts before they become major trends.
3. Visual and Audible Feedback To ensure the user can make quick decisions, the script translates this logic into a real-time dashboard:
- On-Screen Labels: The tool dynamically places text labels on your chart (e.g., "MAIN/UP", "WAIT", "MINI/DN") and changes their colors (Lime, Red, or Orange) based on the identified trend.
- Smart Alerts: The script includes an automated notification system. When the market shifts from a neutral state into a confirmed Main Uptrend or DownTrend, it triggers an alert to notify you of the change immediately.
- Duplicate Prevention: The logic includes a "memory" feature to ensure that alerts are only triggered once per trend change, preventing repetitive notifications for the same price movement.
Summary of Use
Essentially, this tool acts as a filter. Instead of forcing a trader to manually calculate where the price sits in relation to multiple moving averages, the script does the math instantly and presents a simplified summary: "Buy," "Sell," "Wait," or "Minor Shift."
//+------------------------------------------------------------------+
//| PriceTrender2.mq4 |
//| Kalenzo |
//| bartlomiej.gorski@gmail.com |
//+------------------------------------------------------------------+
//mod
#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,
TxtCorner = 1,
TxtXDistance = 0;
double price[],trend[],trend2[];
double alertBar;
double prev;
double last;
string info;
string infoprev;
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);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_LINE);
//----
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);
string ENTRY_price= "",entry_trend2= "",WAIT_price= "",wait_price= "",MINI= "",
mini= "";
info = "";
//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])) {WAIT_price= "WAIT";info = WAIT_price;}
if ((price[i]> trend[i])&& (trend2[i]< trend[i])) {wait_price= "WAIT";info = wait_price;}
if (price[i]> trend2[i]) {MINI= "MINI/UP";}
if (price[i]< trend2[i]) {mini= "MINI/DN";}
if(infoprev == "") infoprev = info;
if(info != infoprev && info == "MAIN/UP" && Bars>alertBar) {Alert("Price Trend changing to UpTrend on ",Symbol()," Period ",Period());alertBar = Bars;infoprev = info;}
if(info != infoprev && info == "MAIN/DN" && Bars>alertBar) {Alert("Price Trend changing to DownTrend on ",Symbol()," Period ",Period());alertBar = Bars;infoprev = info;}
ObjectCreate("ESIG2", OBJ_LABEL, WindowFind("PT"), 0, 0);//TREND UP
ObjectSetText("ESIG2",StringSubstr(entry_trend2,0),10, "Arial Bold", Lime);
ObjectSet("ESIG2", OBJPROP_CORNER, TxtCorner);
ObjectSet("ESIG2", OBJPROP_XDISTANCE, 8 +TxtXDistance);
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, TxtCorner);
ObjectSet("ESIG3", OBJPROP_XDISTANCE, 8 +TxtXDistance);
ObjectSet("ESIG3", OBJPROP_YDISTANCE, 15);
ObjectCreate("ESIG4", OBJ_LABEL, WindowFind("PT"), 0, 0);//WAIT
ObjectSetText("ESIG4",StringSubstr(WAIT_price,0),10, "Arial Bold", Orange);
ObjectSet("ESIG4", OBJPROP_CORNER, TxtCorner);
ObjectSet("ESIG4", OBJPROP_XDISTANCE, 13 +TxtXDistance);
ObjectSet("ESIG4", OBJPROP_YDISTANCE, 15);
ObjectCreate("ESIG5", OBJ_LABEL, WindowFind("PT"), 0, 0);//WAIT
ObjectSetText("ESIG5",StringSubstr(wait_price,0),10, "Arial Bold", Orange);
ObjectSet("ESIG5", OBJPROP_CORNER, TxtCorner);
ObjectSet("ESIG5", OBJPROP_XDISTANCE, 13 +TxtXDistance);
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, TxtCorner);
ObjectSet("ESIG6", OBJPROP_XDISTANCE, 13 +TxtXDistance);
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, TxtCorner);
ObjectSet("ESIG7", OBJPROP_XDISTANCE, 13 +TxtXDistance);
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