Please Help, i can't mix order buy and sell together.

 

Please Help

I tried to make first EA.
in onTick
I can make BuyItNow or SellItOut run seperate is OK !
They make good result in M15

I want to change that if BuyItNow make profit < 0 then instant Call SellItOut to make lower risk.
But It's not running SellItOut :'(

Can someone please help, thank you.
This is my code :X

//+------------------------------------------------------------------+
//|                                                    QUENHETDI.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int nextvolume;
bool volumelock=false;
datetime last;
int Ordered=false;
extern int tyleSL_TP=2; // SL:1 TP":2
extern int MagicNumberBuy=1310; // 
extern int MagicNumberSell=2310; // 
extern int fibocyclereset=13;
double fibovolumestring[]={0.01,0.02,0.03,0.05,0.08,0.13,0.21,0.34,0.55,0.89,1.44,2.33};
string debug;
double  profit  = 0 ;
int iOrders=0;
double StopLossOriginal=0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
         EventSetTimer(60);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
//---
      if(last!=Time[0])
      {  
         debug="QHD-DELL-15-5-2021";
         BuyItNow("BUY");
         //SellItOut("SELL");
         Comment(debug);
         last=Time[0];
      }
   
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
  }
//+------------------------------------------------------------------+



void BuyItNow(string comment)
{   
   debug=debug+"\n1. Call for buy";
   debug=debug+"\n2. Total Order Running: "+OrdersTotal();
   debug=debug+"\n3. Current Volume: "+ fibovolumestring[nextvolume];
   debug=debug+"\n4. StopLoss Original: "+StopLossOriginal;
   if(Close[1]>Open[1])
   {
   /*// => cây số 1 đang tăng ---------------------
      if(Close[2]<Open[2])
      {  // nhưng cây phía sau đang giảm
         if(Close[3]<Open[3])
         { // nhưng cây phía sau vẫn đang giảm
            if (Close[4]<Open[4])
            { // và ôi, cây này cũng giảm
              // đánh thôi chứ còn gì nữa*/

                  int selectedcandle=iLowest(Symbol(),Period(),MODE_LOW,4,1);
                  double SL = iLow(Symbol(),Period(),selectedcandle);
                 
                  if ( StopLossOriginal==0 ) StopLossOriginal=SL;
                  
                  double TP = ((Ask-SL)*2)+Ask;
                  double SLSS = iClose(Symbol(),Period(),selectedcandle);
                  double TPSS = SL-(SLSS-SL);   
                  RefreshRates();               

                  if(SL<Ask){
                        debug=debug+"\n5. SL<ASK";
                        if(safemodelock()==false){ // khóa thị trường biến động mạnh
                        if(orderLock()==false) // khóa nếu nhiều hơn 1 lệnh
                        {        
                           getNextVolume();
                           int OrderTicket=OrderSend(Symbol(),OP_BUY,fibovolumestring[nextvolume],Ask,0,SL,TP,comment,MagicNumberBuy);
 
                        }
                    }
                  }
            }
//       }
//     }
//  }
}


void SellItOut(string comment)
{   
   debug=debug+"\n1. Call for sell";
   debug=debug+"\n2. Total Running Order: "+OrdersTotal();
   debug=debug+"\n3. Current Volume: "+ fibovolumestring[nextvolume];
   debug=debug+"\n4. StopLoss Original: "+StopLossOriginal;
   if(Close[1]<Open[1])
   {
   // => cây số 1 đang tăng ---------------------
      if(Close[2]>Open[2])
      {  // nhưng cây phía sau đang giảm
         if(Close[3]>Open[3])
         { // nhưng cây phía sau vẫn đang giảm
            if (Close[4]>Open[4])
            { // và ôi, cây này cũng giảm
              // đánh thôi chứ còn gì nữa*/
//                simply do not use BID with Buy or ASK with sell
                  int selectedcandle=iHighest(Symbol(),Period(),MODE_HIGH,4,1);
                  double SL = iHigh(Symbol(),Period(),selectedcandle);
                  double TP = Ask-((SL - Ask)*2 );   
                  double price;
                  RefreshRates();
                  
                  if(SL>Ask){
                        debug=debug+"\n5. SL>ASK";
                        if(orderLock()==false) // khóa nếu nhiều hơn 1 lệnh
                        {        
                           getNextVolume();
                           OrderSend(Symbol(),OP_SELL,fibovolumestring[nextvolume],Close[1],0,SL+2,TP,comment,MagicNumberSell);
 
                        }
                  }
            }
         }
      }
   }
}

bool orderLock()
{ // I just want 1 order running
   if(OrdersTotal()>0) return true;
   else return false;
}

bool safemodelock()
{
   debug=debug+"\n6. CheckSafemode: "  +(Ask+"-"+Bid+":"+(Ask-Bid)*100);
   if((Ask-Bid)*100>50)
   {
      return true;
   }
   else
   {
      return false;
   }
}

int getNextVolume()
{
   int ticket      =-1;
   datetime last_time   = 0;
   for(int i = OrdersHistoryTotal(); i>=0; i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderSymbol()==_Symbol && OrderCloseTime()>last_time)
      {
         last_time = OrderCloseTime();
         ticket = OrderTicket();
      }
   }
   if(!OrderSelect(ticket,SELECT_BY_TICKET))
   {
      Print("OrderSelectError: ",GetLastError());
      return 0.0;
   }   

   if (OrderProfit()<0 && (OrderMagicNumber()==MagicNumberBuy || OrderMagicNumber()==MagicNumberSell))
   {
      // Order profit < 0 I have to call Sell
      nextvolume+=1;
      debug=debug+"\n"+OrderProfit()+" >> next volume: "+fibovolumestring[nextvolume];
      if(nextvolume==13) nextvolume=0; // loop for fibonaci volume
      
   }
   else if(OrderProfit()>0 && (OrderMagicNumber()==MagicNumberBuy || OrderMagicNumber()==MagicNumberSell))
   {
      nextvolume=0;
      StopLossOriginal=0;
      debug=debug+"\n"+OrderProfit()+" << next volume: "+fibovolumestring[nextvolume];
   }


return nextvolume;

}


 

Use Code button Alt+S And then ask for help what do you want

like this

I want to change that if BuyItNow make profit < 0 then instant Call SellItOut to make lower risk.
But It's not running SellItOut :'(

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

//|                                                    QUENHETDI.mq4 |

//|                        Copyright 2021, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2021, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict


int nextvolume;

bool volumelock=false;

datetime last;

int Ordered=false;

extern int tyleSL_TP=2; // SL:1 TP":2

extern int MagicNumberBuy=1310; // 

extern int MagicNumberSell=2310; // 

extern int fibocyclereset=13;

double fibovolumestring[]={0.01,0.02,0.03,0.05,0.08,0.13,0.21,0.34,0.55,0.89,1.44,2.33};

string debug;

double  profit  = 0 ;

int iOrders=0;

double StopLossOriginal=0;


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

//| Expert initialization function                                   |

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

int OnInit()

  {

//--- create timer

         EventSetTimer(60);

   

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//--- destroy timer

   EventKillTimer();

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

  

//---

      if(last!=Time[0])

      {  

         debug="QHD-DELL-15-5-2021";

         BuyItNow("BUY");

//         SellItOut("SELL");

         Comment(debug);

         last=Time[0];

      }

   

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//---

   

  }

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

//| Tester function                                                  |

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

double OnTester()

  {

//---

   double ret=0.0;

//---


//---

   return(ret);

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---

   

  }

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




void BuyItNow(string comment)

{   

   debug=debug+"\n1. Call for buy";

   debug=debug+"\n2. Total Order Running: "+OrdersTotal();

   debug=debug+"\n3. Current Volume: "+ fibovolumestring[nextvolume];

   debug=debug+"\n4. StopLoss Original: "+StopLossOriginal;

   if(Close[1]>Open[1])

   {

   /*// => cây số 1 đang tăng ---------------------

      if(Close[2]<Open[2])

      {  // nhưng cây phía sau đang giảm

         if(Close[3]<Open[3])

         { // nhưng cây phía sau vẫn đang giảm

            if (Close[4]<Open[4])

            { // và ôi, cây này cũng giảm

              // đánh thôi chứ còn gì nữa*/


                  int selectedcandle=iLowest(Symbol(),Period(),MODE_LOW,4,1);

                  double SL = iLow(Symbol(),Period(),selectedcandle);

                 

                  if ( StopLossOriginal==0 ) StopLossOriginal=SL;

                  

                  double TP = ((Ask-SL)*2)+Ask;

                  double SLSS = iClose(Symbol(),Period(),selectedcandle);

                  double TPSS = SL-(SLSS-SL);   

                  RefreshRates();               


                  if(SL<Ask){

                        debug=debug+"\n5. SL<ASK";

                        if(safemodelock()==false){ // khóa thị trường biến động mạnh

                        if(orderLock()==false) // khóa nếu nhiều hơn 1 lệnh

                        {        

                           getNextVolume();

                           int OrderTicket=OrderSend(Symbol(),OP_BUY,fibovolumestring[nextvolume],Ask,0,SL,TP,comment,MagicNumberBuy);

 

                        }

                    }

                  }

            }

//       }

//     }

//  }

}



void SellItOut(string comment)

{   

   debug=debug+"\n1. Call for sell";

   debug=debug+"\n2. Total Running Order: "+OrdersTotal();

   debug=debug+"\n3. Current Volume: "+ fibovolumestring[nextvolume];

   debug=debug+"\n4. StopLoss Original: "+StopLossOriginal;

   if(Close[1]<Open[1])

   {

   /*/ => cây số 1 đang tăng ---------------------

      if(Close[2]>Open[2])

      {  // nhưng cây phía sau đang giảm

         if(Close[3]>Open[3])

         { // nhưng cây phía sau vẫn đang giảm

            if (Close[4]>Open[4])

            { // và ôi, cây này cũng giảm

              // đánh thôi chứ còn gì nữa*/

//                simply do not use BID with Buy or ASK with sell

                  int selectedcandle=iHighest(Symbol(),Period(),MODE_HIGH,4,1);

                  double SL = iHigh(Symbol(),Period(),selectedcandle);

                  double TP = Ask-((SL - Ask)*2 );   

                  double price;

                  RefreshRates();

                  

                  if(SL>Ask){

                        debug=debug+"\n5. SL>ASK";

                        if(orderLock()==false) // khóa nếu nhiều hơn 1 lệnh

                        {        

                           getNextVolume();

                           OrderSend(Symbol(),OP_SELL,fibovolumestring[nextvolume],Close[1],0,SL+2,TP,comment,MagicNumberSell);

 

                        }

                  }

            }

//         }

  //    }

  // }

}


bool orderLock()

{ // I just want 1 order running

   if(OrdersTotal()>0) return true;

   else return false;

}


bool safemodelock()

{

   debug=debug+"\n6. CheckSafemode: "  +(Ask+"-"+Bid+":"+(Ask-Bid)*100);

   if((Ask-Bid)*100>50)

   {

      return true;

   }

   else

   {

      return false;

   }

}


int getNextVolume()

{

   int ticket      =-1;

   datetime last_time   = 0;

   for(int i = OrdersHistoryTotal(); i>=0; i--)

   {

      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderSymbol()==_Symbol && OrderCloseTime()>last_time)

      {

         last_time = OrderCloseTime();

         ticket = OrderTicket();

      }

   }

   if(!OrderSelect(ticket,SELECT_BY_TICKET))

   {

      Print("OrderSelectError: ",GetLastError());

      return 0.0;

   }   


   if (OrderProfit()<0 && (OrderMagicNumber()==MagicNumberBuy || OrderMagicNumber()==MagicNumberSell))

   {

      // Order profit < 0 I have to call Sell

      nextvolume+=1;

      debug=debug+"\n"+OrderProfit()+" >> next volume: "+fibovolumestring[nextvolume];

      if(nextvolume==13) nextvolume=0; // loop for fibonaci volume

      RefreshRates();

      SellItOut("SELL NOW");

      

   }

   else if(OrderProfit()>0 && (OrderMagicNumber()==MagicNumberBuy || OrderMagicNumber()==MagicNumberSell))

   {

      nextvolume=0;

      StopLossOriginal=0;

      debug=debug+"\n"+OrderProfit()+" << next volume: "+fibovolumestring[nextvolume];

   }



return nextvolume;


}
 
Thank you, it's look clear now ^^
Reason: