BT2Stop_v2

This tool is designed to analyze market trends by identifying the direction of price movement and establishing a dynamic "safety zone" or trail based on historical volatility. Instead of simply following every minor fluctuation in price, it seeks to filter out market noise to provide a clearer picture of the prevailing trend.

Core Logic Breakdown

1. Volatility Measurement (The Range) The script begins by calculating the average price range over a specific period. It looks at the difference between the highest and lowest prices reached during each interval. To ensure more recent movements carry more weight, it applies a weighted calculation—meaning what happened recently impacts the current "volatility score" more than what happened a long time ago. This score is used to determine how much space to allow for price fluctuations.

2. Trend Identification The logic determines whether the market is currently in an upward or downward trend based on whether the closing price of a candle is higher or lower than its opening price.

  • If the price closes higher, it initializes an Upward Trend.
  • If the price closes lower, it initializes a Downward Trend.

3. The Dynamic Trailing Mechanism Once a trend is established, the script tracks the highest (in an uptrend) or lowest (in a downtrend) price reached since the trend began. It then calculates a "buffer" based on the volatility score mentioned earlier.

  • In an Uptrend: It projects a value slightly below the peak price. If the market price drops below this projected level, the script recognizes that the upward momentum has likely broken and flips the trend to downward.
  • In a Downtrend: It projects a value slightly above the bottom price. If the market price rises above this projected level, it signals a trend reversal to upward.

4. Visual Output The script communicates these shifts using two distinct visual markers on your chart:

  • One set of indicators tracks the "floor" of an uptrend (showing you where the trend might break if price falls too far).
  • The other set tracks the "ceiling" of a downtrend (showing you where the trend might break if price rises too far).

By calculating these levels dynamically, the tool attempts to stay ahead of the price action, providing a visual representation of support and resistance levels that adjust automatically as market conditions change.

Miscellaneous
Implements a curve of type %1
7 Views
0 Downloads
0 Favorites
BT2Stop_v2
//+-----------------------------------------------------------------------+
//|                                                   BrainTrend1Stop.mq4 |
//|                                   Copyright © 2005. Alejandro Galindo |
//|                                                   http://elCactus.com |
//+-----------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
//---- input parameters
extern int AveragePeriod=10;
extern int Reg=1;
//---- buffers
double Buffer1[];
double Buffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator line
   IndicatorBuffers(2);
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexBuffer(0,Buffer1);
   SetIndexBuffer(1,Buffer2);
   SetIndexArrow(0,159);
   SetIndexArrow(1,159);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| BrainTrend1Stop                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int i,shift,counted_bars=IndicatorCounted();
   double value=0, price=0, trend=0, dK=0, AvgRange=0, WATR=0;
   int bar=0;
   int CountBars=500;
   if (CountBars>=Bars) CountBars=Bars;
   SetIndexDrawBegin(0,Bars-CountBars+11+1);
   SetIndexDrawBegin(1,Bars-CountBars+11+1);
//---- initial zero
   AvgRange=0;
   for(i=1;i<=AveragePeriod;i++)
     {
      dK=1+(AveragePeriod-i)/AveragePeriod;
      AvgRange=AvgRange+ dK*MathAbs(High[i]-Low[i]);
     }
   if (Point>=0.01) { WATR=(AvgRange/AveragePeriod)*Point; }
   else { WATR=AvgRange/AveragePeriod; }
//----
   if (Close[Bars-1] > Open[Bars-1])
     {
      value=Close[Bars - 1] * (1 - WATR);
      trend=1;
      Buffer2[Bars-1]=value;
     }
   if (Close[Bars-1] < Open[Bars-1])
     {
      value=Close[Bars - 1] * (1 + WATR);
      trend=-1;
      Buffer1[Bars-1]=value;
     }
   bar=CountBars-11-1;
   while(bar>=0)
     {
      Buffer1[shift]=0;
      Buffer2[shift]=0;
      //----
      if (Reg==2)
        {
         AvgRange=0;
         for(i=1;i<=AveragePeriod;i++)
           {
            dK=1+(AveragePeriod-i)/AveragePeriod;
            AvgRange=AvgRange+ dK*MathAbs(High[bar + i]-Low[bar + i]);
           }
         if (Point>=0.01) { WATR=(AvgRange/AveragePeriod)*Point; }
         else { WATR=AvgRange/AveragePeriod; }
        }
      if (trend>=0)
        {
         if (Close[bar] > price) { price=Close[bar]; }
         value=price * (1 - WATR);
         if (Close[bar] < value)
           {
            price=Close[bar];
            value=price * (1 + WATR);
            trend=-1;
           }
        }
      else
        {
         if (trend<=0)
           {
            if (Close[bar] < price) { price=Close[bar]; }
            value=price * (1 + WATR);
            if (Close[bar] > value)
              {
               price=Close[bar];
               value=price * (1 - WATR);
               trend=1;
              }
           }
        }
      if (trend==-1) { Buffer1[bar]=value; }
      if (trend==1) { Buffer2[bar]=value; }
      //----
      bar--;
     }
   return(0);
  }
//+------------------------------------------------------------------+

Comments