Only 1 position open and only this position close.. Help me for Ticket numbers please..

 
I'm new and I'm trying to move forward. Please help me.
1) I want to open only 1 position for this signal using "if function " but I couldn't. :(  

2) I have more than one signal. and I have a separate shutdown signal for each signal. So I think I need to record the ticket number and I need to use this ticket number to close but I couldn't :(

Can you write me an example that makes a simple AL and CLOSE operation with Signal = 1 and Signal = 0?

So it will be much more comfortable for me to understand. Thank you very much in advance.

I hope I can find a master who will make me happy.

 
 if(signal==1)
     {  
   trade.Buy(0.01,NULL,Ask,0,0,NULL);         
 }

//I use this code but it opens it many times. I can stop with a variable or with positiontotal, 
but I will open many position with many signals. and I will get a different shutdown signal for each transaction. 
so I need to open this position 1 time and i need a ticket number for that content that I can only select and close this position.
how can i do this using a void or a function?
can you create me with an example of how to do this?
 
Emre Bahtlı:
you need to put magicnumber to select the order, from that paramater you can use, ordercount funtion or another paramater like, ordersymbol, etc
 
Achmad Wijaya:
you need to put magicnumber to select the order
I am grateful to you for answering me. so how do i do that? :/
 
Emre Bahtlı:
I am grateful to you for answering me. so how do i do that? :/

I don't know your code for mt5 or mt4, but basicly it's same algorithma

example :

int ordercount()                        
{
   total=0;                         
   int i;                               
   for(i=0; i<OrdersTotal(); i++)       
   {
       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()!=Symbol() ||    
       OrderMagicNumber()!=MagicNumber) 
       continue;                        
       total++;                         
   }
   return(total);                       
}

 

basic on your code

we can put that ordercount function there

 if(ordercount()==0  && signal==1)
     {  
   trade.Buy(0.01,NULL,Ask,0,0,NULL);         
 }
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
Achmad Wijaya:

basic on your code

we can put that ordercount function there

i use mt5

now, my magic number is "OrderMagicNumber()" right ?

Please order a buy and close order for me respectively. with 1234 magic number and signal 1 and 0. I will understand better.

to see exactly how to use it when opening and closing positions.

I am thankful to you <3

 
Emre Bahtlı:

i use mt5

now, my magic number is "OrderMagicNumber()" right ?

Please order a buy and sell order for me respectively. with 1234 magic number and signal 1 and 0. I will understand better.

to see exactly how to use it when opening and closing positions.

I am thankful to you <3

I use MT4 but I think basicly it's same algorithma, only diffrent in syntax

Your OrderMagicNumber()=MagicNumber mean you have parameter extern or non extern variable like 1,2 or another

you can use directly that OrderMagicNumber()=1 with fixed value directly, so you can manage the order you want.

or you can modified the funtion you need to more detail, example if you want to choose price from the spesific mode trade, symbol and magic number, you can choose MyOrder() funtion like this sample  :

double MyOrder ()
{
   double MyOrderPrice;
   MyOrderPrice=0;
 
for(int i=0;i<OrdersTotal();i++)
   {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()!=Symbol() ||  
       OrderMagicNumber()!=MagicNumber) continue;
       if(OrderType()==OP_BUY)
         MyOrderPrice=(OrderOpenPrice());
       if(OrderType()==OP_SELL)
         MyOrderPrice=(OrderOpenPrice()); 
   }
   return (MyOrderPrice);
}
 
Achmad Wijaya:

I use MT4 but I think basicly it's same algorithma, only diffrent in syntax

Your OrderMagicNumber()=MagicNumber mean you have parameter extern or non extern variable like 1,2 or another

you can use directly that OrderMagicNumber()=1 with fixed value directly, so you can manage the order you want.

or you can modified the funtion you need to more detail, example if you want to choose price from the spesific mode trade, symbol and magic number, you can choose MyOrder() funtion like this sample  :

double MyOrder ()
{
.....

Thanks you so much..

I won't need much detail for now. I just want to open 1 posiston and select and close the position I opened. do you think this is true? and how should I enter the code to close the position?

if(signal==1)
{
int ordercount()                        
{
   total=0;                         
   int i;                               
   for(i=0; i<OrdersTotal(); i++)       
   {
       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()!=Symbol() ||    
       OrderMagicNumber()!=MagicNumber) 
       continue;                        
       total++;                         
   }
   return(total);                       
}
}

if(ordercount()==0  && signal==1)
     {  
   trade.Buy(0.01,NULL,Ask,0,0,NULL);        
 }
 
 signal=0;
 
// for close ?
if (signal==0)
{
 
???
   
  }
 
Emre Bahtlı:

Thanks you so much..

I won't need much detail for now. I just want to open 1 posiston and select and close the position I opened. do you think this is true? and how should I enter the code to close the position?

I'm mobile now, ya its possible if you already correct in selecting order, you can use void close, you can find it on google search for it, and insert that void, and activate it
 
Achmad Wijaya:
Ben şimdi mobilim, ya da zaten sipariş seçiminde düzeltirseniz, void close kullanabilirsiniz, google aramada bulabilir ve bu boşluğu ekleyebilir ve etkinleştirebilirsiniz

Thank you for your interest. I will continue to wait for the lineup I want. I will be very glad if you can come back when you switch to PC. I will continue to research and try. I think we will be able to do what I want better with void. because I have to open other processes and also close them with signals. 'll see.