Okay, here's a breakdown of what this script does, explained in a way that's easy to understand even if you don't know anything about programming.
What is this script?
This script is designed to be used with a trading platform called MetaTrader. It's a tool that analyzes price data and displays information on a chart to help traders make decisions. Specifically, it's designed to show the trend and momentum of a financial instrument (like a stock, currency, etc.) using a modified version of a Moving Average Convergence Divergence (MACD) indicator.
How does it work?
Here's a step-by-step explanation:
-
Data Input: The script takes the price of an asset (like a stock or currency) as its starting point.
-
Calculating Moving Averages: It calculates two different Triangular Moving Averages (TMAs) of the price. A moving average is a way to smooth out price fluctuations and see the overall trend. The script uses two TMAs with different periods (length of time), one faster and one slower, to compare them.
-
Finding the Difference (like a MACD): The script then calculates the difference between the two TMAs (fast and slow). This difference helps to show the momentum of the price. When the faster TMA is above the slower one, it suggests the price is trending upwards, and vice versa.
-
Coloring the Difference (Histogram): The script displays this difference as a histogram (a bar chart). The bars are colored based on whether the difference is positive (uptrend, usually green or aqua) or negative (downtrend, usually coral or red).
-
Slope Detection: In addition to showing the difference between the TMAs, the script also figures out the "slope" of the histogram. This slope tells us if the momentum is increasing (getting stronger) or decreasing (getting weaker).
-
Arrow Signals (optional): The script can also display arrows on the chart indicating when the slope changes. An up arrow means the momentum is shifting upwards, and a down arrow means it's shifting downwards.
-
Alerts: The script has an alert system. If turned on, it can send messages, play sounds, or even send emails when certain conditions are met:
-
Slope Change Alert: It can alert you when the slope of the histogram changes direction (from increasing to decreasing or vice versa).
-
Zero Line Cross Alert: It can alert you when the difference between the TMAs crosses the zero line (meaning the fast TMA crosses above or below the slow TMA).
-
In simple terms:
Imagine you're watching a race. This script is like a tool that:
- Smooths out the speed of each runner (using moving averages).
- Compares the speeds of two runners (the two TMAs).
- Shows you the difference in their speeds on a color-coded chart.
- Tells you if a runner is speeding up or slowing down (the slope).
- Alerts you when a runner overtakes another one or changes their pace dramatically.
Key Settings (External Variables):
The script has several settings that you can adjust to customize it to your preferences:
FastTma
andSlowTma
: These control the length of the two moving averages. Smaller numbers make the TMAs more sensitive to price changes.TmaPrice
: This setting determines which price (e.g., typical price, closing price) is used in the calculations.ArrowsOnSlope
: This turns the arrow signals on or off.alertsOn
,alertsOnSlope
,alertsOnCurrent
,alertsMessage
,alertsSound
,alertsEmail
: These control the various alert features.
//+------------------------------------------------------------------+
//| colored macd |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link "www.forex-tsd.com"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Green
#property indicator_color2 Coral
#property indicator_color3 Aqua
#property indicator_color4 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 1
#property indicator_width4 1
#property indicator_levelcolor DarkSlateGray
//
//
//
//
//
extern int FastTma = 25;
extern int SlowTma = 50;
extern int TmaPrice = PRICE_TYPICAL;
extern bool ArrowsOnSlope = true;
extern double hiLevel = 0.005;
extern double loLevel = -0.005;
extern string note = "turn on Alert = true; turn off = false";
extern bool alertsOn = true;
extern bool alertsOnSlope = true;
extern bool alertsOnCurrent = false;
extern bool alertsMessage = true;
extern bool alertsSound = true;
extern bool alertsEmail = false;
//
//
//
//
//
double Upa[];
double Dna[];
double arUp[];
double arDn[];
double diff[];
double trend[];
double slope[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int init()
{
IndicatorBuffers(7);
SetIndexBuffer(0,Upa);
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(1,Dna);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexBuffer(2,arUp);
SetIndexBuffer(3,arDn);
SetIndexBuffer(4,diff);
SetIndexBuffer(5,trend);
SetIndexBuffer(6,slope);
SetLevelValue(0,0);
SetLevelValue(1,0.5);
SetLevelValue(2,-0.5);
if(ArrowsOnSlope)
{
SetIndexStyle(2,DRAW_ARROW); SetIndexArrow(2,159);
SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,159);
}
else
{
SetIndexStyle(2,DRAW_NONE);
SetIndexStyle(3,DRAW_NONE);
}
IndicatorShortName("Tma Slope");
return(0);
}
//
//
//
//
//
int deinit() { return(0);}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int start()
{
int counted_bars=IndicatorCounted();
int i,limit;
limit=Bars-counted_bars;
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
if(counted_bars==0) limit-=1+1;
//
//
//
//
//
for(i=limit; i>=0; i--)
{
double price=iMA(NULL,0,1,0,MODE_SMA,TmaPrice,i);
diff[i] = iTma(price,FastTma,i,0) - iTma(price,SlowTma,i,1);
Upa[i] = EMPTY_VALUE;
Dna[i] = EMPTY_VALUE;
arUp[i] = EMPTY_VALUE;
arDn[i] = EMPTY_VALUE;
trend[i] = trend[i+1];
slope[i] = slope[i+1];
if(diff[i] > 0) trend[i] = 1;
if(diff[i] < 0) trend[i] = -1;
if(diff[i] > diff[i+1]) slope[i] = 1;
if(diff[i] < diff[i+1]) slope[i] = -1;
if(trend[i]== 1) Upa[i] = diff[i];
if(trend[i]==-1) Dna[i] = diff[i];
if(slope[i]== 1) arUp[i] = diff[i];
if(slope[i]==-1) arDn[i] = diff[i];
}
if(alertsOn)
{
if(alertsOnCurrent)
int whichBar=0;
else whichBar=1; whichBar=iBarShift(NULL,0,iTime(NULL,0,whichBar));
//
//
//
//
//
if(alertsOnSlope)
{
if(slope[whichBar]!=slope[whichBar+1])
{
if(slope[whichBar] == 1) doAlert(whichBar,"slope changed to up");
if(slope[whichBar] ==-1) doAlert(whichBar,"slope changed to down");
}
}
else
{
if(trend[whichBar]!=trend[whichBar+1])
{
if(trend[whichBar] == 1) doAlert(whichBar,"crossed zero line up");
if(trend[whichBar] ==-1) doAlert(whichBar,"crossed zero line down");
}
}
}
return(0);
}
//
//
//
//
//
void doAlert(int forBar,string doWhat)
{
static string previousAlert="nothing";
static datetime previousTime;
string message;
if(previousAlert!=doWhat || previousTime!=Time[forBar])
{
previousAlert = doWhat;
previousTime = Time[forBar];
//
//
//
//
//
message=StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Tma ",doWhat);
if(alertsMessage) Alert(message);
if(alertsEmail) SendMail(StringConcatenate(Symbol()," Tma "),message);
if(alertsSound) PlaySound("alert2.wav");
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
double workTma[][2];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double iTma(double price,double period,int r,int instanceNo=0)
{
if(ArraySize(workTma)!=Bars) ArrayResize(workTma,Bars); r=Bars-r-1;
//
//
//
//
//
workTma[r][instanceNo]=price;
double half = (period+1.0)/2.0;
double sum = price;
double sumw = 1;
for(int k=1; k<period && (r-k)>=0; k++)
{
double weight=k+1; if(weight>half) weight=period-k;
sumw += weight;
sum += weight*workTma[r-k][instanceNo];
}
return(sum/sumw);
}
//+------------------------------------------------------------------+
Comments