smC4Scalper_Candles_v3.1_trixBarsCW

Download script
Author: � 2009.07.17, SwingMan
smC4Scalper_Candles_v3.1_trixBarsCW

This script is a specialized visual analysis tool designed to modify how price candles appear on your trading chart based on momentum trends. Instead of just showing standard price bars, it reinterprets the candle structure to help traders identify the underlying strength and direction of the market more clearly.

Core Logic Overview

The primary logic relies on a technical indicator called the TRIX (Triple Exponential Average). The script calculates this momentum through several layers of moving averages to smooth out market noise.

Once this momentum value is calculated, the script uses it as a "filter" to decide how to draw the candles on your screen. It essentially classifies each candle into two categories: Bullish Momentum or Bearish Momentum.

How the Visuals Work

The script dynamically swaps the data points of a candle (Open, High, Low, and Close) depending on whether the momentum is positive or negative. Here is how it handles that logic:

  • Upward Momentum: If the momentum indicator is positive, the script treats the price action as a "Buy" sentiment. It ensures that the visual representation of the candle follows a bullish structure (where the High is at the top and the Low is at the bottom), even if the specific price movement within that minute or hour was slightly messy.
  • Downward Momentum: If the momentum indicator is negative, the script treats the price action as a "Sell" sentiment. It flips the logic to prioritize a bearish visual structure.

Key Features for the User

  • Dynamic Price Mapping: Depending on the strength of the trend, the script can "flip" which price values are displayed as the Open or Close. This is intended to make it easier for a trader to see if the price is moving in harmony with the overall trend.
  • Customizable Smoothing: The logic allows you to choose different types of moving averages (such as Simple, Exponential, Smoothed, or Linear Weighted) and different price inputs (like Close, Median, Typical, or Weighted prices). This allows you to "tune" how sensitive or smooth the momentum detection is.
  • Visual Signal Markers: The script includes specific logic to place visual markers (dots/arrows) on the chart. These serve as quick reference points to highlight specific price actions that meet the trend criteria.

Summary for Traders

Think of this script as a "Trend-Aware" candle overlay. It doesn't change the actual prices of the market; instead, it re-draws the candles so that their shapes align with the momentum of the TRIX indicator. This helps you instantly distinguish between price movements that are supported by the trend and those that are moving against it.

Price Data Components
Series array that contains open prices of each barSeries array that contains close prices for each barSeries array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1
6 Views
0 Downloads
0 Favorites
smC4Scalper_Candles_v3.1_trixBarsCW
//+------------------------------------------------------------------+
//|                                     smC4Scalper Candles_v3.1.mq4 |
//|                                           © 2009.07.17, SwingMan |
//|                                                                  | 
//+------------------------------------------------------------------+ 
#property copyright "© 2009.07.17, SwingMan"
#property link      ""
//  Source:
//+------------------------------------------------------------------+
//|                                                         TRIX.mq4 |
//|                                                          by Raff | 
//+------------------------------------------------------------------+ 
//#property copyright "Copyright © 2006, raff1410@o2.pl"

// Source: smSuper TRIX_v1
/* 2009.07.12 - choice of moving averages
              - choice of prices (close, log(close) )    
   
   2009.07.17 - Coloured candles for Trix
              - Two dots colours 
   2009.07.28 - Switch for the dots colours
*/
//---- indicator settings
#property  indicator_chart_window

#property  indicator_buffers 6
#property  indicator_color1 Red    // histogram
#property  indicator_color2 Green
#property  indicator_color3 Red
#property  indicator_color4 Green
#property  indicator_color5 Aqua   // dots
#property  indicator_color6 Gold
#property  indicator_width1 1
#property  indicator_width2 1
#property  indicator_width3 3
#property  indicator_width4 3
#property  indicator_width5 0
#property  indicator_width6 0

//---- indicator parameters
//-------------------------------------------------------------------
//extern int Period_Avg   = 13;
extern int  TRIX_Period = 3;
extern string _____TRIX_Averages_____ = "0=SMA, 1=EMA, 2=SMMA, 3=LWMA";
extern int Mode_Average_1 = 1;
extern int Mode_Average_2 = 1;
extern int Mode_Average_3 = 1;
extern string _____TRIX_Input_Price_____ = "0=C,4=Median,5=Typical,6=Weighted";
extern int Input_Price = 0;
extern int iArrow = 167;
extern int  CountBars = 500;
extern bool Draw_Closes = true;
//-------------------------------------------------------------------
int  Signal_Period     = 0;
double Scale = 1000.0;

