WATR_Candles

Download script
Author: Written WizardSerg under article konkop in <Modern trading> #4/2001

This script is a sophisticated visual trading tool designed to transform standard price charts into dynamic, color-coded "smart candles." Instead of just showing you the raw price movement, it interprets market momentum and volatility to provide a clearer picture of the current trend.

Here is a breakdown of the logic used by the script:

Core Concept: Trend Awareness

The primary goal of this tool is to identify whether the market is in an upward or downward phase based on price action relative to historical volatility. It achieves this by calculating a "baseline" that shifts according to the Average True Range (ATR)—a standard measure of how much a currency pair typically moves.

How the Logic Works:

The script processes every price bar (candle) on your chart and follows these specific logical steps:

  • Volatility-Adjusted Thresholds: It doesn't just look at whether the price went up or down by a tiny amount; it calculates a required movement "buffer." This ensures that minor, insignificant price fluctuations (market noise) don't trigger a change in trend detection.
  • Dynamic Trend Tracking:
    • Bullish Identification: If the price moves above the calculated upper threshold, the script recognizes an upward trend. It then locks onto this "high-water mark," ensuring that the trend is only considered "broken" if the price fails to maintain a certain level of strength.
    • Bearish Identification: Conversely, if the price drops below the lower threshold, it identifies a downward trend and tracks the new "low-water mark."
  • Anti-Trend Filtering: The script contains specific logic to identify "Anti-Trend Bars"—situations where a single candle moves aggressively against the established trend. This helps the tool stay stable and prevent it from "flipping" colors too quickly during brief, erratic price spikes.

Visual Output (Color Coding):

Once the script determines the trend and identifies the specific behavior of a candle, it applies one of five distinct colors to the chart:

  1. Bullish Trend / Strong Movement: Highlights candles that are moving in favor of the current uptrend.
  2. Bullish Trend / Weak/Corrective Movement: Highlights candles that are slightly retracing but still within an overall upward trend.
  3. Bearish Trend / Strong Movement: Highlights candles moving in favor of a downward trend.
  4. Bearish Trend / Weak/Corrective Movement: Highlights candles that are retracing while the primary trend remains down.
  5. Neutral/Gray: Used for segments where the trend is not clearly established or meets specific transition criteria.

Summary for the User:

Essentially, this script acts as a "filter" for your eyes. It strips away the confusion of messy price movements and replaces them with a visual narrative. By looking at the colors of the candles, you can instantly tell not only which direction the market is moving, but also how much conviction that move has based on the volatility of the recent past.

Indicators Used
Indicator of the average true range
10 Views
0 Downloads
0 Favorites
WATR_Candles
ÿþ//+------------------------------------------------------------------+

//|                                                WATR_Candles.mq5  |

//|                      Written WizardSerg under article konkop in  |

//|                                        "Modern trading" #4/2001  |

//|                                  http://www.wizardserg.inweb.ru  |

//|                                              wizardserg@mail.ru  |

//+------------------------------------------------------------------+

//---- 02B>@AB2> 8=48:0B>@0

#property copyright "Written WizardSerg under article konkop in <Modern trading> #4/2001"

//---- AAK;:0 =0 A09B 02B>@0

#property link      "http://www.wizardserg.inweb.ru"

//---- =><5@ 25@A88 8=48:0B>@0

#property version   "1.00"

//---- >B@8A>2:0 8=48:0B>@0 2 >A=>2=>< >:=5

#property indicator_chart_window

//---- 4;O @0AGQB0 8 >B@8A>2:8 8=48:0B>@0 8A?>;L7>20=> ?OBL 1CD5@>2

#property indicator_buffers 5

//---- 8A?>;L7>20=> 2A53> >4=> 3@0D8G5A:>5 ?>AB@>5=85

#property indicator_plots   1

//+----------------------------------------------+

//|  0@0<5B@K >B@8A>2:8 8=48:0B>@0              |

//+----------------------------------------------+

//---- 2 :0G5AB25 8=48:0B>@0 8A?>;L7>20=K F25B=K5 A25G8

#property indicator_type1   DRAW_COLOR_CANDLES

#property indicator_color1  clrDeepSkyBlue,clrBlue,clrGray,clrMaroon,clrOrchid

//---- >B>1@065=85 <5B:8 8=48:0B>@0

#property indicator_label1  "WATROpen;WATRHigh;WATRLow;WATRClose"

//+----------------------------------------------+

//| >1JO2;5=85 :>=AB0=B                          |

//+----------------------------------------------+

#define RESET 0                                        // >=AB0=B0 4;O 2>72@0B0 B5@<8=0;C :><0=4K =0 ?5@5AG5B 8=48:0B>@0

//+----------------------------------------------+

//| E>4=K5 ?0@0<5B@K 8=48:0B>@0                 |

//+----------------------------------------------+

input int    WATR_K = 10;

input double WATR_M = 4.0;

input int    ATR=21;

input int    Shift=0; // A4283 8=48:0B>@0 ?> 3>@87>=B0;8 2 10@0E 

//+----------------------------------------------+

//---- >1JO2;5=85 48=0<8G5A:8E <0AA82>2, :>B>@K5 1C4CB 2 40;L=59H5< 8A?>;L7>20=K 2 :0G5AB25 8=48:0B>@=KE 1CD5@>2

double ExtOpenBuffer[];

double ExtHighBuffer[];

double ExtLowBuffer[];

double ExtCloseBuffer[];

double ExtColorsBuffer[];

//---- 1JO2;5=85 F5;KE ?5@5<5==KE 4;O E5=4;>2 8=48:0B>@>2

int ATR_Handle;

//---- 1JO2;5=85 F5;KE ?5@5<5==KE =0G0;0 >BAGQB0 40==KE

int min_rates_total;

//+------------------------------------------------------------------+

//| Custom indicator function                                        |

//+------------------------------------------------------------------+

bool AntiTrendBar(const double &Open[],const double &Close[],int index, double NRTR_Up_prev)

  {

//----

   bool res=(TrendUp(Close,index,NRTR_Up_prev) && (Close[index]<Open[index])) || (!TrendUp(Close,index,NRTR_Up_prev) && (Close[index]>Open[index]));

//----   

   return(res);

  }

//+------------------------------------------------------------------+

//| Custom indicator function                                        |

//+------------------------------------------------------------------+

double CalcIndicValue(double &ATRArray[],const double &Close[],int index,bool trend)

  {

//----

   double res=Close[index];

   if(trend)res -= (WATR_K*_Point + WATR_M*ATRArray[index]);

   else     res += (WATR_K*_Point + WATR_M*ATRArray[index]);

//----   

   return(res);

  }

//+------------------------------------------------------------------+

//| Custom indicator function                                        |

//+------------------------------------------------------------------+

bool TrendUp(const double &Close[],int index, double NRTR_Up_prev)

  {

//----

   return(Close[index+1]>NRTR_Up_prev && NRTR_Up_prev);

//----

  }

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+  

int OnInit()

  {

//---- ?>;CG5=85 E5=4;0 8=48:0B>@0 ATR

   ATR_Handle=iATR(NULL,0,ATR);

   if(ATR_Handle==INVALID_HANDLE)

     {

      Print(" 5 C40;>AL ?>;CG8BL E5=4; 8=48:0B>@0 ATR");

      return(INIT_FAILED);

     }

//---- =8F80;870F8O ?5@5<5==KE =0G0;0 >BAGQB0 40==KE

   min_rates_total=ATR;

//---- ?@52@0I5=85 48=0<8G5A:8E <0AA82>2 2 8=48:0B>@=K5 1CD5@K

   SetIndexBuffer(0,ExtOpenBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtHighBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ExtLowBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,ExtCloseBuffer,INDICATOR_DATA);

//---- ?@52@0I5=85 48=0<8G5A:>3> <0AA820 2 F25B>2>9, 8=45:A=K9 1CD5@   

   SetIndexBuffer(4,ExtColorsBuffer,INDICATOR_COLOR_INDEX);

//---- >ACI5AB2;5=85 A42830 =0G0;0 >BAGQB0 >B@8A>2:8 8=48:0B>@0 1

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);

//---- 8=45:A0F8O M;5<5=B>2 2 1CD5@0E :0: 2 B09<A5@8OE   

   ArraySetAsSeries(ExtOpenBuffer,true);

   ArraySetAsSeries(ExtHighBuffer,true);

   ArraySetAsSeries(ExtLowBuffer,true);

   ArraySetAsSeries(ExtCloseBuffer,true);

   ArraySetAsSeries(ExtColorsBuffer,true);



//---- 8=8F80;870F88 ?5@5<5==>9 4;O :>@>B:>3> 8<5=8 8=48:0B>@0

   string shortname;

   StringConcatenate(shortname,"WATR_Candles(",WATR_K,", ",WATR_M,", ",ATR,")");

