PriceTrend1x2
Download scriptOverview of the PriceTrender2 Logic
The PriceTrender2 script is a multi-timeframe trend analysis tool designed to help traders identify market momentum by comparing different moving averages across various timeframes simultaneously. Instead of looking at a single chart, this logic synchronizes data from three distinct time periods to determine "Main" and "Mini" trends.
Core Functionality
The script operates by calculating three separate moving averages (MAs) based on user-defined settings for length, type, and timeframe. It then evaluates the relationship between these averages to categorize the market state into four primary conditions:
1. Main Trend Identification
The tool looks for a "Main" trend by comparing the current price action against two different moving average layers.
- Main UpTrend: Occurs when the price is above the primary moving average and the secondary moving average is also positioned above that same primary average.
- Main DownTrend: Occurs when the price falls below the primary moving average and the secondary moving average is also positioned below it.
2. Mini Trend Identification
In addition to the main trend, the script identifies a "Mini" trend. This provides a faster, more immediate look at price movement relative to a specific threshold.
- Mini UpTrend: Triggered when the current price is above the secondary moving average.
- Mini DownTrend: Triggered when the current price falls below the secondary moving average.
3. Neutral/Wait State
If the price action does not clearly satisfy the specific conditions for a Main UpTrend or a Main DownTrend, the script classifies the market as "WAIT." This indicates a period of transition or indecision where no clear direction has been established by the moving averages.
Visual and Audio Alerts
To ensure the trader is notified of significant shifts in market sentiment, the script includes several automated features:
- On-Screen Labels: The script dynamically creates visual labels on the chart. These labels change color and text (e.g., Lime for UpTrends, Red for DownTrends, Yellow for Wait states) to provide a real-time "dashboard" of the market's current trend status.
- Dynamic Alerts: The system monitors for changes in state. If the trend flips from one direction to another (for example, moving from a Main DownTrend to a Main UpTrend), the script triggers an audible and visual alert.
- Mini Trend Sounds: There is a specific option to enable sounds specifically for "Mini" trend changes, allowing traders to hear immediate feedback on shorter-term price fluctuations without being alerted to every minor movement.
Summary of User Experience
By utilizing this logic, a user can see at a glance whether the market is in a strong primary move (Main Trend), a fast-moving secondary move (Mini Trend), or a neutral zone (Wait). It effectively filters out "noise" by requiring multiple conditions to be met before confirming a trend change.
//+------------------------------------------------------------------+
//| PriceTrender2.mq4 |
//| Kalenzo |
//| bartlomiej.gorski@gmail.com |
//+------------------------------------------------------------------+
//mod.fxtsd increase
#property copyright "Kalenzo"
#property link "bartlomiej.gorski@gmail.com"
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Yellow
#property indicator_color3 Green
#property indicator_width1 2
#property indicator_width2 2
extern bool Comments=false;
extern int TimeFrame2 = 60,
Ma2Length = 5,
Ma2Type = 0,
Ma2Price = 0;
extern int TimeFrame1 = 30,
Ma1Length = 7,
Ma1Type = 0,
Ma1Price = 0;
extern int TimeFrame = 0,
MaLength = 9,
MaType = 0,
MaPrice = 0;
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;
string sname;
extern bool minisounds=true;
#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
SetIndexBuffer(0,trend2);
SetIndexBuffer(1,trend);
SetIndexBuffer(2,price);
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_LINE);
TimeFrame =MathMax(TimeFrame, Period());
TimeFrame1=MathMax(TimeFrame1,Period());
TimeFrame2=MathMax(TimeFrame2,Period());
string name = " MA ("+MaLength+") ["+TimeFrame+"] ";
string name1 = " MA1 ("+Ma1Length+") ["+TimeFrame1+"] ";
string name2 = " MA2 ("+Ma2Length+") ["+TimeFrame2+"] ;";
sname =name+name1+name2;
IndicatorShortName("PT:");
SetIndexLabel(2,name);
SetIndexLabel(1,name1);
SetIndexLabel(0,name2);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit, bigshift, bigshift1, bigshift2;
int counted_bars=IndicatorCounted();
//----
if (counted_bars<0) return(-1);
if (counted_bars>0) counted_bars--;
limit=Bars-counted_bars +TimeFrame2/Period();
for (int i=limit; i>=0; i--)
{
bigshift = iBarShift(Symbol(),TimeFrame, Time[i]);
bigshift1 = iBarShift(Symbol(),TimeFrame1,Time[i]);
bigshift2 = iBarShift(Symbol(),TimeFrame2,Time[i]);
// price[i] = getPrice(bigshift);
price[i] = iMA(Symbol(),TimeFrame, MaLength, 0,MaType, MaPrice, bigshift);
trend[i] = iMA(Symbol(),TimeFrame1,Ma1Length,0,Ma1Type,Ma1Price,bigshift1);
trend2[i]= iMA(Symbol(),TimeFrame2,Ma2Length,0,Ma2Type,Ma2Price,bigshift2);
{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