Need Help on my first EA

 

Hello guys,

Im trying to build my first EA. I got some Visual Basic courses at highschool (years ago), but that hasn't helped me much. I'm very motivated to learn though.
I've been spending a lot of hours on the underneath code (yes, you can laugh) but things aren't working as they should. So it's time to call for some little help :)
Compiling the underneath code, I get no errors. But when attaching the EA to the chart, nothing happens.
I'm pretty sure something is wrong in the int start() part. What I want to do is open a buy or sell order at a certain condition (Moving Averages), hold it, and close the order when another specific condition (Signal line MACD) is met.
Since I don't want it to open orders constantly (when the condition is valid over a longer time) I work with MagicNumbers. An active Buy order gets MN 00001 and I dont want the EA to buy more lots until the order with MN 00001 is closed. Same for Sell orders (using MN 00002)
Help would be greatly appreciated!

Thx in advance,

StormOpZee

***

extern double Lots = 0.01; 
double EMAFast=5; 
double EMAMedium=9; 
double EMASlow=13; 
double MACDEmaFast=5;
double MACDEmaSlow=9;
double MACDSmaSignalLine=3;
double MACDOpenLevel=3;
double MACDCloseLevel=2;
double MACDCurrent, MACDPrevious, SignalCurrent, SignalPrevious, EMASlowCurrent, EMASlowPrevious; 
double EMAMediumCurrent, EMAMediumPrevious, EMAFastCurrent, EMAFastPrevious;
int itotal, icnt, ticket;

//-----------------------------------+
int start()
{
MACDCurrent=iMACD(NULL,0,MACDEmaFast,MACDEmaSlow,MACDSmaSignalLine,PRICE_CLOSE,MODE_MAIN,0);
MACDPrevious=iMACD(NULL,0,MACDEmaFast,MACDEmaSlow,MACDSmaSignalLine,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,MACDEmaFast,MACDEmaSlow,MACDSmaSignalLine,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,MACDEmaFast,MACDEmaSlow,MACDSmaSignalLine,PRICE_CLOSE,MODE_SIGNAL,1);
EMASlowCurrent=iMA(NULL,0,EMASlow,0,MODE_EMA,PRICE_CLOSE,0);
EMASlowPrevious=iMA(NULL,0,EMASlow,0,MODE_EMA,PRICE_CLOSE,1);
EMAMediumCurrent=iMA(NULL,0,EMAMedium,0,MODE_EMA,PRICE_CLOSE,0);
EMAMediumPrevious=iMA(NULL,0,EMAMedium,0,MODE_EMA,PRICE_CLOSE,1);
EMAFastCurrent=iMA(NULL,0,EMAFast,0,MODE_EMA,PRICE_CLOSE,0);
EMAFastPrevious=iMA(NULL,0,EMAFast,0,MODE_EMA,PRICE_CLOSE,1);

itotal=OrdersTotal();

   for(icnt=0;icnt<itotal;icnt++)
     {
      OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
       // check for opened position/MagicNumber
       if (OrderMagicNumber() == 00001) //If the search reveals an order with MN 00001 -> Go to CloseBuy
           {
            CloseBuy(); 
           }
       if (OrderMagicNumber() == 00002) //If the search reveals an order with MN 00002 -> Go to CloseSell
           {
            CloseSell();
           }
       if (OrderMagicNumber() != 00001) //If the search reveals an order with MN different from 00001 or finds no orders at all -> Go to OpenBuySell
           {
           OpenBuySell();
           }
       if (OrderMagicNumber() != 00002) //If the search reveals an order with MN different from 00002 or finds no orders at all -> Go to OpenBuySell
           {
           OpenBuySell();
           }
     }

}
//+------------------------------------------------------------------+
void OpenBuySell()
{
// check for long position (BUY) possibility
if (EMAFastCurrent>EMAMediumCurrent && EMAMediumCurrent>EMASlowCurrent && EMAFastCurrent>EMAFastPrevious && EMAMediumCurrent>EMAMediumPrevious && EMASlowCurrent>EMASlowPrevious)
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Triple EMA and MACD - LONG",00001,0,Green);
         return(0);
        }        
// check for short position (SELL) possibility
if (EMAFastCurrent<EMAMediumCurrent && EMAMediumCurrent<EMASlowCurrent && EMAFastCurrent<EMAFastPrevious && EMAMediumCurrent<EMAMediumPrevious && EMASlowCurrent<EMASlowPrevious)
       {
       ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"Triple EMA and MACD - SHORT",00002,0,Red);
       return(0);
       }
}
//+------------------------------------------------------------------+
void CloseBuy()
{
if(OrderType()==OP_BUY)   // long position is opened
   {
   if(SignalCurrent<SignalPrevious)
   {
   OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
   return(0);
   }
   }
}     
//+------------------------------------------------------------------+
void CloseSell()
{
if(OrderType()==OP_SELL)   // short position is opened
   {           
   if(SignalCurrent>SignalPrevious)
   {
   OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
   return(0);
   }
   }
} 
//+------------------------------------------------------------------+
 

if (OrderMagicNumber() != 00001) //If the search reveals an order with MN different from 00001 or finds no orders at all -> Go to OpenBuySell
{
OpenBuySell();
}

if find no orders at all the for dont enter..

You need also somethng like if (OrdersTotal()<1) openbuy or sell or something ...

 

to have just one order open at a time, you want to use

int total = OrdersTotal();

then if total == 0 make an order. that is, if there are no orders make an order. If total = 1 ( >0 ) then no action will be taken.

As for the code you have presented, it is from an EA you have downloaded and hacked from the internet. It was probably working fine till you messed with it.

If you are just starting out, you want to avoid using functions like that - just keep everything as simple as you can. Read the documentation and learn.

You can have a trade condition recognised, eg if on the previous bar MA1 > MA2 && on the current bar MA1 < MA2 (MA crossover) then open a trade, and then you can have a similar setup in order to close the order, and use orderclose.

I dont have time tonight to put up a full example.

      double MA1=iMA(NULL,0,10,0,1,0,0);
      double MA2=iMA(NULL,0,10,0,1,0,1);
      double MA3=iMA(NULL,0,4,0,1,0,0);
      double MA4=iMA(NULL,0,4,0,1,0,1);
      
      string BUY="false";
      string SELL="false";

      if(MA1 < MA3 && MA2 > MA4 )BUY="true"; 
      if(MA1 > MA3 && MA2 < MA4 )SELL="true";
     

above is an example of a simple MA crossover entry condition

if (BUY=="true" && total == 0)           // total == 0 means if there are no orders present
      {
       Ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP,EAName,Magic,0,Blue);
      }

above is a simple example of making the order if the Buy condition is met, and there are no orders on.

then if the order is on, and the close conditions are met, this code from an EA which closes all open orders should point you in the right direction.

//+------------------------------------------------------------------+
//|                                            close-all-orders.mq4  |
//|                                  Copyright © 2005, Matias Romeo. |
//|                                       Custom Metatrader Systems. |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, Matias Romeo."
#property link      "mailto:matiasDOTromeoATgmail.com"

int start()
{
  int total = OrdersTotal();
  for(int i=total-1; i>=0; i-- )
  {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}
 
  1.  string BUY="false";
          string SELL="false";
    
          if(MA1 < MA3 && MA2 > MA4 )BUY="true"; 
          if(MA1 > MA3 && MA2 < MA4 )SELL="true";
    if (BUY=="true" && total == 0)   
    bool cross = (MA3-MA1) * (MA2-MA4),
         buy   = MA3 > MA1;
    if (cross && total == 0){
      if (buy) OrderSend(OP_BUY...
      else     OrderSend(OP_SELL...
    }

  2.   int total = OrdersTotal();
      for(int i=total-1; i>=0; i-- )
      {
        OrderSelect(i, SELECT_BY_POS);
        int type   = OrderType();
    
    In the presence of multiple order (multiple charts) you MUST count down when modifying/closing/deleteing. Always test return codes. No magic number/symbol means the EA incompatable with other EAs (including itself on other charts) and manual trading.
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
    

  3. ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Trip
    On 5 digit brokers, you must adjust tp, sl, AND SLIPPAGE
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl

  4. int total = OrdersTotal();
    then if total == 0 make an order. that is, if there are no orders make an order. If total = 1 ( >0 ) then no action will be taken.
    OrdersTotal() is meaning less with multiple charts/multiple EAs. Do the orderSelect loop and count YOUR open orders on YOUR chart.
Reason: