BrainTrend2_all_in_one
Download scriptThis script is an all-in-one technical analysis tool designed for the MetaTrader 4 platform. It combines three distinct components of the "BrainTrend2" system into a single visual interface to help traders identify market trends and potential price reversal points more efficiently.
Core Logic and Functionality
The indicator operates by analyzing price action, volatility, and historical trends simultaneously. Here is a breakdown of how it processes data:
1. Trend Detection (The "River" Logic) The script identifies the current direction of the market—whether it is moving in an upward or downward trend. It uses a dynamic calculation based on price extremes (Highs and Lows) and a specialized volatility measure.
- If the price moves significantly against the current trend by a specific margin (determined by the indicator's internal volatility settings), the script "flips" the trend status.
- This allows the tool to adapt to new market directions in real-time rather than staying stuck on an outdated trend.
2. Volatility Awareness To ensure the signals are reliable and not triggered by minor market noise, the script calculates a modified Average True Range (ATR). This helps determine the "width" of price movements. It uses this data to set the boundaries for when a trend change is significant enough to be considered valid.
3. Dynamic Support and Resistance Levels Once the trend is established, the script identifies potential "stop" levels. These are essentially calculated price points where the market might experience resistance or support.
- In an upward trend, it calculates specific buy levels based on a percentage of current volatility.
- In a downward trend, it calculates specific sell levels.
Visual Outputs
The script translates these complex calculations into four distinct visual elements on your chart:
- Trend Lines: Continuous lines that represent the primary "floor" or "ceiling" the price is currently interacting with based on the established trend.
- Stop Dots: Markers that indicate the specific price levels where the tool perceives a potential turning point or a zone of interest.
- Signal Arrows: These are specialized markers that appear only when specific conditions are met—essentially alerting you when the script detects a high-probability transition from one trend to another (e.g., a signal to move from a "Buy" bias to a "Sell" bias).
Summary for the User
Instead of requiring you to load three different indicators to see the trend, the stop levels, and the signals separately, this script synthesizes all that information into one view. It continuously scans price history to determine where the market is headed, calculates the "danger zones" for price reversals based on current volatility, and provides visual cues (dots, lines, and arrows) to help you make informed trading decisions.
//+------------------------------------------------------------------+
//| BrainTrend2-all-in-one.mq4 |
//| www.forex-tsd.com |
//| Nick Bilak |
//| Modified by Serge skhorouji@gmail.com|
//+------------------------------------------------------------------+
/* Serge: This indicator includes 3 original BrainTrend2 indicators as I am 2 lazy to apply
them all one by one on a chart:
BrainTrend2Stop, BrainTrend2StopLine, BrainTrend2Sig
Note: BrainTrend2 indicator is not included as we use BrainTrend1 indicator for trend direction
It has customisable external variables that can be played with during optimisation
Also I renamed variables, simplified & logically re-arranged the codes of the original indicators
to make them more understandable (I mean for myself :-)
*/
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "www.forex-tsd.com"
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Red
#property indicator_color4 Blue
#property indicator_color5 Red
#property indicator_color6 Blue
//---- input parameters
extern int NumBars=0; //If 0, all indicator will be drawn on the whole chart (from 1st bar)
extern bool spread_eq_0=false; //if true, we will use spread=0 in all our calculations, if false we will use spread from the broker
//---- buffers
double sell_stop_dot_buf[]; //Sell stop dots, aka BrainTrend2Stop
double buy_stop_dot_buf[]; //Buy stop dots
double sell_stop_line_buf[]; //Sell stop line, aka BrainTrend2StopLine
double buy_stop_line_buf[]; //Buy stop line
double sell_signal_buf[]; //Sell signal dots, aka BrainTrend2Sig
double buy_signal_buf[]; //Buy signal dots
double spread;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{ SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,115);
SetIndexBuffer(0,sell_stop_dot_buf);
SetIndexLabel(0,"sell_stop_dot");
SetIndexEmptyValue(0, EMPTY_VALUE);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,115);
SetIndexBuffer(1,buy_stop_dot_buf);
SetIndexLabel(1,"buy_stop_dot");
SetIndexEmptyValue(1, EMPTY_VALUE);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,sell_stop_line_buf);
SetIndexLabel(2,"sell_stop_line");
SetIndexEmptyValue(2, EMPTY_VALUE);
SetIndexStyle(3,DRAW_LINE);
SetIndexBuffer(3,buy_stop_line_buf);
SetIndexLabel(3,"buy_stop_line");
SetIndexEmptyValue(3, EMPTY_VALUE);
SetIndexStyle(4,DRAW_ARROW);
SetIndexArrow(4,234);
SetIndexBuffer(4,sell_signal_buf);
SetIndexLabel(4,"sell_signal");
SetIndexEmptyValue(4, EMPTY_VALUE);
SetIndexStyle(5,DRAW_ARROW);
SetIndexArrow(5,233);
SetIndexBuffer(5,buy_signal_buf);
SetIndexLabel(5,"buy_signal");
SetIndexEmptyValue(5, EMPTY_VALUE);
if (spread_eq_0 == false)
spread=MarketInfo(Symbol(),MODE_SPREAD)*Point;
else
spread=0;
}
int start()
{
double true_range;
int artp=7;
bool river=True;
double Emaxtra=0;
double Values[100];
double ATR=0;
int glava=0;
double Weight=0;
int Curr=0;
double widcha=0;
double cecf=0.7;
double Range1=0;
double r=0;
double r1=0;
int p=0;
double s=2;
if (NumBars == 0)
NumBars = Bars;
else
NumBars = MathMin(Bars,NumBars);
if (Close[NumBars - 2] > Close[NumBars - 1])
river = True;
else
river = False;
Emaxtra = Close[NumBars - 2];
true_range = MathMax(spread + High[NumBars-3] - Low[NumBars-3],
MathMax(MathAbs(spread+High[NumBars-3]-Close[NumBars-2]),
MathAbs(spread+High[NumBars-3]-Close[NumBars-3])));
for (int i = 0; i <= artp-1; i++)
Values[i] = true_range;
for (int shift=NumBars-3; shift>=0; shift--)
{ true_range = MathMax(spread + High[shift] - Low[shift],
MathMax(MathAbs(spread+High[shift]-Close[shift+1]),MathAbs(spread+High[shift]-Close[shift+1])));
Values[glava] = true_range;
ATR = 0;
Weight = artp; // 7
Curr = glava; // 0
for (i = 0; i<=artp-1; i++)
{ ATR += Values[Curr] * Weight;
Weight -= 1;
Curr -= 1;
if (Curr == -1)
Curr = artp - 1;
}
ATR = 2.0 * ATR / (artp * (artp + 1.0));
glava += 1;
if (glava == artp)
glava = 0;
widcha = cecf * ATR; //cecf = 0.7
if (river && Low[shift] < Emaxtra - widcha)
{ river = False;
Emaxtra = spread + High[shift];
}
if (river && Low[shift] > Emaxtra)
{ Emaxtra = Low[shift];
}
if (!river && spread + High[shift] > Emaxtra + widcha)
{ river = True;
Emaxtra = Low[shift];
}
if (!river && spread + High[shift] < Emaxtra)
{ Emaxtra = spread + High[shift];
}
Range1 = iATR(NULL,0,10,shift)+spread/10.0;
if (river)
{ if (Low[shift] - Range1 * s < r && r != 0)
r1 = r;
else
r1 = Low[shift] - Range1 * s / 3.0;
if (p == 2)
{ r1 = Low[shift] - Range1 * s / 3.0;
buy_signal_buf[shift]=r1;
}
buy_stop_dot_buf[shift]=r1;
buy_stop_line_buf[shift]=r1;
r = r1;
p = 1;
} else
{ if (spread + High[shift] + Range1 * s > r && r != 0)
r1 = r;
else
r1 = spread + High[shift] + Range1 * s / 3.0;
if (p == 1)
{ r1 = spread + High[shift] + Range1 * s / 3.0;
sell_signal_buf[shift]=r1;
}
sell_stop_dot_buf[shift]=r1;
sell_stop_line_buf[shift]=r1;
r = r1;
p = 2;
}
}
}
Comments