trafficlight_637

Download script
Author: Wtf11
trafficlight_637

This "Traffic Light" indicator is designed to provide traders with a multi-timeframe visual summary of market trends. Instead of looking at a single price chart, it analyzes the price action across four different timeframes simultaneously—ranging from Weekly and Daily charts down to 4-hour and 1-hour intervals.

Core Logic Overview

The tool works by checking the relationship between the current price and two specific Exponential Moving Averages (EMAs) on each of the selected timeframes. By comparing these data points, the script assigns a "signal" to each timeframe, which is then displayed as a set of visual markers in a separate window at the bottom of your platform.

How the Signals Are Calculated

For every timeframe monitored by the script, it performs the following checks:

  • Green Signal (Bullish): If the current price on that specific timeframe is higher than both the short-term and medium-term moving averages, the script identifies a bullish trend.
  • Red Signal (Bearish): If the current price on that specific timeframe is lower than both moving averages, the script identifies a bearish trend.
  • Neutral/No Signal: If the price does not clearly exceed or fall below these benchmarks (or if there is a significant data gap), no signal is displayed for that timeframe.

Visual Representation

The indicator populates a separate window with 8 distinct markers. These are grouped into pairs to represent the "Traffic Light" status for each of the four timeframes:

  1. Timeframe 1 (Weekly): Displays its status via the first set of markers.
  2. Timeframe 2 (Daily): Displays its status via the second set of markers.
  3. Timeframe 3 (4-Hour): Displays its status via the third set of markers.
  4. Timeframe 4 (1-Hour): Displays its status via the fourth set of markers.

Key Features for the User

  • Multi-Timeframe Synchronization: The script includes a specialized function to ensure that even if you are looking at a small timeframe (like 1-hour), it accurately pulls the corresponding data from the larger timeframes (like Weekly or Daily) without causing calculation errors.
  • Gap Detection: The logic includes a safety check for "gaps." If there is a break in historical data between different timeframes, the script is programmed to recognize this rather than providing an inaccurate signal.
  • Visual Clarity: By isolating these signals into a separate window with dedicated colors (Green for bullish, FireBrick/Red for bearish), it allows you to see at a glance whether the "higher" trends align with the "lower" trends, helping you identify stronger areas of support or resistance across the entire market structure.
Price Data Components
Series array that contains close prices for each bar
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
7 Views
0 Downloads
0 Favorites
trafficlight_637
#property copyright "Wtf11"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 5
#property indicator_buffers 8
#property indicator_color1 Green
#property indicator_color2 Green
#property indicator_color3 Green
#property indicator_color4 Green
#property indicator_color5 FireBrick
#property indicator_color6 FireBrick
#property indicator_color7 FireBrick
#property indicator_color8 FireBrick


//---- input parameters
extern int Period1=5;
extern int Period2=10;
extern int Period3=20;
extern int Period4=40;
extern int tf1=PERIOD_W1;
extern int tf2=PERIOD_D1;
extern int tf3=PERIOD_H4;
extern int tf4=PERIOD_H1;
extern double GapStop=1; //size of gap relative to previous bar size.  Will display black if there is a gap on relevant TF.

//---- buffers
double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double Buffer5[];
double Buffer6[];
double Buffer7[];
double Buffer8[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(8);

//---- indicator line
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexBuffer(0,Buffer1);
   SetIndexArrow(0, 174);
//---- indicator line
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexBuffer(1,Buffer2);
   SetIndexArrow(1, 174);
//---- indicator line
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexBuffer(2,Buffer3);
   SetIndexArrow(2, 174);
//---- indicator line
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexBuffer(3,Buffer4);
   SetIndexArrow(3, 174);
//---- indicator line
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexBuffer(4,Buffer5);
   SetIndexArrow(4, 174);
//---- indicator line
   SetIndexStyle(5,DRAW_ARROW);
   SetIndexBuffer(5,Buffer6);
   SetIndexArrow(5, 174);
//---- indicator line
   SetIndexStyle(6,DRAW_ARROW);
   SetIndexBuffer(6,Buffer7);
   SetIndexArrow(6, 174);
//---- indicator line
   SetIndexStyle(7,DRAW_ARROW);
   SetIndexBuffer(7,Buffer8);
   SetIndexArrow(7, 174);

//---- name for DataWindow and indicator subwindow label
   short_name="Traffic Light";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,Period4);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
int start()
  {
   int    i,counted_bars=IndicatorCounted();
//----
   if(Bars<=Period4) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=Period4;i++) 
         {
         Buffer1[Bars-i]=-10.0;
         Buffer2[Bars-i]=-10.0;
         Buffer3[Bars-i]=-10.0;
         Buffer4[Bars-i]=-10.0;
         Buffer5[Bars-i]=-10.0;
         Buffer6[Bars-i]=-10.0;
         Buffer7[Bars-i]=-10.0;
         Buffer8[Bars-i]=-10.0;
         }
//----
   i=Bars-Period4-1;
   if(counted_bars>=Period4) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double MA, MA1, MA2, MA3, MA4;
      int RYG;
      

 //   traffic light code.  Must set RYG to -1 for red, 0 for black, 1 for green
 
      int bb=HighBar(Symbol(), 0, i, tf1);
      if (bb==-1) continue;
      MA1=iMA(NULL,tf1,Period1,0,MODE_EMA,PRICE_CLOSE,bb);
      MA3=iMA(NULL,tf1,Period3,0,MODE_EMA,PRICE_CLOSE,bb);
      double bbCL=iClose(Symbol(), tf1, bb);
      
      RYG=0;
      if ((bbCL>MA1)&&(bbCL>MA3)) RYG=1;
      if ((bbCL<MA1)&&(bbCL<MA3)) RYG=-1;
      
 //   display lowest traffic light
      switch(RYG) 
         {
         case 1:
            Buffer1[i]=1.0;
            Buffer5[i]=-10.0;
            break;
         case -1:   
            Buffer1[i]=-10.0;
            Buffer5[i]=1.0;
            break;
         case 0:
            Buffer1[i]=-10.0;
            Buffer5[i]=-10.0;
            break;   
         }
      
  //   traffic light code.  Must set RYG to -1 for red, 0 for black, 1 for green
 
      bb=HighBar(Symbol(), 0, i, tf2);
      if (bb==-1) continue;
      MA1=iMA(NULL,tf2,Period1,0,MODE_EMA,PRICE_CLOSE,bb);
      MA3=iMA(NULL,tf2,Period3,0,MODE_EMA,PRICE_CLOSE,bb);
      bbCL=iClose(Symbol(), tf2, bb);
      
      RYG=0;
      if ((bbCL>MA1)&&(bbCL>MA3)) RYG=1;
      if ((bbCL<MA1)&&(bbCL<MA3)) RYG=-1;
      
 //   display second lowest traffic light
      switch(RYG) 
         {
         case 1:
            Buffer2[i]=2.0;
            Buffer6[i]=-10.0;
            break;
         case -1:   
            Buffer2[i]=-10.0;
            Buffer6[i]=2.0;
            break;
         case 0:
            Buffer2[i]=-10.0;
            Buffer6[i]=-10.0;
            break;   
         }
      
   //   traffic light code.  Must set RYG to -1 for red, 0 for black, 1 for green
      bb=HighBar(Symbol(), 0, i, tf3);
      if (bb==-1) continue;
      MA1=iMA(NULL,tf3,Period1,0,MODE_EMA,PRICE_CLOSE,bb);
      MA3=iMA(NULL,tf3,Period3,0,MODE_EMA,PRICE_CLOSE,bb);
      bbCL=iClose(Symbol(), tf3, bb);
      
      RYG=0;
      if ((bbCL>MA1)&&(bbCL>MA3)) RYG=1;
      if ((bbCL<MA1)&&(bbCL<MA3)) RYG=-1;
      
 //   display second top traffic light
      switch(RYG) 
         {
         case 1:
            Buffer3[i]=3.0;
            Buffer7[i]=-10.0;
            break;
         case -1:   
            Buffer3[i]=-10.0;
            Buffer7[i]=3.0;
            break;
         case 0:
            Buffer3[i]=-10.0;
            Buffer7[i]=-10.0;
            break;   
         }
     
   //   traffic light code.  Must set RYG to -1 for red, 0 for black, 1 for green
      bb=HighBar(Symbol(), 0, i, tf4);
      if (bb==-1) continue;
      MA1=iMA(NULL,tf4,Period1,0,MODE_EMA,PRICE_CLOSE,bb);
      MA3=iMA(NULL,tf4,Period3,0,MODE_EMA,PRICE_CLOSE,bb);
      bbCL=iClose(Symbol(), tf4, bb);
      
      RYG=0;
      if ((bbCL>MA1)&&(bbCL>MA3)) RYG=1;
      if ((bbCL<MA1)&&(bbCL<MA3)) RYG=-1;
      
 //   display second lowest traffic light
      switch(RYG) 
         {
         case 1:
            Buffer4[i]=4.0;
            Buffer8[i]=-10.0;
            break;
         case -1:   
            Buffer4[i]=-10.0;
            Buffer8[i]=4.0;
            break;
         case 0:
            Buffer4[i]=-10.0;
            Buffer8[i]=-10.0;
            break;   
         }
     
 
 
   
      i--;
     }
//----      TempBuffer[i]=
   return(0);
  }
//+------------------------------------------------------------------+

int HighBar(string symbol, int TFLow, int BarLow, int TFHigh)
{
   datetime BarTimeLow=iTime(symbol, TFLow, BarLow);
   int error=GetLastError();
   if(error==4066) return (-1);
   
   int res=iBarShift(symbol, TFHigh, BarTimeLow, false);
   error=GetLastError();
   if(error==4066) return (-1);
   
   return (res);
   
   }


Comments