See the orders open - page 3

 

Example:

//+------------------------------------------------------------------+
//|                                                         Test.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.000"
//---
#include <Trade\Trade.mqh>
#include <Trade\OrderInfo.mqh>
//---
CTrade         m_trade;                      // object of CTrade class
COrderInfo     m_order;                      // object of COrderInfo class
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   m_trade.SetExpertMagicNumber(519);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(Symbol());
   m_trade.SetDeviationInPoints(30);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(IsPendingOrdersExists())
      return;
   MqlTick tick;
   if(!SymbolInfoTick(Symbol(),tick))
      return;
   double volume_min=0.0;
   if(!SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN,volume_min))
      return;
   if(volume_min==0.0)
      return;
   Print("- - -");
   if(m_trade.BuyStop(volume_min,tick.ask+100*Point(),Symbol()))
     {
      if(m_trade.ResultOrder()==0)
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","#1 ",EnumToString(ORDER_TYPE_BUY_STOP)," -> false. Result Retcode: ",m_trade.ResultRetcode(),
               ", description of result: ",m_trade.ResultRetcodeDescription());
         return;
        }
      else
        {
         Print(__FILE__," ",__FUNCTION__,", OK: ","#2 ",EnumToString(ORDER_TYPE_BUY_STOP)," -> true. Result Retcode: ",m_trade.ResultRetcode(),
               ", description of result: ",m_trade.ResultRetcodeDescription());
         return;
        }
     }
   else
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: ","#3 ",EnumToString(ORDER_TYPE_BUY_STOP)," -> false. Result Retcode: ",m_trade.ResultRetcode(),
            ", description of result: ",m_trade.ResultRetcodeDescription());
      return;
     }
//---
  }
//+------------------------------------------------------------------+
//| Is pending orders exists                                         |
//+------------------------------------------------------------------+
bool IsPendingOrdersExists(void)
  {
   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()==Symbol() && m_order.Magic()==519)
            return(true);
//---
   return(false);
  }
//+------------------------------------------------------------------+

Result:

buy stop 0.01 EURUSD at 1.16123 (1.15983 / 1.16023 / 1.15983)
CTrade::OrderSend: buy stop 0.01 EURUSD at 1.16123 [done]
Test.mq5 OnTick, OK: #2 ORDER_TYPE_BUY_STOP -> true. Result Retcode: 10009, description of result: done
Files:
Test.mq5  7 kb
 

Ok!

When I tried this code I received a lot of errors.


 

if(m_trade.OrderOpen(m_symbol.Name(),
                           SPending[index].pending_type,check_lot,0.0,
                           m_symbol.NormalizePrice(SPending[index].price),
                           m_symbol.NormalizePrice(sl),
                           m_symbol.NormalizePrice(tp),
                           ORDER_TIME_SPECIFIED,
                           time_expiration))

{


}
 
Cesar:

Ok!

When I tried this code I received a lot of errors.


 

See the orders open
See the orders open
  • 2019.11.01
  • www.mql5.com
Hi, I´m trying to understand how I can now if I have one position open like...
 

Hi Vladmir

Tks again for your help!


I used your code but receive the same result.

I had executed the backtest to 10 months and itthe made a lot of orders.

I would like to do the sequecen bellow.


1 - I buy one stock / Price - $20 / StopLoss - $20 / Takeprofit  - $21

This order will be executed  and if I tried to see if have one orderopen I will receive return > 0 correct?

2 - If the order rise to $21 ou $22 the MQL5 will send a order to sell or buy correct? this is a orderopen? 

3 - After it if I want to send a new order to buy one stock / Price - $22 / StopLoss - $24 / Takeprofit  - $22

I cannot submit a new order if I have a StopLoss or Takeprofit waiting to be executed.

Testing trading strategies on real ticks
Testing trading strategies on real ticks
  • www.mql5.com
The article provides the results of testing a simple trading strategy in three modes: "1 minute OHLC" using only Open, High, Low and Close prices of minute bars; detailed modeling in "Every tick" mode, as well as the most accurate "Every tick based on real ticks" mode applying actual historical data. Comparing the results allows us to assess...
 
Cesar :

Hi Vladmir

Tks again for your help!


I used your code but receive the same result.

I had executed the backtest to 10 months and itthe made a lot of orders.

I would like to do the sequecen bellow.


1 - I buy one stock / Price - $20 / StopLoss - $20 / Takeprofit  - $21

This order will be executed  and if I tried to see if have one orderopen I will receive return > 0 correct?

2 - If the order rise to $21 ou $22 the MQL5 will send a order to sell or buy correct? this is a orderopen? 

3 - After it if I want to send a new order to buy one stock / Price - $22 / StopLoss - $24 / Takeprofit  - $22

I cannot submit a new order if I have a StopLoss or Takeprofit waiting to be executed.

Use the code from  .

This is the basis of the skeleton.

A check must be added to this skeleton. A lot of checks.

But the main idea will be this:

* if there are no pending orders in the market (Buy Stop for example) - the adviser issues an order "to place a pending Buy Stop order)

* when a pending order is triggered, a position will appear and the adviser will again issue an order "to place a pending Buy Stop order)


Added: it is possible to complicate the logic - the trade order "place a pending Buy stop order" is issued only if there are NO pending orders in the market and if there are NO positions in the market.

See the orders open
See the orders open
  • 2019.11.01
  • www.mql5.com
Hi, I´m trying to understand how I can now if I have one position open like...
 
This was helpful. Thanks to all contributors