//---- indicator buffers
double      ind_buffer1a[];
double      ind_buffer2a[];
double      ind_buffer3[];
double      ind_buffer4[];
double      ind_buffer5[];
double      ind_buffer6up[];
double      ind_buffer6dn[];
double      ind_buffer7[];
double Buffer1[], Buffer2[], Buffer3[], Buffer4[];
double CloseUpBuffer[], CloseDnBuffer[];
//---- variables
int ModeAvg1, ModeAvg2, ModeAvg3;
int inputPrice;
datetime thisTime, oldTime;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   ArraySetAsSeries(ind_buffer1a,true);
   ArraySetAsSeries(ind_buffer2a,true);
   ArraySetAsSeries(ind_buffer7,true);   
   ArraySetAsSeries(ind_buffer5,true);
   ArraySetAsSeries(ind_buffer6up,true);
   ArraySetAsSeries(ind_buffer6dn,true);
   ArraySetAsSeries(ind_buffer3,true);
   ArraySetAsSeries(ind_buffer4,true);
   
//---- indicator buffers mapping
   IndicatorDigits(Digits);
   SetIndexBuffer(0,Buffer1); SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexLabel(0,"High/Low 1");
   SetIndexBuffer(1,Buffer2); SetIndexStyle(1,DRAW_HISTOGRAM); SetIndexLabel(1,"High/Low 2");  
   SetIndexBuffer(2,Buffer3); SetIndexStyle(2,DRAW_HISTOGRAM); SetIndexLabel(2,"Open");  
   SetIndexBuffer(3,Buffer4); SetIndexStyle(3,DRAW_HISTOGRAM); SetIndexLabel(3,"Close");  
   SetIndexBuffer(4,CloseUpBuffer); SetIndexStyle(4,DRAW_ARROW); SetIndexArrow(4,iArrow); SetIndexLabel(4,NULL);  
   SetIndexBuffer(5,CloseDnBuffer); SetIndexStyle(5,DRAW_ARROW); SetIndexArrow(5,iArrow); SetIndexLabel(5,NULL);  
   
//---- parameters
   //-- default
   ModeAvg1 = MODE_SMMA;
   ModeAvg2 = MODE_SMMA;
   ModeAvg3 = MODE_EMA;
   inputPrice = PRICE_CLOSE;
   
   //-- TRIX studies
   string sPrice, sAvg1, sAvg2, sAvg3;
   //moving averages
   switch (Mode_Average_1) {
      case 0: ModeAvg1 = MODE_SMA;  sAvg1 = "S"; break;
      case 1: ModeAvg1 = MODE_EMA;  sAvg1 = "E"; break;
      case 2: ModeAvg1 = MODE_SMMA; sAvg1 = "M"; break;
      case 3: ModeAvg1 = MODE_LWMA; sAvg1 = "W"; break;
   }
   switch (Mode_Average_2) {
      case 0: ModeAvg2 = MODE_SMA;  sAvg2 = "S"; break;
      case 1: ModeAvg2 = MODE_EMA;  sAvg2 = "E"; break;
      case 2: ModeAvg2 = MODE_SMMA; sAvg2 = "M"; break;
      case 3: ModeAvg2 = MODE_LWMA; sAvg2 = "W"; break;
   }
   switch (Mode_Average_3) {
      case 0: ModeAvg3 = MODE_SMA;  sAvg3 = "S"; break;
      case 1: ModeAvg3 = MODE_EMA;  sAvg3 = "E"; break;
      case 2: ModeAvg3 = MODE_SMMA; sAvg3 = "M"; break;
      case 3: ModeAvg3 = MODE_LWMA; sAvg3 = "W"; break;
   }
   //-- prices
   switch(Input_Price) {
      case 0: inputPrice = PRICE_CLOSE;    sPrice = "C"; break;
      case 4: inputPrice = PRICE_MEDIAN;   sPrice = "M"; break;
      case 5: inputPrice = PRICE_TYPICAL;  sPrice = "T"; break;
      case 6: inputPrice = PRICE_WEIGHTED; sPrice = "W"; break;
   }
  
//---- name for DataWindow and indicator subwindow label
   string sParameters = ", "+sAvg1+","+sAvg2+","+sAvg3+", "+sPrice;
   string sName = "smC4Scalper Candles_v3.1 ("+TRIX_Period+sParameters+")";
   IndicatorShortName(sName);
   Comment(sName);

