How to check if a position is open?

 

Hello,

I'm stuck with what should be an easy problem to solve, apparently, but I'm going in circle without finding the answer (read a lot of thread and MQL documentation).


I want to check if a specific position is currently opened by using either the magic number or the order ticket of the OrderSend() request that potentially opened this position.


Here is the code :


#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
#define EXPERT_MAGIC 1
CTrade myTradingControlPanel;
double currentAsk;
double StopLose, TakeProfit, Size;
ulong ticketM1;


//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

   currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK); // Get latest Ask Price


               if(???????) // DON'T HAVE A POSITION OPEN WITH THIS EXPERT_MAGIC OR TICKET NUMBER
                  {
                  StopLose = xxxx;
                  TakeProfit = xxxx;
                  Size = xxxx;

                  MqlTradeRequest request;
                  request.action = TRADE_ACTION_DEAL;
                  request.magic = EXPERT_MAGIC;
                  request.symbol = _Symbol;
                  request.volume = Size;
                  request.type = ORDER_TYPE_BUY;
                  request.type_filling = ORDER_FILLING_FOK; 
                  request.price = currentAsk;
                  request.sl = StopLose;
                  request.tp = TakeProfit;
                  MqlTradeResult result;
                  
                  myTradingControlPanel.OrderSend(request,result);

                  ticketM1 = result.order;
                  }
  }
//+------------------------------------------------------------------+


I tried something with the magic number and the ticker number like this, no error is report in the code but it's doesn't work as wished :


        first try :    

if(PositionSelectByTicket(ticketM1) == true) // Have position opened

        second try :

if(OrderSelect(ticketM1) == true) // Have position opened

        third try :

long magic = OrderGetInteger(ORDER_MAGIC);
if(magic == EXPERT_MAGIC) // Have position opened


Can you explain me like I'm 5 year old how to code this thing?


Thank you.

 
Hi!
You can check like this
for (int i = 0; i < PositionsTotal(); i++) {
   if (PositionSelectByTicket(PositionGetTicket(i) && PositionGetInteger(POSITION_MAGIC) == EXPERT_MAGIC) {
// your code
}
}
 
Anton Ohurtsov #:
Hi!
You can check like this
for (int i = 0; i < PositionsTotal(); i++) {
   if (PositionSelectByTicket(PositionGetTicket(i) && PositionGetInteger(POSITION_MAGIC) == EXPERT_MAGIC) {
// your code
}
}


I tried as you say but now in the back test the EA doesn't take any trade. No error reported, just never open a trade.


#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
#define EXPERT_MAGIC 1
CTrade myTradingControlPanel;
double currentAsk;
double StopLose, TakeProfit, Size;


//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
                   
   currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   

               for (int i = 0; i < PositionsTotal(); i++)
                  {
                  if(PositionSelectByTicket(PositionGetTicket(i)) && PositionGetInteger(POSITION_MAGIC) == EXPERT_MAGIC)
                     {
                     }
                  else 
                     {
                     StopLose = xxxx;
                     TakeProfit = xxxx;
                     Size = xxxx;

                     MqlTradeRequest request;
                     request.action = TRADE_ACTION_DEAL;
                     request.magic = EXPERT_MAGIC;
                     request.symbol = _Symbol;
                     request.volume = Size;
                     request.type = ORDER_TYPE_BUY;
                     request.type_filling = ORDER_FILLING_FOK; 
                     request.price = currentAsk;
                     request.sl = StopLose;
                     request.tp = TakeProfit;
                     MqlTradeResult result;
                  
                     myTradingControlPanel.OrderSend(request,result);
                     }
  }
//+------------------------------------------------------------------+
 
CrokCrypto #:


I tried as you say but now in the back test the EA doesn't take any trade. No error reported, just never open a trade.


Excuse me, but I did not understand, what do you want to do by your code.
 
Anton Ohurtsov #:
Excuse me, but I did not understand, what do you want to do by your code.


My EA can open a position in different scenarii.

I allow my EA to open only one position at a time per scenarii. So for example if I have 5 scenarii then the EA can send 5 different OrderSend() for open a position each one on top of each other.

So that's means : if the conditions are meet for open a position in one of this scenarii BUT there is already a position opened before because this scenarii and this position is still open : then I not allow my EA to send the order for add to this position.


Not sure I'm really clear in my explanation... Tell me if it's the case I will try to explain differently.

 
CrokCrypto #:


My EA can open a position in different scenarii.

I allow my EA to open only one position at a time per scenarii. So for example if I have 5 scenarii then the EA can send 5 different OrderSend() for open a position each one on top of each other.

So that's means : if the conditions are meet for open a position in one of this scenarii BUT there is already a position opened before because this scenarii and this position is still open : then I not allow my EA to send the order for add to this position.


Not sure I'm really clear in my explanation... Tell me if it's the case I will try to explain differently.

Ok.
I would like to recommend you use different magic numbers for different scenarios.
Also I would like to recommend you to have method of checking for positions absence by certain magic number. If such method returns true, then you can allow EA to open position.
 
Anton Ohurtsov #:
Ok.
I would like to recommend you use different magic numbers for different scenarios.
Also I would like to recommend you to have method of checking for positions absence by certain magic number. If such method returns true, then you can allow EA to open position.


Yes, the code I posted here is simplified to show only one scenarii, but I have defined several Magic numbers for each scenarii that are implemented during the Mqltraderequest orderSend() of each scenarii.


#include <Trade\Trade.mqh>
#define EXPERT_MAGICM1 1
#define EXPERT_MAGICM2 2
CTrade myTradingControlPanel;
double currentAsk;
double StopLose, TakeProfit, Size;

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
                   
   currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK); // Get latest Ask Price
   

   if(bunch of condition and then the last one is : no position already opened with this specific magic number)
               {
               for (int i = 0; i < PositionsTotal(); i++)
                  {
                  if(PositionSelectByTicket(PositionGetTicket(i)) && PositionGetInteger(POSITION_MAGIC) == EXPERT_MAGICM1)
                     {
                     }
                  else
                     {
                     StopLose = xxx;
                     TakeProfit = xxx;
                     Size = xxx; 

                     MqlTradeRequest request;
                     request.action = TRADE_ACTION_DEAL;
                     request.magic = EXPERT_MAGICM1;
                     request.symbol = _Symbol;
                     request.volume = Size;
                     request.type = ORDER_TYPE_BUY;
                     request.type_filling = ORDER_FILLING_FOK; 
                     request.price = currentAsk;
                     request.sl = StopLose;
                     request.tp = TakeProfit;
                     MqlTradeResult result;
                  
                     myTradingControlPanel.OrderSend(request,result);
                     }
                  }

   if(another bunch of condition and then the last one is : no position already opened with this specific magic number)
               {
               for (int i = 0; i < PositionsTotal(); i++)
                  {
                  if(PositionSelectByTicket(PositionGetTicket(i)) && PositionGetInteger(POSITION_MAGIC) == EXPERT_MAGICM2)
                     {
                     }
                  else
                     {
                     StopLose = xxx;
                     TakeProfit = xxx;
                     Size = xxx; 

                     MqlTradeRequest request;
                     request.action = TRADE_ACTION_DEAL;
                     request.magic = EXPERT_MAGICM2;
                     request.symbol = _Symbol;
                     request.volume = Size;
                     request.type = ORDER_TYPE_BUY;
                     request.type_filling = ORDER_FILLING_FOK; 
                     request.price = currentAsk;
                     request.sl = StopLose;
                     request.tp = TakeProfit;
                     MqlTradeResult result;
                  
                     myTradingControlPanel.OrderSend(request,result);
                     }
                  }
  }
//+------------------------------------------------------------------+


I don't understand how to " have method of checking for positions absence by certain magic number. If such method returns true, then you can allow EA to open position."

 
CrokCrypto #:


Yes, the code I posted here is simplified to show only one scenarii, but I have defined several Magic numbers for each scenarii that are implemented during the Mqltraderequest orderSend() of each scenarii.



I don't understand how to " have method of checking for positions absence by certain magic number. If such method returns true, then you can allow EA to open position."


You can write method, for example, IsAbsentPositions(ulong magicNumber), which checks that there no positions with magicNumber.
 
Anton Ohurtsov #:

You can write method, for example, IsAbsentPositions(ulong magicNumber), which checks that there no positions with magicNumber.

 

That's the part of identifying a position by magic number that i struggle with. It always seems to not work properly.

 
CrokCrypto #:

 

That's the part of identifying a position by magic number that i struggle with. It always seems to not work properly.

Try to use one loop for identifying and after loop ending open or not open position.
 
Anton Ohurtsov #:
Try to use one loop for identifying and after loop ending open or not open position.


Okay, so I try to code something as you said. No error on the compilation, but when I run it in the strategy tester, I get an "invalid request" error. But I didn't change anything on my OrderSend() request, and it was working perfectly fine before.


Obviously I'm still missing something, what is it?





#include <Trade\Trade.mqh>
#define EXPERT_MAGICM1 1
#define EXPERT_MAGICM2 2
CTrade myTradingControlPanel;
double currentAsk;
double StopLose, TakeProfit, Size;
bool HavePosition;

  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
                   
   currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK);


            if (bunch of condition)
               {
               for(int v = PositionsTotal() - 1;v >= 0 || HavePosition == true ; v--)
                  {
                  ulong positionticket = PositionGetTicket(v);
                  if(PositionSelectByTicket(positionticket))
                     {
                     if(PositionGetInteger(POSITION_MAGIC) == EXPERT_MAGICM1)
                        {
                        HavePosition = true;
                        }
                     else
                        {
                        HavePosition = false;
                        }
                     }
                   }
                if(HavePosition == false)
                  {
                  StopLose = xxx;
                  TakeProfit = xxx; 
                  SizeB = xxx; 

                  MqlTradeRequest request;
                  request.action = TRADE_ACTION_DEAL;
                  request.magic = EXPERT_MAGICM1;
                  request.symbol = _Symbol;
                  request.volume = Size;
                  request.type = ORDER_TYPE_BUY;
                  request.type_filling = ORDER_FILLING_FOK; 
                  request.price = currentAsk;
                  request.sl = StopLose;
                  request.tp = TakeProfit;
                  MqlTradeResult result;
                  
                  myTradingControlPanel.OrderSend(request,result);
                  }
               }


            if (bunch of condition)
               {
               for(int v = PositionsTotal() - 1;v >= 0 || HavePosition == true; v--)
                  {
                  ulong positionticket = PositionGetTicket(v);
                  if(PositionSelectByTicket(positionticket))
                     {
                     if(PositionGetInteger(POSITION_MAGIC) == EXPERT_MAGICM2)
                        {
                        HavePosition = true;
                        }
                     else
                        {
                        HavePosition = false;
                        }
                     }
                   }
                if(HavePosition == false)
                  {
                  StopLose = xxx;
                  TakeProfit = xxx; 
                  SizeB = xxx; 
                  
                  MqlTradeRequest request;
                  request.action = TRADE_ACTION_DEAL;
                  request.magic = EXPERT_MAGICM2;
                  request.symbol = _Symbol;
                  request.volume = Size;
                  request.type = ORDER_TYPE_SELL;
                  request.type_filling = ORDER_FILLING_FOK; 
                  request.price = currentAsk;
                  request.sl = StopLose;
                  request.tp = TakeProfit;
                  MqlTradeResult result;
                  
                  myTradingControlPanel.OrderSend(request,result);
                  }
               }
  }
//+------------------------------------------------------------------+
Reason: