New article: Common Errors in MQL4 Programs and How to Avoid Them - page 2

To add comments, please log in or register
Keith Watford
Moderator
14019
Keith Watford  
   double totprof=0;
   int    w,total=OrdersTotal();
//----
   for(w=0; w<total; w++)
     {
      if(!OrderSelect(w,SELECT_BY_POS)) continue;
      if(OrderType()==OP_BUY || OrderType()==OP_SELL) continue;
       if(OrderSymbol() == Symbol()) continue;
          totprof+=OrderProfit()+OrderCommission()+OrderSwap();
      }

Remove the highlighted code

Brunda
11
Brunda  

Thanks a lot Keith for your suggestion on my code.

I request please could you advise/guide/suggest on how to read profit part of indicator iExposure from any EA using iCustom() function. how to check for any errors when I am trying to read values from indicator with iCustom function.



when I am using below code to read values from indicator there were no errors. there is no out put as well

double pl=iCustom(NULL,0,"iExposureSymbol",Red,1,0); 

below is the indicator code:

Many thanks in Advance

//+------------------------------------------------------------------+
//|                                                    iExposureSymbol.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 1
#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]=Symbol();
   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);
  }
//+------------------------------------------------------------------+
Files:
Keith Watford
Moderator
14019
Keith Watford  

The indicator only uses 1 buffer, ExtMapBuffer[], and when I say use, it doesn't actually use it for anything.

just

//---- to avoid minimum==maximum
   ExtMapBuffer[Bars-1]=-1;

So an iCustom call is no use at all with the indicator as it is.


double pl=iCustom(NULL,0,"iExposureSymbol",Red,1,0); 

checks buffer 1, when as there is only 1 buffer it would have to be 0. It still would be no use though as explained above.

Please use the code button when posting code. I will edit it for you this time.

Brunda
11
Brunda  
Keith Watford:

The indicator only uses 1 buffer, ExtMapBuffer[], and when I say use, it doesn't actually use it for anything.

just

So an iCustom call is no use at all with the indicator as it is.


checks buffer 1, when as there is only 1 buffer it would have to be 0. It still would be no use though as explained above.

Please use the code button when posting code. I will edit it for you this time.

thanks for your advise Keith,

I have been trying to get the floating p/l from the attached EA. I tried many ways to get the currency wise floating pl  from this EA. In my all afforts, OrderProfit() is not getting sum of more than 2 orders.

please could you help/suggest/guide/advise on how to get floating pl from this attached EA. Different types of code which I tried so far is working from other EAs but not from this attached EA

Keith Watford
Moderator
14019
Keith Watford  
Brunda:

thanks for your advise Keith,

I have been trying to get the floating p/l from the attached EA. I tried many ways to get the currency wise floating pl  from this EA. In my all afforts, OrderProfit() is not getting sum of more than 2 orders.

please could you help/suggest/guide/advise on how to get floating pl from this attached EA. Different types of code which I tried so far is working from other EAs but not from this attached EA

Your code is an indicator and would need to use the EA's magic number to filter trades.

Brunda
11
Brunda  
Keith Watford:

The indicator only uses 1 buffer, ExtMapBuffer[], and when I say use, it doesn't actually use it for anything.

just

So an iCustom call is no use at all with the indicator as it is.


checks buffer 1, when as there is only 1 buffer it would have to be 0. It still would be no use though as explained above.

Please use the code button when posting code. I will edit it for you this time.

Hi,


//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property  strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red

double ExtMapBuffer[];   // Declaring data arrays
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

   SetIndexStyle(0,DRAW_LINE);     // Line style
   SetIndexBuffer(0,ExtMapBuffer); // Assigning an array to buffer 0

   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   
      ExtMapBuffer[0]=plcheck();
   

   return(0);

  }
//+------------------------------------------------------------------+

double plcheck()
  {
   double profit=0;

   for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==_Symbol)
           {
            profit+=OrderProfit()+OrderCommission()+OrderSwap();
           }
        }
     }
   return profit;
  }
//++++++++++++

I have written a small indicator script going through the documentation. please some one guide/advise/suggest/help me to modify this script to read floating profit/loss value from iCustom() function.

this indicator is displaying floating p/l on the chart

whroeder1
17942
whroeder1  

Your script "is displaying floating p/l on the chart." It is working. You don't modify it. It is done.

You write another indicator or EA to use iCustom to read the buffer.
          Detailed explanation of iCustom - MQL4 and MetaTrader 4 - MQL4 programming forum

Brunda
11
Brunda  
whroeder1:

Your script "is displaying floating p/l on the chart." It is working. You don't modify it. It is done.

You write another indicator or EA to use iCustom to read the buffer.
          Detailed explanation of iCustom - MQL4 and MetaTrader 4 - MQL4 programming forum

thanks for your reply whroeder1,

when I tried to access the p/l value from my ea using the below command it is displaying different results.

 double pl = 0;

 pl=iCustom(NULL,0,"profitind",0,0);

please advise;

thanks for extension of your help.

Brunda
11
Brunda  
Brunda:

thanks for your reply whroeder1,

when I tried to access the p/l value from my ea using the below command it is displaying different results.

 double pl = 0;

 pl=iCustom(NULL,0,"profitind",0,0);

please advise;

thanks for extension of your help.

Hi,

Now I am able to read floating pl from my ea as below. here I am facing strange problem. when sum consists of more than 2 orders, it is not reflecting in my EA variable (sum). please some body help on this

double sum=0;  sum=iCustom(_Symbol,0,"profitind","",0,0);

Brunda
11
Brunda  
Brunda:

Hi,

Now I am able to read floating pl from my ea as below. here I am facing strange problem. when sum consists of more than 2 orders, it is not reflecting in my EA variable (sum). please some body help on this

double sum=0;  sum=iCustom(_Symbol,0,"profitind","",0,0);

Please could any one help on providing the code to restart the EA from EA code with out restarting the MT4 terminal
12
To add comments, please log in or register