Probelm OrderSelect() - page 2

 
Carl Schreiber:

This

if(OrdersTotal() == 0 && OrderSelect(Ticket,SELECT_BY_TICKET))

is contradictory. If there are no orders there is nothing to select!!

You probably don't want the second part if you want to open only if there are no open orders.

Read about OrderSelect(..) (mark it and press F!) to understand what it is meant for.

Actually, as Ticket is a globally declared variable, the line of code checks that there are currently no open orders, then selects the last order ticket (which must be closed as there are no open orders).



  if(Ticket>0 && OrderSelect(Ticket,SELECT_BY_TICKET))
    {
     if(OrderCloseTime()!=0)
       {
       //Code to check profit
       }
    }
 


would probably be better
 

Thank you all for your contribution! I tested the Keith Watford code, but the result is not as expected. The condition to be searched in the order just closed, is whether after a SELL order, the closing price is greater than that of opening, so as to determine the Loss and open another order .... only another order.

//+------------------------------------------------------------------+
//|                                                    PrimoTest.mq4 |
//+------------------------------------------------------------------+
#property copyright "PrimoTest"
#property link      ""
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+



int Ticket;
double myPoint;
int Count1 = 0;



int OnInit()
  {
//---


Ticket = -1;

//initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 4 || Digits() == 3 || Digits() == 2)
     {
      myPoint *= 10;
     }
  
  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason,)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  
   // Ask == Buy
   // Bid == Sell
  
   //Condition SELL First Trade
   if(OrdersTotal() == 0  // Control open orders
   && iRSI(Symbol(),0,2,PRICE_CLOSE,2) > 70
   && iRSI(Symbol(),0,2,PRICE_CLOSE,1) < 70  
   )
  
  
   {    
   Ticket = OrderSend(Symbol(), OP_SELL, 10.0, Open[0], 0, 0, 0, "BO exp:60", 0, 0, clrRed); // send order (dedicated to Binary Options Broker)
   if(Ticket < 0) Print("Errore invio ordine:"+IntegerToString(GetLastError())); // order control
   else Print("Ordine inviato correttamente"); // Order ok
   }  
  
//-------------------------------------------------------------------------------------------


   //Condition SELL Second Trade
   if(Ticket >0 && OrderSelect(Ticket,SELECT_BY_TICKET))
    {
     if(OrderCloseTime()!=0)
       {
       if(OrderClosePrice() > OrderOpenPrice() )// Determining whether the selected order closure has a higher price at the opening  
       Ticket = OrderSend(Symbol(), OP_SELL, 10.0, Open[0], 0, 0, 0, "BO exp:60", 0, 0, clrRed);// send the new order (ONLY ONE)
      
       if(Ticket < 0) Print("Errore invio ordine:"+IntegerToString(GetLastError())); // order control
       else Print("Ordine inviato correttamente");  // Order ok
       }
    }

//-------------------------------------------------------------------------------------------
   //Condition BUY First Trade
   if( OrdersTotal() == 0 // Control open orders
   && iRSI(Symbol(),0,2,PRICE_CLOSE,2) < 30
   && iRSI(Symbol(),0,2,PRICE_CLOSE,1) > 30
   )
  
  
   {    
   Ticket = OrderSend(Symbol(), OP_BUY, 10.0, Open[0], 0, 0, 0, "BO exp:60", 0, 0, clrGreen); // send order (dedicated to Binary Options Broker)
   if(Ticket < 0) Print("Errore invio ordine:"+IntegerToString(GetLastError())); // order control
   else Print("Ordine inviato correttamente"); // Order ok    
   }
//-------------------------------------------------------------------------------------------
   //Condition BUY Second Trade
   if(Ticket >0 && OrderSelect(Ticket,SELECT_BY_TICKET))
    {
     if(OrderCloseTime()!=0)
       {
       if(OrderClosePrice() < OrderOpenPrice() )// Determining whether the selected order closing the opening has a lower price
       Ticket = OrderSend(Symbol(), OP_SELL, 10.0, Open[0], 0, 0, 0, "BO exp:60", 0, 0, clrRed);// send the new order (ONLY ONE)
      
       if(Ticket < 0) Print("Errore invio ordine:"+IntegerToString(GetLastError())); // order control
       else Print("Ordine inviato correttamente");  // Order ok
      }
    }  
  }
//+------------------------------------------------------------------+
 
fly7680:

Thank you all for your contribution! I tested the Keith Watford code, but the result is not as expected. The condition to be searched in the order just closed, is whether after a SELL order, the closing price is greater than that of opening, so as to determine the Loss and open another order .... only another order.


As you use the same global variable to store buy ticket numbers and sell ticket numbers, after you select the order, you must then check whether it is an OP_BUY or OP_SELL.
 

I added this option in the Code and does not return errors, can fit?

//Condition BUY Second Trade
   if(Ticket >0 && OrderSelect(Ticket,SELECT_BY_TICKET))
    {
     if(OrderCloseTime()!=0)
       {
       if(OP_BUY && OrderClosePrice() < OrderOpenPrice() )// Determining whether the selected order closing the opening has a lower price
       Ticket = OrderSend(Symbol(), OP_SELL, 10.0, Open[0], 0, 0, 0, "BO exp:60", 0, 0, clrRed);// send the new order (ONLY ONE)
      
       if(Ticket < 0) Print("Errore invio ordine:"+IntegerToString(GetLastError())); // order control
       else Print("Ordine inviato correttamente");  // Order ok
      }
    }


 

No

if(OrderType()==OP_BUY && .......


.
 
Keith Watford:

No

if(OrderType()==OP_BUY && .......


.
Perfect, thank you so much, tomorrow I will update the code and test it!
 

Hello Keith Watford, I've updated the code and it seems to work, however, print  every tick this:

/Condition BUY Second Trade
   if(Ticket >0 && OrderSelect(Ticket,SELECT_BY_TICKET))
    {
     if(OrderCloseTime()!=0)
       {
       if(OrderType()==OP_BUY && OrderClosePrice() < OrderOpenPrice() )// Determining whether the selected order closing the opening has a lower price
       Ticket = OrderSend(Symbol(), OP_BUY, 10.0, Open[0], 0, 0, 0, "BO exp:60", 0, 0, clrRed);// send the new order (ONLY ONE)
      
       if(Ticket < 0) Print("Errore invio ordine:"+IntegerToString(GetLastError())); // order control
       else Print("Ordine inviato correttamente");  // Order ok
      }
    }
 
fly7680:

Hello Keith Watford, I've updated the code and it seems to work, however, print  every tick this:

/Condition BUY Second Trade
   if(Ticket >0 && OrderSelect(Ticket,SELECT_BY_TICKET))
    {
     if(OrderCloseTime()!=0)
       {
       if(OrderType()==OP_BUY && OrderClosePrice() < OrderOpenPrice() )// Determining whether the selected order closing the opening has a lower price
       Ticket = OrderSend(Symbol(), OP_BUY, 10.0, Open[0], 0, 0, 0, "BO exp:60", 0, 0, clrRed);// send the new order (ONLY ONE)
      
       if(Ticket < 0) Print("Errore invio ordine:"+IntegerToString(GetLastError())); // order control
       else Print("Ordine inviato correttamente");  // Order ok
      }
    }

Yes it will do it every tick.

As I don't know exactly what you are trying to achieve


  {
   /Condition BUY Second Trade
   if(Ticket>0 && OrderSelect(Ticket,SELECT_BY_TICKET))
     {
      if(OrderCloseTime()!=0)
        {
         if(OrderType()==OP_BUY && OrderClosePrice()<OrderOpenPrice())// Determining whether the selected order closing the opening has a lower price
           {
            Ticket=OrderSend(Symbol(),OP_BUY,10.0,Open[0],0,0,0,"BO exp:60",0,0,clrRed);// send the new order (ONLY ONE)

            if(Ticket<0) Print("Errore invio ordine:"+IntegerToString(GetLastError())); // order control
            else Print("Ordine inviato correttamente");  // Order ok
           }
        }
     }


 

Ok I solved this little bug. If you wanted to open the order at the end of the candle 1, and not the first tick of candle 0, is possible?

I tried this solution but the order always opens to the first tick candle 0.

//Condition SELL First Trade
   if(OrdersTotal() == 0  // Control open orders
   && iRSI(Symbol(),0,2,PRICE_CLOSE,2) > 70
   && iRSI(Symbol(),0,2,PRICE_CLOSE,1) < 70  
   )
  
  
   {    
   Ticket = OrderSend(Symbol(), OP_SELL, 10.0, Close[1], 0, 0, 0, "BO exp:60", 0, 0, clrRed); // send order (dedicated to Binary Options Broker)
   if(Ticket < 0) Print("Errore invio ordine:"+IntegerToString(GetLastError())); // order control
   else Print("Ordine inviato correttamente"); // Order ok
   }  


 
fly7680:

Ok I solved this little bug. If you wanted to open the order at the end of the candle 1, and not the first tick of candle 0, is possible?

I tried this solution but the order always opens to the first tick candle 0.


I didn't even look at your code.

You only know that candle 1 has closed when candle 0 opens, so you can never place any orders at the last tick of candle 1.

Reason: