Experts: Good EA but needs FridayClose to perform better

 

Good EA but needs FridayClose to perform better:

I got this EA from a friend. It is based on the previous day's open and close price, but I would want it to close all trade on friday at a certain time.

Author: ChuQ Dennis

 
if ( DayOfWeek() == 5 && TimeCurrent() >= StrToTime("13:00") ) {// close all orders on friday
 

Much wrong with the coding here but someone has thought about the strategy

I fixed it to work on full-pip or sub-pip pairs automatically, fixed it so the MagicNumber worked so can be used on several pairs in same account

The Friday close has been added but it may be better without - see what you think

//+------------------------------------------------------------------+
//|                                            Udy Ivan Madumere.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
 
extern  int TakeProfit_L = 39; // Take Profit in points   -- alter for subpip
extern  int StopLoss_L = 147;  // Stop Loss in points     -- alter for subpip
extern  int TakeProfit_S = 200; // Take Profit in points  -- alter for subpip
extern  int StopLoss_S = 267;  // Stop Loss in points     -- alter for subpip
extern  int TrailingStop  = 30; //  -- alter for subpip
 
extern  int delta_L=6;  //  -- alter for subpip       
extern  int delta_S=21; //  -- alter for subpip
extern int Slippage = 3; //  -- alter for subpip
 
extern int TradeTime=18;      // Time to enter the market
extern int FridayCloseHour = 25;
extern int magic=101; 
extern  int t1=6;              
extern  int t2=2;  
    
extern double lot = 0.01;      // Lot size
extern  int Orders=1;          // maximal number of positions opened at a time }
extern  int MaxOpenTime=504;
extern  int BigLotSize = 1;    // By how much lot size is multiplicated in Big lot
extern  bool AutoLot=false;
 
extern string comment="20/200 expert v101";
 
int ticket,total,cnt;
bool cantrade=true;
double closeprice;
double tmp;
int Real.TakeProfit_L,Real.StopLoss_L,Real.TakeProfit_S,Real.StopLoss_S, Real.TrailingStop,Real.delta_L, Real.delta_S, Real.Slippage;
int OpenTradesForMN(int iMN)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersTotal();
   for(icnt=0;icnt<itotal;icnt++)
     {
      OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
       // check for opened position, symbol & MagicNumber
       if(OrderMagicNumber() == iMN)
        if (OrderSymbol() == Symbol())
         if(OrderType() <= OP_SELL)
           {
             retval++;
           }
     }
return(retval);
}
void CloseAllBuyOrders(int MN)
  {
  int i, iTotalOrders;
  
  //iTotalOrders=OrdersTotal(); 
  //for (i=0; i<iTotalOrders; i++)
   
   iTotalOrders=OrdersTotal()-1; // Rosh line
  
   for (i=iTotalOrders; i>=0; i--) // Rosh line     
   
   { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      { 
         if (OrderMagicNumber()==MN)
          if (OrderSymbol()== Symbol())
           { 
            if (OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);
            if (OrderType()==OP_BUYSTOP) OrderDelete(OrderTicket());
            if (OrderType()==OP_BUYLIMIT) OrderDelete(OrderTicket());
            
           }
      }
   }
}
  void CloseAllSellOrders(int MN)
  {
  int i, iTotalOrders;
  
  // iTotalOrders=OrdersTotal(); 
  // for (i=0; i<iTotalOrders; i++)
   
   iTotalOrders=OrdersTotal()-1; // Rosh line
  
   for (i=iTotalOrders; i>=0; i--) // Rosh line  
   { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      { 
         if (OrderMagicNumber()==MN)
           if (OrderSymbol()== Symbol())
             { 
              if (OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet);
              if (OrderType()==OP_SELLSTOP) OrderDelete(OrderTicket());
              if (OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket());
             }
      }
   }
}
void HandleDigits()
 {
 
    if (Digits == 4 || Digits == 2) // Full pip pair
     {
      Real.TakeProfit_L = TakeProfit_L;
      Real.StopLoss_L = StopLoss_L;
      Real.TakeProfit_S = TakeProfit_S;
      Real.StopLoss_S = StopLoss_S;
      Real.TrailingStop = TrailingStop;
      Real.delta_L  = delta_L;
      Real.delta_S = delta_S;
      Real.Slippage = Slippage;
     }
  
   if (Digits == 5 || Digits == 3) // Sub-pip pair
     {
      Real.TakeProfit_L = TakeProfit_L*10;
      Real.StopLoss_L = StopLoss_L*10;
      Real.TakeProfit_S = TakeProfit_S*10;
      Real.StopLoss_S = StopLoss_S*10;
      Real.TrailingStop = TrailingStop*10;
      Real.delta_L  = delta_L*10;
      Real.delta_S = delta_S*10;
      Real.Slippage = Slippage*10;
     }
 
 
 } 
 
int LotSize()
// The function opens a short position with lot size=volume
{
if (AccountBalance()>=50) lot=0.02;
if (AccountBalance()>=100) lot=0.04;
if (AccountBalance()>=200) lot=0.08;
if (AccountBalance()>=300) lot=0.12;
if (AccountBalance()>=400) lot=0.16;
if (AccountBalance()>=500) lot=0.2;
if (AccountBalance()>=600) lot=0.24;
if (AccountBalance()>=700) lot=0.28;
if (AccountBalance()>=800) lot=0.32;
if (AccountBalance()>=900) lot=0.36;
if (AccountBalance()>=1000) lot=0.4;
if (AccountBalance()>=1500) lot=0.6;
if (AccountBalance()>=2000) lot=0.8;
if (AccountBalance()>=2500) lot=1.0;
if (AccountBalance()>=3000) lot=1.2;
if (AccountBalance()>=3500) lot=1.4;
if (AccountBalance()>=4000) lot=1.6;
if (AccountBalance()>=4500) lot=1.8;
if (AccountBalance()>=5000) lot=2.0;
if (AccountBalance()>=5500) lot=2.2;
if (AccountBalance()>=6000) lot=2.4;
if (AccountBalance()>=7000) lot=2.8;
if (AccountBalance()>=8000) lot=3.2;
if (AccountBalance()>=9000) lot=3.6;
if (AccountBalance()>=10000) lot=4.0;
if (AccountBalance()>=15000) lot=6.0;
if (AccountBalance()>=20000) lot=8.0;
if (AccountBalance()>=30000) lot=12;
if (AccountBalance()>=40000) lot=16;
if (AccountBalance()>=50000) lot=20;
if (AccountBalance()>=60000) lot=24;
if (AccountBalance()>=70000) lot=28;
if (AccountBalance()>=80000) lot=32;
if (AccountBalance()>=90000) lot=36;
if (AccountBalance()>=100000) lot=40;
if (AccountBalance()>=200000) lot=80;
}
 
int globPos()
// the function calculates big lot size
{
int v1=GlobalVariableGet("globalPosic");
GlobalVariableSet("globalPosic",v1+1);
  return(0);
}
 
int OpenLong(double volume=0.1)
// the function opens a long position with lot size=volume 
{
  color arrow_color=Blue;
 
    if (GlobalVariableGet("globalBalans")>AccountBalance()) volume=lot*BigLotSize;
  //  if (GlobalVariableGet("globalBalans")>AccountBalance()) if (AutoLot) LotSize();
   
  ticket=OrderSend(Symbol(),OP_BUY,volume,Ask,Real.Slippage,Ask-Real.StopLoss_L*Point, Ask+Real.TakeProfit_L*Point,comment,magic,0,arrow_color);
 
  GlobalVariableSet("globalBalans",AccountBalance());                    
  globPos();
//  if (GlobalVariableGet("globalPosic")>25)
//  {
  GlobalVariableSet("globalPosic",0);
  if (AutoLot) LotSize();
//  }
 
  if(ticket>0)
  {
    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
    {
      return(0);
    }
    else
      {
        Print("OpenLong(),OrderSelect() - returned an error : ",GetLastError()); 
        return(-1);
      }   
  }
  else 
  {
    Print("Error opening Buy order : ",GetLastError()); 
    return(-1);
  }
}
  
int OpenShort(double volume=0.1)
// The function opens a short position with lot size=volume
{
  
  color arrow_color=Red;
   
 
  if (GlobalVariableGet("globalBalans")>AccountBalance()) volume=lot*BigLotSize;
   
  ticket=OrderSend(Symbol(),OP_SELL,volume,Bid,Real.Slippage,Bid+Real.StopLoss_S*Point, Bid-Real.TakeProfit_S*Point,comment,magic,0,arrow_color);
  GlobalVariableSet("globalBalans",AccountBalance());
  globPos();
//  if (GlobalVariableGet("globalPosic")>25)
//  {
  GlobalVariableSet("globalPosic",0);
  if (AutoLot) LotSize();
//  }
 
  if(ticket>0)
  {
    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
      {
        return(0);
      }
    else
      {
        Print("OpenShort(),OrderSelect() - returned an error : ",GetLastError()); 
        return(-1);
      }    
  }
  else 
  {
    Print("Error opening Sell order : ",GetLastError()); 
    return(-1);
  }
}
 
int init()
{
  // control of a variable before using
  if (AutoLot) LotSize();
  if(!GlobalVariableCheck("globalBalans"))
    GlobalVariableSet("globalBalans",AccountBalance());
  if(!GlobalVariableCheck("globalPosic"))
    GlobalVariableSet("globalPosic",0);
  return(0);
}
 
int deinit()
{   
  return(0);
}
 
 
int start()
{
  HandleDigits();
  
  if (DayOfWeek()==5)
    if(TimeHour(TimeCurrent())>=FridayCloseHour)
     {
       CloseAllBuyOrders(magic);
       CloseAllSellOrders(magic);
     
     }
  if((TimeHour(TimeCurrent())>TradeTime)) cantrade=true;  
  // check if there are open orders ...
  
  total=OpenTradesForMN(magic);
  
  if(total<Orders)
  {
    // ... if no open orders, go further
    // check if it's time for trade
    if((TimeHour(TimeCurrent())==TradeTime)&&(cantrade))
    {
      // ... if it is
      if (((Open[t1]-Open[t2])>Real.delta_S*Point)) //if it is
      {
        //condition is fulfilled, enter a short position:
        // check if there is free money for opening a short position
        if(AccountFreeMarginCheck(Symbol(),OP_SELL,lot)<=0 || GetLastError()==134)
        {
          Print("Not enough money");
          return(0);
        }
        OpenShort(lot);
        
        cantrade=false; //prohibit repeated trade until the next bar
        return(0);
      }
      if (((Open[t2]-Open[t1])>Real.delta_L*Point)) //if the price increased by delta
      {
        // condition is fulfilled, enter a long position
        // check if there is free money
        if(AccountFreeMarginCheck(Symbol(),OP_BUY,lot)<=0 || GetLastError()==134)
        {
          Print("Not enough money");
          return(0);
        }
        OpenLong(lot);
        
        cantrade=false;
        return(0);
      }
    }
  }
 
 
    //Manage open orders
         //move Real.TrailingStop if profit>TS
            
            if(OrderType()==OP_SELL) 
            {
               if(OrderOpenPrice()-Ask>Real.TrailingStop*Point && OrderStopLoss()>Ask+Real.TrailingStop*Point)
              {
                  OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Real.TrailingStop*Point,0,0, Red);
              }           
           }
 
// block of a trade validity time checking, if MaxOpenTime=0, do not check.
   if(MaxOpenTime>0)
   {
      for(cnt=0;cnt<total;cnt++)
      {
         if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
         {
            tmp = (TimeCurrent()-OrderOpenTime())/3600.0;
               
               if (((NormalizeDouble(tmp,8)-MaxOpenTime)>=0))
                 if (OrderMagicNumber()==magic)
                   if (OrderSymbol()== Symbol())
                     {     
                        RefreshRates();
                        if (OrderType()==OP_BUY)
                           closeprice=Bid;
                        else  
                           closeprice=Ask;          
                        if (OrderClose(OrderTicket(),OrderLots(),closeprice,10,Green))
                        {
                        Print("Forced closing of the trade - No",OrderTicket());
                           OrderPrint();
                        }
                        else 
                           Print("OrderClose() in block of a trade validity time checking returned an error - ",GetLastError());        
                        } 
                     }
               else 
                  Print("OrderSelect() in block of a trade validity time checking returned an error - ",GetLastError());
         } 
      }     
      return(0);
   }
 
 
 
BarrowBoy:

After a second look, I think some obfuscation has occurred somewhere along the line...

I suggest that this

Open[t2]

is replaced with this

Close[t2]

and t1=8, t2=1 should be tried - the implication is that the EA should be on the H1 chart

Also, keep in mind that this appears to be a follow-the-London-session-up-or-down strategy, so that TradeTime=18 is for CET summertime (i.e. 16:00)
If your broker is GMT all year try TradeTime=16

Another error, replace this

if((TimeHour(TimeCurrent())>TradeTime)) cantrade=true;

with this

if((TimeHour(TimeCurrent())!=TradeTime)) cantrade=true;

otherwise the EA wont run the same day you load it!

Finally, this EA, as almost every system is very dependent on adequate action levels
Q4 2009 and Q1 2010 were very poor for this EA, Q2 2010 was very good, but we are losing market volume with the holiday season, World Cup etc, etc - so consider keeping this on demo until September!

This could work on several pairs but watch the D1 ATR (20 period) doesnt drop too low for the pair...

As always, lets be careful in the Forex :)

FWIW

-BB-

 

Nice work, gonna give it a try with the added friday filter.


Top Performing Forex Robot Testing Results

 

Nil,


What timeframe and pair have you tested this on? Thx for sharing! GL!

 
bnbb2004:

Nil,


What timeframe and pair have you tested this on? Thx for sharing! GL!


No idea on the origins but has some promise on the H1 chart of the majors plus EURJPY

-BB-

 
BarrowBoy:
bnbb2004:

Nil,


What timeframe and pair have you tested this on? Thx for sharing! GL!


No idea on the origins but has some promise on the H1 chart of the majors plus EURJPY

-BB-

So far it's traded 3 wins. Majors + E/J on 1H chart.
 

I've had some posts asking about the detail of this EA

Re Code Logic

((Open[t1]-Open[t2]) > Real.delta_S * Point))

t1 and t2 are numbers set at the top of the EA

So the EA is looking at the Open price t1 candles ago and comparing it to the Open price t2 candles ago
If the price drops by more than the set amount, the EA sells

The Real.xxxxxxx values are set by the function

HandleDigits()

to automatically handle full or sub-pip pairs/accounts


Re Optimization

The values for TakeProfit & StopLoss have been generated by back-testing, so the values may need changing from time to time
Maybe a better way would be based on a percentage of the daily range and so be self-optimizing, I dont know

Certainly the EA needs a certain minimum average daily range (iATR indicator) to work well
The time of day changes as mentioned below are also crucial
FWIW
-BB-

 

No idea on the origins but has some promise on the H1 chart of the majors plus EURJPY

After some thought I'd leave off GBPUSD and NZDUSD from the list, IIRC they have too different a 'wave action' for this

So the list would be on the hourly chart for EURUSD, USDJPY, USDCHF, AUDUSD, EURJPY

And as always, lets be careful in the Forex

-BB-

 
bnbb2004: I run it on live account on EURUSD H1 only and on a VPS platform 24/7
BarrowBoy:
bnbb2004:

Nil,


What timeframe and pair have you tested this on? Thx for sharing! GL!


No idea on the origins but has some promise on the H1 chart of the majors plus EURJPY

-BB-

So far it's traded 3 wins. Majors + E/J on 1H chart.

Reason: