Ordem pendente sendo criada sozinha quando mudo o timeframe

 

Chapas,

Estou testando um EA da biblioteca em conta DEMO.

Ele coloca ordens pendentes.

Quando eu mudo o timeframe ele coloca mais 1 ordem pendente.

Qual trecho de código eu posso inserir para impedir isso ? ...quero mudar o timeframe livremente sem que isso gere aberturas de novas ordens

Abaixo segue o trecho do código


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int TotalPositionsAndPendingOrders()
  {
   int total=0;
   for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positions
      if(m_position.SelectByIndex(i))     // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
            total++;

   for(int i=OrdersTotal()-1;i>=0;i--) // returns the number of current orders
      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties
         if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==m_magic)
            total++;

   return(total);
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value 
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_ORDER_ADD)
     {
      long     order_type        =0;
      double   order_price       =0.0;
      double   order_volume      =0.0;
      string   order_symbol      ="";
      long     order_magic       =0;
      
      if(OrderSelect(trans.order)) // select pending orders 
        {
         order_type=OrderGetInteger(ORDER_TYPE);
         order_price=OrderGetDouble(ORDER_PRICE_OPEN);
         order_volume=OrderGetDouble(ORDER_VOLUME_INITIAL);
         order_symbol=OrderGetString(ORDER_SYMBOL);
         order_magic=OrderGetInteger(ORDER_MAGIC);
        }
      else
         return;
      if(order_symbol==m_symbol.Name() && order_magic==m_magic)
        {
         if(order_type==ORDER_TYPE_BUY_LIMIT)
           {
            LastBuyLimitPrice=order_price;
            LastBuyLimitLot=order_volume;
           }
         if(order_type==ORDER_TYPE_SELL_LIMIT)
           {
            LastSellLimitPrice=order_price;
            LastSellLimitlLot=order_volume;
           }
        }
     }
  }
//+------------------------------------------------------------------+
 
sergio.fg:

Chapas,

Estou testando um EA da biblioteca em conta DEMO.

Ele coloca ordens pendentes.

Quando eu mudo o timeframe ele coloca mais 1 ordem pendente.

Qual trecho de código eu posso inserir para impedir isso ? ...quero mudar o timeframe livremente sem que isso gere aberturas de novas ordens

Abaixo segue o trecho do código


Oi Sérgio. Não olhei seu código ok? 
Mas o que vou explicar vai te ajudar...

O evento OnInit é chamado toda vez que o:

1) EA vai para o gráfico
2) troca o ativo
3) troca o timeframe
4) abre o mt5 que estava fechado
5) faz novo login

Entao esse é o motivo de pendurar outra ordem.
Vc deve fazer um filtro que olhe se já tem ordem pendente do Magic number no mesmo ativo.
Se tiver a ordem pendente não faz nada (ou cancela e coloca outra, depende da sua estratégia).
 

Valeu Ricardo,

Fui lá no OnInit e percebi que ele estava zerando estes dois valores:

LastSellLimitPrice=0.0;

LastBuyLimitPrice=0.0;


Aí eu comentei estas duas linhas e parou de abrir novas ordens na troca do timeframe. Agora vou ver se isso prejudica o EA de alguma forma.

Obrigado 

 
sergio.fg #:

Valeu Ricardo,

Fui lá no OnInit e percebi que ele estava zerando estes dois valores:

LastSellLimitPrice=0.0;

LastBuyLimitPrice=0.0;


Aí eu comentei estas duas linhas e parou de abrir novas ordens na troca do timeframe. Agora vou ver se isso prejudica o EA de alguma forma.

Obrigado 

Blza.
Que bom que deu certo!
Razão: