Final EA modified





//+--------------------------------------------------+
//|Final EA
//+--------------------------------------------------+


#property link      " "
#include  <stdlib.mqh>
#define   NL    "\n"

// Regular Variables
extern double Lots          =  0.01;      
extern double Increment     =  0.01;
extern int    BasketLoss=2000;        // if equity reaches this negative level, close trades
extern int    Orders        =  1;    
extern int    PipTarget     =  20;
extern int    Step          =  1;
extern int    Spacing       =  4;
extern double CloseDelay    =  5;
extern bool   CloseBuys     =  false;  
extern bool   CloseSells    =  false;  
extern bool   CloseBuysInProfit     =  false;  
extern bool   CloseSellsInProfit    =  false;  
extern bool   EndTrading    =  false;  
extern bool   Boost         =  true;
extern bool TimeFilter=false; //time filter
extern int StartHour=8;
extern int EndHour=21;
extern bool CloseFriday=false; //the orders are closed before the week end
extern double CloseFridayHour=22; //friday end hour
extern bool OpenMonday=false; //the orders are open only after this time
extern double OpenMondayHour=2; //friday end hour

// ixternal settings
int      Slippage      = 0;                   
int      MagicNumber   = 12345;     
string   TradeComment  = "Final EA";   
datetime bartime       = 0;                    
bool     TradeAllowed  = true;             
double   BoostTrack    = 0;        
double minEquity;        

//+-------------+
//| Custom init |
//|-------------+
int init()
  {

  }

//+----------------+
//| Custom DE-init |
//+----------------+

int deinit()
  {

  }
    
//+------------------------------------------------------------------------+
//| cancels all pending orders and closes open positions                   |
//+------------------------------------------------------------------------+
void CloseAllThisSymbolBuy()
{
  double LastBuyTime;

   for(int i = OrdersTotal()-1; i >=0; i--)
       {
       OrderSelect(i, SELECT_BY_POS);
       bool result = false;
       if (OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY)  
         {
           if (OrderOpenTime()>LastBuyTime) LastBuyTime=OrderOpenTime();
           if (TimeCurrent()-LastBuyTime >= CloseDelay) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, Red );
         }
       }
     
  return; 
}
   
//+-----------------------------------------------------+
//| Closes all Sells this symbol only                   
//+-----------------------------------------------------+
void CloseAllThisSymbolSell()
{
  double LastSellTime;

   for(int i = OrdersTotal()-1; i >=0; i--)
      {
      OrderSelect(i, SELECT_BY_POS);
      bool result = false;
      if (OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL) 
       {
         if (OrderOpenTime()>LastSellTime) LastSellTime=OrderOpenTime();
         if (TimeCurrent()-LastSellTime >= CloseDelay) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 0, Red );
       }
     }
 
  return; 
}

//+----------------------------------------------+
//| Closes all Buys in Profit                   
//+----------------------------------------------+
void CloseBuysinProfit()
{
  double OrdersBUY, BuyProfit, LastBuyTime;
 
   for(int i = OrdersTotal()-1; i >=0; i--)
       {
       OrderSelect(i, SELECT_BY_POS);
       bool result = false;
       if (OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY)  
         {
           BuyProfit = OrderProfit() + OrderSwap() + OrderCommission();
           if (OrderOpenTime()>LastBuyTime) LastBuyTime=OrderOpenTime();
           if (TimeCurrent()-LastBuyTime >= CloseDelay && (OrderProfit()+OrderSwap()+OrderCommission())>0)  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
         }
       }
  return; 
}
   
//+-----------------------------------------------+
//| Closes all Sells in Profit                   
//+-----------------------------------------------+
void CloseSellsinProfit()
{
  double OrdersSELL, SellProfit, LastSellTime;
 
   for(int i = OrdersTotal()-1; i >=0; i--)
      {
      OrderSelect(i, SELECT_BY_POS);
      bool result = false;
      if (OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL)  
       {
         SellProfit = OrderProfit() + OrderSwap() + OrderCommission();
         if (OrderOpenTime()>LastSellTime) LastSellTime=OrderOpenTime();
         if (TimeCurrent()-LastSellTime >= CloseDelay && (OrderProfit()+OrderSwap()+OrderCommission())>0) result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red);
       }
     }
 
  return; 
}
      

//+------------------------------------------------------------------+
//| Check for BUY order conditions                                   |
//+------------------------------------------------------------------+
void CheckForBuy()
  {
   double   BuyOrders, LowestBuy=1000;
   int      cnt=0;
   int      gle=0;
   
   if(bartime!=Time[0]) 
     {
      bartime=Time[0];
      BoostTrack=0;
      TradeAllowed=true;
     }
     
   for(cnt=OrdersTotal();cnt>=0;cnt--)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if( OrderSymbol()==Symbol() && OrderType() == OP_BUY && OrderMagicNumber()==MagicNumber)
        {
            if (OrderOpenPrice()<LowestBuy) LowestBuy=OrderOpenPrice();
            BuyOrders++;
        }
     }
     
   if(TradeAllowed && Ask < LowestBuy-(Spacing*Point))
     {
        for(int i=1;i<Orders+1;i++) {
        OrderSend(Symbol(),OP_BUY,Lots+(BuyOrders*Increment),Ask,Slippage,0,0,TradeComment,MagicNumber,Blue);
           }
         gle=GetLastError();
         if(gle==0)
           {
            BoostTrack=Close[0];
            TradeAllowed=false;
           }
       }
   }
    
//+------------------------------------------------------------------+
//| Check for SELL order conditions                                   |
//+------------------------------------------------------------------+
void CheckForSell()
  {
   double   SellOrders, HighestSell;
   int      cnt=0;
   int      gle=0;
      
   if(bartime!=Time[0]) 
     {
      bartime=Time[0];
      BoostTrack=0;
      TradeAllowed=true;
     }
     
   for(cnt=OrdersTotal();cnt>=0;cnt--)
     {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if( OrderSymbol()==Symbol() && OrderType() == OP_SELL && OrderMagicNumber()==MagicNumber)
        {
            if (OrderOpenPrice()>HighestSell) HighestSell=OrderOpenPrice();
            SellOrders++;
        }
     } 
    
   if(TradeAllowed && Bid > HighestSell+(Spacing*Point))
     {
        for(int i=1;i<Orders+1;i++) {
        OrderSend(Symbol(),OP_SELL,Lots+(SellOrders*Increment),Bid,Slippage,0,0,TradeComment,MagicNumber,Red);
           }
         gle=GetLastError();
         if(gle==0)
           {
           BoostTrack=Close[0];
           TradeAllowed=false;
           }
       }
   }

//+-----------+
//| Main      |
//+-----------+
int start()
  {
  
/*   if (Period() == 1)
      {
         Alert("5 Minute Chart or higher...");
         return;
      }
*/   
// Regular count
 
   double CurrentBasket=0;
   
   CurrentBasket=AccountEquity()-AccountBalance();
   if(CurrentBasket<minEquity) minEquity=CurrentBasket;
   
   double   i, Profit, OrdersSELL, OrdersBUY, BuyPips, SellPips;
   double   HighestSell, LowestBuy=1000, BuyLots, SellLots, BuyProfit, SellProfit, BuyProfitTarget, SellProfitTarget;
   double   LowestBuyTicket, LowestBuyProfit, HighestSellTicket, HighestSellProfit;
   double   CurrentTime = (TimeHour(CurTime()+TimeMinute(CurTime())));
   bool     SELLme=false;
   bool     BUYme=false;
  
   string CLFR="false";
   string OPMO="false";
   string TIFI="false";
   if(CloseFriday==true&&DayOfWeek()==5&&Hour()>=CloseFridayHour){CLFR="true";}
   if(OpenMonday==true&&((DayOfWeek()==7)||(DayOfWeek()==1&&Hour()<=OpenMondayHour))){OPMO="true";}
   if(TimeFilter){if(!(Hour()>=StartHour && Hour()<=EndHour)){TIFI="true";}}

   string CUBA="false";
   if(CurrentBasket<=(BasketLoss*(-1))){CUBA="true";}
   
   for(i = 0; i < OrdersTotal(); i++)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       if (OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
        {
           Profit = OrderProfit() + OrderSwap() + OrderCommission();

           if (OrderType()==OP_BUY && OrderOpenPrice() < LowestBuy)
               {
                  LowestBuy = OrderOpenPrice();
                  LowestBuyTicket = OrderTicket();
                  LowestBuyProfit = Profit;
               }

           if (OrderType()==OP_SELL && OrderOpenPrice() > HighestSell)
               {
                  HighestSell = OrderOpenPrice();
                  HighestSellTicket = OrderTicket();
                  HighestSellProfit = Profit;
               }

           if(OrderType()==OP_BUY)  OrdersBUY++;
           if(OrderType()==OP_SELL) OrdersSELL++;
           if(OrderType()==OP_BUY)  BuyLots += OrderLots();
           if(OrderType()==OP_SELL) SellLots += OrderLots();
           if (OrderType() == OP_BUY)  BuyProfit += OrderProfit() + OrderCommission() + OrderSwap();
           if (OrderType() == OP_SELL) SellProfit += OrderProfit() + OrderCommission() + OrderSwap();
        }
     }
   
    BuyPips   = (PipTarget * BuyLots); 
    SellPips  = (PipTarget * SellLots); 
    BuyProfitTarget = BuyPips + (BuyPips * OrdersBUY * 0.1);
    SellProfitTarget = SellPips + (SellPips * OrdersSELL * 0.1);
    
    if(BuyProfit > BuyProfitTarget && HighestSellProfit > 0) CloseAllThisSymbolBuy(); 
    if(SellProfit > SellProfitTarget && LowestBuyProfit > 0) CloseAllThisSymbolSell();
    
//Manual Close Options

    if (CloseBuys||CLFR=="true"||OPMO=="true"||TIFI=="true"||CUBA=="true") CloseAllThisSymbolBuy();
    if (CloseSells||CLFR=="true"||OPMO=="true"||TIFI=="true"||CUBA=="true") CloseAllThisSymbolSell();
    if (CloseBuysInProfit) CloseBuysinProfit();
    if (CloseSellsInProfit) CloseSellsinProfit();
      
// BUY Trade Criteria

    if (Bid<=(Open[0]-(Step*Point)))   
       {
       BUYme=true;  
       if(Boost && BoostTrack>0 && Close[0]<BoostTrack)
         {
         BUYme=true;
         TradeAllowed=true;
         }
      }
    if (CUBA=="true")return(0);
    if (EndTrading == true && OrdersBUY == 0) BUYme = false;
    if (BUYme == true && CLFR=="false" && OPMO=="false" && TIFI=="false") CheckForBuy();

// SELL Trade Criteria

    if (Bid>=(Open[0]+(Step*Point)))  
       {
       SELLme=true; 
       if(Boost && BoostTrack>0 && Close[0]>BoostTrack)
        {
         SELLme=true; 
         TradeAllowed=true;
        }
     } 
    if (EndTrading == true && OrdersSELL == 0) SELLme = false;
    if (SELLme == true && CLFR=="false" && OPMO=="false" && TIFI=="false") CheckForSell();

   Comment("                    Final EA", NL,
           "                    Sells ", OrdersSELL, NL,
           "                    SellLots ", SellLots, NL,
           "                    HighestSell ", HighestSell, NL,
           "                    Buys ", OrdersBUY, NL,
           "                    BuyLots ", BuyLots, NL,
           "                    LowestBuy ", LowestBuy, NL,
           "                    Current Time is  ",TimeHour(CurTime()),":",TimeMinute(CurTime()),".",TimeSeconds(CurTime()));

      string BuyProf = DoubleToStr(BuyProfit, 2);
      string SellProf = DoubleToStr(SellProfit, 2);
      string BuyProfTarget = DoubleToStr(BuyProfitTarget, 2);
      string SellProfTarget = DoubleToStr(SellProfitTarget, 2);
      
      string AcctBalance = DoubleToStr(AccountBalance(), 2);

      if (ObjectFind("MarginPercent") != 0)
      {
         ObjectCreate("MarginPercent", OBJ_LABEL, 0, 0, 0);
      	ObjectSet("MarginPercent", OBJPROP_CORNER, 3);
      	ObjectSet("MarginPercent", OBJPROP_XDISTANCE, 10);
         ObjectSet("MarginPercent", OBJPROP_YDISTANCE, 5);       
      }
      else
      {
         ObjectSetText("MarginPercent", "Buy $" + BuyProf + "   Sell $" + SellProf + "   $" + AcctBalance, 24, "Arial", White);
      }
      
      if (ObjectFind("MarginPercent2") != 0)
      {
         ObjectCreate("MarginPercent2", OBJ_LABEL, 0, 0, 0);
      	ObjectSet("MarginPercent2", OBJPROP_CORNER, 3);
      	ObjectSet("MarginPercent2", OBJPROP_XDISTANCE, 10);
         ObjectSet("MarginPercent2", OBJPROP_YDISTANCE, 60);       
      }
      else
      {
         ObjectSetText("MarginPercent2", "BuyP $" + BuyProfTarget + "   SellP $" + SellProfTarget + "                 ", 24, "Arial", White);
      }
    
 } // end start()





Sample





Analysis



Market Information Used:

Series array that contains open time of each bar
Series array that contains close prices for each bar
Series array that contains open prices of each bar


Indicator Curves created:


Indicators Used:



Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders

It Closes Orders by itself
It automatically opens orders when conditions are reached

Other Features:

It issuies visual alerts to the screen

BackTest : EURUSD on H1

From 2009-11-01 to 2009-11-30 Profit Factor:0.76 Total Net Profit:-608.98

BackTest : USDJPY on H1

From 2009-11-01 to 2009-11-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2009-12-01 to 2010-01-17 Profit Factor:0.75 Total Net Profit:-684.89

BackTest : USDCAD on H1

From 2009-12-01 to 2010-01-01 Profit Factor:3.79 Total Net Profit:1222.78

BackTest : EURUSD on H1

From 2009-08-01 to 2009-10-01 Profit Factor:3.64 Total Net Profit:2491.71

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-02-27 Profit Factor:0.95 Total Net Profit:-315.38

BackTest : USDCAD on H1

From 2009-01-01 to 2010-01-01 Profit Factor:0.61 Total Net Profit:-10000.75

BackTest : EURUSD on H1

From 2010-03-01 to 2010-03-27 Profit Factor:0.51 Total Net Profit:-1564.95

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-04-16 Profit Factor:0.30 Total Net Profit:-9913.34

BackTest : EURUSD on H1

From 2010-04-01 to 2010-04-30 Profit Factor:0.75 Total Net Profit:-596.39

BackTest : EURUSD on H1

From 2010-05-01 to 2010-05-31 Profit Factor:0.53 Total Net Profit:-2408.31

BackTest : EURUSD on H1

From 2010-06-01 to 2010-06-30 Profit Factor:1.97 Total Net Profit:1056.97

Request Backtest for Final EA modified


From : (yyyy/mm/dd) To: (yyyy/mm/dd)

Pair: Period: