PriceTrend1_001
Download scriptThe PriceTrender2 tool is designed to provide traders with a visual and automated way to identify current market trends by comparing the asset's price against multiple Moving Average (MA) indicators. It simplifies complex technical analysis into clear, real-time labels on your trading chart, allowing for quicker decision-making.
Core Logic Overview
The script operates by analyzing three primary data points: the current price of an asset (which can be customized to various types like Open, Close, High, or Low), a "Main" Moving Average, and a "Secondary" Moving Average. By comparing the relationship between these three elements, the tool categorizes the market into specific states.
Market Trend Categorization
The logic classifies the market into four distinct statuses based on where the price and the moving averages sit in relation to one another:
- Main UpTrend: Triggered when the price is above the Main Moving Average, and the Secondary Moving Average is also trending above the Main one. This suggests strong upward momentum.
- Main DownTrend: Triggered when the price is below the Main Moving Average, and the Secondary Moving Average is also trending below the Main one. This suggests strong downward momentum.
- Wait (Mixed Signal): When the price and the moving averages are in conflict (e.g., the price is above the main average but the secondary is below it, or vice versa), the tool identifies this as a "WAIT" state, signaling potential indecision in the market.
- Mini Trends: Additionally, the script provides a "Mini" trend check. This compares the current price directly against the Secondary Moving Average to show shorter-term fluctuations.
Visual and Alert Features
To make this data actionable for the user, the script performs the following actions automatically:
- Dynamic On-Screen Labels: The tool draws real-time text labels on the chart (colored in Lime for Up, Red for Down, and Orange for Wait). These labels update instantly as the price moves, providing an immediate "at-a-glance" status of the market.
- Smart Alerts: The script is programmed to monitor for trend changes. When the market shifts from one primary state to another (for example, switching from a DownTrend to an UpTrend), the tool will trigger a system alert. This ensures that traders are notified of significant momentum shifts without having to constantly watch the screen.
- Multi-Timeframe Flexibility: While it operates on a specific timeframe, the logic is built to fetch data consistently, ensuring that the trend information remains synchronized with the user's selected parameters.
Summary for the User
Essentially, this script acts as an automated "trend interpreter." It looks at the relationship between price and two different averages to filter out market noise, telling you clearly when the market is trending up, trending down, or in a state of equilibrium where caution is advised.
//+------------------------------------------------------------------+
//| 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 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,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);
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, 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", Orange);
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", Orange);
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