Author: Copyright � 2009, Arif E. Nugroho <arif_endro@vectra.web.id>
Percentage

Overview

This script is designed as an informational dashboard for MetaTrader 4. Its primary purpose is to provide traders with a real-time visual summary of price performance across three different timeframes: Daily, Weekly, and Monthly. Instead of requiring the user to manually calculate price movements, the tool automatically tracks opening and closing prices and displays the percentage gain or loss directly on the chart.

Key Functionalities

1. Multi-Timeframe Performance Tracking The script monitors three specific layers of market data:

  • Daily: Tracks the movement from the start to the current end of the trading day.
  • Weekly: Monitors the price change relative to the beginning of the current week.
  • Monthly: Displays the percentage change since the start of the current month.

2. Dynamic Data Display For each timeframe, the script generates a text string that includes:

  • The opening price and the current closing price.
  • The absolute numerical difference (the "pips" or points moved).
  • The exact percentage gain or loss relative to the opening price.

3. Visual Color Coding To allow for quick interpretation at a glance, the script uses dynamic color changes:

  • Positive Movement: If the current price is higher than the opening price for that period, the text is displayed in Blue.
  • Negative Movement: If the current price has dropped below the opening price, the text shifts to Red.

4. Interactive Price Level Markers (Optional) The script includes an optional feature that can project specific percentage milestones onto the chart. If enabled, it draws visual markers and labels at various intervals (such as +/- 0.5%, 1.0%, 1.5%, and 2.0%) based on the day's opening price. This helps traders identify exactly where specific profit targets or risk levels are located in real-time.

User Experience

The information is displayed as a clean, stacked list of labels on the chart corner. It automatically updates as the market moves, ensuring that the trader always sees the most current percentage performance without needing to switch between different timeframe charts manually.

Price Data Components
Series array that contains open prices of each barSeries array that contains close prices for each bar
13 Views
0 Downloads
0 Favorites
Percentage
//+------------------------------------------------------------------+
//|                                                   Percentage.mq4 |
//|  Copyright © 2009, Arif Endro Nugroho <arif_endro@vectra.web.id> |
//|                                         http://www.vectra.web.id |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Arif E. Nugroho <arif_endro@vectra.web.id>"
#property link      "http://www.vectra.web.id"

#property indicator_chart_window
//---- input parameters
extern int       corner=2;
extern int       xdis=5;
extern int       ydis=20;
extern string    Font="Lucida Console";
extern int       FontSize=8;
extern color     FontPlus=Blue;
extern color     FontMinus=Red;
extern int       timezone=7;
extern bool      back=0;

extern bool      ShowPriceLabel=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete("DailyStat");
   ObjectDelete("WeeklyStat");
   ObjectDelete("MonthlyStat");

if(ShowPriceLabel){   
   ObjectDelete("PLUp50");
   ObjectDelete("PLDown50");
   ObjectDelete("PLUp100");
   ObjectDelete("PLDown100");
   ObjectDelete("PLUp150");
   ObjectDelete("PLDown150");
   ObjectDelete("PLUp200");
   ObjectDelete("PLDown200");
   ObjectDelete("+0.5");
   ObjectDelete("-0.5");
   ObjectDelete("+1.0");
   ObjectDelete("-1.0");
   ObjectDelete("+1.5");
   ObjectDelete("-1.5");
   ObjectDelete("+2.0");
   ObjectDelete("-2.0");
   
   ObjectDelete("PL0.0");
   ObjectDelete("0.0");
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();

   int    DayOpShift  = iBarShift(NULL, PERIOD_D1, iTime(NULL, PERIOD_D1, 0)) + 0;
   int    DayClShift  = iBarShift(NULL, PERIOD_D1, iTime(NULL, PERIOD_D1, 0)) + 0;
   double DayOpPrice  = iOpen(NULL, PERIOD_D1, DayOpShift); // this should be the close of the first hour (I doesn't like this idea) :(
   double DayClPrice  = iClose(NULL, PERIOD_D1, DayClShift);

//   int    WeekOpShift = iBarShift(NULL, PERIOD_H4, iTime(NULL, PERIOD_W1, 0)) - 1;
   int    WeekClShift = iBarShift(NULL, PERIOD_W1, iTime(NULL, PERIOD_W1, 0)) + 0;
   double WeekOpPrice = iOpen(NULL, PERIOD_W1, WeekClShift); // this should be the close of the first at 4H bar in the first day (this from thomson reuters)
   double WeekClPrice = iClose(NULL, PERIOD_W1, WeekClShift);

   int    MonthClShift= iBarShift(NULL, PERIOD_MN1, iTime(NULL, PERIOD_MN1, 0)) + 0;
   double MonthOpPrice= iOpen(NULL, PERIOD_MN1, MonthClShift);
   double MonthClPrice= iClose(NULL, PERIOD_MN1, MonthClShift);

   color  FontColorDayPercentGain = Silver;
   color  FontColorWeekPercentGain = Silver;
   color  FontColorMonthPercentGain = Silver;
   

   string DailyInfo  = "Daily....: " + DoubleToStr(DayOpPrice, Digits) + "/" + DoubleToStr(DayClPrice, Digits) + " [" + DoubleToStr(MathPow(10, Digits)*MathAbs(DayClPrice-DayOpPrice), 0) + "|" + DoubleToStr(100*(MathAbs(DayClPrice-DayOpPrice)/DayOpPrice),2) + "%]";
   string WeeklyInfo = "Weekly...: " + DoubleToStr(WeekOpPrice, Digits) + "/" + DoubleToStr(WeekClPrice, Digits) + " [" + DoubleToStr(MathPow(10, Digits)*MathAbs(WeekClPrice-WeekOpPrice), 0) + "|" + DoubleToStr(100*(MathAbs(WeekClPrice-WeekOpPrice)/WeekOpPrice),2) + "%]";
   string MonthlyInfo= "Monthly..: " + DoubleToStr(MonthOpPrice, Digits) + "/" + DoubleToStr(MonthClPrice, Digits) + " [" + DoubleToStr(MathPow(10, Digits)*MathAbs(MonthClPrice-MonthOpPrice), 0) + "|" + DoubleToStr(100*(MathAbs(MonthClPrice-MonthOpPrice)/MonthOpPrice),2) + "%]";
   
   if ((DayClPrice - DayOpPrice) < 0 )
     FontColorDayPercentGain = FontMinus;
    else
     FontColorDayPercentGain = FontPlus;
    
   if ((WeekClPrice - WeekOpPrice) < 0)
     FontColorWeekPercentGain = FontMinus;
    else
     FontColorWeekPercentGain = FontPlus;
    
   if ((MonthClPrice - MonthOpPrice) < 0)
     FontColorMonthPercentGain = FontMinus;
    else
     FontColorMonthPercentGain = FontPlus;

//----
   if(ObjectFind("DailyStat") == -1) {
     SetUpTextObject("DailyStat"  ,  0);
     ObjectSetText("DailyStat", DailyInfo, FontSize, Font, FontColorDayPercentGain);
     } else {
     ObjectSetText("DailyStat", DailyInfo, FontSize, Font, FontColorDayPercentGain);
     }
   if(ObjectFind("WeeklyStat") == -1) {
     SetUpTextObject("WeeklyStat" , 10);
     ObjectSetText("WeeklyStat", WeeklyInfo, FontSize, Font, FontColorWeekPercentGain);
     } else {
     ObjectSetText("WeeklyStat", WeeklyInfo, FontSize, Font, FontColorWeekPercentGain);
     }
   if(ObjectFind("MonthlyStat") == -1) {
     SetUpTextObject("MonthlyStat", 20);
     ObjectSetText("MonthlyStat", MonthlyInfo, FontSize, Font, FontColorMonthPercentGain);
     } else {
     ObjectSetText("MonthlyStat", MonthlyInfo, FontSize, Font, FontColorMonthPercentGain);
     }
//----

   if(ShowPriceLabel) {
   SetUpPriceLabel("PL0.0"    , Gold, 1.000*DayOpPrice);
   SetUpPLText    ("0.0"      , Gold, 1.000*DayOpPrice);
   SetUpPriceLabel("PLUp50"   , Blue, 1.005*DayOpPrice); /* up   0.50% means 100.50% from opening price right :) */
   SetUpPLText    ("+0.5"     , Blue, 1.005*DayOpPrice);
   SetUpPriceLabel("PLDown50" ,  Red, 0.995*DayOpPrice); /* down 0.50% means  99.50% from opening price right :) */
   SetUpPLText    ("-0.5"     ,  Red, 0.995*DayOpPrice);
   SetUpPriceLabel("PLUp100"  , Blue, 1.010*DayOpPrice);  /* up   1.00% means 101.00% from opening price right :) */
   SetUpPLText    ("+1.0"     , Blue, 1.010*DayOpPrice);
   SetUpPriceLabel("PLDown100",  Red, 0.990*DayOpPrice);  /* down 1.00% means  99.00% from opening price right :) */
   SetUpPLText    ("-1.0"     ,  Red, 0.990*DayOpPrice);
   SetUpPriceLabel("PLUp150"  , Blue, 1.015*DayOpPrice);  /* up   1.50% means 101.50% from opening price right :) */
   SetUpPLText    ("+1.5"     , Blue, 1.015*DayOpPrice);
   SetUpPriceLabel("PLDown150",  Red, 0.985*DayOpPrice);  /* down 1.50% means  98.50% from opening price right :) */
   SetUpPLText    ("-1.5"     ,  Red, 0.985*DayOpPrice);
   SetUpPriceLabel("PLUp200"  , Blue, 1.020*DayOpPrice);  /* up   2.00% means 102.00% from opening price right :) */
   SetUpPLText    ("+2.0"     , Blue, 1.020*DayOpPrice);
   SetUpPriceLabel("PLDown200",  Red, 0.980*DayOpPrice);  /* down 2.00% means  98.00% from opening price right :) */
   SetUpPLText    ("-2.0"     ,  Red, 0.980*DayOpPrice);
   }
   
   return(0);
  }
  
void SetUpTextObject(string name, int offset)
  {
   ObjectDelete(name);
   ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
   ObjectSet(name, OBJPROP_BACK, back);
   ObjectSet(name, OBJPROP_CORNER, corner);
   ObjectSet(name, OBJPROP_XDISTANCE, xdis);
   ObjectSet(name, OBJPROP_YDISTANCE, ydis + offset);
   GetLastError();  
  }

void SetUpPLText(string Name, color Color, double price)
  {
   ObjectDelete(Name);
   ObjectCreate(Name, OBJ_TEXT, 0, 0, 0);
   ObjectSet(Name, OBJPROP_BACK, back);
   ObjectSet(Name, OBJPROP_TIME1, Time[0]);
   ObjectSet(Name, OBJPROP_PRICE1, price); /* double price */
   ObjectSetText(Name, Name, 7, Font, Color);
   GetLastError();  
  }

void SetUpPriceLabel(string Name, color Color, double price)
  { 
   ObjectDelete(Name);
   ObjectCreate(Name, OBJ_ARROW, 0, 0, 0 );
   ObjectSet(Name, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
   ObjectSet(Name, OBJPROP_COLOR, Color);
   ObjectSet(Name, OBJPROP_TIME1, Time[0]);
   ObjectSet(Name, OBJPROP_PRICE1, price); /* double price */
   GetLastError();
  }
//+------------------------------------------------------------------+  

Comments