Breakeven line

Author: © 2022, Alexey Viktorov
0 Views
0 Downloads
0 Favorites
Breakeven line
ÿþ/********************************************************************\

|                                                 Breakeven line.mq5 |

|                                            © 2022, Alexey Viktorov |

|                       https://www.mql5.com/ru/users/alexeyvik/news |

\********************************************************************/

#property copyright "© 2022, Alexey Viktorov"

#property link      "https://www.mql5.com/ru/users/alexeyvik/news"

#property version   "1.10"

#property indicator_chart_window

//---

#property indicator_buffers 2

#property indicator_plots   2

#property indicator_type1   DRAW_LINE

#property indicator_type2   DRAW_LINE

#property indicator_color1  clrDarkViolet

#property indicator_color2  clrDeepPink

#property indicator_label1  "Buy breakeven";

#property indicator_label2  "Sell breakeven";

//---

input int MagicNumber = 0;

//---

int posTotal,

    firstBar,

    width_in_bars;

/****************indicator buffers****************/

double Breakeven_Buy[],

       Breakeven_Sell[];

/**************Custom indicator initialization function**************/

int OnInit()

 {

  SetIndexBuffer(0, Breakeven_Buy);

  SetIndexBuffer(1, Breakeven_Sell);

  PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);

  PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0);

  IndicatorSetInteger(INDICATOR_DIGITS, _Digits);

  plotShift();

  return(INIT_SUCCEEDED);

 }/******************************************************************/



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

 {

  double tickSize     = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);

  if(posTotal != PositionsTotal())

   {

    ArrayInitialize(Breakeven_Buy, 0.0);

    ArrayInitialize(Breakeven_Sell, 0.0);

    posTotal = PositionsTotal();

    double lotsBuy    = 0.0,

           lotsSell   = 0.0,

           priceBuy   = 0.0,

           priceSell  = 0.0,

           swapBuy    = 0.0,

           swapSell   = 0.0;

    int    totalBuy   = 0,

           totalSell  = 0;

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

     {

      if(PositionGetSymbol(i) == _Symbol && (PositionGetInteger(POSITION_MAGIC) == MagicNumber || MagicNumber == 0))

       {

        ENUM_POSITION_TYPE  posType       = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);

        double              posOpen       = PositionGetDouble(POSITION_PRICE_OPEN),

                            posProfit     = PositionGetDouble(POSITION_PROFIT),

                            posSwap       = PositionGetDouble(POSITION_SWAP),

                            posLots       = PositionGetDouble(POSITION_VOLUME),

                            posComission  = 0.0;

        if(HistorySelectByPosition(PositionGetInteger(POSITION_IDENTIFIER)))

         {

          for(int d = 0; d < HistoryDealsTotal(); d++)

           {

            ulong dealTicket = HistoryDealGetTicket(d);

            ENUM_DEAL_ENTRY entry = (ENUM_DEAL_ENTRY)HistoryDealGetInteger(dealTicket, DEAL_ENTRY);

            if(entry == DEAL_ENTRY_IN)

              posComission += HistoryDealGetDouble(dealTicket, DEAL_COMMISSION)*2;

           }

         }

        if(posType == POSITION_TYPE_BUY)

         {

          totalBuy++;

          swapBuy = (posComission+posSwap)/posLots*tickSize;

          double posPrice = (posOpen-swapBuy)*posLots;

          lotsBuy += posLots;

          priceBuy  += posPrice;

         }

        if(posType == POSITION_TYPE_SELL)

         {

          totalSell++;

          swapSell = (posComission+posSwap)/posLots*tickSize;

          double posPrice = (posOpen-swapSell)*posLots;

          lotsSell  += posLots;

          priceSell += posPrice;

         }

       }

     }

    ArrayInitialize(Breakeven_Buy, (lotsBuy > 0.0 ? priceBuy/lotsBuy : 0.0));

    ArrayInitialize(Breakeven_Sell, (lotsSell > 0.0 ? priceSell/lotsSell : 0.0));

   }

  else

    if(rates_total-prev_calculated > 0)

     {

      Breakeven_Buy[rates_total-1] = Breakeven_Buy[rates_total-2];

      Breakeven_Sell[rates_total-1] = Breakeven_Sell[rates_total-2];

      double price = 0.0;

     }

  return(rates_total);

 }/******************************************************************/



/****************************OnChartEvent****************************/

void OnChartEvent(const int id,         // 845=B8D8:0B>@ A>1KB8O

                  const long& lparam,   // ?0@0<5B@ A>1KB8O B8?0 long

                  const double& dparam, // ?0@0<5B@ A>1KB8O B8?0 double

                  const string& sparam  // ?0@0<5B@ A>1KB8O B8?0 string

                 )

 {

  if(id == CHARTEVENT_CHART_CHANGE)

   {

    plotShift();

    ChartRedraw();

   }

 }/******************************************************************/



/****************************plotShift*******************************/

void plotShift()

 {

  width_in_bars = (int)ChartGetInteger(0, CHART_WIDTH_IN_BARS);

  firstBar = (int)ChartGetInteger(0, CHART_FIRST_VISIBLE_BAR);

  PlotIndexSetInteger(0, PLOT_SHIFT, width_in_bars-firstBar);

  PlotIndexSetInteger(1, PLOT_SHIFT, width_in_bars-firstBar);

 }/******************************************************************/



/********************************************************************/

void OnDeinit(const int reason)

 {

  Comment("");

 }/******************************************************************/

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