My Bid price is not updating in my code

 

Hello, 

I want to open a new trade when price has passed a threshold, when I did this with a for loop and with OrderSelect(i, SELECT_BY_POS) it worked fine, but now I'm directly selecting one order, and when I print both the bid price, and the order opening price, they have the same value. For some reason Bid price in my code is now always the OrderOpeningPrice of the selected order, what is happening here?


See attached image of my console  


void OnTick()
{                                                       
//SELL-----------------------------------------------------------                    
                         
             if(CountBuyPositions() < maxordersbuy && CountSellPositions() < maxorderssell)
             {                             
                                     int order = OrderSend
                                    (
                                       Symbol(),
                                       OP_SELL,       //Buy without delay
                                       0.5,       //10 Microlot
                                       Bid,           //Bid price
                                       3,             //Only 3 points slippage
                                       0,            //Stop loss  //Bid+SL*_Point,  
                                       Bid-TP*_Point,//Take profit 
                                       NULL,          //No comment text
                                       0,             //No ID Number
                                       0,             //No expiration date
                                       Red            //Draw red arow
                                    );  

                } 
                                        
  
                
 
           
//-------------------------------------------------------------------------------------------------------------------------------------------

                   //Select Order
                   if(OrderSelect(order, SELECT_BY_TICKET)==true)
                   {                                                               
                     //Check if order symbol maches the currency pair
                     if(_Symbol== OrderSymbol())
                     {                                   
                        //Check if it is a sell order
                        if(OrderType()== OP_SELL)
                        {       
                        
                           Alert("Order has been selected");
                           Alert("Bid price is", Bid);
                           Alert("Retest price is", OrderOpenPrice()+reopen*_Point);
                           Alert("Order opening price is", OrderOpenPrice());
                                            
                                             //THIS STATEMENT IS NEVER CALLED BECAUSE THEY ARE ALWAYS DIFFERENT AS BID PRICES EQUALS OPENING PRICE
                                             if(Bid == OrderOpenPrice()+reopen*_Point)
                                             {  
                                                   int order2 = OrderSend

                                                   (
                                                      Symbol(),
                                                      OP_SELL,       //Buy without delay
                                                      0.5   ,       //lot
                                                      Bid,           //Bid price
                                                      3,             //Only 3 points slippage
                                                      0,            //Stop loss  //Bid+SL*_Point,  
                                                      Bid-35*_Point,//Take profit 
                                                      NULL,          //No comment text
                                                      0,             //No ID Number
                                                      0,             //No expiration date
                                                      Red            //Draw red arow
                                                   );  

                                             }  
                    
                        }
                     }
                 }
}
Documentation on MQL5: Trade Functions / OrderSelect
Documentation on MQL5: Trade Functions / OrderSelect
  • www.mql5.com
Selects an order to work with. Returns true if the function has been successfully completed. Returns false if the function completion has failed. For more information about an error call GetLastError(). Do not confuse current pending orders with positions, which are also displayed on the "Trade" tab of the "Toolbox" of the client terminal...
 

Except for slippage, Bid will always == OrderOpenPrice()

You are only selecting the order immediately after opening the order.

Get into the habit of always using

#property strict

it will help you.

In this case it will give you the error "order - undeclared identifier"

You assign a value to order in the code where you place the order, but the next tick, order will not be the ticket number, so the OrderSelect() will fail.


In future please post in the correct section

I will move your topic to the MQL4 and Metatrader 4 section.


 
Keith Watford:

Except for slippage, Bid will always == OrderOpenPrice()

You are only selecting the order immediately after opening the order.

Get into the habit of always using

it will help you.

In this case it will give you the error "order - undeclared identifier"

You assign a value to order in the code where you place the order, but the next tick, order will not be the ticket number, so the OrderSelect() will fail.


In future please post in the correct section

I will move your topic to the MQL4 and Metatrader 4 section.


Hello, thank you for your answer! Sorry about the misplacing of the post, will take that into account for the next, didn't realize it. 


I'm still not completely understanding the problem, why does every tick change the ticket number when I already assigned it, and it only opens a new trade when the previous one is closed? shouldn't the ticket number remain fixed to the order?  

I indeed tried the #Property strick mode, but can't find out how to use to fix the issue. 

Do you have any recommendation about how to modify the order selecting by ticket? Most information I find about modifies orders is by position. 

 
int order = OrderSend

You declare order locally.

That means that the next time OnTick() is called, order is not assigned a value.

If you want a variable to retain its value, you should declare it as a static variable or declare it globally.

 
Skester96: why does every tick change the ticket number when I already assigned it,
             {                             
                                     int order = OrderSend
                                    (
                                       Symbol(),
                                       OP_SELL,       //Buy without delay
                                       
                                       Red            //Draw red arow
                                    );  

                } ««««««««««« After reaching here, order is gone.

Use strict and you would know that. It's not changing, you assign it and then forget it. Don't forget it; remember it; read about global or static variables.

 

I fixed it with #property strict,


Thank you!

Reason: