Trade through the Bank

Author: Copyright © 2022, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Trade through the Bank
ÿþ//+------------------------------------------------------------------+

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

#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 0

#property indicator_plots   0

//--- plot Label1

#property indicator_label1  "Label1"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- input parameters

input ENUM_OBJECT       InpObject   = OBJ_ARROW_RIGHT_PRICE;   // Object type

input string            InpPrefix   = "TTB_";                  // Prefix

//---

string   m_hline_name               = InpPrefix+"Breakeven line";

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

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

  {

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

   return(rates_total);

  }

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

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

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

     {

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

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

           }

        }

     }

//---

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

     {

      net_price_buy=total_price_multiply_volume_buy/total_volume_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;

         /*

         total_invest_buy  -> 100.0%

         dbl_profit        -> x%

         */

         profit+=DoubleToString(dbl_profit,2)+"("+DoubleToString(dbl_profit*100.0/total_invest_buy,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

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 ---