as EA only open a position by the way?

 

My EA works as follows

--------------------------------------------------------------------------------------------------------------

"BUY"

When MACD1 is less than 0 and MACD2 is greater than 0 it opens a BUY

MACD1< 0 && MACD2 >0

ORDERCLOSE

And when they are close to the same effect

MACD1 <0 && MACD2 <0 | | MACD1> 0 && MACD2> 0

--------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------

"SELL"

When MACD1 is greater than 0 and is less than 0 MACD2 it opens a SELL

MACD1 >0 && MACD2< 0

ORDERCLOSE

And when they are close to the same effect

MACD1 <0 && MACD2 <0 | | MACD1> 0 && MACD2> 0

---------------------------------------------------------------------------------------------------------------

I put the "TrailingStop" but when it hits the "TrailingStop" he re-opens the position

I wonder how can I stop the "TrailingStop" and wait for the new EA MACD signal?

EX. EA open BUY and "TrailingStop" was reached for him to open BUY another must wait another intersection of line 0 in reverse.

Files:
myhea.mq4  7 kb
 
sky6nove: I put the "TrailingStop" but when it hits the "TrailingStop" he re-opens the position
Because you only check if your buy/sell condition is true. Instead you need check for a change in condition.
void OnTick(){
   static bool isBuy=false;
   bool isBuyPre = isBuy;
   isBuy = MACD1< 0 && MACD2 >0;
   if(!isBuy) OrderCloseAll(OP_BUY);      // No longer a buy
// else OrderOpen(OP_BUY) your code just opens on continuing buy signal
   else if (!isBuyPre) OrderOpen(OP_BUY); // Open on a new buy signal
Note the above does not handle chart changes
 
WHRoeder:
Because you only check if your buy/sell condition is true. Instead you need check for a change in condition.
Note the above does not handle chart changes

could help me put in the code?
 
I did
 
sky6nove:


WHRoeder showed you one way to do it.

Another way that would give you more wider window to use the signal could be the following :

Define a global variable like this :

extern double TakeProfit=10000;
extern double Lots=0.01;
extern double TrailingStop=500;
//---
string lastSignalUsed="No signal"; // this is the new one

 Then ,add the code to buy and sell conditions as follows :

      if(MACD1<0 && MACD2>0&& lastSignalUsed!="Buy")//new condition added
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"EA SKY TRADE",123456,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
               {
               Print("îòêðûòà ïîçèöèÿ BUY : ",OrderOpenPrice());
               lastSignalUsed="Buy";// if the buy signal was used and order placed
               }
           }
         else Print("Îøèáêà ïðè îòêðûòèè BUY ïîçèöèè : ",GetLastError());
         return(0);
        }

 and

      if(MACD1>0 && MACD2<0 && lastSignalUsed!="Sell")// new condition added
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"EA SKY TRADE",123456,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
               {
               Print("îòêðûòà ïîçèöèÿ SELL : ",OrderOpenPrice());
               lastSignalUsed="Sell";// if the sell signal was used and order placed
               }
           }
         else Print("Îøèáêà ïðè îòêðûòèè SELL ïîçèöèè : ",GetLastError());
         return(0);
        }

 If the terminal is restarted, EA will possibly use the same signal again.

I've cleaned up the warnings a bit and reattached the file. 

Hope it helps. 

Files:
myhea.mq4  9 kb
 
helped incredibly well 


I made some changes and now the code to understand this better.

        MACD1=iMACD(NULL,0,8,12,1,PRICE_CLOSE,MODE_MAIN,1); //BUY or SELL trigger
  MACD2=iMACD(NULL,0,12,120,1,PRICE_CLOSE,MODE_MAIN,1); //trend


Now the code this way.

--------------------------------------------------------------------------------

OPEN BUY OR RE-OPEN    MACD1>0 && MACD2>0 && lastSignalUsed!="Buy"
OPEN SELL OR RE- SELL   MACD1<0 && MACD2<0 && lastSignalUsed!="Sell"

--------------------------------------------------------------------------------

CLOSE BUY OR SELL     (MACD1>0 && MACD2<0)) //"NEUTRAL ZONE" "close BUY or SELL"
CLOSE BUY OR SELL     (MACD1<0 && MACD2>0)) //"NEUTRAL ZONE" "close BUY or SELL"

CLOSE BUY    (MACD1<0 && MACD2<0))     //"close BUY"
CLOSE SELL   (MACD1<0 && MACD2<0))    //"close SELL"


--------------------------------------------------------------------------------

if(
            (MACD1>0 && MACD2<0) ||   //neutral zone
            (MACD1<0 && MACD2>0) || //neutral zone
            (MACD1<0 && MACD2<0)    //CLOSE BUY
            ) 


if(
            
            (MACD1>0 && MACD2<0) || //neutral zone
            (MACD1<0 && MACD2>0) || //neutral zone
            (MACD1>0 && MACD2>0)    //CLOSE SELL
            
            ) 
--------------------------------------------------------------------------------




I have 4 more problems. 


1: It does not close when a divergence when the MACD is activated when the Traingstop. 



2nd: we have to create a condition of neutrality where EA poses reopen the same position. 
EX. we are in a position to buy and Traingstop was reached but a new condition BUY 
(MACD1> 0 && MACD2 <0) / / the MACD enters the neutral zone and then they take the same direction but did not reopen again because the last position was buy 

as no more open position because the acid has reached traingstop he expects please neutrality zone 
and to return to condition BUY again without having to open a SELL 




3rd: mobile stop loss before opening pick Traingstop example: BUY A quantity of 17 candles before, and EA 
mark 10 pips below the lowest candle 17 candle and then to spend the same amount of candle forward, 
the stop loss will follow the 17 cadle before the current. but if we put traingstop he takes over when the value of Traingstop is reached. 


4 open only with the MACD Histogram of closed and not with the current 



use my template "eur usd h4 2003 2003 07 03 24 the 30.tpl" and go up to the date 2003-03-24 and see the example. 




"If the last condition is "NEUTRAL" "re-BUY OR re-SELL"



excuse the flaws in English because I used the google translator.
 
//+------------------------------------------------------------------+
//|                                                     EA SKY TRADE |
//|                                                                  |
//|                                      https://forum.mql4.com/62312 |
//+------------------------------------------------------------------+
extern double TakeProfit=10000;
extern double Lots=0.01;
extern double TrailingStop=800;
//---
string lastSignalUsed="No signal";
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   double MACD1,MACD2;
   int cnt,ticket,total;

   MACD1=iMACD(NULL,0,8,12,1,PRICE_CLOSE,MODE_MAIN,1);
   MACD2=iMACD(NULL,0,12,120,1,PRICE_CLOSE,MODE_MAIN,1);



   total=OrdersTotal();
   if(total<1)
     {

      if(AccountFreeMargin()<(1*Lots))
        {
         Print("Account Free Margin= ",AccountFreeMargin());
         return(0);
        }

      if(MACD1>0 && MACD2>0 && lastSignalUsed!="Buy")
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"EA SKY TRADE",123456,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
               {
               Print("BUY Order Open Price : ",OrderOpenPrice());
               lastSignalUsed="Buy";
               }
           }
         else Print("BUY Get Last Error : ",GetLastError());
         return(0);
        }

      if(MACD1<0 && MACD2<0 && lastSignalUsed!="Sell")
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"EA SKY TRADE",123456,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
               {
               Print("SELL Order Open Price: ",OrderOpenPrice());
               lastSignalUsed="Sell";
               }
           }
         else Print("SELL Get Last Error : ",GetLastError());
         return(0);
        }
      return(0);
     }

   for(cnt=0;cnt<total;cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
        {
         Print("OrderSelect() failed with error :  "+GetLastError());
        }
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        {
         if(OrderType()==OP_BUY)
           {

            if(
            (MACD1>0 && MACD2<0) ||   //neutral zone
            (MACD1<0 && MACD2>0) ||  //neutral zone
            (MACD1<0 && MACD2<0)    //CLOSE BUY
            )
              {
               if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
                 {
                  Print("OrderClose() failed with error :  "+GetLastError());
                  return(0);
                 }
              }

            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
                       {
                        Print("OrderModify() failed with error :  "+GetLastError());
                       }
                     return(0);
                    }
                 }
              }
           }
         else
           {

            if(
            
            (MACD1>0 && MACD2<0) ||   //neutral zone
            (MACD1<0 && MACD2>0) ||  //neutral zone
            (MACD1>0 && MACD2>0)    //CLOSE SELL
            
            )
              {
               if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))
                 {
                  Print("OrderClose() failed with error :  "+GetLastError());
                  return(0);
                 }
              }
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
                       {
                        Print("OrderModify() failed with error :  "+GetLastError());
                        return(0);
                       }
                    }
                 }
              }
           }
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
 
sky6nove:
helped incredibly well 
I have 4 more problems. 
1: It does not close when a divergence when the MACD is activated when the Traingstop. 
2nd: we have to create a condition of neutrality where EA poses reopen the same position. 
EX. we are in a position to buy and Traingstop was reached but a new condition BUY 
(MACD1> 0 && MACD2 <0) / / the MACD enters the neutral zone and then they take the same direction but did not reopen again because the last position was buy 
as no more open position because the acid has reached traingstop he expects please neutrality zone 
and to return to condition BUY again without having to open a SELL 
3rd: mobile stop loss before opening pick Traingstop example: BUY A quantity of 17 candles before, and EA 
mark 10 pips below the lowest candle 17 candle and then to spend the same amount of candle forward, 
the stop loss will follow the 17 cadle before the current. but if we put traingstop he takes over when the value of Traingstop is reached. 
4 open only with the MACD Histogram of closed and not with the current 
use my template "eur usd h4 2003 2003 07 03 24 the 30.tpl" and go up to the date 2003-03-24 and see the example. 
"If the last condition is "NEUTRAL" "re-BUY OR re-SELL"

OK sky6nove

 I think I've got most of it but some things need more explaining.

1.To fix problem number 1 you need to tell me what error you get when it doesn't close. It may be that the data isn't very good , gaps in data,wrong indicator parameters, etc. 

2.You can have that but since you have the conditions for buy and sell inside the : if(total<1), you need to change that.

You could use a function to check the signal for you at every tick and a couple of datetime variables to keep the time when the cross or neutral happens . If you need help with that let us know.

3. You can use a function for trailing by the low of the lowest candle 1-17 and condition that from user input .

4. About the MACD indicator, try this and let me know if it works : 

Open a new chart  on any pair.  Add MACD indicator to the chart. Change MACD inputs to your EA settings of  8,12,1  or 12,120,1  and see what happens.

MACD1=iMACD(NULL,0,8,12,1,PRICE_CLOSE,MODE_MAIN,1);
MACD2=iMACD(NULL,0,12,120,1,PRICE_CLOSE,MODE_MAIN,1);

 If you get an error in the experts tab*  and the indicator is deleted from chart, it means that those parameters are wrong.

* if you need help to get to the experts tab, let us know. 

Reason: