Getting OrderOpenPrice to use the current OrderPrice and not the previous.

 

Good day.


When I use OrderOpenPrice() in code, it only uses the correct OpenPrice the first time, after that it always uses the previous OpenPrice.

It seems like when a order closes, the OrderOpenPrice() keeps the data and the cach is not cleared so that when the new order opens up, it keeps the old orders data.


Can you fix that?

 

 OrderOpenPrice() stores the value for the last selected order.

Select another order if you want its open price.

 
 int ordersell_6 = OrderSend(_Symbol,OP_SELL,0.01,Bid,3,Bid+(200*_Point),Bid-(200*_Point),NULL,116);
if(OrderSelect(ordersell_6,SELECT_BY_POS)==true)

try

if(OrderSelect(ordersell_6,SELECT_BY_TICKET)==true)
 
    if(Bid==b-(20*_Point))

Doubles are rarely equal. Understand the links in:
          The == operand. - MQL4 programming forum #2 2013.06.07

 
William Roeder:

Doubles are rarely equal. Understand the links in:
          The == operand. - MQL4 programming forum #2 2013.06.07

Hi William


Okey that makes sense. 

Didn't know that but thinking about it, they wont be exact.


Thanks.

 
William Roeder:

Doubles are rarely equal. Understand the links in:
          The == operand. - MQL4 programming forum #2 2013.06.07

   bool CheckIfOpenOrdersByMagicNB(int magicNB)
{
   int openOrders = OrdersTotal();
   
   for(int i = 0; i < openOrders; i++)
   {
      if(OrderSelect(i,SELECT_BY_POS)==true)
      {
         if(OrderMagicNumber() == magicNB) 
         {
            return true;
         }  
      }
   }
   return false;
}



void OnTick()
  {
  static double b;

    if(!CheckIfOpenOrdersByMagicNB(116))
    if(Hour()>3)
    if(Hour()<23)
    
   
    {
      int ordersell_6 = OrderSend(_Symbol,OP_SELL,0.01,Bid,3,0,0,NULL,116);
    } 
     if(OrderSelect(ordersell_6,SELECT_BY_TICKET)==true)
    {
      b = OrderOpenPrice();
    }
    double x = (b-(20*_Point));
    if(Bid<x)
    if(OrderSelect(ordersell_6,SELECT_BY_TICKET)==true)
    if(OrderSymbol()==Symbol())
     
        
      
      {
         OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Red);
      }
   
     
    double v = (b+(100*_Point));
    if(Bid>v)
    if(OrderSelect(ordersell_6,SELECT_BY_TICKET)==true)
    if(OrderSymbol()==Symbol())
     
      
      
      {
         OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Red);
      }
   
     
     Alert(b);
     Alert("TP    ", x);
     Alert("SL    ",v);
     ObjectDelete("line");
     ObjectDelete("lin2");
     ObjectDelete("line3");
     ObjectCreate("line",OBJ_HLINE,0,0,b);
     ObjectCreate("lin2",OBJ_HLINE,0,0,x);
     ObjectCreate("line3",OBJ_HLINE,0,0,v);
  }

I replaced ==  with < and >, and it still doesn't close the order.

I looked at the articles you suggested, I am using _Point, so the Normalization should not be necessary.

Is there something else happening that I am not seeing?

 
LouisBreet:

Is there something else happening that I am not seeing?

Please do NOT delete your posts once you have been given the solution. It looks strange when answers are given to questions that apparently have not been asked!

ALWAYS use

#property strict

In your code, it will help you track down errors like you have made here. If you had used it, you would have had the error notification

'ordersell_6' - undeclared identifier

        {
         int ordersell_6 = OrderSend(_Symbol,OP_SELL,0.01,Bid,3,0,0,NULL,116);
        }

ordersell_6 is a locally declared variable and only has a value in the block of code where it is declared.

It has no value here

if(OrderSelect(ordersell_6,SELECT_BY_TICKET)==true)
 {
 }

so an order will not be selected and the following block of code will not be executed.

 
Keith Watford:

Please do NOT delete your posts once you have been given the solution. It looks strange when answers are given to questions that apparently have not been asked!

ALWAYS use

In your code, it will help you track down errors like you have made here. If you had used it, you would have had the error notification

ordersell_6 is a locally declared variable and only has a value in the block of code where it is declared.

It has no value here

so an order will not be selected and the following block of code will not be executed.

Thanks Keith.


Thought I had to remove the post if i repost more or less the same thing. You previously said that I should not duplicate. My bad.


Okey thanks for the help. Making rookie mistakes over here. Should probably do a course or something.


Kind regards.

 
   bool CheckIfOpenOrdersByMagicNB(int magicNB)
{
   int openOrders = OrdersTotal();
   
   for(int i = 0; i < openOrders; i++)
   {
      if(OrderSelect(i,SELECT_BY_POS)==true)
      {
         if(OrderMagicNumber() == magicNB) 
         {
            return true;
         }  
      }
   }
   return false;
}



void OnTick()
  {
  static double b;

    if(!CheckIfOpenOrdersByMagicNB(116))
    if(Hour()>3)
    if(Hour()<23)
    
   
    {
      int ordersell_6 = OrderSend(_Symbol,OP_SELL,0.01,Bid,3,0,0,NULL,116);
     
     if(OrderSelect(ordersell_6,SELECT_BY_TICKET)==true)
    {
      b = OrderOpenPrice();
    
    double x = (b-(20*_Point));
    if(Bid<x)
     
        
      
      {
         OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Red);
      }
   
     
    double v = (b+(100*_Point));
    if(Bid>v)
      {
         OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Red);
      }
   
     
     Alert(b);
     Alert("TP    ", x);
     Alert("SL    ",v);
     ObjectDelete("line");
     ObjectDelete("lin2");
     ObjectDelete("line3");
     ObjectCreate("line",OBJ_HLINE,0,0,b);
     ObjectCreate("lin2",OBJ_HLINE,0,0,x);
     ObjectCreate("line3",OBJ_HLINE,0,0,v);
  }
  }
}

Okey so I have made it so that it is all included in the same block, still not working. 

 
LouisBreet:

Okey so I have made it so that it is all included in the same block, still not working. 

Yes, so the whole block will be executed by these conditions.

if(!CheckIfOpenOrdersByMagicNB(116))
    if(Hour()>3)
    if(Hour()<23)

What do you expect to happen?

Think.

 
Keith Watford:

Yes, so the whole block will be executed by these conditions.

What do you expect to happen?

Think.

thanks Keith

Sigh😟

Ill do my best 
Reason: