Split up trade

Author: Copyright © 2022, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Split up trade
ÿþ//+------------------------------------------------------------------+

//|                                               Split up trade.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.00"

//---

#include <Trade\SymbolInfo.mqh>

//---

CSymbolInfo    m_symbol;                     // object of CSymbolInfo class

//--- input parameters

input group             "Split"

input double               InpVolume         = 2.67;              // Position volume (ONLY TWO DIGITS after the comma!)

input uchar                InpXSmaller       = 5;                 // X smaller trades

input group             "Labels"

input int                  InpX              = 50;                // Starting coordinate "X"

input int                  InpY              = 150;               // Starting coordinate "Y"

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

input int                  InpFontSize       = 10;                // Font size

input color                InpColor          = clrDarkOrange;     // Color

//input ENUM_ANCHOR_POINT    InpAnchor         = ANCHOR_LEFT_UPPER; // Anchor type

input bool                 InpBack           = false;             // Background object

input bool                 InpSelection      = false;             // Highlight to move

input bool                 InpHidden         = false;             // Hidden in the object list

//---

string   m_prefix="Split up trade_";

string   m_labels_names_array[];

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

//| Expert initialization function                                   |

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

int OnInit()

  {

   ResetLastError();

   if(!m_symbol.Name(Symbol())) // sets symbol name

     {

      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");

      return(INIT_FAILED);

     }

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

   ArrayResize(m_labels_names_array,InpXSmaller+1);

   int counter=1;

   for(int i=0; i<InpXSmaller+1; i++)

     {

      m_labels_names_array[i]=m_prefix+"#"+IntegerToString(i);

      if(!LabelCreate(ChartID(),m_labels_names_array[i],0,InpX,InpY+counter*20,CORNER_LEFT_UPPER,"#"+IntegerToString(i),InpFont,

                      InpFontSize,InpColor,0,ANCHOR_LEFT_UPPER,InpBack,InpSelection,InpHidden,0))

         return(INIT_FAILED);

      counter++;

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

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

//---

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   double smaller_array[];

   ArrayResize(smaller_array,InpXSmaller);

//---

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

     {

      smaller_array[i]=LotCheck(InpVolume/((double)InpXSmaller),m_symbol);

     }

//---

   double summ=0.0;

   for(int i=0; i<InpXSmaller-1; i++)

     {

      summ+=smaller_array[i];

     }

   smaller_array[InpXSmaller-1]=LotCheck(InpVolume-summ,m_symbol);

   summ+=smaller_array[InpXSmaller-1];

   for(int i=0; i<InpXSmaller+1; i++)

     {

      if(i==0)

         LabelTextChange(ChartID(),m_labels_names_array[i],"Position volume: "+DoubleToString(InpVolume,2)+", X smaller trades: "+IntegerToString(InpXSmaller));

      else

         LabelTextChange(ChartID(),m_labels_names_array[i],"#"+IntegerToString(i)+": "+DoubleToString(smaller_array[i-1],2));

     }

  }

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

//| Lot Check                                                        |

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

double LotCheck(double lots,CSymbolInfo &symbol)

  {

//--- calculate maximum volume

   double volume=NormalizeDouble(lots,2);

   double stepvol=symbol.LotsStep();

   if(stepvol>0.0)

      volume=stepvol*MathFloor(volume/stepvol);

//---

   double minvol=symbol.LotsMin();

   if(volume<minvol)

      volume=0.0;

//---

   double maxvol=symbol.LotsMax();

   if(volume>maxvol)

      volume=maxvol;

//---

   return(volume);

  }

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

//| 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=clrRed,               // 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);

  }

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

//| Move the text label                                              |

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

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

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

               const int    x=0,          // X coordinate

               const int    y=0)          // Y coordinate

  {

//--- reset the error value

   ResetLastError();

//--- move the text label

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x))

     {

      Print(__FUNCTION__,

            ": failed to move X coordinate of the label! Error code = ",GetLastError());

      return(false);

     }

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y))

     {

      Print(__FUNCTION__,

            ": failed to move Y coordinate of the label! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Change corner of the chart for binding the label                 |

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

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

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

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

  {

//--- reset the error value

   ResetLastError();

//--- change anchor corner

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner))

     {

      Print(__FUNCTION__,

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

      return(false);

     }

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

  }

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

//| Delete a text label                                              |

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

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

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

  {

//--- reset the error value

   ResetLastError();

//--- delete the label

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

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

      return(false);

     }

//--- successful execution

   return(true);

  }

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

Comments