Using an EA to Close a Fruity-Basket of Pairs

To add comments, please log in or register
2171572
10
2171572  

Hi all,

I am trying to write an EA that will monitor my account and then close all orders based on AccountEquity()+Target$.

The idea is the EA loads and sets a variable that represents an equity target.  It also sets a variable to compare order dates to by using CheckDate = TimeCurrent().  Then on a timer it loops through closed orders checking that it only adds the profit for orders OrderOpenTime()>= CheckDate.  Then it adds that to the AccountEquity().  If the history + AccountEquity() > AccountEquity() + Target then close all orders.

The idea is this will be used for scalping/grid EAs that sometimes eat up the account.  The way to minimise the risk is to reach a profit number and then close all orders - lock that profit in.  I figure if I can get several of these scalping/grid EAs running together a separate EA that monitors the account can close them all which will average the risk out over several pairs.

Trouble is it doesn't work.  

For some reason the EA closes all the orders and AccountEquity() is lower than the target.  When there are only a few orders it looks fine but add more pairs and for some reason it just closes the orders.

Any assistance or pointers where to look is gratefully accepted.

#include <Controls/Button.mqh>
#property strict
#include <stderror.mqh>
#include <stdlib.mqh>

datetime CheckDate=0;//This will be used to check back for a bucket

extern int BucketProfitTarget=45; //Dollar amount at which to close all trades

int magic=200;

double HistoryTotal=0;
double OpenTotal=0;
double Banked=0;
double StartingBucketEquity=0;
double TargetBucketEquity=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   int myResult=ObjectCreate(0,"EA Loaded",OBJ_VLINE,0,TimeCurrent(),0);
   CheckDate=TimeCurrent();
   EventSetMillisecondTimer(500);

   StartingBucketEquity=AccountEquity();
   TargetBucketEquity=AccountEquity()+BucketProfitTarget;

   ObjectCreate("eName",OBJ_LABEL,0,0,0);
   ObjectSet("eName",OBJPROP_CORNER,10);
   ObjectSet("eName",OBJPROP_XDISTANCE,50);
   ObjectSet("eName",OBJPROP_YDISTANCE,50);
   ObjectSetText("eName","Total Profit Banked: $ ",20,"Arial",clrCadetBlue);

   ObjectCreate("eName1",OBJ_LABEL,0,0,0);
   ObjectSet("eName1",OBJPROP_CORNER,10);
   ObjectSet("eName1",OBJPROP_XDISTANCE,50);
   ObjectSet("eName1",OBJPROP_YDISTANCE,80);
   ObjectSetText("eName1","Accumulating Total $ ",20,"Arial",clrCadetBlue);

   ObjectCreate("eName2",OBJ_LABEL,0,0,0);
   ObjectSet("eName2",OBJPROP_CORNER,10);
   ObjectSet("eName2",OBJPROP_XDISTANCE,50);
   ObjectSet("eName2",OBJPROP_YDISTANCE,110);
   ObjectSetText("eName2","Open Order Total: $ ",20,"Arial",clrCadetBlue);

   ObjectCreate("eName3",OBJ_LABEL,0,0,0);
   ObjectSet("eName3",OBJPROP_CORNER,10);
   ObjectSet("eName3",OBJPROP_XDISTANCE,50);
   ObjectSet("eName3",OBJPROP_YDISTANCE,140);
   ObjectSetText("eName3","Equity When Bucket Opened: $ ",20,"Arial",clrCadetBlue);

   ObjectCreate("eName13",OBJ_LABEL,0,0,0);
   ObjectSet("eName13",OBJPROP_CORNER,10);
   ObjectSet("eName13",OBJPROP_XDISTANCE,50);
   ObjectSet("eName13",OBJPROP_YDISTANCE,170);
   ObjectSetText("eName13","Target Equity: $ ",20,"Arial",clrCadetBlue);

   ObjectCreate("eName23",OBJ_LABEL,0,0,0);
   ObjectSet("eName23",OBJPROP_CORNER,10);
   ObjectSet("eName23",OBJPROP_XDISTANCE,50);
   ObjectSet("eName23",OBJPROP_YDISTANCE,200);
   ObjectSetText("eName23","Amount To Go: $ ",20,"Arial",clrCadetBlue);

   ObjectCreate("eName5",OBJ_LABEL,0,0,0);
   ObjectSet("eName5",OBJPROP_CORNER,10);
   ObjectSet("eName5",OBJPROP_XDISTANCE,550);
   ObjectSet("eName5",OBJPROP_YDISTANCE,50);
   ObjectSetText("eName5",DoubleToStr(Banked,2),20,"Arial",clrCadetBlue);

   ObjectCreate("eName15",OBJ_LABEL,0,0,0);
   ObjectSet("eName15",OBJPROP_CORNER,10);
   ObjectSet("eName15",OBJPROP_XDISTANCE,550);
   ObjectSet("eName15",OBJPROP_YDISTANCE,80);
   ObjectSetText("eName15",DoubleToStr(HistoryTotal,2),20,"Arial",clrCadetBlue);

   ObjectCreate("eName25",OBJ_LABEL,0,0,0);
   ObjectSet("eName25",OBJPROP_CORNER,10);
   ObjectSet("eName25",OBJPROP_XDISTANCE,550);
   ObjectSet("eName25",OBJPROP_YDISTANCE,110);
   ObjectSetText("eName25",DoubleToStr(OpenTotal,2),20,"Arial",clrCadetBlue);

   ObjectCreate("eName35",OBJ_LABEL,0,0,0);
   ObjectSet("eName35",OBJPROP_CORNER,10);
   ObjectSet("eName35",OBJPROP_XDISTANCE,550);
   ObjectSet("eName35",OBJPROP_YDISTANCE,140);
   ObjectSetText("eName35",(string)StartingBucketEquity,20,"Arial",clrCadetBlue);

   ObjectCreate("eName135",OBJ_LABEL,0,0,0);
   ObjectSet("eName135",OBJPROP_CORNER,10);
   ObjectSet("eName135",OBJPROP_XDISTANCE,550);
   ObjectSet("eName135",OBJPROP_YDISTANCE,170);
   ObjectSetText("eName135",(string)TargetBucketEquity,20,"Arial",clrCadetBlue);

   ObjectCreate("eName235",OBJ_LABEL,0,0,0);
   ObjectSet("eName235",OBJPROP_CORNER,10);
   ObjectSet("eName235",OBJPROP_XDISTANCE,550);
   ObjectSet("eName235",OBJPROP_YDISTANCE,200);
   ObjectSetText("eName235",DoubleToStr((TargetBucketEquity-AccountEquity()),2),20,"Arial",clrCadetBlue);


   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert timer function                                             |
//+------------------------------------------------------------------+
void OnTimer()
  {
   CheckBucketProfit();
  }
//+------------------------------------------------------------------+
//|       Check profit of the whole bucket function                  |
//+------------------------------------------------------------------+
void CheckBucketProfit()
  {
//Totals are reset
   HistoryTotal=0;
   OpenTotal=0;

//find profit of all open orders
   int i=0;
   for(i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         OpenTotal=OpenTotal+OrderProfit();
        }
     }
//find profit of all closed orders since CheckDate
   i=0;
   for(i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         if(OrderOpenTime()>=CheckDate)
           {
            HistoryTotal=HistoryTotal+OrderProfit();
           }
     }
//Update Screen Totals Here
   ObjectSetText("eName5",DoubleToStr(Banked,2),20,"Arial",clrCadetBlue);
   ObjectSetText("eName15",DoubleToStr(HistoryTotal,2),20,"Arial",clrCadetBlue);
   ObjectSetText("eName25",DoubleToStr(OpenTotal,2),20,"Arial",clrCadetBlue);
   ObjectSetText("eName35",(string)StartingBucketEquity,20,"Arial",clrCadetBlue);
   ObjectSetText("eName135",(string)TargetBucketEquity,20,"Arial",clrCadetBlue);
   ObjectSetText("eName235",DoubleToStr((TargetBucketEquity-AccountEquity()),2),20,"Arial",clrCadetBlue);

   if((HistoryTotal+AccountEquity())>TargetBucketEquity)
     {
      CloseAll();
     }
  }
//+------------------------------------------------------------------+
//Close all orders
//+------------------------------------------------------------------+
void CloseAll()
  {
   Banked=Banked+(AccountEquity()-StartingBucketEquity);
   StartingBucketEquity=AccountEquity();
   TargetBucketEquity=AccountEquity()+BucketProfitTarget;
   
   int i=0,result=0;
   for(i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         result=OrderClose(OrderTicket(),OrderLots(),Ask,50,clrAntiqueWhite);
     }
   CheckDate=TimeCurrent(); //orders from now on go into a new, time-bound bucket
   string MyName="Equity Reset $"+(string)AccountEquity();
   result=ObjectCreate(0,MyName,OBJ_VLINE,0,CheckDate,0);
  }
//+------------------------------------------------------------------+
To add comments, please log in or register