Price in selected currency

Author: Copyright © 2022, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
Price in selected currency
ÿþ//+------------------------------------------------------------------+

//|                                   Price in selected currency.mq5 |

//|                              Copyright © 2022, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2022, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.000"

#property description "Price in selected currency"

#property description "For example, a stock is worth $170 and you entered a rate of 64.80"

#property description "The indicator will display you prices: 11016"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots 0

//--- input parameters

input double            InpRate        = 64.80;             // Your course

//---

input ENUM_BASE_CORNER  InpCorner      = CORNER_RIGHT_LOWER;// Chart corner for anchoring

input string            InpFont        = "Lucida Console";  // Font

input int               InpFontSize    = 12;                // Font size

input ENUM_ANCHOR_POINT InpAnchor      = ANCHOR_LEFT;       // Anchor type

//---

input color    InpColor       = clrBlue;     // The color

/*input color    InpColorBearish= clrRed;      // The color of bearish bars

input bool     InpIndent      = true;        // Indent*/

//---

string         m_name         = "Price_in_selected_currency_"; // name for object

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//---

   ObjectDelete(ChartID(),m_name);

   ResetLastError();

   if(!TextCreate(ChartID(),m_name,0,0,0.0,"---",0,InpFont,InpFontSize,InpColor,InpAnchor))

     {

      ObjectDelete(ChartID(),m_name);

      return(INIT_SUCCEEDED);

     }

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator deinitialization function                       |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

   ObjectDelete(ChartID(),m_name);

  }

//+------------------------------------------------------------------+

//| 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[])

  {

//---

   int i=rates_total-1;

   if(!TextMove(ChartID(),m_name,time[i],close[i]))

      return(0);

   string text=" "+DoubleToString(close[i]*InpRate,Digits());

   if(!TextChange(ChartID(),m_name,text))

      return(0);

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| Creating Text object                                             |

//+------------------------------------------------------------------+

bool TextCreate(const long              chart_ID=0,               // chart's ID

                const string            name="Text",              // object name

                const int               sub_window=0,             // subwindow index

                datetime                time=0,                   // anchor point time

                double                  price=0,                  // anchor point price

                const string            text="Text",              // the text itself

                const double            angle=0.0,                // text slope

                const string            font="Lucida Console",    // font

                const int               font_size=10,             // font size

                const color             clr=clrBlue,              // color

                const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT,// anchor type

                const bool              back=false,               // in the background

                const bool              selection=false,          // highlight to move

                const bool              hidden=true,              // hidden in the object list

                const long              z_order=0)                // priority for mouse click

  {

//--- set anchor point coordinates if they are not set

//ChangeTextEmptyPoint(time,price);

//--- reset the error value

   ResetLastError();

//--- create Text object

   if(!ObjectCreate(chart_ID,name,OBJ_TEXT,sub_window,time,price))

     {

      Print(__FUNCTION__,

            ": failed to create \"Text\" object! Error code = ",GetLastError());

      return(false);

     }

//--- set the text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//--- set text font

   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);

//--- set font size

   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);

//--- set the slope angle of the text

   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);

//--- set anchor type

   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);

//--- set color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of moving the object by mouse

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- hide (true) or display (false) graphical object name in the object list

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- set the priority for receiving the event of a mouse click in the chart

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,10);

//--- successful execution

   return(true);

  }

//+------------------------------------------------------------------+

//| Move the anchor point                                            |

//+------------------------------------------------------------------+

bool TextMove(const long   chart_ID=0,  // chart's ID

              const string name="Text", // object name

              datetime     time=0,      // anchor point time coordinate

              double       price=0)     // anchor point price coordinate

  {

//--- if point position is not set, move it to the current bar having Bid price

   if(!time)

      time=TimeCurrent();

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- move the anchor point

   if(!ObjectMove(chart_ID,name,0,time,price))

     {

      Print(__FUNCTION__,

            ": failed to move the anchor point! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

//+------------------------------------------------------------------+

//| Change the object text                                           |

//+------------------------------------------------------------------+

bool TextChange(const long   chart_ID=0,              // chart's ID

                const string name="Text",             // object name

                const string text="Text")             // text

  {

//--- reset the error value

   ResetLastError();

//--- change object text

   if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text))

     {

      Print(__FUNCTION__,

            ": failed to change the text! Error code = ",GetLastError());

      return(false);

     }

   return(true);

  }

//+------------------------------------------------------------------+

Comments