Snippets

nicholishen Symbol in background

Created by nicholishen last modified
//+------------------------------------------------------------------+
//|                                                SymbolOnChart.mq4 |
//|                                                      nicholishen |
//|                                   www.reddit.com/u/nicholishenFX |
//+------------------------------------------------------------------+
#property copyright "nicholishen"
#property link      "www.reddit.com/u/nicholishenFX"
#property version   "1.00"
#property strict
#property indicator_chart_window

#define MAX_FONT 100

#include <ChartObjects\ChartObjectsTxtControls.mqh>

input color LabelColor = clrDarkSlateGray;


CChartObjectLabel *label;
long x_distance; 
long y_distance; 

int OnInit()
{
   Comment("");
   ObjectDelete(0,"symbol_on_chart");
   label = new CChartObjectLabel();
   if(!ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0,x_distance)) 
   { 
      Print("Failed to get the chart width! Error code = ",GetLastError()); 
      return INIT_FAILED; 
   } 
   if(!ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0,y_distance)) 
   { 
      Print("Failed to get the chart height! Error code = ",GetLastError()); 
      return INIT_FAILED; 
   } 
   int font_size = (int)x_distance/10;
   font_size = font_size > MAX_FONT ? MAX_FONT : font_size;
   
   string name = Symbol()+IntegerToString(GetMicrosecondCount());
   if(!label.Create(0,"symbol_on_chart",0,(int)x_distance/2,(int)y_distance/2))
   {
      Print("FAILED");
      return INIT_FAILED;
   }
   label.Description(Symbol());
   label.Anchor(ANCHOR_CENTER);
   label.FontSize(font_size); 
   label.Font("Arial Black");
   label.Background(true);
   label.Color(LabelColor);
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   return(rates_total);
}
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
//---
   if( id == CHARTEVENT_CHART_CHANGE)
   {
      if(!ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0,x_distance)) 
      { 
         Print("Failed to get the chart width! Error code = ",GetLastError()); 
      } 
      if(!ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0,y_distance)) 
      { 
         Print("Failed to get the chart height! Error code = ",GetLastError()); 
      } 
      int font_size = (int)x_distance/10;
      font_size = font_size > MAX_FONT ? MAX_FONT : font_size;
      label.FontSize(font_size);
      label.X_Distance((int)x_distance/2);
      label.Y_Distance((int)y_distance/2);  
   }
}

void OnDeinit(const int reason)
{
   delete label;
}
//+------------------------------------------------------------------+

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.