Horizontal line on chart of iExposure average price

Mar 10, 2017
23
0
27
India
Hello Fellows. I hope everyone doing good.

Here I would like to have code for Horizontal line in Chart for iExposure average price values.
Here is the iExposure Code. Could anyone write code for me?




MQL4:
//+------------------------------------------------------------------+
//|                                                    iExposure.mq4 |
//|                   Copyright 2007-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "2007-2014, MetaQuotes Software Corp."
#property link      "http://www.mql4.com"
#property strict
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_minimum 0.0
#property indicator_maximum 0.1
 
#define SYMBOLS_MAX 1024
#define DEALS          0
#define BUY_LOTS       1
#define BUY_PRICE      2
#define SELL_LOTS      3
#define SELL_PRICE     4
#define NET_LOTS       5
#define PROFIT         6
 
input color InpColor=LightSeaGreen;  // Text color
 
string ExtName="Exposure";
string ExtSymbols[SYMBOLS_MAX];
int    ExtSymbolsTotal=0;
double ExtSymbolsSummaries[SYMBOLS_MAX][7];
int    ExtLines=-1;
string ExtCols[8]={"Symbol",
                   "Deals",
                   "Buy lots",
                   "Buy price",
                   "Sell lots",
                   "Sell price",
                   "Net lots",
                   "Profit"};
int    ExtShifts[8]={ 10, 80, 130, 180, 260, 310, 390, 460 };
int    ExtVertShift=14;
double ExtMapBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
    IndicatorShortName(ExtName);
   SetIndexBuffer(0,ExtMapBuffer);
   SetIndexStyle(0,DRAW_NONE);
   IndicatorDigits(0);
    SetIndexEmptyValue(0,0.0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   int windex=WindowFind(ExtName);
   if(windex>0)
      ObjectsDeleteAll(windex);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
   string name;
   int    i,col,line,windex=WindowFind(ExtName);
//----
   if(windex<0)
      return(rates_total);
//---- header line
   if(ExtLines<0)
     {
      for(col=0; col<8; col++)
        {
         name="Head_"+string(col);
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[col]);
            ObjectSet(name,OBJPROP_YDISTANCE,ExtVertShift);
            ObjectSetText(name,ExtCols[col],9,"Arial",InpColor);
           }
        }
      ExtLines=0;
     }
//----
   ArrayInitialize(ExtSymbolsSummaries,0.0);
   int total=Analyze();
   if(total>0)
     {
      line=0;
      for(i=0; i<ExtSymbolsTotal; i++)
        {
         if(ExtSymbolsSummaries[i][DEALS]<=0) continue;
         line++;
         //---- add line
         if(line>ExtLines)
           {
            int y_dist=ExtVertShift*(line+1)+1;
            for(col=0; col<8; col++)
              {
               name="Line_"+string(line)+"_"+string(col);
               if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
                 {
                  ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[col]);
                  ObjectSet(name,OBJPROP_YDISTANCE,y_dist);
                 }
              }
            ExtLines++;
           }
         //---- set line
         int    digits=(int)MarketInfo(ExtSymbols[i],MODE_DIGITS);
         double buy_lots=ExtSymbolsSummaries[i][BUY_LOTS];
         double sell_lots=ExtSymbolsSummaries[i][SELL_LOTS];
         double buy_price=0.0;
         double sell_price=0.0;
         if(buy_lots!=0)  buy_price=ExtSymbolsSummaries[i][BUY_PRICE]/buy_lots;
         if(sell_lots!=0) sell_price=ExtSymbolsSummaries[i][SELL_PRICE]/sell_lots;
         name="Line_"+string(line)+"_0";
         ObjectSetText(name,ExtSymbols[i],9,"Arial",InpColor);
         name="Line_"+string(line)+"_1";
         ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][DEALS],0),9,"Arial",InpColor);
         name="Line_"+string(line)+"_2";
         ObjectSetText(name,DoubleToStr(buy_lots,2),9,"Arial",InpColor);
         name="Line_"+string(line)+"_3";
         ObjectSetText(name,DoubleToStr(buy_price,digits),9,"Arial",InpColor);
         name="Line_"+string(line)+"_4";
         ObjectSetText(name,DoubleToStr(sell_lots,2),9,"Arial",InpColor);
         name="Line_"+string(line)+"_5";
         ObjectSetText(name,DoubleToStr(sell_price,digits),9,"Arial",InpColor);
         name="Line_"+string(line)+"_6";
         ObjectSetText(name,DoubleToStr(buy_lots-sell_lots,2),9,"Arial",InpColor);
         name="Line_"+string(line)+"_7";
         ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][PROFIT],2),9,"Arial",InpColor);
        }
     }
//---- remove lines
   if(total<ExtLines)
     {
      for(line=ExtLines; line>total; line--)
        {
         name="Line_"+string(line)+"_0";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_1";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_2";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_3";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_4";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_5";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_6";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_7";
         ObjectSetText(name,"");
        }
     }
//---- to avoid minimum==maximum
   ExtMapBuffer[Bars-1]=-1;
//----
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int Analyze()
  {
   double profit;
   int    i,index,type,total=OrdersTotal();
//----
   for(i=0; i<total; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS)) continue;
      type=OrderType();
      if(type!=OP_BUY && type!=OP_SELL) continue;
      index=SymbolsIndex(OrderSymbol());
      if(index<0 || index>=SYMBOLS_MAX) continue;
      //----
      ExtSymbolsSummaries[index][DEALS]++;
      profit=OrderProfit()+OrderCommission()+OrderSwap();
      ExtSymbolsSummaries[index][PROFIT]+=profit;
      if(type==OP_BUY)
        {
         ExtSymbolsSummaries[index][BUY_LOTS]+=OrderLots();
         ExtSymbolsSummaries[index][BUY_PRICE]+=OrderOpenPrice()*OrderLots();
        }
      else
        {
         ExtSymbolsSummaries[index][SELL_LOTS]+=OrderLots();
         ExtSymbolsSummaries[index][SELL_PRICE]+=OrderOpenPrice()*OrderLots();
        }
     }
//----
   total=0;
   for(i=0; i<ExtSymbolsTotal; i++)
     {
      if(ExtSymbolsSummaries[i][DEALS]>0) total++;
     }
//----
   return(total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int SymbolsIndex(string SymbolName)
  {
   bool found=false;
   int  i;
//----
   for(i=0; i<ExtSymbolsTotal; i++)
     {
      if(SymbolName==ExtSymbols[i])
        {
         found=true;
         break;
        }
     }
//----
   if(found)
      return(i);
   if(ExtSymbolsTotal>=SYMBOLS_MAX)
      return(-1);
//----
   i=ExtSymbolsTotal;
   ExtSymbolsTotal++;
   ExtSymbols[i]=SymbolName;
   ExtSymbolsSummaries[i][DEALS]=0;
   ExtSymbolsSummaries[i][BUY_LOTS]=0;
   ExtSymbolsSummaries[i][BUY_PRICE]=0;
   ExtSymbolsSummaries[i][SELL_LOTS]=0;
   ExtSymbolsSummaries[i][SELL_PRICE]=0;
   ExtSymbolsSummaries[i][NET_LOTS]=0;
   ExtSymbolsSummaries[i][PROFIT]=0;
//----
   return(i);
  }
//+------------------------------------------------------------------+
Thanks with Regards
 

hayseed

Master Trader
Jul 27, 2010
458
127
109
usa
hayseedfx.com
Dear hayseed when i having buy and selling same time your code doesn't show the same as i exposure shows (average buy price HLine and average sell price HLine.
//-----
hey ufc..... your probably right..... we can have opposing trades here in the states so i tend to forget the possibility of it in other lands..... i'll look at the code and get back with you.....h
 
Mar 10, 2017
23
0
27
India
Dear @Enivid I've coded my self. it works ok when i load the indicator 1st time. but if i'm taking another order or moving the Horizontal line accidently it's not recalculating and going to the right place.
My modification
MQL4:
//+------------------------------------------------------------------+
//|                                                    iExposure.mq4 |
//|                   Copyright 2007-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "2007-2014, MetaQuotes Software Corp."
#property link      "http://www.mql4.com"
#property strict
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_minimum 0.0
#property indicator_maximum 0.1
 
#define SYMBOLS_MAX 1024
#define DEALS          0
#define BUY_LOTS       1
#define BUY_PRICE      2
#define SELL_LOTS      3
#define SELL_PRICE     4
#define NET_LOTS       5
#define PROFIT         6
 
input color InpColor=LightSeaGreen;  // Text color
 
string ExtName="Exposure";
string ExtSymbols[SYMBOLS_MAX];
int    ExtSymbolsTotal=0;
double ExtSymbolsSummaries[SYMBOLS_MAX][7];
int    ExtLines=-1;
string ExtCols[8]={"Symbol",
                   "Deals",
                   "Buy lots",
                   "Buy price",
                   "Sell lots",
                   "Sell price",
                   "Net lots",
                   "Profit"};
int    ExtShifts[8]={ 10, 80, 130, 180, 260, 310, 390, 460 };
int    ExtVertShift=14;
double ExtMapBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
    IndicatorShortName(ExtName);
   SetIndexBuffer(0,ExtMapBuffer);
   SetIndexStyle(0,DRAW_NONE);
   IndicatorDigits(0);
    SetIndexEmptyValue(0,0.0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
      ObjectDelete("Buy Average Price");
      ObjectDelete("Sell Average Price");
   int windex=WindowFind(ExtName);
   if(windex>0)
      ObjectsDeleteAll(windex);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
   string name;
   int    i,col,line,windex=WindowFind(ExtName);
//----
   if(windex<0)
      return(rates_total);
//---- header line
   if(ExtLines<0)
     {
      for(col=0; col<8; col++)
        {
         name="Head_"+string(col);
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[col]);
            ObjectSet(name,OBJPROP_YDISTANCE,ExtVertShift);
            ObjectSetText(name,ExtCols[col],9,"Arial",InpColor);
           }
        }
      ExtLines=0;
     }
//----
   ArrayInitialize(ExtSymbolsSummaries,0.0);
   int total=Analyze();
   if(total>0)
     {
      line=0;
      for(i=0; i<ExtSymbolsTotal; i++)
        {
         if(ExtSymbolsSummaries[i][DEALS]<=0) continue;
         line++;
         //---- add line
         if(line>ExtLines)
           {
            int y_dist=ExtVertShift*(line+1)+1;
            for(col=0; col<8; col++)
              {
               name="Line_"+string(line)+"_"+string(col);
               if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
                 {
                  ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[col]);
                  ObjectSet(name,OBJPROP_YDISTANCE,y_dist);
                 }
              }
            ExtLines++;
           }
         //---- set line
         double buyHLne,sellHLne;
         int    digits=(int)MarketInfo(ExtSymbols[i],MODE_DIGITS);
         double buy_lots=ExtSymbolsSummaries[i][BUY_LOTS];
         double sell_lots=ExtSymbolsSummaries[i][SELL_LOTS];
         double buy_price=0.0;
         double sell_price=0.0;
         if(buy_lots!=0)  buy_price=ExtSymbolsSummaries[i][BUY_PRICE]/buy_lots;
         if(sell_lots!=0) sell_price=ExtSymbolsSummaries[i][SELL_PRICE]/sell_lots;
         name="Line_"+string(line)+"_0";
         ObjectSetText(name,ExtSymbols[i],9,"Arial",InpColor);
         name="Line_"+string(line)+"_1";
         ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][DEALS],0),9,"Arial",InpColor);
         name="Line_"+string(line)+"_2";
         ObjectSetText(name,DoubleToStr(buy_lots,2),9,"Arial",InpColor);
 
         buyHLne=buy_price;
      if(buyHLne>0)
        {
         ObjectCreate("Buy Average Price", OBJ_HLINE, 0, Time[0], buyHLne);
         ObjectSet("Average Buy Price", OBJPROP_STYLE, STYLE_SOLID);
         ObjectSet("Average Buy Price", OBJPROP_COLOR, Magenta);
         ObjectSet("Average Buy Price", OBJPROP_WIDTH, 1);
        }
         name="Line_"+string(line)+"_3";
         ObjectSetText(name,DoubleToStr(buy_price,digits),9,"Arial",InpColor);
         name="Line_"+string(line)+"_4";
         ObjectSetText(name,DoubleToStr(sell_lots,2),9,"Arial",InpColor);
         sellHLne=sell_price;
      if (sellHLne > 0)
      {
         ObjectCreate("Average Sell Price", OBJ_HLINE, 0, Time[0], sellHLne);
         ObjectSet("Average Sell Price", OBJPROP_STYLE, STYLE_SOLID);
         ObjectSet("Average Sell Price", OBJPROP_COLOR, Magenta);
         ObjectSet("Average Sell Price", OBJPROP_WIDTH, 1);
      }
         name="Line_"+string(line)+"_5";
         ObjectSetText(name,DoubleToStr(sell_price,digits),9,"Arial",InpColor);
         name="Line_"+string(line)+"_6";
         ObjectSetText(name,DoubleToStr(buy_lots-sell_lots,2),9,"Arial",InpColor);
         name="Line_"+string(line)+"_7";
         ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][PROFIT],2),9,"Arial",InpColor);
        }
     }
//---- remove lines
   if(total<ExtLines)
     {
      for(line=ExtLines; line>total; line--)
        {
         name="Line_"+string(line)+"_0";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_1";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_2";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_3";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_4";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_5";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_6";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_7";
         ObjectSetText(name,"");
        }
     }
//---- to avoid minimum==maximum
   ExtMapBuffer[Bars-1]=-1;
//----
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int Analyze()
  {
   double profit;
   int    i,index,type,total=OrdersTotal();
//----
   for(i=0; i<total; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS)) continue;
      type=OrderType();
      if(type!=OP_BUY && type!=OP_SELL) continue;
      index=SymbolsIndex(OrderSymbol());
      if(index<0 || index>=SYMBOLS_MAX) continue;
      //----
      ExtSymbolsSummaries[index][DEALS]++;
      profit=OrderProfit()+OrderCommission()+OrderSwap();
      ExtSymbolsSummaries[index][PROFIT]+=profit;
      if(type==OP_BUY)
        {
         ExtSymbolsSummaries[index][BUY_LOTS]+=OrderLots();
         ExtSymbolsSummaries[index][BUY_PRICE]+=OrderOpenPrice()*OrderLots();
        }
      else
        {
         ExtSymbolsSummaries[index][SELL_LOTS]+=OrderLots();
         ExtSymbolsSummaries[index][SELL_PRICE]+=OrderOpenPrice()*OrderLots();
        }
     }
//----
   total=0;
   for(i=0; i<ExtSymbolsTotal; i++)
     {
      if(ExtSymbolsSummaries[i][DEALS]>0) total++;
     }
//----
   return(total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int SymbolsIndex(string SymbolName)
  {
   bool found=false;
   int  i;
//----
   for(i=0; i<ExtSymbolsTotal; i++)
     {
      if(SymbolName==ExtSymbols[i])
        {
         found=true;
         break;
        }
     }
//----
   if(found)
      return(i);
   if(ExtSymbolsTotal>=SYMBOLS_MAX)
      return(-1);
//----
   i=ExtSymbolsTotal;
   ExtSymbolsTotal++;
   ExtSymbols[i]=SymbolName;
   ExtSymbolsSummaries[i][DEALS]=0;
   ExtSymbolsSummaries[i][BUY_LOTS]=0;
   ExtSymbolsSummaries[i][BUY_PRICE]=0;
   ExtSymbolsSummaries[i][SELL_LOTS]=0;
   ExtSymbolsSummaries[i][SELL_PRICE]=0;
   ExtSymbolsSummaries[i][NET_LOTS]=0;
   ExtSymbolsSummaries[i][PROFIT]=0;
//----
   return(i);
  }
//+------------------------------------------------------------------+
Kind Regards
 
Last edited:

hayseed

Master Trader
Jul 27, 2010
458
127
109
usa
hayseedfx.com
Dear hayseed when i having buy and selling same time your code doesn't show the same as i exposure shows (average buy price HLine and average sell price HLine.
//----

hey ufc..... try this and see how it works..... we can not have opposing trades here so i can not test it..... let me know if you don't mind..... thanks.....h

//-----
 

Attachments

Mar 10, 2017
23
0
27
India
@hayseed I've added some code to make sound alert but it shows error. could you please correct it?
MQL4:
//+------------------------------------------------------------------+
//|                                                    iExposure.mq4 | ufc
//|                   Copyright 2007-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "2007-2014, MetaQuotes Software Corp."
#property link      "http://www.mql4.com"
#property strict
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_minimum 0.0
#property indicator_maximum 0.1
 
#define SYMBOLS_MAX 1024
#define DEALS          0
#define BUY_LOTS       1
#define BUY_PRICE      2
#define SELL_LOTS      3
#define SELL_PRICE     4
#define NET_LOTS       5
#define PROFIT         6
 
input color InpColor=LightSeaGreen;  // Text color
extern bool ShowAveragePrice  = true;
 
string ExtName="Exposure";
string ExtSymbols[SYMBOLS_MAX];
int    ExtSymbolsTotal=0;
double ExtSymbolsSummaries[SYMBOLS_MAX][7];
int    ExtLines=-1;
string ExtCols[8]={"Symbol",
                   "Deals",
                   "Buy lots",
                   "Buy price",
                   "Sell lots",
                   "Sell price",
                   "Net lots",
                   "Profit"};
int    ExtShifts[8]={ 10, 80, 130, 180, 260, 310, 390, 460 };
int    ExtVertShift=14;
double ExtMapBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
    IndicatorShortName(ExtName);
   SetIndexBuffer(0,ExtMapBuffer);
   SetIndexStyle(0,DRAW_NONE);
   IndicatorDigits(0);
    SetIndexEmptyValue(0,0.0);
   EventSetTimer(2);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   int windex=WindowFind(ExtName);
   if(windex>0)
      ObjectsDeleteAll(windex);
      ObjectDelete("AverageBuyPrice");
      ObjectDelete("AverageSellPrice");
      EventKillTimer();
  }
//+------------------------------------------------------------------+
//| 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[])
  {
   string name;
   int    i,col,line,windex=WindowFind(ExtName);
//----
   if(windex<0)
      return(rates_total);
//---- header line
   if(ExtLines<0)
     {
      for(col=0; col<8; col++)
        {
         name="Head_"+string(col);
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[col]);
            ObjectSet(name,OBJPROP_YDISTANCE,ExtVertShift);
            ObjectSetText(name,ExtCols[col],9,"Arial",InpColor);
           }
        }
      ExtLines=0;
     }
//----
   ArrayInitialize(ExtSymbolsSummaries,0.0);
   int total=Analyze();
   if(total>0)
     {
      line=0;
      for(i=0; i<ExtSymbolsTotal; i++)
        {
         if(ExtSymbolsSummaries[i][DEALS]<=0) continue;
         line++;
         //---- add line
         if(line>ExtLines)
           {
            int y_dist=ExtVertShift*(line+1)+1;
            for(col=0; col<8; col++)
              {
               name="Line_"+string(line)+"_"+string(col);
               if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
                 {
                  ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[col]);
                  ObjectSet(name,OBJPROP_YDISTANCE,y_dist);
                 }
              }
            ExtLines++;
           }
         //---- set line
         int    digits=(int)MarketInfo(ExtSymbols[i],MODE_DIGITS);
         double buy_lots=ExtSymbolsSummaries[i][BUY_LOTS];
         double sell_lots=ExtSymbolsSummaries[i][SELL_LOTS];
         double buy_price=0.0;
         double sell_price=0.0;
         if(buy_lots!=0)  buy_price=ExtSymbolsSummaries[i][BUY_PRICE]/buy_lots;
         if(sell_lots!=0) sell_price=ExtSymbolsSummaries[i][SELL_PRICE]/sell_lots;
         name="Line_"+string(line)+"_0";
         ObjectSetText(name,ExtSymbols[i],9,"Arial",InpColor);
         name="Line_"+string(line)+"_1";
         ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][DEALS],0),9,"Arial",InpColor);
         name="Line_"+string(line)+"_2";
         ObjectSetText(name,DoubleToStr(buy_lots,2),9,"Arial",InpColor);
         name="Line_"+string(line)+"_3";
         ObjectSetText(name,DoubleToStr(buy_price,digits),9,"Arial",InpColor);
         name="Line_"+string(line)+"_4";
         ObjectSetText(name,DoubleToStr(sell_lots,2),9,"Arial",InpColor);
         name="Line_"+string(line)+"_5";
         ObjectSetText(name,DoubleToStr(sell_price,digits),9,"Arial",InpColor);
         name="Line_"+string(line)+"_6";
         ObjectSetText(name,DoubleToStr(buy_lots-sell_lots,2),9,"Arial",InpColor);
         name="Line_"+string(line)+"_7";
         ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][PROFIT],2),9,"Arial",InpColor);
        }
     }
//---- remove lines
   if(total<ExtLines)
     {
      for(line=ExtLines; line>total; line--)
        {
         name="Line_"+string(line)+"_0";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_1";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_2";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_3";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_4";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_5";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_6";
         ObjectSetText(name,"");
         name="Line_"+string(line)+"_7";
         ObjectSetText(name,"");
        }
     }
//---- to avoid minimum==maximum
   ExtMapBuffer[Bars-1]=-1;
//----
if(ShowAveragePrice) {show();}
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int Analyze()
  {
   double profit;
   int    i,index,type,total=OrdersTotal();
//----
   for(i=0; i<total; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS)) continue;
      type=OrderType();
      if(type!=OP_BUY && type!=OP_SELL) continue;
      index=SymbolsIndex(OrderSymbol());
      if(index<0 || index>=SYMBOLS_MAX) continue;
      //----
      ExtSymbolsSummaries[index][DEALS]++;
      profit=OrderProfit()+OrderCommission()+OrderSwap();
      ExtSymbolsSummaries[index][PROFIT]+=profit;
      if(type==OP_BUY)
        {
         ExtSymbolsSummaries[index][BUY_LOTS]+=OrderLots();
         ExtSymbolsSummaries[index][BUY_PRICE]+=OrderOpenPrice()*OrderLots();
        }
      else
        {
         ExtSymbolsSummaries[index][SELL_LOTS]+=OrderLots();
         ExtSymbolsSummaries[index][SELL_PRICE]+=OrderOpenPrice()*OrderLots();
        }
     }
//----
   total=0;
   for(i=0; i<ExtSymbolsTotal; i++)
     {
      if(ExtSymbolsSummaries[i][DEALS]>0) total++;
     }
//----
   return(total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int SymbolsIndex(string SymbolName)
  {
   bool found=false;
   int  i;
//----
   for(i=0; i<ExtSymbolsTotal; i++)
     {
      if(SymbolName==ExtSymbols[i])
        {
         found=true;
         break;
        }
     }
//----
   if(found)
      return(i);
   if(ExtSymbolsTotal>=SYMBOLS_MAX)
      return(-1);
//----
   i=ExtSymbolsTotal;
   ExtSymbolsTotal++;
   ExtSymbols[i]=SymbolName;
   ExtSymbolsSummaries[i][DEALS]=0;
   ExtSymbolsSummaries[i][BUY_LOTS]=0;
   ExtSymbolsSummaries[i][BUY_PRICE]=0;
   ExtSymbolsSummaries[i][SELL_LOTS]=0;
   ExtSymbolsSummaries[i][SELL_PRICE]=0;
   ExtSymbolsSummaries[i][NET_LOTS]=0;
   ExtSymbolsSummaries[i][PROFIT]=0;
//----
   return(i);
  }
//+------------------------------------------------------------------+
 
 
 
void show()
  {
//----
    if(ShowAveragePrice)
   {
    double p = NormalizeDouble(ObjectGet("AverageBuyPrice",1),Digits);
    double q = NormalizeDouble(AveragePrice("buys"),Digits);
     if(p!=q) {ObjectDelete("AverageBuyPrice");}
   ObjectCreate("AverageBuyPrice", OBJ_TREND, 0, Time[6], AveragePrice("buys"), Time[0]+4*(60*60*60), AveragePrice("buys"));
   ObjectSet("AverageBuyPrice", OBJPROP_COLOR, Lime);
   ObjectSet("AverageBuyPrice", OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet("AverageBuyPrice", OBJPROP_WIDTH,2);
   ObjectSet("AverageBuyPrice", OBJPROP_RAY,false);
   ObjectSet("AverageBuyPrice",OBJPROP_BACK,true);
 
 
 
    double pp = NormalizeDouble(ObjectGet("AverageSellPrice",1),Digits);
    double qq = NormalizeDouble(AveragePrice("sells"),Digits);
     if(pp!=qq) {ObjectDelete("AverageSellPrice");}
   ObjectCreate("AverageSellPrice", OBJ_TREND, 0, Time[6], AveragePrice("sells"), Time[0]+4*(60*60*60), AveragePrice("sells"));
   ObjectSet("AverageSellPrice", OBJPROP_COLOR, Red);
   ObjectSet("AverageSellPrice", OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet("AverageSellPrice", OBJPROP_WIDTH,2);
   ObjectSet("AverageSellPrice", OBJPROP_RAY,false);
   ObjectSet("AverageSellPrice",OBJPROP_BACK,true);
   }
 
//----
 
  }
 
 
//----
//----
 
 
   double AveragePrice(string type)
   {
   ///int    total         = CountOrders(OP_BUY) + CountOrders(OP_SELL);
   double buyaverage       = 0;
   double sellaverage      = 0;
   double buycount         = 0;
   double sellcount        = 0;
   double average          = 0;
 
   for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
   {
    if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
    if(OrderSymbol()!=Symbol())  continue;
    if(OrderType() > OP_SELL)    continue;
 
      if(OrderType() == OP_BUY)
      {
       buyaverage  = buyaverage +OrderOpenPrice()*OrderLots();
       buycount    = buycount + OrderLots();
      }
 
      if(OrderType() == OP_SELL)
      {
       sellaverage = sellaverage + OrderOpenPrice()*OrderLots();
       sellcount   = sellcount + OrderLots();
      } 
 
    }
 
   if(buycount > 0)
    {buyaverage = NormalizeDouble(buyaverage/buycount, Digits);}
 
   if(sellcount > 0)
    {sellaverage = NormalizeDouble(sellaverage/sellcount, Digits);}
 
    if(type == "buys") return(buyaverage);
                       return(sellaverage);
    }
 
void OnTimer()
{
   if ((Bid > buyaverage) && (buyaverage > 0))
   {
      PlaySound("alert.wav");
   }
   if ((Bid < sellaverage) && (sellaverage > 0))
   {
      PlaySound("alert.wav");
   }
}
 
Last edited:

hayseed

Master Trader
Jul 27, 2010
458
127
109
usa
hayseedfx.com
@hayseed I've added some code to make sound alert but it shows error. could you please correct it?
//-----

hey ufc....... that's a very good idea...... actually never thought of doing that myself.....

might be better to delete your function and put the alert code up top.... sometimes when using playsounds in a function it will continue to alert even after you have deleted the indicator from the chart..... and sometimes it will continue to alert even after you have closed the chart itself.....

by using the standard alert function you can add text .... this is useful if you are watching several pairs..... the text can let you know which symbol....

add the checkalerts bool to the inputs.....h
//----

MQL4:
input color InpColor=LightSeaGreen;  // Text color
extern bool ShowAveragePrice  = true;
extern bool checkalerts       = true;
MQL4:
//----
if(ShowAveragePrice) {show();}
 
//------
   if(checkalerts)
   {
   if ((Bid > AveragePrice("buys")) && (AveragePrice("buys") > 0))
   {
      Alert(Symbol()+ " Bid > Average buy price");
   }
   if ((Bid < AveragePrice("sells")) && (AveragePrice("sells") > 0))
   {
      Alert(Symbol()+ " Bid < Average sell price");
   }
 
   }
 
  }
 
Mar 10, 2017
23
0
27
India
Hello hayseed. Thanks for spending your time for my doubts. I could not get success with this. I would like to have an alert window once after that sound alert for every 2 seconds. if i add normal sound alert it'll make sound on every tick which the sound is not good (it'll bell like ting,ting,titititnnng,tinting). so making alert after every 2 seconds will be good. that's why i added the
MQL4:
Kind Regards
 

hayseed

Master Trader
Jul 27, 2010
458
127
109
usa
hayseedfx.com
I would like to have an alert window once after that sound alert for every 2 seconds. if i add normal sound alert it'll make sound on every tick which the sound is not good
//-----

het ufc..... in that case your function would be the far best route..... might want to add another input, seconds, so you can adjust the frequency.....h
//-----

MQL4:
input color InpColor=LightSeaGreen;  // Text color
extern bool ShowAveragePrice  =  true;
extern int  seconds           =    2;
//----
MQL4:
EventSetTimer(seconds);  // add the input up top for seconds to allow adjustment
//----
MQL4:
void OnTimer()
{
   if ((Bid > AveragePrice("buys")) && (AveragePrice("buys") > 0))
   {
          Alert(Symbol()+ " Bid > Average buy price");     // PlaySound("alert.wav"); //
   }
   if ((Bid < AveragePrice("sells")) && (AveragePrice("sells") > 0))
   {
          Alert(Symbol()+ " Bid < Average buy price");   // PlaySound("alert.wav"); //
   }
}