See the orders open

 

Hi,

I´m trying to understand how I can now if I have one position open like. Because If I have a open position I will wating the stoploss or takeprofit close the order before open a new.

I´m using this code in the backtest to see the result, but all the time the PositionsTotal show 0. 

Example:

void OnTick()
  {
        CTrade trade;

         if (PositionsTotal() == 0)
         {
                trade.BuyStop (100,Ask,_Symbol,StopLoss,TakeProfit,ORDER_TIME_GTC,0,"Teste Buy");  
		Alert("no position");    
         }
	 else
	{
		Alert("I have -", PositionsTotal());
	}

  }
 
You can't do a BuyStop at market (only a buy.) You would know this, if you Check your return codes for errors, and report them
 
"BuyStop" is a trading team to place a PENDING ORDER, not a POSITION.
 
William Roeder:
You can't do a BuyStop at market (only a buy.) You would know this, if you Check your return codes for errors, and report them

Hi Willian Tks,

But I don´t understand why I can send a BuyStop? 

I don´t receive any error, the order is okay.

 
Vladimir Karputov:
"BuyStop" is a trading team to place a PENDING ORDER, not a POSITION.

Hi Vladimir,

What is the difference between Pending Order and Position? 

How I can see if I have a Pending Order open?

 
Cesar :

Hi Vladimir,

What is the difference between Pending Order and Position? 

How I can see if I have a Pending Order open?

Help: Executing Trades - Basic Principles

  • An order is an instruction given to a broker to buy or sell a financial instrument. There are two main  types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels.
  • A deal is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the supply price (Bid). A deal can be opened as a result of market order execution or pending order triggering. Note that in some cases, execution of an order can result in several deals.
  • A position is a trade obligation, i.e. the number of bought or sold contracts of a financial instrument. A long position is financial security bought expecting the security price go higher. A short position is an obligation to supply a security expecting the price will fall in future.


More about pending orders: Pending Order #

Types of pending orders

Current market state

— current market state

Forecast

— forecast

Current price

— current price

Order price

— order price

Price, reaching which a pending order will be placed

— price, reaching which a pending order will be placed

Expected growth

— expected growth

Expected fall

— expected fall



I will post the code for calculating positions and pending orders later ...

Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
 

Code:

//+------------------------------------------------------------------+
//|                       Calculate Positions and Pendong Orders.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\OrderInfo.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
COrderInfo     m_order;                      // object of COrderInfo class
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int buys=0,sells=0;
   int buy_limits=0,sell_limits=0,buy_stops=0,sell_stops=0;

   CalculateAllPositions(buys,sells);
   CalculateAllPendingOrders(buy_limits,sell_limits,buy_stops,sell_stops);

   string text="BUY: "+IntegerToString(buys)+", SELL: "+IntegerToString(sells)+"\n"+
               "Buy limits "+IntegerToString(buy_limits)+", Sell limits "+IntegerToString(sell_limits)+
               ", Buy stops "+IntegerToString(buy_stops)+", Sell stops "+IntegerToString(sell_stops);
   Comment(text);

  }
//+------------------------------------------------------------------+
//| Calculate all positions Buy and Sell                             |
//+------------------------------------------------------------------+
void CalculateAllPositions(int &count_buys,int &count_sells)
  {
   count_buys=0;
   count_sells=0;

   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         //if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
        {
         if(m_position.PositionType()==POSITION_TYPE_BUY)
            count_buys++;

         if(m_position.PositionType()==POSITION_TYPE_SELL)
            count_sells++;
        }
//---
   return;
  }
//+------------------------------------------------------------------+
//| Calculate all pending orders                                     |
//+------------------------------------------------------------------+
void CalculateAllPendingOrders(int &count_buy_limits,int &count_sell_limits,int &count_buy_stops,int &count_sell_stops)
  {
   count_buy_limits  = 0;
   count_sell_limits = 0;
   count_buy_stops   = 0;
   count_sell_stops  = 0;

   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders
      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties
         //if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==InpMagic)
        {
         if(m_order.OrderType()==ORDER_TYPE_BUY_LIMIT)
            count_buy_limits++;
         else
            if(m_order.OrderType()==ORDER_TYPE_SELL_LIMIT)
               count_sell_limits++;
            else
               if(m_order.OrderType()==ORDER_TYPE_BUY_STOP)
                  count_buy_stops++;
               else
                  if(m_order.OrderType()==ORDER_TYPE_SELL_STOP)
                     count_sell_stops++;
        }
  }
//+------------------------------------------------------------------+


Open positions: GBPUSD BUY 0.03 lot and USDPLN SELL 0.01

Pending orders are placed: USDPLN Sell limit 0.01 and GBPUSD Buy limit 0.03


Calculate Positions and Pendong Orders

 

Hi Vladmir


Tks for explanation its help me a lot.


But I tried to user youd code to see the pending order but I can´t.


//+------------------------------------------------------------------+
//|                       Calculate Positions and Pendong Orders.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\OrderInfo.mqh>
#include <Trade\Trade.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
COrderInfo     m_order;                      // object of COrderInfo class
int x = 1;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

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

   CTrade trade;

   int buys=0,sells=0;
   int buy_limits=0,sell_limits=0,buy_stops=0,sell_stops=0;

   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   if (x == 1) {
   
        double StopLoss_pt = 200; 
        double TakeProfit_pt = 300; 
        double StopLoss = NormalizeDouble(Ask-(Ask*0.20),2); 
        double TakeProfit = NormalizeDouble(Ask+(Ask*0.10),2); 
            
        trade.BuyStop (100,Ask,_Symbol,StopLoss,TakeProfit,ORDER_TIME_GTC,0,"Test Buy-Stop");
      
        x = 0;
   }
   
   CalculateAllPositions(buys,sells);
   CalculateAllPendingOrders(buy_limits,sell_limits,buy_stops,sell_stops);

   string text="BUY: "+IntegerToString(buys)+", SELL: "+IntegerToString(sells)+"\n"+
              "Buy limits "+IntegerToString(buy_limits)+", Sell limits "+IntegerToString(sell_limits)+
              ", Buy stops "+IntegerToString(buy_stops)+", Sell stops "+IntegerToString(sell_stops);
   Alert(text);

  }
//+------------------------------------------------------------------+
//| Calculate all positions Buy and Sell                             |
//+------------------------------------------------------------------+
void CalculateAllPositions(int &count_buys,int &count_sells)
  {
   count_buys=0;
   count_sells=0;

   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         //if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
        {
         if(m_position.PositionType()==POSITION_TYPE_BUY)
            count_buys++;

         if(m_position.PositionType()==POSITION_TYPE_SELL)
            count_sells++;
        }
//---
   return;
  }
//+------------------------------------------------------------------+
//| Calculate all pending orders                                     |
//+------------------------------------------------------------------+
void CalculateAllPendingOrders(int &count_buy_limits,int &count_sell_limits,int &count_buy_stops,int &count_sell_stops)
  {
   count_buy_limits  = 0;
   count_sell_limits = 0;
   count_buy_stops   = 0;
   count_sell_stops  = 0;

   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders
      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties
         //if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==InpMagic)
        {
         if(m_order.OrderType()==ORDER_TYPE_BUY_LIMIT)
            count_buy_limits++;
         else
            if(m_order.OrderType()==ORDER_TYPE_SELL_LIMIT)
               count_sell_limits++;
            else
               if(m_order.OrderType()==ORDER_TYPE_BUY_STOP)
                  count_buy_stops++;
               else
                  if(m_order.OrderType()==ORDER_TYPE_SELL_STOP)
                     count_sell_stops++;
        }
  }
//+------------------------------------------------------------------+
 

I´m try this code in the backtest

 
Cesar :

Hi Vladmir


Tks for explanation its help me a lot.


But I tried to user youd code to see the pending order but I can´t.


You cannot do it this way! This is dirty code! For this you need to chop off your hands! You create a new object on every tick !!!

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

   CTrade trade; 
 

You don't need to do this:

 double Ask = NormalizeDouble ( SymbolInfoDouble ( _Symbol , SYMBOL_ASK ), _Digits );

Prices are as follows:

//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
     {
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error");
      return(false);
     }
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
     {
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0");
      return(false);
     }
//---
   return(true);
  }
Reason: