how can i share priorities on OnTick function?

 
Hello and thanks for you time! I would like to me a question about the the OnTick function, the goal is to open 10 buy, then put 10 sell stop, if buy hit tp close the sell stop, 

if close sell stop open 10 sell, any idea how can i achive that? Like this now i can not beacuse all ifs stucks simultaneously, how can i put them in order and start making scenarios?


//+------------------------------------------------------------------+
//|                                               HedgingProject.mq5 |
//|                            Copyright 10/05/2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>

input double InpLots = 0.01;
input double InpTakeProfit = 1000.0;
input double InpDistance = 2000.0;
input int InpPosOpen = 10;

input int InpMagicNumber = 549687;

MqlTick currentTick, previousTick;
CTrade trade;
bool tpHit = false;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit(){

   // set magicnumber
   trade.SetExpertMagicNumber(InpMagicNumber);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(){
  
   // count open positions
   int cntBuy, cntSell;
   if(!CountOpenPositions(cntBuy,cntSell)){return;}

   // count open positions
   int cntBuyOrder, cntSellOrder;
   if(!CountOpenOrders(cntBuyOrder,cntSellOrder)){return;}               
         
      // check for buy position
   if(cntBuy < 10){

      // calculate tp buy
      double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      ask = NormalizeDouble(ask,_Digits);
      double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
      bid = NormalizeDouble(bid,_Digits);
                  
      double tpB = bid + 1000 * _Point;  
      tpB = NormalizeDouble(tpB,_Digits);       

      for (int i = 0; i < 10; i++) {//we open 10 position ine one tick
      trade.Buy(InpLots,_Symbol,ask,0,tpB);
      }
   }         
   if(cntBuy == 10){ 
      double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
      bid = NormalizeDouble(bid,_Digits);                 
      // calculate opposite side for sell orders
      double oppositeSide = bid - 2000 * _Point;
      // calculate tp sell
      double tpS = oppositeSide - 1000 * _Point;  
      tpS = NormalizeDouble(tpS,_Digits); 
      for (int i = 0; i < 10; i++) {//we open 10 position ine one tick               
      trade.SellStop(InpLots,oppositeSide,_Symbol,0,tpS);                           
      
      }
   }    
   if (cntBuy == 0){
      CloseOrders();   
   }   
   if(cntSellOrder ==0 ){

      // calculate tp buy
      double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      ask = NormalizeDouble(ask,_Digits);
      double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
      bid = NormalizeDouble(bid,_Digits);
                  
      double tpS = ask - 1000 * _Point;  
      tpS = NormalizeDouble(tpS,_Digits);       

      for (int i = 0; i < 10; i++) {//we open 10 position ine one tick
      trade.Sell(InpLots,_Symbol,bid,0,tpS);         
      }
   }
}   
                               


// count open orders
bool CountOpenOrders(int &cntBuyOrder, int &cntSellOrder){

   cntBuyOrder   = 0;
   cntSellOrder  = 0;
   int total = OrdersTotal();
   for(int i=total-1; i>=0; i--){
      ulong ticket = OrderGetTicket(i);
      if(ticket<=0){Print("Failed to get order ticket"); return false;}
      long magic;
      if(!OrderGetInteger(ORDER_MAGIC,magic)){Print("Failed to get order magic number"); return false;}
      if(magic==InpMagicNumber){
         long type;
         if(!OrderGetInteger(ORDER_TYPE,type)){Print("Failed to get order type"); return false;}
         if(type==ORDER_TYPE_BUY){cntBuyOrder++;}
         if(type==ORDER_TYPE_SELL){cntSellOrder++;}      
      }
   }      
   return true;
}

// count open positions
bool CountOpenPositions(int &cntBuy, int &cntSell){

   cntBuy   = 0;
   cntSell  = 0;
   int total = PositionsTotal();
   for(int i=total-1; i>=0; i--){
      ulong ticket = PositionGetTicket(i);
      if(ticket<=0){Print("Failed to get position ticket"); return false;}
      if(!PositionSelectByTicket(ticket)){Print("Failed to get position"); return false;}
      long magic;
      if(!PositionGetInteger(POSITION_MAGIC,magic)){Print("Failed to get position magic number"); return false;}
      if(magic==InpMagicNumber){
         long type;
         if(!PositionGetInteger(POSITION_TYPE,type)){Print("Failed to get position type"); return false;}
         if(type==POSITION_TYPE_BUY){cntBuy++;}
         if(type==POSITION_TYPE_SELL){cntSell++;}      
      }
   }      
   return true;
}


// close all positions
bool ClosePositions(){
      
   int total = PositionsTotal();
   for(int i=total-1; i>=0; i--){
      if(total!=PositionsTotal()){total=PositionsTotal(); i=total; continue;}
      ulong ticket = PositionGetTicket(i); // Select position
      if(ticket<=0){Print("Failed to get position ticket"); return false;}
      if(!PositionSelectByTicket(ticket)){Print("Failed to select psition by ticket"); return false;}
      long magicnumber;
      if(!PositionGetInteger(POSITION_MAGIC,magicnumber)){Print("Failed to get psition magicnumber"); return false;}
      if(magicnumber == InpMagicNumber && PositionGetString(POSITION_SYMBOL)==_Symbol){
         trade.PositionClose(ticket);
         if(trade.ResultRetcode()!=TRADE_RETCODE_DONE){
            Print("Failed to close position. Result: "+(string)trade.ResultRetcode()+":"+trade.ResultRetcodeDescription());
            return false;
         }     
      }  
   }   
   return true;
}
// close all positions
bool CloseOrders(){
   
   int total = OrdersTotal();
   for(int i=total-1; i>=0; i--){
      if(total!=OrdersTotal()){total=OrdersTotal(); i=total; continue;}
      ulong ticket = OrderGetTicket(i); // Select position
      if(ticket<=0){Print("Failed to get order ticket"); return false;}
      long magicnumber;
      if(!OrderGetInteger(ORDER_MAGIC,magicnumber)){Print("Failed to get order magicnumber"); return false;}
      if(magicnumber == InpMagicNumber && OrderGetString(ORDER_SYMBOL)==_Symbol){
         trade.OrderDelete(ticket);     
      }  
   }   
   return true;
}

// Check if take profit hit
void CheckTakeProfitHit(){

   int total = PositionsTotal();
   for(int i=total-1; i>=0; i--){
      ulong ticket = PositionGetTicket(i);
      if(ticket<=0){Print("Failed to get position ticket"); continue;}
      if(!PositionSelectByTicket(ticket)){Print("Failed to select position by ticket"); continue;}
      
      double takeProfit = PositionGetDouble(POSITION_TP);
      double currentPrice;
      
      // Determine current price based on position type
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
          currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      else
          currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && currentPrice >= takeProfit){
          // Take profit hit for buy position
          tpHit = true;
          return;
      }
      else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL && currentPrice <= takeProfit){
          // Take profit hit for sell position
          tpHit = true;
          return;
      }
   }
}
 
onTradeTransaction is the standard approach to achieve that kind of things.
 
Yashar Seyyedin #:
onTradeTransaction is the standard approach to achieve that kind of things.

thx a lot!

Reason: