Supertrend Audible Alert_v1
Download scriptThis tool is designed to help traders visually identify the prevailing direction of the market by plotting a dynamic trend line on their price charts. Instead of relying on simple price movements, it uses a combination of mathematical filters to smooth out "market noise" and clearly distinguish between upward (bullish) and downward (bearish) trends.
Core Logic and Mechanism
The script functions by analyzing three primary components of market data:
1. Momentum Filtering The tool utilizes a momentum oscillator (CCI) to determine the strength of the current price move. By comparing the current momentum value against a specific threshold, the script identifies whether the market is pushing strongly in one direction or if it is becoming exhausted. This prevents the indicator from giving "false signals" during minor, insignificant price flickers.
2. Volatility Adjustment To ensure the trend line reacts realistically to different market conditions (such as high-volatility news events versus calm trading sessions), the script incorporates an Average True Range (ATR) calculation. This allows the trend line to expand or contract its distance from the price, maintaining a consistent "buffer" that respects the current market volatility.
3. Dynamic Trend Tracking The logic follows these specific rules to draw the lines on your screen:
- Trend Identification: When momentum crosses above the established threshold, the system recognizes an upward trend and initiates a green line. Conversely, when it falls below, it identifies a downward trend and initiates a red line.
- Line Continuity: The script is programmed with "memory." It ensures that the trend line does not move backward against the trend. For example, in an uptrend, the line will only stay flat or move higher; it will never drop lower until a formal trend reversal is confirmed by the momentum data.
- Trend Switching: When a significant shift in momentum occurs—crossing the threshold from top to bottom or vice versa—the script automatically switches the active line color and direction, signaling a potential change in market sentiment.
User Experience Features
- Visual Cues: The indicator plots two distinct lines (Lime for Up, Red for Down) directly on the main price chart window, providing an immediate visual map of the trend.
- Audible Notifications: The script includes an integrated alert system. When the market shifts from an upward trend to a downward trend (or vice versa), the platform can trigger an audible alert to notify the trader instantly, ensuring they don't miss a significant change in price action.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| Supertrend.mq4 |
//| Copyright © 2005, Jason Robinson (jnrtrading). |
//| http://www.jnrtrading.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Jason Robinson (jnrtrading)."
#property link "http://www.jnrtrading.co.uk"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Lime
#property indicator_color2 Red
double TrendUp[];
double TrendDown[];
int st = 0;
//extern int SlowerEMA = 6;
extern bool Enable_AudibleAlert = true;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
SetIndexBuffer(0, TrendUp);
SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
SetIndexBuffer(1, TrendDown);
/*SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexArrow(0, 159);
SetIndexBuffer(0, TrendUp);
SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(1, 159);
SetIndexBuffer(1, TrendDown);*/
/*for(int i = 0; i < Bars; i++) {
TrendUp[i] = NULL;
TrendDown[i] = NULL;
}*/
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
/*for(int i = 0; i < Bars; i++) {
TrendUp[i] = NULL;
TrendDown[i] = NULL;
}*/
//----
return(0);
}
//+------------------------------------------------------------------+
//| This fucntion will make audible alert when condition has met |
//+------------------------------------------------------------------+
void AudibleAlert(bool condition)
{
int iCurrent = 0;
static int iLast = 0;
static bool first_time = true;
if(condition==true)
iCurrent = 1;
else
iCurrent = 2;
if(first_time == true)
{
iLast = iCurrent; //is it first run?
first_time = false;
}
if(iCurrent != iLast)
{
string sDirection="";
if(iCurrent==1) sDirection = "TrendUp";
if(iCurrent==2) sDirection = "TrendDown";
string sMsg = "Supertrend on ("+ Symbol() + ") has changed (" + sDirection + ")!";
Alert(sMsg);
iLast = iCurrent;
}
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit, i, counter;
double Range, AvgRange, cciTrendNow, cciTrendPrevious, var;
int counted_bars = IndicatorCounted();
//---- check for possible errors
if(counted_bars < 0) return(-1);
//---- last counted bar will be recounted
if(counted_bars > 0) counted_bars--;
limit=Bars-counted_bars;
for(i = limit; i >= 0; i--) {
cciTrendNow = iCCI(NULL, 0, 50, PRICE_TYPICAL, i);
cciTrendPrevious = iCCI(NULL, 0, 50, PRICE_TYPICAL, i+1);
//st = st * 100;
counter = i;
Range = 0;
AvgRange = 0;
for (counter = i; counter >= i-9; counter--) {
AvgRange = AvgRange + MathAbs(High[counter]-Low[counter]);
}
Range = AvgRange/10;
if (cciTrendNow >= st && cciTrendPrevious < st) {
TrendUp[i+1] = TrendDown[i+1];
}
if (cciTrendNow <= st && cciTrendPrevious > st) {
TrendDown[i+1] = TrendUp[i+1];
}
if (cciTrendNow >= st) {
TrendUp[i] = Low[i] - iATR(NULL, 0, 5, i);
if (TrendUp[i] < TrendUp[i+1]) {
TrendUp[i] = TrendUp[i+1];
}
}
else if (cciTrendNow <= st) {
TrendDown[i] = High[i] + iATR(NULL, 0, 5, i);
if (TrendDown[i] > TrendDown[i+1]) {
TrendDown[i] = TrendDown[i+1];
}
}
}
//---- code added by coders guru
//---- Alerts on color changing
if(Enable_AudibleAlert)
AudibleAlert(cciTrendNow >= st);
//----
//----
return(0);
}
//+------------------------------------------------------------------+
Comments