Download MetaTrader 5
To add comments, please log in or register
Having questions to the author of an article? Discuss them on the Forum!
Jinsong Zhang
14639
Jinsong Zhang 2010.06.25 06:07 

Hello, please tell us when to use POSITION_MAGIC, ORDER_MAGIC, DEAL_MAGIC? Could you give some examples? Thank you

Wackena
647
Wackena 2010.06.25 16:50  
song_song:

Hello, please tell us when to use POSITION_MAGIC, ORDER_MAGIC, DEAL_MAGIC? Could you give some examples? Thank you

OrderSend() sequence.

1. OrderSend() call initiates an Order.

2. The Order then initiates a Deal and then Order is closed.

3 On acceptance of Deal by Broker's server, a Symbol Position is opened  and then Deal is closed.

ORDER_MAGIC and DEAL_MAGIC would be used in HistorySelect as part of closed Order or Deal selection process.

HistorySelect() Example:

double LastDealPrice() // Last Deal open price for chart symbol
{
   uint total=0;
   long ticket;
   string symbol;
   long magic;
   
   HistorySelect(0,TimeCurrent());
   total=HistoryDealsTotal();

   for(uint i=0;i <total; i++)
      {
         ticket=HistoryDealGetTicket(i);
         symbol=HistoryDealGetString(ticket,DEAL_SYMBOL);
         magic=HistoryDealGetInteger(ticket,DEAL_MAGIC);
         if( symbol==Symbol() && magic==MagicNumber)
            {
               Lprice=HistoryDealGetDouble(ticket,DEAL_PRICE);
            }
      }

return(Lprice);
}

 

POSITION_MAGIC is used in current open Position selection process.

PositionSelect() example:

int CurPosLots() // total lots in current symbol position
{
   int total=PositionsTotal();
   double lots=0;
   for (int cnt=0; cnt<total; cnt++) 
      {
         if(PositionSelect(Sysbol()) )
            {
               if(PositionGetInteger(POSITION_MAGIC)==MagicNumber)
                  {
                     lots=PositionGetDouble(POSITION_VOLUME); 
                  }
            }
      }

    return(lots);
}
Jinsong Zhang
14639
Jinsong Zhang 2010.06.26 06:19  
Thanks, wackena. I have more questions, for example: I have a position which POSITION_MAGIC is 1234, and then I sent  an order with same symbol and same direction but ORDER_MAGIC is 4321, you know the two positions will combine as one. So, how dose the POSITION_MAGIC changed?
Rogerio Figurelli
Moderator
36155
Rogerio Figurelli 2010.06.26 10:01  

About CurPosLots(), my suggestion is change declaration from int to double to a more precise result.


double CurPosLots()
Wackena
647
Wackena 2010.06.26 15:42  
song_song:
Thanks, wackena. I have more questions, for example: I have a position which POSITION_MAGIC is 1234, and then I sent  an order with same symbol and same direction but ORDER_MAGIC is 4321, you know the two positions will combine as one. So, how dose the POSITION_MAGIC changed?
I do not know. would need to use Print() call to find Position MagicNumber after 2 same type orders with different MagicNumbers.
Wackena
647
Wackena 2010.06.26 15:44  
figurelli:

About CurPosLots(), my suggestion is change declaration from int to double to a more precise result.


I agree, my mistake not to use double declaration.
Rashid Umarov
Admin
6602
Rashid Umarov 2010.06.27 09:50  
song_song:
Thanks, wackena. I have more questions, for example: I have a position which POSITION_MAGIC is 1234, and then I sent  an order with same symbol and same direction but ORDER_MAGIC is 4321, you know the two positions will combine as one. So, how dose the POSITION_MAGIC changed?
The POSITION_MAGIC will not be changed.
Jinsong Zhang
14639
Jinsong Zhang 2010.06.28 07:27  

I have done the testing, but there is some trouble. When it is debugging, it seems all right.  

 

 

 

 

