PriceTrend
Download scriptThis tool is designed as a visual aid for traders to analyze market direction and price behavior by consolidating data from different timeframes into a single viewing area. Rather than looking at standard price charts alone, it creates three distinct visual layers that help identify trends based on specific mathematical averages.
Core Functionality
The indicator works by pulling data from a user-defined timeframe (for example, looking at hourly data while you are on a different chart) and plotting three primary components:
- Price Reference: The first layer displays the actual price of the asset. This can be customized to show various types of data points, such as the opening price, closing price, the highest and lowest points reached during a period, or various mathematical averages (like the midpoint or mean) of those values.
- Primary Trend (Ma1): The second layer calculates a long-term moving average. By looking at a larger window of past data, this line helps smooth out "market noise" to show the broader direction in which the price is moving.
- Secondary Trend (Ma2): The third layer calculates a shorter-term moving average. Because it looks at a smaller window of time, it reacts more quickly to recent price changes than the primary trend.
How the Logic Works
The script functions by synchronizing the data across different time scales. When you move through your current chart, the script automatically calculates where that moment in time falls on the larger timeframe you have selected. It then performs the following steps:
- Data Retrieval: It identifies the specific price point (based on your preference) for that historical moment.
- Trend Calculation: It simultaneously calculates two different moving averages based on those same historical moments.
- Visual Mapping: It plots all three of these values as lines in a separate window at the bottom of your screen.
Practical Use for Traders
By providing these three lines together, the script allows you to see the relationship between current price action and two different types of momentum. You can observe how closely the price is following the long-term trend, how it interacts with the short-term movements, and identify potential shifts in sentiment by watching where these lines cross or diverge from one another.
//+------------------------------------------------------------------+
//| PriceTrender2.mq4 |
//| Kalenzo |
//| bartlomiej.gorski@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Kalenzo"
#property link "bartlomiej.gorski@gmail.com"
#property indicator_buffers 3
#property indicator_color1 DodgerBlue
#property indicator_color2 Gold
#property indicator_color3 Crimson
extern int TimeFrame = 60,
Price = 0,
Ma1Type = 0,
Ma1Price = 0,
Ma1Length = 24,
Ma2Type = 0,
Ma2Price = 0,
Ma2Length = 5;
double price[],trend[],trend2[];
#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);
}
//----
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