CustomRectangle

Download script
Author: Kalenzo
CustomRectangle

This script is designed to provide a clear, visual way to monitor price movements on your trading charts by automatically drawing a distinct rectangle based on historical data. Rather than requiring you to manually draw shapes to mark specific zones, this tool handles the geometry for you in real-time.

Core Functionality

The primary purpose of this script is to identify and highlight a price range between two specific points in time. It looks back at a set number of candles (which you can customize) and draws a rectangle connecting that past point to the current live price. This allows traders to visually see how far the price has moved or "stretched" over a specific timeframe.

Key Features and Logic:

  • Dynamic Price Selection: You have the flexibility to choose which part of the candle the script uses to define the rectangle's boundaries. You can set it to reference the Opening price, the High, the Low, or the Closing price. This allows you to tailor the visual data to your specific trading strategy.
  • Automated Visual Updates: The script is designed to refresh every time the chart updates. It removes the previous drawing and creates a new one instantly, ensuring that the rectangle always accurately reflects the most recent market data.
  • Smart Color Coding: One of the most useful features is the automated color logic. If you enable colors, the script "analyzes" the price movement between your chosen past candle and the current candle:
    • If the price has dropped compared to the historical point, the rectangle will change to a specific "down" color (Red).
    • If the price has risen, it will switch to an "up" color (Green).
    • This provides an immediate visual cue regarding the direction of the trend.
  • Customization Options: The tool includes several user-friendly settings:
    • Candle Shift: Determines how far back into the past the script should look.
    • Rectangle Name: Ensures that your custom shapes are organized and unique on your chart.
    • Width & Colors: Allows you to define the thickness of the line and the default colors used if you prefer a neutral appearance rather than the automatic color-switching.

In summary, this script simplifies chart analysis by creating a dynamic, color-coded visual bridge between past market history and current price action, helping you identify trends and price levels at a glance.

5 Views
0 Downloads
0 Favorites
CustomRectangle
//+------------------------------------------------------------------+
//|                                              CustomRectangle.mq4 |
//|                                                          Kalenzo |
//|                                          http://www.fxservice.eu |
//+------------------------------------------------------------------+
#property copyright "Kalenzo"
#property link      "http://www.fxservice.eu"

#property indicator_chart_window
//---- input parameters
extern int       candleShift   = 5;
extern string    rectangleName = "REC1";//needs to be unique
extern int       iPrice = 3;// 0 - open, 1- high, 2-low, 3-close
extern bool      useColors = true;
extern color     defaultColor = DodgerBlue;
extern color     upColor = Green;
extern color     dnColor = Red;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
    ObjectDelete(rectangleName); 
    ObjectCreate(rectangleName, OBJ_RECTANGLE, 0, Time[candleShift], getPrice(iPrice,candleShift),Time[0], getPrice(iPrice,0)); 
    ObjectSet(rectangleName, OBJPROP_WIDTH, 2); 
    
    if(useColors)
    {
      if(Close[candleShift]>Close[0])
      ObjectSet(rectangleName, OBJPROP_COLOR, dnColor); 
      else
      ObjectSet(rectangleName, OBJPROP_COLOR, upColor); 
    }
    else
    {
      ObjectSet(rectangleName, OBJPROP_COLOR, defaultColor); 
    }   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
double getPrice(int type,int x)
{
   switch(type)
   {
      case 0:   return(Open[x]);
      case 1:   return(High[x]);
      case 2:   return(Low[x]);
      case 3:   return(Close[x]);
      default : return(Close[x]);
   }
}

Comments