//--- A>740=85 8<5=8 4;O >B>1@065=8O 2 >B45;L=>< ?>4>:=5 8 2> 2A?;K20NI59 ?>4A:07:5

   IndicatorSetString(INDICATOR_SHORTNAME,shortname);

//--- >?@545;5=85 B>G=>AB8 >B>1@065=8O 7=0G5=89 8=48:0B>@0

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

//--- 7025@H5=85 8=8F80;870F88

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(

                const int rates_total,    // :>;8G5AB2> 8AB>@88 2 10@0E =0 B5:CI5< B8:5

                const int prev_calculated,// :>;8G5AB2> 8AB>@88 2 10@0E =0 ?@54K4CI5< B8:5

                const datetime &time[],

                const double &open[],

                const double& high[],     // F5=>2>9 <0AA82 <0:A8<C<>2 F5=K 4;O @0AGQB0 8=48:0B>@0

                const double& low[],      // F5=>2>9 <0AA82 <8=8<C<>2 F5=K  4;O @0AGQB0 8=48:0B>@0

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[]

                )

  {

//---- ?@>25@:0 :>;8G5AB20 10@>2 =0 4>AB0B>G=>ABL 4;O @0AGQB0

   if(BarsCalculated(ATR_Handle)<rates_total || rates_total<min_rates_total) return(RESET);



//---- >1JO2;5=8O ;>:0;L=KE ?5@5<5==KE 

   int limit,to_copy,bar;

   double iATR_[],Up,Down;

   static double Up_prev,Down_prev;



//---- 8=45:A0F8O M;5<5=B>2 2 <0AA820E :0: 2 B09<A5@8OE  

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(iATR_,true);



//---- @0AGQB AB0@B>2>3> =><5@0 first 4;O F8:;0 ?5@5AGQB0 10@>2

   if(prev_calculated>rates_total || prev_calculated<=0) // ?@>25@:0 =0 ?5@2K9 AB0@B @0AGQB0 8=48:0B>@0

     {

      limit=rates_total-min_rates_total; // AB0@B>2K9 =><5@ 4;O @0AGQB0 2A5E 10@>2

      to_copy=rates_total;

      Up_prev=Down_prev=close[limit+1]-WATR_K*_Point;;

     }

   else

     {

      limit=rates_total-prev_calculated; // AB0@B>2K9 =><5@ 4;O @0AGQB0 =>2KE 10@>2

      to_copy=limit+1;

     }



//---- :>?8@C5< 2=>2L ?>O282H85AO 40==K5 2 <0AA82 iATR_[]

   if(CopyBuffer(ATR_Handle,0,0,to_copy,iATR_)<=0) return(RESET);



//---- >A=>2=>9 F8:; @0AGQB0 8=48:0B>@0

   for(bar=limit; bar>=0 && !IsStopped(); bar--)

     {

      if(AntiTrendBar(open,close,bar,Up_prev))

        {

         Up=Up_prev;

         Down=Down_prev;

        }

      else

        {

         if(TrendUp(close,bar,Up_prev))

           {

            Up=CalcIndicValue(iATR_,close,bar,true);

            if(Up<Up_prev) Up=Up_prev;

            Down=NULL;

           }

         else

           {

            Down=CalcIndicValue(iATR_,close,bar,false);

            if(Down>Down_prev) Down=Down_prev;

            Up=NULL;

           }

        }

      //---- ?5@5A5G5=8O A F5=>9                 

      if(TrendUp(close,bar,Up_prev) && close[bar]<Up)

        {

         Down=CalcIndicValue(iATR_,close,bar,false);

         Up=NULL;

        }

      if((!TrendUp(close,bar,Up_prev)) && close[bar]>Down)

        {

         Up=CalcIndicValue(iATR_,close,bar,true);

         Down=NULL;

        }



      ExtOpenBuffer[bar]=open[bar];

      ExtHighBuffer[bar]=high[bar];

      ExtLowBuffer[bar]=low[bar];

      ExtCloseBuffer[bar]=close[bar];

      

      if(Up)

        {

         if(open[bar]<=close[bar]) ExtColorsBuffer[bar]=0;

         else ExtColorsBuffer[bar]=1;

        }

      if(Down)

        {

          if(open[bar]>=close[bar]) ExtColorsBuffer[bar]=4;

         else ExtColorsBuffer[bar]=3;

        }



      if(bar)

        {

         Up_prev=Up;

         Down_prev=Down;

        }

     }

//----     

   return(rates_total);

  }

//+------------------------------------------------------------------+

Comments