Position ticket not available

 
I have a global variable 
ulong   posTicket;


and on the onTick function I use this condition to open a trade

if(posTicket<=0)
{
	trade.Sell(lot_size,_Symbol);
	posTicket = trade.ResultOrder();
}

Using a print statement I found out that the global variable does not hold the ticket value and is 0 at every check in the onTick, could somebody please help me understand what I'm doing wrong ?

 
// The code below is uncheck and will not compile. It is only an sample code.

ulong posTicket = 0;

int OnInit( void ) {
   posTicket = 0;
};

void OnTick( void ) {
   if( posTicket == 0 ) {
           if( trade.Sell( lot_size, _Symbol ) ) {
              posTicket = trade.ResultOrder();
              Print( "Sell order placed with ticket number = ", posTicket );
           } else
              Print( "Sell order failed!" );
        };
};
 
Fernando Carreiro #:

Thanks for the quick reply, the method you suggested works but I need to access the ticket number outside of the if statement in other functions as well and it returns 0 every time

 
DroidM #: Thanks for the quick reply, the method you suggested works but I need to access the ticket number outside of the if statement in other functions as well and it returns 0 every time

Then what is stopping you?

In both your example and mine, the ticket number is stored in a "global" variable (i.e. "posTicket"), so it is accessible globally/anywhere in your program.

EDIT: Unless you are declaring a local variable with the same name which overrides it. Are you getting any compiler warning about that?

 
DroidM:
I have a global variable 


and on the onTick function I use this condition to open a trade

Using a print statement I found out that the global variable does not hold the ticket value and is 0 at every check in the onTick, could somebody please help me understand what I'm doing wrong ?

trade.ResultOrder() will return an Order Ticket. Although technically the position ticket will be same when opening a position but it is better to perform checks ( if a position actually opened ) before storing that in the position ticket variable.

Documentation on MQL5: Standard Library / Trade Classes / CTrade / ResultOrder
Documentation on MQL5: Standard Library / Trade Classes / CTrade / ResultOrder
  • www.mql5.com
ResultOrder - CTrade - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

Then what is stopping you?

In both your example and mine, the ticket number is stored in a "global" variable (i.e. "posTicket"), so it is accessible globally/anywhere in your program.

EDIT: Unless you are declaring a local variable with the same name which overrides it. Are you getting any compiler warning about that?

That's what I'm confused about, I've declared it globally and yet it returns 0 every time. 0 errors and 0 warnings.

 
DroidM #:That's what I'm confused about, I've declared it globally and yet it returns 0 every time. 0 errors and 0 warnings.

Then you will have to show more of your code. We cannot read your mind nor see your computer. We can't possible know what you are doing to be able to offer you advice.

 
Fernando Carreiro #:

Then you will have to show more of your code. We cannot read your mind nor see your computer. We can't possible know what you are doing to be able to offer you advice.

Sorry for the confusion, it looks like it works on the live markets but not in the strategy tester, would you happen to know why ?
 
i also want to know why.  same experience here when using strategy tester.
 
DroidM #: Sorry for the confusion, it looks like it works on the live markets but not in the strategy tester, would you happen to know why ?
Emver #: i also want to know why.  same experience here when using strategy tester.

If you don't show you your actual code, how are we se supposed to know what you are doing? We can't read you mind nor see your computer.

Reason: