Stop and reverse

 

any one know how to do a true stop an reverse ea? ...I have been trying like below but hasn't worked:


void check_entry()
{
/////////////////////
if(ptriggerline>pbaseline)short_allowed=true; // previous(index of 2) trig line is above previous candle baseline
if(ptriggerline<pbaseline)long_allowed=true; // previous (index of 2) trig line is below previous candle baseline

if(triggerline>baseline)buy=true; // trig line is now above baseline ( current trig& base line index = 1)
if(triggerline<baseline)sell=true; // trig line is now below baseline ( current trig& base line index = 1)
/////////////////////
}

void check_exit()
{
//////////////////////////
if(triggerline<baseline)close_longs=true;  // close the buys if trig line drops below baseline
if(triggerline>baseline)close_shorts=true; // close the sells if trig line goes above baseline
////////////////////////
if(close_longs)close_longs();
if(close_shorts)close_shorts();
//////////////////////////
}

I have also tried this method :

int Crossed (double line1 , double line2)

{

static int last_direction = 0;

static int current_direction = 0;

if(line1>line2)current_direction = 1; //up

if(line1<line2)current_direction = 2; //down

if(current_direction != last_direction) //changed

{

last_direction = current_direction;

return (last_direction);

 }

  else

  {

   return (0);

  }

}

isCrossed = Crossed (shortEma,longEma);

total = OrdersTotal();

if(total < 1)

  {

   if(isCrossed == 1)

     {

     ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,comment,magic,0,Green);

      if(ticket>0)

        {

         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

           Print("BUY order opened : ",OrderOpenPrice());

        }

 else Print("Error opening BUY order : ",GetLastError());

 return(0);

  }

     if(isCrossed == 2)

       {

       ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,comment,magic,0,Red);

         if(ticket>0)

           {

           if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

            Print("SELL order opened : ",OrderOpenPrice());

          }

 else Print("Error opening SELL order : ",GetLastError());

 return(0);

 }

return(0);

}

for(cnt=0;cnt<total;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())

{

if(OrderType()==OP_BUY) // long position is opened

{
// check for trailing stop

if(TrailingStop>0)

{

if(Bid-OrderOpenPrice()>Point*TrailingStop)

{

if(OrderStopLoss()<Bid-Point*TrailingStop)

{

OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);

return(0);

}

}

}

}

else // go to short position

{

// check for trailing stop

if(TrailingStop>0)

{

if((OrderOpenPrice()-Ask)>(Point*TrailingStop))

{

if((OrderStopLoss()>(Ask+Point*TrailingStop)) ||(OrderStopLoss()==0))

{

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);

return(0);

}

}

}

}

}

}

return(0);

}

//+------------------------------------------------------------------+

void check_exit()
{
/////////////////////////
int total = OrdersTotal();
////////////////////////
for(int cnt=0;cnt<total;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())

{

if(OrderType()==OP_BUY)
{
if(shortEma<longEma)

{

OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);

// close position

return(0); // exit
}


if(OrderType()==OP_SELL)
{
if(shortEma>longEma)

{

OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);

// close position

return(0); // exit
}
}
}
}
}
//////////////////////////////
}


none of these actually take a trade when the two MA's cross each other and then close the trade and open a opposite trade if the MA's cross the opposite way...


its a simple stop and reverse i want but just is not working...any one hav any ideas?


thanx guys

 

tried it like this as well :


if(total==0&&triggerline>baseline)
    {
    ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,sll,tpl,comment,magic,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
    
    if(total==0&&triggerline<baseline)
    {
    ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,sls,tps,comment,magic,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     }
//----
for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else // go to short position
           {
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
void check_exit()
{
if(triggerline<baseline)close_longs=true;
if(triggerline>baseline)close_shorts=true;
////////////////////////
if(close_longs)close_longs();
if(close_shorts)close_shorts();
}

void close_shorts()
   {
///////////////////////////////////
if (total==0)close_shorts=false;   
  int totalorders=OrdersTotal();
  for (int i=totalorders-1;i>=0;i--)
     { 
OrderSelect(i,SELECT_BY_POS);
          {
        if (OrderMagicNumber()==magic&&OrderComment()==comment&&OrderSymbol()==Symbol()
&&OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),100,Red);
     }
   }
 return;
 /////////////////////////////////
    }
//+----------------------------------------------------------------------------------+       
   
void close_longs()   
   {
///////////////////////////////////
if (total==0)close_longs=false;   
  int totalorders=OrdersTotal();
  for (int i=totalorders-1;i>=0;i--)
     { 
OrderSelect(i,SELECT_BY_POS);
          {
        if (OrderMagicNumber()==magic&&OrderComment()==comment&&OrderSymbol()==Symbol()
&&OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),100,Blue);
     }
   }
 return;
 /////////////////////////////////
    }
//+----------------------------------------------------------------------------------+ 
didnt work
 
23510:

tried it like this as well :


didnt work

What didn't work? The modify part? Have you checked in journal if it's showing any errors?

 
robofx.org:

What didn't work? The modify part? Have you checked in journal if it's showing any errors?

it takes the first trade but does not close the trade and open a opposite order on a fresh signal...


e.g :


if ma1>ma2 -- Open a buy....time goes bye...now ma1<ma2...soo close the buy and open a sell....etc etc...


hope im making myself understood, im not great at explaining


I havnt been lazy either I have searched 3 forums for stop and revers code but cant find anything of value that will help me understand what i doing wrong

 
23510 wrote >>

it takes the first trade but does not close the trade and open a opposite order on a fresh signal...

e.g :

if ma1>ma2 -- Open a buy....time goes bye...now ma1<ma2...soo close the buy and open a sell....etc etc...

hope im making myself understood, im not great at explaining

I havnt been lazy either I have searched 3 forums for stop and revers code but cant find anything of value that will help me understand what i doing wrong

Hi,

I don't see you calling the checkexit() fuction anywhere from your main program. How does it get's invoked?

One more thing is that you can actually do the closing if the crossing happens in the main program itself instead of calling the checkexit(). i.e. you can actually do a orderclose() inside the main for(cnt=0;cnt<total;cnt++) { } itself by checking the isCrossed variable for any reversal.

for(cnt=0;cnt<total;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())

{

if(OrderType()==OP_BUY) // long position is opened

{
// check for trailing stop
Here you can check whether the isCrossed variable is 2. if it "2" then you need to close it since "2" means a crossing has definitely happened. Similarlyyou can do for if (OrderType() == OP_SELL) as well.

 

if crossed

loop through open orders (decrement)

if order magic == magic number and ordersymbol == symbol

if order type == buy

close buy

open sell

if order type == sell

close sell

open buy

 
if(triggerline<baseline)close_longs=true;
if(triggerline>baseline)close_shorts=true;

If close_longs=true then close_shorts must be set to false. Eventually they will both be true.

Reason: