average_price_v_1_0

Author: Joca
average_price_v_1_0
Orders Execution
Checks for the total of open orders
0 Views
0 Downloads
0 Favorites
average_price_v_1_0
//+------------------------------------------------------------------+
//|                                           Average Price v1.0.mq4 |
//|                                        Joca - nc32007a@gmail.com |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Joca"
#property indicator_chart_window

extern color font_color=Navy;
extern int font_size=12;

int PipAdjust,NrOfDigits;
double point;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnInit()
  {
   ObjectDelete("Average_Price_Line_"+Symbol());
   ObjectDelete("Information_"+Symbol());

   NrOfDigits=Digits;

   if(NrOfDigits==5 || NrOfDigits==3) PipAdjust=10;
   else
      if(NrOfDigits==4 || NrOfDigits==2) PipAdjust=1;

   point=Point*PipAdjust;

   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void deinit()
  {
   ObjectDelete("Average_Price_Line_"+Symbol());
   ObjectDelete("Information_"+Symbol());
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int Total_Buy_Trades;
   double Total_Buy_Size;
   double Buy_Profit;

   int Total_Sell_Trades;
   double Total_Sell_Size;
   double Sell_Profit;

   int Net_Trades;
   double Net_Lots;
   double Net_Result;

   double Average_Price;
   double distance;
   double Pip_Value=MarketInfo(Symbol(),MODE_TICKVALUE)*PipAdjust;
   double Pip_Size=MarketInfo(Symbol(),MODE_TICKSIZE)*PipAdjust;

   for(int i=0;i<OrdersTotal();i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
        {
         if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
           {
            Total_Buy_Trades++;
            Total_Buy_Size+=OrderLots();
            Buy_Profit+=OrderProfit()+OrderSwap()+OrderCommission();
           }
         if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
           {
            Total_Sell_Trades++;
            Total_Sell_Size+=OrderLots();
            Sell_Profit+=OrderProfit()+OrderSwap()+OrderCommission();
           }
        }
     }

   Net_Trades=Total_Buy_Trades+Total_Sell_Trades;
   Net_Lots=Total_Buy_Size-Total_Sell_Size;
   Net_Result=Buy_Profit+Sell_Profit;

   ObjectDelete("Average_Price_Line_"+Symbol());
   ObjectDelete("Information_"+Symbol());

   if(Net_Trades>0)
     {
      if(Net_Lots!=0)
        {
         distance=(Net_Result/(MathAbs(Net_Lots)*MarketInfo(Symbol(),MODE_TICKVALUE)))*MarketInfo(Symbol(),MODE_TICKSIZE);
         Average_Price=Bid-distance;
        }
      else
        {
         distance=(Net_Result*Point)/MarketInfo(Symbol(),MODE_TICKVALUE);
         Average_Price=Bid-distance;
        }

      ObjectDelete("Average_Price_Line_"+Symbol());
      ObjectCreate("Average_Price_Line_"+Symbol(),OBJ_HLINE,0,0,Average_Price);
      ObjectSet("Average_Price_Line_"+Symbol(),OBJPROP_WIDTH,3);

      color cl=Blue;
      if(Net_Lots<0) cl=Red;
      if(Net_Lots==0) cl=White;

      ObjectSet("Average_Price_Line_"+Symbol(),OBJPROP_COLOR,cl);

      ObjectCreate("Information_"+Symbol(),OBJ_LABEL,0,0,0);

      int x,y;
      ChartTimePriceToXY(0,0,Time[0],Average_Price,x,y);

      ObjectSet("Information_"+Symbol(),OBJPROP_XDISTANCE,x+20);
      ObjectSet("Information_"+Symbol(),OBJPROP_YDISTANCE,y);
      ObjectSetText("Information_"+Symbol(),"Avrg= "+DoubleToStr(Average_Price,NrOfDigits)+"  "+DoubleToStr(distance/(point),1)+" pips ("+Net_Result+" "+AccountInfoString(ACCOUNT_CURRENCY)+")  Lots= "+Net_Lots+"  Orders= "+Net_Trades,font_size,"Arial",font_color);
     }

   return(0);
  }
//+------------------------------------------------------------------+

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