See the orders open - page 2

 
Vladimir Karputov:

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 !!!

Yes tks, I fix it but the problem is the same.


I can´t see the peinding orders.


I need to know if after I send one order (BuyStop) if its finish or not. If is finish I want to open a new order, if not I will wating the order finish (stopprofit or stoploss)

 
Cesar :


See my example: there are counters of all pending orders.

Use these counters.


//+------------------------------------------------------------------+
//| 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);

  }
 

Hi Vladmir,

I had tried it.

but the backtest shows me:

2019.10.29 12:17:09.586 Core 1 2019.10.08 16:54:59   Alert: BUY: 0, SELL: 0, Buy limits 0, Sell limits 0, Buy stops 0, Sell stops 0


My Problem is simple:

I want to send a order with stoploss and takeprofit for each Tick (because here I will put my rules).

After a send the order I need to see if this order is finish or close I don´t know the best name. If the order was executed I want to put other new order with stopp loss and take profit.

To test it I will use the backtester of MetaTrader5.


Regards

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...
 
Cesar :


Submit a trade order. Count the number of positions and pending orders. What is the problem? Do you know how to add numbers?


Added: please decide - are you placing a pending order or are you opening a position?

Documentation on MQL5: Trade Functions / PositionsTotal
Documentation on MQL5: Trade Functions / PositionsTotal
  • www.mql5.com
Trade Functions / PositionsTotal - Reference on algorithmic/automated trading language for MetaTrader 5
 

Vladimir,

One question w hen I used: trade.BuyStop (100,Ask,_Symbol,StopLoss,TakeProfit,ORDER_TIME_GTC,0,"Teste Buy com Stop"); I send a Position or a Pending Order?  

See the attachment.

Files:
orders.jpg  53 kb
 
Cesar:

Vladimir,

One question w hen I used: trade.BuyStop (100,Ask,_Symbol,StopLoss,TakeProfit,ORDER_TIME_GTC,0,"Teste Buy com Stop"); I send a Position or a Pending Order?  

See the attachment.

Your code places a pending Buy stop order
 

Hi,


Okay, If my code place a pending Buy stop order, how I can see in the next tick if exist one pending order? 

 
Cesar :

Hi,


Okay, If my code place a pending Buy stop order , how I can see in the next tick if exist one pending order? 

I do this in my codes: if there is a trade order “place a pending order”, I check the result of the trade operation:

   if(free_margin_check>margin_check)
     {
      datetime time_expiration=TimeCurrent()+InpPendingExpiration*60;
      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))
        {
         if(m_trade.ResultOrder()==0)
           {
            if(InpPrintLog)
               Print(__FILE__," ",__FUNCTION__,", ERROR: ","#1 ",EnumToString(SPending[index].pending_type)," -> false. Result Retcode: ",m_trade.ResultRetcode(),
                     ", description of result: ",m_trade.ResultRetcodeDescription());
            if(InpPrintLog)
               PrintResultTrade(m_trade,m_symbol);
            return(false);
           }
         else
           {
            if(InpPrintLog)
               Print(__FILE__," ",__FUNCTION__,", OK: ","#2 ",EnumToString(SPending[index].pending_type)," -> true. Result Retcode: ",m_trade.ResultRetcode(),
                     ", description of result: ",m_trade.ResultRetcodeDescription());
            if(InpPrintLog)
               PrintResultTrade(m_trade,m_symbol);
            return(true);
           }
        }
      else
        {
         if(InpPrintLog)
            Print(__FILE__," ",__FUNCTION__,", ERROR: ","#3 ",EnumToString(SPending[index].pending_type)," -> false. Result Retcode: ",m_trade.ResultRetcode(),
                  ", description of result: ",m_trade.ResultRetcodeDescription());
         if(InpPrintLog)
            PrintResultTrade(m_trade,m_symbol);
         return(false);
        }
     }
   else
     {
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", ERROR: CAccountInfo.FreeMarginCheck returned the value ",DoubleToString(free_margin_check,2));
      return(false);
     }

Thus, I know quite accurately whether a pending order was rejected or a pending order was placed.

 

Hi, I made this code to check what you told me.

After testing , I have only one stock purchase and one stock sale.


Doubts:

1 - When testing is complete, will all positions be closed?

2 - Why do I have only one stock purchase?

3 - trade.ResultOrder () returns 2 all the time.


After the first purchase, I want to wait until the stock price reaches stop loss or take profit and sets a new position.

   if (trade.ResultOrder() == 0 )
     {
      trade.BuyStop( 100 ,Ask, _Symbol , 5 r4,TakeProfit, ORDER_TIME_GTC , 0 , "Teste Buy com Stop" );
     }
   else
     {
       Alert ( "I can but, because exist one open order" );
     }
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 : 

You mixed everything up.

First a trade order is sent: (if -> "true")

      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))
        {
         if(m_trade.ResultOrder()==0)
           {

if the trade order returns true, check

      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))
        {
         if(m_trade.ResultOrder()==0)
           {
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Trading is done by sending orders to open positions using the OrderSend() function, as well as to place, modify or delete pending orders. Each trade order refers to the type of the requested operation. Trading operations are described in the ENUM_TRADE_REQUEST_ACTIONS enumeration...
Reason: