This script is designed as a technical analysis tool for the MetaTrader platform, aimed at helping traders identify potential momentum shifts and price breakouts. It combines two classic trading concepts—Donchian Channels and Exponential Moving Averages (EMA)—to filter out market "noise" and pinpoint specific entry points.
Core Components of the Logic
1. Price Range Tracking (Donchian Channels) The tool first establishes a dynamic price corridor based on a user-defined period. It constantly monitors the highest highs and the lowest lows over that timeframe to draw three distinct lines:
- Upper Line: Represents the ceiling of recent price action.
- Lower Line: Represents the floor of recent price action.
- Midline: The mathematical center between the high and low, which helps identify the equilibrium of the current trend.
2. Momentum and Trend Analysis (EMA Angle) To ensure that a price breakout isn't just a random spike, the script calculates the "angle" of an Exponential Moving Average (EMA). By comparing the value of the EMA at two different points in time, the tool determines the strength and direction of the current trend:
- Positive Angle: Indicates upward momentum.
- Negative Angle: Indicates downward momentum.
- Angle Threshold: A safety filter that ensures the script only triggers signals when the trend is sufficiently steep, ignoring flat or sideways markets.
Signal Generation
The script generates visual alerts (arrows) on your chart based on a specific "Double Confirmation" logic:
- Buy Signals: These occur when the current price breaks above the previous Upper Line and the moving average angle confirms strong upward momentum. This suggests that the price is breaking out of its recent range with significant force.
- Sell Signals: These occur when the current price drops below the previous Lower Line and the moving average angle confirms a sharp downward trend. This indicates a breakdown of support coupled with bearish momentum.
Automated Alerts and Visual Aids
To enhance the user experience, the script includes several automated features:
- Real-time Notifications: When a valid Buy or Sell signal is detected, the system generates an instant alert on your platform, providing the price and the specific currency pair/timeframe.
- Dynamic Stop Loss Reference: The script automatically calculates and displays a "Stop Loss" value on the chart based on the current Midline of the channel, providing a quick visual reference for risk management.
- Visual Clarity: The tool draws the channel lines directly on the chart window, allowing you to see the boundaries of price action in real-time alongside the entry signals.
//+------------------------------------------------------------------+
//| Doda-Donchian.mq4 |
//| Copyright © 2010, Gopal Krishan Doda |
//| http://www.DodaCharts.com |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 CLR_NONE
#property indicator_color2 CLR_NONE
#property indicator_color3 Red
#property indicator_color4 CLR_NONE // Green
#property indicator_color5 CLR_NONE // Red
#property indicator_style3 2
extern int ChannelPeriod=28; // from 28 to 20
extern int EMAPeriod=120;
extern int StartEMAShift=6;
extern int EndEMAShift=0;
extern double AngleTreshold=0.32;
double UpperLine[];
double LowerLine[];
double MidLine[];
double BuyBuffer[];
double SellBuffer[];
bool BuyAlert=false, SellAlert=false;
int init()
{
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,UpperLine);
SetIndexLabel(0,"UpperLine");
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,LowerLine);
SetIndexLabel(1,"LowerLine");
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,MidLine);
SetIndexLabel(2,"MidLine");
SetIndexStyle(3,DRAW_ARROW,EMPTY);
SetIndexArrow(3,241);
SetIndexBuffer(3,BuyBuffer);
SetIndexLabel(3,"Buy");
SetIndexStyle(4,DRAW_ARROW,EMPTY);
SetIndexArrow(4,242);
SetIndexBuffer(4,SellBuffer);
SetIndexLabel(4,"Sell");
IndicatorShortName("DonchianChannel("+ChannelPeriod+")");
SetIndexDrawBegin(0,ChannelPeriod);
SetIndexDrawBegin(1,ChannelPeriod);
ObjectCreate("mywebsite",OBJ_LABEL,0,0,0);
ObjectCreate("mysl",OBJ_LABEL,0,0,0);
return(0);
}
int deinit()
{
ObjectDelete("mysl");
ObjectDelete("mywebsite");
}
//+------------------------------------------------------------------+
//| Price Channel |
//+------------------------------------------------------------------+
int start()
{
int i, start ,counted_bars=IndicatorCounted();
int k;
double high,low,price, fEndMA, fStartMA, fAngle;
if(Bars<=ChannelPeriod) return(0);
if(counted_bars>=ChannelPeriod) {
start=Bars-counted_bars-1;
} else {
start=Bars-ChannelPeriod-1;
}
BuyBuffer[0]=0;
SellBuffer[0]=0;
for(i=start;i>=0;i--) {
UpperLine[i]=High[Highest(NULL, 0, MODE_HIGH, ChannelPeriod, i)];
LowerLine[i]=Low[Lowest(NULL, 0, MODE_LOW, ChannelPeriod, i)];
MidLine[i] =(UpperLine[i]+LowerLine[i])/2;
// next 3 lines from jpkfox, EMAAngle.mq4
fEndMA=iMA(NULL,0,EMAPeriod,0,MODE_EMA,PRICE_MEDIAN,i+EndEMAShift);
fStartMA=iMA(NULL,0,EMAPeriod,0,MODE_EMA,PRICE_MEDIAN,i+StartEMAShift);
fAngle = 10000.0 * (fEndMA - fStartMA)/(StartEMAShift-EndEMAShift);
if(UpperLine[i+1]<High[i] && fAngle > AngleTreshold) {
BuyBuffer[i]=High[i];
}
if(LowerLine[i+1]>Low[i] && fAngle < -AngleTreshold) {
SellBuffer[i]=Low[i];
}
}
if (BuyBuffer[0]!=0 && BuyAlert==False) {
Alert ("Doda-Donchian Buy Signal at " + BuyBuffer[0] + " -> " + Symbol() + "/" + Period());
BuyAlert = True;
SellAlert = False;
}
if (SellBuffer[0]!=0 && SellAlert==False) {
Alert ("Doda-Donchian Sell Signal at " + SellBuffer[0] + " -> " + Symbol() + "/" + Period());
BuyAlert = false;
SellAlert = True;
}
ObjectSetText("mysl","Stop Loss: " + DoubleToStr( MidLine[0],Digits) , 10, "Arial", Red);
ObjectSet("mysl",OBJPROP_XDISTANCE,2);
ObjectSet("mysl",OBJPROP_YDISTANCE,138);
ObjectSet("mysl", OBJPROP_CORNER, 3);
ObjectSetText("mywebsite","Powered by www.DodaCharts.com", 10, "Arial", Red);
ObjectSet("mywebsite",OBJPROP_XDISTANCE,2);
ObjectSet("mywebsite",OBJPROP_YDISTANCE,84);
ObjectSet("mywebsite", OBJPROP_CORNER, 3);
}
//+------------------------------------------------------------------+
Comments