//---- initialization done
   return(0);
}
int deinit()
{
   Comment("");
}
//+------------------------------------------------------------------+
//| TRIX Candles                                                       |
//+------------------------------------------------------------------+
int start()
{
string st;
   if (TRIX_Period==Signal_Period) return(0);

   ArrayResize(ind_buffer1a,Bars);
   ArrayResize(ind_buffer2a,Bars);
   ArrayResize(ind_buffer7,Bars);   
   ArrayResize(ind_buffer5,Bars);
   ArrayResize(ind_buffer6up,Bars);
   ArrayResize(ind_buffer6dn,Bars);
   ArrayResize(ind_buffer3,Bars);
   ArrayResize(ind_buffer4,Bars);
      
   int i, limit=CountBars;
   if (limit>Bars) limit=Bars-1;
//limit = CountBars;
//---- trix    
   //-- calculation
   for(i=0; i<limit; i++) ind_buffer1a[i]=iMA(Symbol(),Period(),TRIX_Period,0,ModeAvg1,inputPrice,i);
   for(i=0; i<limit; i++) ind_buffer2a[i]=iMAOnArray(ind_buffer1a,0,TRIX_Period,0,ModeAvg2,i);
   for(i=0; i<limit; i++) ind_buffer7[i] =iMAOnArray(ind_buffer2a,0,TRIX_Period,0,ModeAvg3,i);
   //-- TRIX
   for(i=0; i<limit-1; i++) {
      //-- Rate of change
      if (ind_buffer7[i+1] != 0)
            ind_buffer1a[i] = Scale * (ind_buffer7[i]-ind_buffer7[i+1]) / ind_buffer7[i+1];
   }
   
   for(i=0; i<limit-1; i++) ind_buffer2a[i]=iMAOnArray(ind_buffer1a,0,Signal_Period,0,MODE_EMA,i);

   for(i=0; i<limit-1; i++) {
      /*thisTime = Time[i];
      if (thisTime != oldTime) {
         Set_CandleValues(i+1);
         Set_CandleValues(i);
         oldTime = thisTime;
      }
      else*/
      Set_CandleValues(i);
      
   }
//---- done
   return(0);
}

//-------------------------------------------------------------------
//    Set Candle Values
//-------------------------------------------------------------------
void Set_CandleValues(int iBar)
{
   double TrixValue = ind_buffer1a[iBar]-ind_buffer2a[iBar];
   if (TrixValue >= 0) {
      bool CandleUP = true; bool CandleDN = false;
   }
   else {
      CandleUP = false; CandleDN = true;
   }

   double dOpen  = iOpen(Symbol(),Period(),iBar);
   double dClose = iClose(Symbol(),Period(),iBar);
   double dHigh  = iHigh(Symbol(),Period(),iBar);
   double dLow   = iLow(Symbol(),Period(),iBar);
      
   //---- Candle Up ---------------------------------------------
   if (CandleUP == true) {
      if (dClose >= dOpen) {
         if (Draw_Closes == true) {
            CloseUpBuffer[iBar] = dClose;
            CloseDnBuffer[iBar] = EMPTY_VALUE;
         }
         Buffer1[iBar] = dLow;
         Buffer2[iBar] = dHigh;
         Buffer3[iBar] = dOpen;
         Buffer4[iBar] = dClose;
      }
      else {
         if (Draw_Closes == true) {
            CloseUpBuffer[iBar] = EMPTY_VALUE;
            CloseDnBuffer[iBar] = dClose;
         }
         Buffer1[iBar] = dLow;
         Buffer2[iBar] = dHigh;
         Buffer3[iBar] = dClose;
         Buffer4[iBar] = dOpen; 
      }
   }
   else
      
   //---- Candle Dn ---------------------------------------------
   if (CandleDN == true) {
      if (dClose >= dOpen) {
         if (Draw_Closes == true) {
            CloseUpBuffer[iBar] = dClose;
            CloseDnBuffer[iBar] = EMPTY_VALUE;
         }
         Buffer1[iBar] = dHigh;
         Buffer2[iBar] = dLow; 
         Buffer3[iBar] = dClose;
         Buffer4[iBar] = dOpen; 
      }
      else {
         if (Draw_Closes == true) {
            CloseUpBuffer[iBar] = EMPTY_VALUE;
            CloseDnBuffer[iBar] = dClose;
         }
         Buffer1[iBar] = dHigh;
         Buffer2[iBar] = dLow;
         Buffer3[iBar] = dOpen;
         Buffer4[iBar] = dClose;
      }
   }
}

Comments