Last three deals OUT

Author: Copyright © 2021, Vladimir Karputov
Miscellaneous
It issuies visual alerts to the screen
2 Views
0 Downloads
0 Favorites
Last three deals OUT
ÿþ//+------------------------------------------------------------------+

//|                                         Last three deals OUT.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43161 |

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

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43161"

#property version   "1.001"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input int               InpX        = 15;                   // X-axis distance

input int               InpY        = 25;                   // Y-axis distance

input int               InpGapY     = 18;                   // Gap by Y coordinate

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

input int               InpFontSize = 14;                   // Font size

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

input ENUM_ANCHOR_POINT InpAnchor   = ANCHOR_LEFT_UPPER;    // Anchor type

//---

string   m_prefix                   = "L3UOT_";

bool     m_init_error               = false;    // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- forced initialization of variables

   m_init_error               = false;    // error on InInit

//--- create a timer with a 1 second period

   ResetLastError();

   if(!EventSetTimer(1))

     {

      if(MQLInfoInteger(MQL_TESTER)) // when testing, we will only output to the log about incorrect input parameters

         Print(__FILE__," ",__FUNCTION__,", ERROR: SetTimer ",GetLastError());

      else // if is run on the chart, tell the user about the error

         Alert(__FILE__," ",__FUNCTION__,", ERROR: SetTimer ",GetLastError());

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//---

   long chart_id=ChartID();

   for(int i=0; i<3; i++)

      LabelCreate(chart_id,m_prefix+IntegerToString(i),0,InpX,InpY+i*InpGapY,InpCorner,"start",InpFont,InpFontSize);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   ObjectsDeleteAll(ChartID(),m_prefix,0,OBJ_LABEL);

  }

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

//| 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 value of prev_calculated for next call

   return(rates_total);

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//---

   long chart_id=ChartID();

   for(int i=0; i<3; i++)

     {

      if(ObjectFind(chart_id,m_prefix+IntegerToString(i))<0)

         LabelCreate(chart_id,m_prefix+IntegerToString(i),0,InpX,InpY+i*InpGapY,InpCorner,"***",InpFont,InpFontSize);

      LabelTextChange(chart_id,m_prefix+IntegerToString(i),"#"+IntegerToString(i)+":");

     }

//---

   int counter=2;

//--- request trade history

   datetime time_current      = TimeCurrent();

   MqlDateTime STimeCurrent;

   if(!TimeToStruct(time_current,STimeCurrent))

      return;

   STimeCurrent.hour=0;

   STimeCurrent.min=0;

   STimeCurrent.sec=0;

//--- HistorySelect

   datetime from_date         = StructToTime(STimeCurrent);

   datetime to_date           = from_date+86400;

   HistorySelect(from_date,to_date);

   uint total_deals           = HistoryDealsTotal();

   ulong ticket_history_deal  = 0;

   if(total_deals>0)

     {

      //--- for all deals

      for(uint i=total_deals-1; i>=0; i--)

        {

         if(counter<0)

            break;

         //--- try to get deals ticket_history_deal

         if((ticket_history_deal=HistoryDealGetTicket(i))>0)

           {

            long     deal_time         = HistoryDealGetInteger(ticket_history_deal,DEAL_TIME);

            long     deal_type         = HistoryDealGetInteger(ticket_history_deal,DEAL_TYPE);

            if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)

              {

               long     deal_entry        = HistoryDealGetInteger(ticket_history_deal,DEAL_ENTRY);

               if(deal_entry!=DEAL_ENTRY_IN)

                 {

                  long     deal_magic        = HistoryDealGetInteger(ticket_history_deal,DEAL_MAGIC);

                  long     deal_reason       = HistoryDealGetInteger(ticket_history_deal,DEAL_REASON);

                  double   deal_volume       = HistoryDealGetDouble(ticket_history_deal,DEAL_VOLUME);

                  double   deal_commission   = HistoryDealGetDouble(ticket_history_deal,DEAL_COMMISSION);

                  double   deal_swap         = HistoryDealGetDouble(ticket_history_deal,DEAL_SWAP);

                  double   deal_profit       = HistoryDealGetDouble(ticket_history_deal,DEAL_PROFIT);

                  string   deal_symbol       = HistoryDealGetString(ticket_history_deal,DEAL_SYMBOL);

                  string   deal_comment      = HistoryDealGetString(ticket_history_deal,DEAL_COMMENT);

                  //---

                  string type=(deal_type==DEAL_TYPE_BUY)?"buy ":"sell";

                  double dbl_profit=deal_commission+deal_swap+deal_profit;

                  string profit=(dbl_profit>=0.0)?"+"+DoubleToString(dbl_profit,2):DoubleToString(dbl_profit,2);

                  string text=deal_symbol+" | "+TimeToString(deal_time,TIME_DATE|TIME_SECONDS)+" | "+type+" | "+DoubleToString(deal_volume,2)+" | "+

                              profit;

                  LabelTextChange(chart_id,m_prefix+IntegerToString(counter),text);

                  counter--;

                 }

              }

           }

        }

     }

   if(counter<0)

      ChartRedraw();

  }

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

//| Create a text label                                              |

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

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

                 const string             name="Label",              // label name

                 const int                sub_window=0,              // subwindow index

                 const int                x=0,                       // X coordinate

                 const int                y=0,                       // Y coordinate

                 const ENUM_BASE_CORNER   corner=CORNER_LEFT_UPPER,  // chart corner for anchoring

                 const string             text="Label",              // text

                 const string             font="Arial",              // font

                 const int                font_size=10,              // font size

                 const color              clr=clrYellowGreen,        // color

                 const double             angle=0.0,                 // text slope

                 const ENUM_ANCHOR_POINT  anchor=ANCHOR_LEFT_UPPER,  // 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

  {

//--- reset the error value

   ResetLastError();

//--- create a text label

   if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))

     {

      Print(__FUNCTION__,

            ": failed to create text label! Error code = ",GetLastError());

      return(false);

     }

//--- set label coordinates

   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);

   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);

//--- set the chart's corner, relative to which point coordinates are defined

   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);

//--- 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 label 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);

//--- successful execution

   return(true);

  }

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

//| Change the label text                                            |

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

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

                     const string   name="Label", // 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);

     }

//--- successful execution

   return(true);

  }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---