Trade through the Bank 2

Author: Copyright © 2022, Vladimir Karputov
Price Data Components
Series array that contains open time of each bar
0 Views
0 Downloads
0 Favorites
Trade through the Bank 2
ÿþ//+------------------------------------------------------------------+

//|                                     Trade through the Bank 2.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   "2.006"

#property description "The indicator calculates the breakeven line and profit, "

#property description "and it receives information about position prices through graphical objects"

#property description " "

#property description "Encoding information occurs through the name of the object"

#property description "Example: prefix \"TTB_\", 1 lot, price $288.75. Object name: \"TTB_1_288.75_Daily Arrow 42518\""

#property description "\"Daily Arrow 42518\" is the unique name given to the graphic object on the graphic"

//---

#include<ChartObjects\ChartObjectsLines.mqh>

//---

CChartObjectHLine  m_hline;               // object of CChartObjectHLine class

//---

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   0

//--- plot Net_Price

#property indicator_label1  "Net_Price"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Profit_money

#property indicator_label2  "Profit_money"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Profit_percentage

#property indicator_label3  "Profit_percentage"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot Total_invested

#property indicator_label4  "Total_invested"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrRed

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//+------------------------ PanelDialog -----------------------------+

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

//|                                                  PanelDialog.mqh |

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

#include <Controls\Dialog.mqh>

#include <Controls\Button.mqh>

#include <Controls\Label.mqh>

#include <Controls\EditKVN.mqh>

#include <Controls\Edit.mqh>

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

//| defines                                                          |

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

//--- indents and gaps

#define INDENT_LEFT                         (11)      // indent from left (with allowance for border width)

#define INDENT_TOP                          (11)      // indent from top (with allowance for border width)

#define CONTROLS_GAP_X                      (2)       // gap by X coordinate

#define CONTROLS_GAP_Y                      (5)       // gap by Y coordinate

//--- for buttons

#define BUTTON_HEIGHT                       (15)      // size by Y coordinate

//--- for the indication area

#define EDIT_WIDTH                          (120)     // size by X coordinate

#define EDIT_HEIGHT                         (15)      // size by Y coordinate

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

//| Class CPanelDialog                                               |

//| Usage: main dialog of the Controls application                   |

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

class CPanelDialog : public CAppDialog

  {

private:

   CLabel            m_label_lot;                     // CLabel object

   CEditKVN          m_editkvn_lot;                   // CEditKVN object

   CLabel            m_label_price;                   // CLabel object

   CEditKVN          m_editkvn_price;                 // CEditKVN object

   CLabel            m_label_time;                    // CLabel object

   CEdit             m_edit_time;                     // CEdit time

   CButton           m_button_create;                 // CButton object



public:

                     CPanelDialog(void);

                    ~CPanelDialog(void);

   //--- create

   virtual bool      Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);

   //--- chart event handler

   virtual bool      OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);





protected:

   //--- create dependent controls

   bool              CreateLabels(void);

   bool              CreateEdits(void);

   bool              CreateButtonCreate(void);

   //--- handlers of the dependent controls events

   void              On_Click_Button_Create(void);

   void              On_EndEdit_EditKVN_Lot(void);

   void              On_EndEdit_EditKVN_Price(void);

   void              On_EndEdit_Edit_Time(void);



  };

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

//| Event Handling                                                   |

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

EVENT_MAP_BEGIN(CPanelDialog)

ON_EVENT(ON_CLICK,m_button_create,On_Click_Button_Create)

ON_EVENT(ON_END_EDIT,m_editkvn_lot,On_EndEdit_EditKVN_Lot)

ON_EVENT(ON_END_EDIT,m_editkvn_price,On_EndEdit_EditKVN_Price)

ON_EVENT(ON_END_EDIT,m_edit_time,On_EndEdit_Edit_Time)

EVENT_MAP_END(CAppDialog)

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

//| Constructor                                                      |

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

CPanelDialog::CPanelDialog(void)

  {

  }

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

//| Destructor                                                       |

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

CPanelDialog::~CPanelDialog(void)

  {

  }

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

//| Create                                                           |

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

bool CPanelDialog::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)

  {

   if(!CAppDialog::Create(chart,name,subwin,x1,y1,x2,y2))

      return(false);

//--- create dependent controls

   if(!CreateLabels())

      return(false);

   if(!CreateEdits())

      return(false);

   if(!CreateButtonCreate())

      return(false);

//--- succeed

   return(true);

  }

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

//| Create "Labels"                                                  |

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

bool CPanelDialog::CreateLabels(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   int y1=INDENT_TOP;

   int x2=x1+EDIT_WIDTH;

   int y2=y1+EDIT_HEIGHT;

//--- create label "Label Lot"

   if(!m_label_lot.Create(m_chart_id,m_name+"Label Lot",m_subwin,x1,y1,x2,y2))

      return(false);

   if(!m_label_lot.Font("Consolas"))

      return(false);

   if(!m_label_lot.Text("Lots"))

      return(false);

   if(!Add(m_label_lot))

      return(false);

//--- create label "Label Price"

   x1=x1+CONTROLS_GAP_X+EDIT_WIDTH;

   x2=x1+EDIT_WIDTH;

   if(!m_label_price.Create(m_chart_id,m_name+"Label Price",m_subwin,x1,y1,x2,y2))

      return(false);

   if(!m_label_price.Font("Consolas"))

      return(false);

   if(!m_label_price.Text("Price"))

      return(false);

   if(!Add(m_label_price))

      return(false);

//--- create label "Label Time"

   x1=x1+EDIT_WIDTH+CONTROLS_GAP_X;

   x2=x1+EDIT_WIDTH;

   if(!m_label_time.Create(m_chart_id,m_name+"Label Time",m_subwin,x1,y1,x2,y2))

      return(false);

   if(!m_label_time.Font("Consolas"))

      return(false);

   if(!m_label_time.Text("yyyy.mm.dd hh:mi"))

      return(false);

   if(!Add(m_label_time))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create "Edits"                                                   |

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

bool CPanelDialog::CreateEdits(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   int y1=INDENT_TOP+CONTROLS_GAP_Y+EDIT_HEIGHT;

   int x2=x1+EDIT_WIDTH;

   int y2=y1+EDIT_HEIGHT;

//--- create editkvn "Edit Lot"

   if(!m_editkvn_lot.Create(m_chart_id,m_name+"Edit Lot",m_subwin,x1,y1,x2,y2))

      return(false);

   if(!m_editkvn_lot.Font("Consolas"))

      return(false);

   if(!m_editkvn_lot.Text("1"))

      return(false);

   if(!m_editkvn_lot.ReadOnly(false))

      return(false);

   m_editkvn_lot.Accuracy(0);

   if(!Add(m_editkvn_lot))

      return(false);

//--- create editkvn "Edit Price"

   x1=x1+CONTROLS_GAP_X+EDIT_WIDTH;

   x2=x1+EDIT_WIDTH;

   if(!m_editkvn_price.Create(m_chart_id,m_name+"Edit Price",m_subwin,x1,y1,x2,y2))

      return(false);

   if(!m_editkvn_price.Font("Consolas"))

      return(false);

   if(!m_editkvn_price.Text("310.02"))

      return(false);

   if(!m_editkvn_price.ReadOnly(false))

      return(false);

   m_editkvn_price.Accuracy(2);

   if(!Add(m_editkvn_price))

      return(false);

//--- create editkvn "Edit Time"

   x1=x1+CONTROLS_GAP_X+EDIT_WIDTH;

   x2=x1+EDIT_WIDTH;

   if(!m_edit_time.Create(m_chart_id,m_name+"Edit Time",m_subwin,x1,y1,x2,y2))

      return(false);

   if(!m_edit_time.Font("Consolas"))

      return(false);

   if(!m_edit_time.Text("2022.02.21 16:30"))

      return(false);

   if(!m_edit_time.ReadOnly(false))

      return(false);

   if(!Add(m_edit_time))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create "Buttons Create"                                          |

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

bool CPanelDialog::CreateButtonCreate(void)

  {

//--- coordinates

   int x1=INDENT_LEFT+EDIT_WIDTH+CONTROLS_GAP_X+EDIT_WIDTH+CONTROLS_GAP_X;

   int y1=INDENT_TOP+CONTROLS_GAP_Y+EDIT_HEIGHT+CONTROLS_GAP_Y+EDIT_HEIGHT;

   int x2=x1+EDIT_WIDTH;

   int y2=y1+EDIT_HEIGHT;

//--- create button "Close Buy"

   if(!m_button_create.Create(m_chart_id,m_name+"Create",m_subwin,x1,y1,x2,y2))

      return(false);

   if(!m_button_create.Font("Consolas"))

      return(false);

   if(!m_button_create.Text("Create"))

      return(false);

   if(!Add(m_button_create))

      return(false);

//--- succeed

   return(true);

  }

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

//| Event handler                                                    |

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

void CPanelDialog::On_Click_Button_Create(void)

  {

   Print("Create");

   Print(StringToDouble(m_editkvn_lot.Text()));

   Print(StringToDouble(m_editkvn_price.Text()));

   Print(StringToTime(m_edit_time.Text()));

//--- reset the error value

   ResetLastError();

//--- create a price label

//---

   long chart_id=ChartID();

   string name=InpPrefix+m_editkvn_lot.Text()+"_"+m_editkvn_price.Text()+"_"+"Daily Arrow "+IntegerToString(rand(),5,'0');

   if(!ObjectCreate(ChartID(),name,OBJ_ARROW_RIGHT_PRICE,0,StringToTime(m_edit_time.Text()),StringToDouble(m_editkvn_price.Text())))

     {

      Print(__FUNCTION__,

            ": failed to create the right price label! Error code = ",GetLastError());

      return;

     }

//--- set the label color

   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,clrMediumAquamarine);

//--- set the border line style

   ObjectSetInteger(chart_id,name,OBJPROP_STYLE,STYLE_SOLID);

//--- set the label size

   ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,1);

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

   ObjectSetInteger(chart_id,name,OBJPROP_BACK,false);

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

//--- when creating a graphical object using ObjectCreate function, the object cannot be

//--- highlighted and moved by default. Inside this method, selection parameter

//--- is true by default making it possible to highlight and move the object

   ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,true);

   ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,false);

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

   ObjectSetInteger(chart_id,name,OBJPROP_HIDDEN,false);

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

   ObjectSetInteger(chart_id,name,OBJPROP_ZORDER,0);

//--- successful execution

   return;

  }

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

//| Event handler                                                    |

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

void CPanelDialog::On_EndEdit_EditKVN_Lot(void)

  {

//Print(__FUNCTION__);

   Print(m_editkvn_lot.Text());

  }

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

//| Event handler                                                    |

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

void CPanelDialog::On_EndEdit_EditKVN_Price(void)

  {

//Print(__FUNCTION__);

   Print(m_editkvn_price.Text());

  }

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

//| Event handler                                                    |

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

void CPanelDialog::On_EndEdit_Edit_Time(void)

  {

//Print(__FUNCTION__);

   Print(StringToTime(m_edit_time.Text()));

  }

//+------------------------- Indicator ------------------------------+

//---

CPanelDialog   m_dialog;                              // object of CPanelDialog class

//--- input parameters

input string            InpPrefix               = "TTB_";   // Prefix

input bool              InpRemovePreviousPanel  = true;     // Remove all objects from the previous panel

//--- indicator buffers

double   Net_Price_Buffer[];

double   Profit_money_Buffer[];

double   Profit_percentage_Buffer[];

double   Total_invested_Buffer[];

//---

string   m_hline_name               = InpPrefix+"Breakeven line";

double   m_net_price                = 0.0;

double   m_profit_money             = 0.0;

double   m_profit_percentage        = 0.0;

double   m_total_invested           = 0.0;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- protection: remove all objects from the previous panel

   if(InpRemovePreviousPanel)

     {

      string string_numbers="0123456789";

      uchar  uchar_array[];// array

      StringToCharArray(string_numbers,uchar_array,0,WHOLE_ARRAY,CP_ACP);

      int arr_size=ArraySize(uchar_array);

      //---

      long chart_id=ChartID();

      int objects_total=ObjectsTotal(chart_id,0,-1);

      for(int i=objects_total-1; i>=0; i--)

        {

         string object_name=ObjectName(chart_id,i,0,-1);

         ushort symbol_code=StringGetCharacter(object_name,0);

         bool non_number=true;

         for(int j=0; j<arr_size; j++)

           {

            if(symbol_code==uchar_array[j])

              {

               non_number=false;

               //Print(object_name," ",StringGetCharacter(object_name,0));

               break;

              }

           }

         if(!non_number)

            ObjectDelete(chart_id,object_name);

        }

     }

//--- create application dialog

   if(!m_dialog.Create(0,"Trade through the Bank 2",0,50,50,444,155))

      return(INIT_FAILED);

//--- run application

   if(!m_dialog.Run())

      return(INIT_FAILED);

//--- indicator buffers mapping

   SetIndexBuffer(0,Net_Price_Buffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(1,Profit_money_Buffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(2,Profit_percentage_Buffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,Total_invested_Buffer,INDICATOR_CALCULATIONS);

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

   EventSetTimer(3);

//---

   m_hline.Create(ChartID(),m_hline_name,0,0.0);

   m_hline.Color(clrBlue);

   m_hline.Width(2);

   m_hline.Selectable(true);

   m_hline.Selected(false);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//--- destroy application dialog

   m_dialog.Destroy(reason);

//--- protection: remove all objects from the previous panel

   if(InpRemovePreviousPanel)

     {

      string string_numbers="0123456789";

      uchar  uchar_array[];// array

      StringToCharArray(string_numbers,uchar_array,0,WHOLE_ARRAY,CP_ACP);

      int arr_size=ArraySize(uchar_array);

      //---

      long chart_id=ChartID();

      int objects_total=ObjectsTotal(chart_id,0,-1);

      for(int i=objects_total-1; i>=0; i--)

        {

         string object_name=ObjectName(chart_id,i,0,-1);

         ushort symbol_code=StringGetCharacter(object_name,0);

         bool non_number=true;

         for(int j=0; j<arr_size; j++)

           {

            if(symbol_code==uchar_array[j])

              {

               non_number=false;

               //Print(object_name," ",StringGetCharacter(object_name,0));

               break;

              }

           }

         if(!non_number)

            ObjectDelete(chart_id,object_name);

        }

     }

//--- destroy the timer after completing the work

   EventKillTimer();

//---

   m_hline.Delete();

  }

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

//| 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 limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

   for(int i=limit; i<rates_total; i++)

     {

      Net_Price_Buffer[i]=0.0;

      Profit_money_Buffer[i]=0.0;

      Profit_percentage_Buffer[i]=0.0;

      Total_invested_Buffer[i]=0.0;

     }

//--- now we fill the current bar through the variables declared in the 'header'

   int i=rates_total-1;

   Net_Price_Buffer[i]        = m_net_price;

   Profit_money_Buffer[i]     = m_profit_money;

   Profit_percentage_Buffer[i]= m_profit_percentage;

   Total_invested_Buffer[i]   = m_total_invested;

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

   return(rates_total);

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

   m_dialog.ChartEvent(id,lparam,dparam,sparam);

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

   double total_price_multiply_volume_buy    = 0.0;

   double total_volume_buy                   = 0.0;

   double total_invest_buy                   = 0.0;

   double net_price_buy                      = 0.0;

   int    count_buys                         = 0;

//---

   long chart_id=ChartID();

   int total=ObjectsTotal(chart_id,0,OBJ_ARROW_RIGHT_PRICE);

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

     {

      string name=ObjectName(chart_id,i,0,OBJ_ARROW_RIGHT_PRICE);

      //Print(name);

      string to_split=name;   // a string to split into substrings

      string sep="_";         // a   separator as a character

      ushort u_sep;           // the code of the separator character

      string result[];        // an array to get strings

      //--- get the separator code

      u_sep=StringGetCharacter(sep,0);

      //--- split the string to substrings

      int k=StringSplit(to_split,u_sep,result);

      //--- Now output all obtained strings

      if(k>3)

        {

         for(int j=0; j<k; j++)

           {

            //PrintFormat("result[%d]=\"%s\"",j,result[j]);

           }

         double result_volume=StringToDouble(result[1]);

         double result_price=StringToDouble(result[2]);

         //Print(result[1]," to double: ",StringToDouble(result[1]));

         if(result_volume>0.0 && result_price>0.0)

           {

            total_price_multiply_volume_buy+=result_volume*result_price;

            total_volume_buy+=result_volume;

            total_invest_buy+=result_price;

            count_buys++;

           }

         m_total_invested=total_invest_buy;

        }

     }

//---

   if(total_price_multiply_volume_buy!=0 && total_volume_buy!=0)

     {

      net_price_buy=total_price_multiply_volume_buy/total_volume_buy;

      m_net_price=net_price_buy;

      //---

      string profit=" Profit ";

      MqlTick tick_array;

      if(SymbolInfoTick(Symbol(),tick_array))

        {

         double tick_value=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);

         double dbl_profit=(tick_array.bid-net_price_buy)*tick_value*total_volume_buy;

         m_profit_money=dbl_profit;

         /*

         total_invest_buy  -> 100.0%

         dbl_profit        -> x%

         */

         double profit_percentage=dbl_profit*100.0/total_invest_buy;

         m_profit_percentage=profit_percentage;

         profit+=DoubleToString(dbl_profit,2)+"("+DoubleToString(profit_percentage,2)+"%)";

        }

      //---

      if(ObjectFind(ChartID(),m_hline_name)<0)

        {

         m_hline.Create(ChartID(),m_hline_name,0,0.0);

         m_hline.Color(clrBlue);

         m_hline.Width(2);

         m_hline.Selectable(true);

         m_hline.Selected(false);

        }

      //---

      m_hline.Price(0,net_price_buy);

      m_hline.Description("Breakeven "+DoubleToString(net_price_buy,2)+profit+

                          ", Total invested "+/*currency_profit+*/DoubleToString(total_invest_buy,2));

      ChartRedraw();

     }

  }

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

Comments