but if run the script, the result seems wrong. Why?

 

 

  
void OnStart()
{
   double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   long posMagic,ordMagic,DeaMagic; double posVOL;
  
  
   MqlTradeRequest mrequest;
   MqlTradeResult mresult;
   mrequest.action = TRADE_ACTION_DEAL;                                
   mrequest.type = ORDER_TYPE_BUY;                                      
   mrequest.type_filling = ORDER_FILLING_AON;  
   mrequest.symbol = _Symbol;                                            
   mrequest.price = NormalizeDouble(Ask,_Digits);          
   mrequest.deviation=10;                          
   //mrequest.sl = NormalizeDouble(mySL,_Digits);
   //mrequest.tp = NormalizeDouble(myTP,_Digits);
   mrequest.volume = 0.1;                                                
   mrequest.magic = 1234;                                            
   mrequest.comment="";
   OrderSend(mrequest,mresult);
  
   if(mresult.retcode==10009){// || mresult.retcode==10008
      if(PositionSelect(_Symbol,100)==true){
         posMagic=PositionGetInteger(POSITION_MAGIC);
         posVOL=PositionGetDouble(POSITION_VOLUME);
      }    
      if(HistorySelect(0,TimeCurrent())){
         ordMagic=HistoryOrderGetInteger(mresult.order,ORDER_MAGIC);
         DeaMagic=HistoryDealGetInteger(mresult.deal,DEAL_MAGIC);
      }
      Print("1:",posMagic," ",ordMagic," ",DeaMagic," ",posVOL," ",mresult.order," ",mresult.deal);
      
   }
  

  
   MqlTradeRequest mrequest_2;
   MqlTradeResult mresult_2;
   mrequest_2.action = TRADE_ACTION_DEAL;                                
   mrequest_2.type = ORDER_TYPE_BUY;                                      
   mrequest_2.type_filling = ORDER_FILLING_AON;  
   mrequest_2.symbol = _Symbol;                                            
   mrequest_2.price = NormalizeDouble(Ask,_Digits);          
   mrequest_2.deviation=10;                          
   //mrequest_2.sl = NormalizeDouble(mySL,_Digits);
   //mrequest_2.tp = NormalizeDouble(myTP,_Digits);
   mrequest_2.volume = 0.2;                                                
   mrequest_2.magic = 4321;                                            
   mrequest_2.comment="";
   OrderSend(mrequest_2,mresult_2);

   if(mresult_2.retcode==10009 ){//|| mresult_2.retcode==10008
      if(PositionSelect(_Symbol,100)==true){
         posMagic=PositionGetInteger(POSITION_MAGIC);
         posVOL=PositionGetDouble(POSITION_VOLUME);
      }    
      if(HistorySelect(0,TimeCurrent())){
         ordMagic=HistoryOrderGetInteger(mresult_2.order,ORDER_MAGIC);
         DeaMagic=HistoryDealGetInteger(mresult_2.deal,DEAL_MAGIC);
      }
      Print("2:",posMagic," ",ordMagic," ",DeaMagic," ",posVOL," ",mresult_2.order," ",mresult_2.deal);
      
   }
  
  
  
}

 

I found I can get mresult_2.order and mresult_2.deal but there isn't this order ticket or deal ticket in "History" ! Why?

 

 

 

 

Jinsong Zhang
14639
Jinsong Zhang 2010.06.28 11:25  

I found I have to select "All History" manually again to show the last ticket!! That is no good.

Wackena
647
Wackena 2010.06.28 15:57  

Try this: HistorySelectByPosition() works in Demo, but does not work in Strategy Tester(ST). MetaQuotes is checking out the problem with ST.  

   uint pos_total, total, order_total, deal_total;
   long pos_id;
   long order_ticket, deal_ticket;
   uint ord=0;
   uint dea=0;

   pos_total=PositionsTotal(); // total number of open positions
   for(uint i=0;i<pos_total;i++)
     {
      if(PositionGetSymbol(i)==_Symbol)
        { 
         posMagic=PositionGetInteger(POSITION_MAGIC);
         posVOL=PositionGetDouble(POSITION_VOLUME);
         pos_id=PositionGetInteger(POSITION_IDENTIFIER); // find postion ID
         HistorySelectByPosition(pos_id); 
         order_total=HistoryOrdersTotal();
         for(uint t=0;t<order_total;t++)
          {
           order_ticket=HistoryOrderGetTicket(t);
           ordMagic=HistoryOrderGetInteger(order_ticket,ORDER_MAGIC);
           if(order_total>0) { ord++; Print("Order # ",ord," ordMagic ",ordMagic); }
          }
         deal_total=HistoryDealsTotal();
         for(uint x=0;x<deal_total;x++)
          {
           deal_ticket=HistoryDealGetTicket(x);
           dealMagic=HistoryDealGetInteger(deal_ticket,DEAL_MAGIC);
           if(deal_total>0) { dea++; Print("Deal # ",dea," dealMagic ",dealMagic); }          
          }
         Print("2:",posMagic," ",ordMagic," ",DeaMagic," ",posVOL," ",order_ticket," ",mresult_2.order," ",deal_ticket," ",mresult_2.deal);       }
     }
Jinsong Zhang
14639
Jinsong Zhang 2010.06.28 18:26  

Hi, wackena, I think the problem is: The "History" tab can't refresh automatically after HistorySelect() function

 

You could test it with follow code:

 

  
void OnStart()
{
   long posMagic_1,posMagic_2,ordMagic,DeaMagic; double posVOL_1,posVOL_2;
   double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   HistorySelect(0,TimeCurrent());
  
   MqlTradeRequest mrequest;
   MqlTradeResult mresult;
   mrequest.action = TRADE_ACTION_DEAL;                                
   mrequest.type = ORDER_TYPE_BUY;                                      
   mrequest.type_filling = ORDER_FILLING_AON;  
   mrequest.symbol = _Symbol;                                            
   mrequest.price = NormalizeDouble(Ask,_Digits);          
   mrequest.deviation=10;                          
   //mrequest.sl = NormalizeDouble(mySL,_Digits);
   //mrequest.tp = NormalizeDouble(myTP,_Digits);
   mrequest.volume = 0.1;                                                
   mrequest.magic = 1234;                                            
   mrequest.comment="";
   OrderSend(mrequest,mresult);
  
  
}

 then you please do some deals and check "History" tab.

/ /12
To add comments, please log in or register