Download MetaTrader 5

Reverse Trade Needed of The opposite signal

To add comments, please log in or register
Having troubles understanding the code? Articles can help you!
Vantho Sokphal
21
Vantho Sokphal 2016.09.20 14:44 
Please somebody help me on this one.

 I want it close the current order and open a reverse order when an opposite signal appear.

but This EA close the order and no new reverse order.

please give me some advice and some code to correct it.
//+------------------------------------------------------------------+
//|                                                        Test2.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com 
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Parameter                                 |
//+------------------------------------------------------------------+
extern string EAName = " Trending Mind";
extern double Lots = 0.1;
extern double TakeProfit = 100;
extern double StopLoss = 30;
extern bool EnterOpenBar = true;
extern bool OppositeClose = true;
extern double MagicNumber = 59483;
extern int Slippage = 8;
int vSlippage;
double ticket, number, vPoint;
bool Result;
int OnInit()
  {if(Digits==3 || Digits==5)
{ vPoint=Point*10; vSlippage=Slippage*10; }
else{ vPoint=Point; vSlippage=Slippage; } 
    return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      static int last_trade=0;
      bool    OpenBar=true;
      double ArrowUP = iCustom(NULL,0,"TVA",0,1);
      double ArrowDown = iCustom(NULL,0,"TVA",1,1);
      double Potential = iCustom(NULL,0,"TVA",2,1);
      int OType=-1;
      int Ticket;
      double Price, SL, TP;
      bool CloseBuy=false, CloseSell=false, OpenBuy=false, OpenSell=false;
      for(int Counter=1; Counter<=OrdersTotal(); Counter++)
      {if (OrderSelect(Counter-1,SELECT_BY_POS)==true)
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
      {Ticket=OrderTicket();
      OType =OrderType();
      Price =OrderOpenPrice();
      SL =OrderStopLoss();
      TP =OrderTakeProfit();
      Lots =OrderLots();
   } }
     // Entry Condition    
     if(EnterOpenBar) if(iVolume(NULL,0,0)>1) OpenBar=false;
      {            
  if(ArrowUP == Potential && ArrowDown != Potential &&  OpenBar &&last_trade <= 0)
  { OpenBuy = true;
      last_trade =1 ;
    if (OppositeClose) CloseSell=true;                                 
        }                               
             if(ArrowDown == Potential && ArrowUP != Potential && OpenBar && last_trade >= 0){
                  OpenSell = true;last_trade=-1;
                 if (OppositeClose) CloseBuy=true;                 
                } }                            
        while(true)
      {
      if (OType==0 && CloseBuy==true)    {close (OP_BUY); // Close Buy
         return;
            }if (OType==1 && CloseSell==true)
            { close (OP_SELL); // Close Sell 
            return;} break;     }
      
      while(true)
{
if (OrdersTotalMagicOpen()==0 && OpenBuy==true)
{if(StopLoss>0){SL=Bid - StopLoss*vPoint;}else{SL=0;} if(TakeProfit>0){TP=Bid+TakeProfit*vPoint;}else{TP=0;}
ticket=0;number=0;while(ticket<=0 && number<100){
RefreshRates();ticket = OrderSend(Symbol(),OP_BUY,NormalizeDouble(Lots,Digits), Ask,vSlippage,SL,TP,EAName, MagicNumber, 0, Green);}}
if (OrdersTotalMagicOpen()==0 && OpenSell==true)
{if(StopLoss>0){SL=Ask + StopLoss*vPoint;}else{SL=0;} if(TakeProfit>0){TP=Ask-TakeProfit*vPoint;}else{TP=0;}
ticket=0;number=0;while(ticket<=0 && number<100){
RefreshRates();ticket= OrderSend(Symbol(),OP_SELL, NormalizeDouble(Lots,Digits), Bid,vSlippage,SL,TP, EAName, MagicNumber, 0, Red);
}}break;}    
  }
  
  void close(int type){
  int Counter;
if(OrdersTotal()>0){
for( Counter=OrdersTotal()-1;Counter>=0;Counter--){
 Result = OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES);
if(type==OP_BUY && OrderType()==OP_BUY){
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) {
RefreshRates(); 
Result = OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits), vSlippage);
} }
if(type==OP_SELL && OrderType()==OP_SELL){
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) {
RefreshRates(); 
Result = OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),vSlippage); 
}}
}}}
int OrdersTotalMagicOpen() {
int l_count_0 = 0;
for (int l_pos_4 = OrdersTotal() - 1; l_pos_4 >= 0; l_pos_4--) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
if (OrderType() == OP_SELL || OrderType() == OP_BUY) l_count_0++;
}
return (l_count_0);
}
//+------------------------------------------------------------------+
To add comments, please log in or register