Download MetaTrader 5

Please help with my EA, I am desperate

To add comments, please log in or register
Read the interviews of the Automated Trading Championship participants
FFOOXX
11
FFOOXX 2014.01.08 14:03 
 //---- input parameters
extern int       TP           = 300;// take profit
extern int       TP_Increment   = 200;// take profit incressement
extern int       SL           = 500; // stop loss
extern int       RangePoint   = 0; // range
extern int       AddPoint     = 0;// add point to place stop order  [high+AddPoint] and [low-AddPoint]
extern double    LotSize      = 0.25; // position size
extern int       LotDevider   = 5;// LotSize must be dividable by this number; number must be 1 or higher. Also Broker must support the resulting Lotsize
extern int       MagicNumber  = 123456789;
extern bool      DelOpposite = false; //delete opposite order
extern bool      MoveToBEP = true; // move to break even point
extern int       LockProfit = 1; // move to break even + 1 point
extern int       TrailingStop = 0;// if 0, no Trailing Stop
extern bool      tradingtimes = true; // if you want the EA to trade during particular hours, set this to true
extern int       starttime = 0; //just input start hour of your broker
extern int       endtime = 24; //just input the end hour of your broker

static double LotPart; // LotPart = LotSize/LotDevider
static int  nextTP;
int  realTP;
int  TP1;


#define slip 2
//+------------------------------------------------------------------+
//| inicializace funkce                                   |
//+------------------------------------------------------------------+
int init()
  {
//----

TP1 = TP;
if(LotDevider<1) LotDevider =1;
realTP = TP_Increment*(LotDevider-1) + TP;
LotPart = LotSize/LotDevider;

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  
  if (tradingtimes)
  {
  int currenttime = TimeHour(TimeCurrent());
  
  
  if (currenttime < starttime || currenttime >= endtime)
      {
      Comment ("outside of trading times");
      return;
      }
      
  Comment ("Start Time ", starttime, "\n", "End Time is ", endtime, "\n", "current time is ", currenttime);
  }    
//----
   double stoplevel = MarketInfo(Symbol(),MODE_STOPLEVEL)*point();
   double tik = iVolume(NULL,0,0);
   int exp = TimeCurrent() + (Period()*59);
   double buyPrice, sellPrice;
   
   RefreshRates();
   if(High[1]-Ask < stoplevel) buyPrice = Ask + RangePoint*point();
   else buyPrice = High[1] + AddPoint*point();
   
   if(Bid-Low[1] < stoplevel) sellPrice = Bid - RangePoint*point();
   else sellPrice = Low[1] - AddPoint*point();
   
   
   if(OrdersTotal() == 0)
   {
      nextTP = TP;
      if(tik <= 3)
      {
         openOrder(Symbol(), OP_BUYSTOP,LotSize,buyPrice,buyPrice-SL*point(), buyPrice+realTP*point(),"_Buy_",MagicNumber,exp,Navy);
         openOrder(Symbol(), OP_SELLSTOP,LotSize,sellPrice,sellPrice+SL*point(), sellPrice-realTP*point(),"_Sell_",MagicNumber,exp,Crimson);   
      }   
   }
   
   if(OrdersTotal()==1)
   {
      if(!DelOpposite) nextTP = TP;
   }
   
   if(OrdersTotal() == 2)
   {
      for (int i=0;i<OrdersTotal();i++)
      {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol())
         {
            if(OrderMagicNumber()==MagicNumber)
            {
               if(DelOpposite)
               {
                  if(OrderType()==OP_BUY) delPending(OP_SELLSTOP,MagicNumber);
                  if(OrderType()==OP_SELL) delPending(OP_BUYSTOP,MagicNumber);
               }
            }
         
         }
      }
   
   }
   
   if(OrdersTotal() > 0)
   {
      if(LotDevider>1) manageTP();
      if(MoveToBEP) BEP();
      if(TrailingStop>0) TS();
   }   
//----
   return(0);
  }
//+------------------------------------------------------------------+

void openOrder(string simbol, int trade, double lotsize, double price, double sl, double tp,string pesan, int magic, int exp, color warna) 
{                     
  int tiket=OrderSend(simbol,trade,lotsize,price,slip,sl,tp,pesan,magic,exp,warna);                              
      if(tiket>0)
      {  
         if(OrderSelect(tiket,SELECT_BY_TICKET,MODE_TRADES)) OrderPrint(); 
      }
         else Print("Cannot send order because of : ",GetLastError());        
}

void delPending(int trade, int MagicNumber)
{  
    for(int i=0;i<OrdersTotal();i++)
    {
       OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       if(OrderType()==trade)
       { 
         if(OrderMagicNumber()==MagicNumber) OrderDelete(OrderTicket(),CLR_NONE);
       }  
    }   
}

double point()
{
   double p;
   if(Digits==5 || Digits==3) p = Point*10;
   else p = Point;
   
   return(p);
}

void BEP()
{
      for (int i = 0; i < OrdersTotal(); i++)
      {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
         {
            if(OrderType()==OP_BUY)
            {
               if(Bid-OrderOpenPrice() >= point()*TP1)
               {
                  if(OrderStopLoss()<OrderOpenPrice())
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+point()*LockProfit,OrderTakeProfit(),0,CLR_NONE);
                     return(0);
                  }
               }
            }
         
            if(OrderType()==OP_SELL)
            {
               if(OrderOpenPrice()-Ask >= point()*TP1)
               {
                  if(OrderStopLoss()>OrderOpenPrice())
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-point()*LockProfit,OrderTakeProfit(),0,CLR_NONE);
                     return(0);
                  }
               }      
            }
         } 
      }   
}

void TS()
{
 for (int i = 0; i < OrdersTotal(); i++)
      {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
         {
            if(OrderType()==OP_BUY)
            {
               if(Bid-OrderOpenPrice()>point()*TrailingStop)
               {
                  if(OrderStopLoss()<Bid-point()*TrailingStop || (OrderStopLoss()==0))
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-point()*TrailingStop,OrderTakeProfit(),0,CLR_NONE);
                     return(0);
                  }
               }
            }
         
            if(OrderType()==OP_SELL)
            {
               if((OrderOpenPrice()-Ask)>(point()*TrailingStop))
               {
                  if((OrderStopLoss()>(Ask+point()*TrailingStop)) || (OrderStopLoss()==0))
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+point()*TrailingStop,OrderTakeProfit(),0,CLR_NONE);
                     return(0);
                  }
               }      
            }
         } 
      
      }   
}

void manageTP()
{
      for (int i = 0; i < OrdersTotal(); i++)
      {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
         {
            if(OrderType()==OP_BUY)
            {
               if(Bid-OrderOpenPrice() >= point()*nextTP)
               {
                  OrderClose(OrderTicket(),LotPart,Bid,slip,Blue);   
                  nextTP += TP_Increment;
                  return(0);
               }
            }
         
            if(OrderType()==OP_SELL)
            {
               if(OrderOpenPrice()-Ask > point()*nextTP)
               {
                  OrderClose(OrderTicket(),LotPart,Ask,slip,Blue);   
                  nextTP += TP_Increment;
                  return(0);
               }      
            }
         } 
      }   
}

Hello,

I would like to use this EA, but if I try it on a live account, it sometimes open good orders, sometimes open only one order, sometimes open no order. It is simple breakout strategy which place order to previous high(buy) and low(sell) and then divide this into 5 orders and gradually close in TP. Please help me I try to find the mistake for 14 days. My mt4 is set well (smailing smile, trading is permitted, backtest is working). I really don't know what should I do. Sorry for my bad english and thank you for help.

Tjipke de Vries
6315
Tjipke de Vries 2014.01.08 15:28  
FAUX1:

Hello,

I would like to use this EA, but if I try it on a live account, it sometimes open good orders, sometimes open only one order, sometimes open no order. It is simple breakout strategy which place order to previous high(buy) and low(sell) and then divide this into 5 orders and gradually close in TP. Please help me I try to find the mistake for 14 days. My mt4 is set well (smailing smile, trading is permitted, backtest is working). I really don't know what should I do. Sorry for my bad english and thank you for help.


unbelievable you're desperate ???? you tried to find this mistake for 14 days ??? It is very simple to find...

there is a problem opening trades on a live account....

then what are the lines in your EA where trades should be opened ??

can you point that out ???

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.01.08 16:36  
FAUX1:

Hello,

I would like to use this EA, but if I try it on a live account, it sometimes open good orders, sometimes open only one order, sometimes open no order. It is simple breakout strategy which place order to previous high(buy) and low(sell) and then divide this into 5 orders and gradually close in TP. Please help me I try to find the mistake for 14 days. My mt4 is set well (smailing smile, trading is permitted, backtest is working). I really don't know what should I do. Sorry for my bad english and thank you for help.

Read these and implement them . . . or don't and continue in ignorance: Loops and Closing or Deleting Orders & What are Function return values ? How do I use them ?
To add comments, please log in or register