gainlossinfo

Download script
Author: Copyright � 2013, Andriy Moraru
gainlossinfo

This tool is designed to enhance your trading chart by providing an instant visual summary of price movements for each individual candle. Instead of forcing you to manually calculate how much a price has moved, it automatically identifies significant gains and losses and labels them directly on your screen.

Core Functionality

The script monitors price action and calculates two specific types of data: Percentage Change and Pip Movement. It acts as a visual overlay, placing text labels above or below the candles based on whether the price moved up (Gain) or down (Loss).

Key Features & Logic:

  • Customizable Comparison Methods: The tool allows you to choose how it calculates profit and loss. You can set it to compare the current candle's closing price against the previous candle's close, or alternatively, compare the current candle's close against its own opening price.

  • Dynamic Visual Feedback: To keep your chart clean and readable, the script uses a "threshold" system. It will only display a label if the movement meets your specific requirements:

    • Percentage Limit: You can decide the minimum percentage change required before a label appears (e.g., only show labels for moves greater than 1%).
    • Pip Limit: Similarly, you can set a minimum pip distance to filter out minor market noise.
  • Color-Coded Clarity: The script uses intuitive color coding to help you identify market trends at a glance. Gains are typically displayed in Green, while losses are highlighted in Red. This allows for a quick visual audit of price volatility over a specific period.

  • Smart Positioning: To ensure the text does not overlap with the actual price candles, the tool is programmed to place "Gain" labels at a set distance above the high of the candle and "Loss" labels at the low of the candle.

  • Performance Optimization: The script includes a limit on how many historical bars it processes at once. This ensures that while you get the data you need, the tool remains lightweight and does not cause your trading platform to lag or consume excessive memory.

11 Views
0 Downloads
0 Favorites
gainlossinfo
//+------------------------------------------------------------------+
//|                                                 GainLossInfo.mq4 |
//|                                  Copyright © 2013, Andriy Moraru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, Andriy Moraru"
#property link      "http://www.earnforex.com"
/*
   Shows percentage and pip gain/loss for a candle.
   Can calculate gain/loss compared either to the previous Close or to the current Open.
*/
//--- The indicator uses only objects for display, but the line below is required for it to work.
#property indicator_chart_window
//---
extern double PercentageLimit=1.0; // Will not display number if percentage gain/loss is below limit.
extern int PipsLimit=1000; // Will not display number if pips gain/loss is below limit.
//--- If true, will compare Close of the current candle to Close of the previous one. Otherwise compares current Close to current Open.
extern bool CloseToClose=true;
extern color DisplayLossColor = Red;
extern color DisplayGainColor = LimeGreen;
extern int DisplayDistance=100; // Distance in pips from High/Low to percentage display.
extern int MaxBars=100; // More bars - more objects - more lag and memory usage.
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   for(int i=0; i<Bars; i++)
     {
      ObjectDelete("RedPercent-"+TimeToStr(Time[i],TIME_DATE|TIME_MINUTES));
      ObjectDelete("GreenPercent-"+TimeToStr(Time[i],TIME_DATE|TIME_MINUTES));
      ObjectDelete("RedPips-"+TimeToStr(Time[i],TIME_DATE|TIME_MINUTES));
      ObjectDelete("GreenPips-"+TimeToStr(Time[i],TIME_DATE|TIME_MINUTES));
     }
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   string perc,pips,name;
   double start;
//---
   int counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(limit>MaxBars) limit=MaxBars-1;
//---
   for(int i=0; i<=limit; i++)
     {
      if((CloseToClose) && (i+1<Bars)) start=Close[i+1];
      else start=Open[i];
      //---
      if(((Close[i]-start)/start)*100>=PercentageLimit) // Gain percent display
        {
         name = "GreenPercent-" + TimeToStr(Time[i], TIME_DATE|TIME_MINUTES);
         perc = DoubleToStr(((Close[i] - start) / start) * 100, 1) + "%";
         if(ObjectFind(name)!=-1) ObjectDelete(name);
         ObjectCreate(name,OBJ_TEXT,0,Time[i],High[i]+DisplayDistance*Point);
         ObjectSetText(name,perc,10,"Verdana",DisplayGainColor);
        }
      else if(((start-Close[i])/start)*100>=PercentageLimit) // Loss percent display
        {
         name = "RedPercent-" + TimeToStr(Time[i], TIME_DATE|TIME_MINUTES);
         perc = DoubleToStr(((start - Close[i]) / start) * 100, 1) + "%";
         if(ObjectFind(name)!=-1) ObjectDelete(name);
         ObjectCreate(name,OBJ_TEXT,0,Time[i],High[i]+DisplayDistance*Point);
         ObjectSetText(name,perc,10,"Verdana",DisplayLossColor);
        }
      //---
      if((Close[i]-start)/Point>=PipsLimit) // Gain pips display
        {
         name = "GreenPips-" + TimeToStr(Time[i], TIME_DATE|TIME_MINUTES);
         pips = DoubleToStr((Close[i] - start) / Point, 0);
         if(ObjectFind(name)!=-1) ObjectDelete(name);
         ObjectCreate(name,OBJ_TEXT,0,Time[i],Low[i]);
         ObjectSetText(name,pips,10,"Verdana",DisplayGainColor);
        }
      else if((start-Close[i])/Point>=PipsLimit) // Loss pips display
        {
         name = "RedPips-" + TimeToStr(Time[i], TIME_DATE|TIME_MINUTES);
         pips = DoubleToStr((start - Close[i]) / Point, 0);
         if(ObjectFind(name)!=-1) ObjectDelete(name);
         ObjectCreate(name,OBJ_TEXT,0,Time[i],Low[i]);
         ObjectSetText(name,pips,10,"Verdana",DisplayLossColor);
        }
     }

   return(0);
  }
//+------------------------------------------------------------------+

Comments