Calculation Net Price Indicator

Author: Copyright © 2019-2021, Vladimir Karputov
Price Data Components
Series array that contains tick volumes of each bar
0 Views
0 Downloads
0 Favorites
Calculation Net Price Indicator
ÿþ//+------------------------------------------------------------------+

//|                              Calculation Net Price Indicator.mq5 |

//|                         Copyright © 2019-2021, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2019-2021, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.004"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots 0

//---

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>

CPositionInfo  m_position;                   // trade position object

CTrade         m_trade;                      // trading object

CSymbolInfo    m_symbol;                     // symbol info object

//--- input parameters

input string   InpHlineNameBuy         = "Buy net price";   // Hline Buy

input string   InpHlineNameSell        = "Sell net price";  // Hline Sell

input string   InpHlineNameBreakEven   = "Breakeven price"; // Hline Breakeven

input uchar    InpOffset               = 40;                // Offset (number of spaces to the left)

//---

datetime m_last_signals=0;        // "0" -> D'1970.01.01 00:00';

string   m_offset="";

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

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

     {

      m_offset=m_offset+" ";

     }

//---

   HLineCreate(0,m_offset+InpHlineNameBuy,0,0.0,clrBlue);

   HLineCreate(0,m_offset+InpHlineNameSell,0,0.0,clrRed);

   HLineCreate(0,m_offset+InpHlineNameBreakEven,0,0.0,clrDeepPink);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   HLineDelete(0,m_offset+InpHlineNameBuy);

   HLineDelete(0,m_offset+InpHlineNameSell);

   HLineDelete(0,m_offset+InpHlineNameBreakEven);

  }

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

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

  {

//---

   CalculationNetPrice();

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

   return(rates_total);

  }

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

//| Create the horizontal line                                       |

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

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

                 const string          name="HLine",      // line name

                 const int             sub_window=0,      // subwindow index

                 double                price=0,           // line price

                 const color           clr=clrRed,        // line color

                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style

                 const int             width=1,           // line width

                 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

  {

//--- if the price is not set, set it at the current Bid price level

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- create a horizontal line

   if(!ObjectCreate(chart_ID,name,OBJ_HLINE,sub_window,0,price))

     {

      Print(__FUNCTION__,

            ": failed to create a horizontal line! Error code = ",GetLastError());

      return(false);

     }

//--- set line color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set line display style

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set line width

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

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

//---

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,name);

//--- successful execution

   return(true);

  }

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

//| Move horizontal line                                             |

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

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

               const string name="HLine", // line name

               double       price=0)      // line price

  {

//--- if the line price is not set, move it to the current Bid price level

   if(!price)

      price=0.0;

//--- reset the error value

   ResetLastError();

//--- move a horizontal line

   if(!ObjectMove(chart_ID,name,0,0,price))

     {

      Print(__FUNCTION__,

            ": failed to move the horizontal line! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Delete a horizontal line                                         |

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

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

                 const string name="HLine") // line name

  {

//--- reset the error value

   ResetLastError();

//--- delete a horizontal line

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

            ": failed to delete a horizontal line! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Checks if the specified filling mode is allowed                  |

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

bool IsFillingTypeAllowed(int fill_type)

  {

//--- Obtain the value of the property that describes allowed filling modes

   int filling=m_symbol.TradeFillFlags();

//--- Return true, if mode fill_type is allowed

   return((filling & fill_type)==fill_type);

  }

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

//| Calculation Net Price                                            |

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

void CalculationNetPrice()

  {

   datetime time_current=TimeCurrent();

   if(time_current-m_last_signals<3)

      return;

   m_last_signals=time_current;

   double total_price_multiply_volume_buy    = 0.0;

   double total_volume_buy                   = 0.0;

   double net_price_buy                      = 0.0;

   double total_price_multiply_volume_sell   = 0.0;

   double total_volume_sell                  = 0.0;

   double net_price_sell                     = 0.0;

   int    count_buys                         = 0;

   int    count_sells                        = 0;

   int total=PositionsTotal();

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

     {

      if(!m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

         break;

      if(m_position.Symbol()==Symbol())

        {

         if(m_position.PositionType()==POSITION_TYPE_BUY)

           {

            total_price_multiply_volume_buy+=m_position.PriceOpen()*m_position.Volume();

            total_volume_buy+=m_position.Volume();

            count_buys++;

           }

         else

           {

            total_price_multiply_volume_sell+=m_position.PriceOpen()*m_position.Volume();

            total_volume_sell+=m_position.Volume();

            count_sells++;

           }

        }

     }

//---

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

     {

      net_price_buy=total_price_multiply_volume_buy/total_volume_buy;

      HLineMove(0,m_offset+InpHlineNameBuy,net_price_buy);

      //Print(InpHlineNameBuy," -> ",DoubleToString(net_price_buy,Digits()));

     }

   else

      HLineMove(0,m_offset+InpHlineNameBuy);

   if(total_price_multiply_volume_sell!=0 && total_volume_sell!=0)

     {

      net_price_sell=total_price_multiply_volume_sell/total_volume_sell;

      HLineMove(0,m_offset+InpHlineNameSell,net_price_sell);

      //Print(InpHlineNameSell," -> ",DoubleToString(net_price_sell,Digits()));

     }

   else

      HLineMove(0,m_offset+InpHlineNameSell);

   if(total_volume_buy-total_volume_sell!=0 && (total_volume_buy!=0.0 && total_volume_sell!=0.0) && (count_buys>0 || count_sells>0))

     {

      double breakeven_price=(total_price_multiply_volume_buy-total_price_multiply_volume_sell)/

                             (total_volume_buy+total_volume_sell*-1);

      HLineMove(0,m_offset+InpHlineNameBreakEven,breakeven_price);

      //Print(InpHlineNameBreakEven," -> ",DoubleToString(breakeven_price,Digits()));

     }

   else

      HLineMove(0,m_offset+InpHlineNameBreakEven);

//---

   return;

  }

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

Comments