Ajuda com EA MQL4 - Apenas um trade por barra

Para adicionar comentários, por favor Faça o login ou registrar
Mat5284
9
Mat5284  

Pessoas, não sei se estou postando no lugar certo, se não, perdão.

Eu estou com um EA que está funcionando bem, mas com um problema que não sei como resolver de jeito nenhum!

Se eu colocar o Take curto, e ele atingir na mesma barra que abriu, o EA fecha a ordem e abre uma nova (pois ele refaz a leitura da barra e considera que tem que abrir uma nova ordem)

Algum gênio pode me ajudar a evitar isso? Pq quando ele abre uma ordem na mesma barra que ele fechou, a chance de dar loss aumenta muito!

Vou colocar o algoritmo aqui. Agradeço se alguém puder ajudar, e mesmo aqueles com idéias para resolver.


//+------------------------------------------------------------------+

//                        DO NOT DELETE THIS HEADER

//             DELETING THIS HEADER IS COPYRIGHT INFRIGMENT 

//

//                   Copyright ©2011, ForexEAdvisor.com

//                 ForexEAdvisor Strategy Builder version 0.2

//                        http://www.ForexEAdvisor.com 

//

// THIS EA CODE HAS BEEN GENERATED USING FOREXEADVISOR STRATEGY BUILDER 0.2 

// on: 12/28/2018 2:24:24 PM

// Disclaimer: This EA is provided to you "AS-IS", and ForexEAdvisor disclaims any warranty

// or liability obligations to you of any kind. 

// UNDER NO CIRCUMSTANCES WILL FOREXEADVISOR BE LIABLE TO YOU, OR ANY OTHER PERSON OR ENTITY,

// FOR ANY LOSS OF USE, REVENUE OR PROFIT, LOST OR DAMAGED DATA, OR OTHER COMMERCIAL OR

// ECONOMIC LOSS OR FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, STATUTORY, PUNITIVE,

// EXEMPLARY OR CONSEQUENTIAL DAMAGES WHATSOEVER RELATED TO YOUR USE OF THIS EA OR 

// FOREXEADVISOR STRATEGY BUILDER     

// Because software is inherently complex and may not be completely free of errors, you are 

// advised to verify this EA. Before using this EA, please read the ForexEAdvisor Strategy Builder

// license for a complete understanding of ForexEAdvisor' disclaimers.  

// USE THIS EA AT YOUR OWN RISK. 

//  

// Before adding this expert advisor to a chart, make sure there are NO

// open positions.

//                      DO NOT DELETE THIS HEADER

//             DELETING THIS HEADER IS COPYRIGHT INFRIGMENT 

//+------------------------------------------------------------------+



extern int MagicNumber=10001;

extern double Lots =0.01;

extern double StopLoss=0;

extern double TakeProfit=0;

extern int TrailingStop=0;

extern int Slippage=3;

//+------------------------------------------------------------------+

//    expert start function

//+------------------------------------------------------------------+

int start()

{

  double MyPoint=Point;

  if(Digits==3 || Digits==5) MyPoint=Point*10;

  

  double TheStopLoss=0;

  double TheTakeProfit=0;

  if( TotalOrdersCount()==0 ) 

  {

     int result=0;

     if((iRSI(NULL,0,14,PRICE_CLOSE,1)>iRSI(NULL,0,14,PRICE_CLOSE,2))&&(iRSI(NULL,0,14,PRICE_CLOSE,2)>35)&&(iRSI(NULL,0,14,PRICE_CLOSE,3)<35)&&(iSAR(NULL,0,0.02,0.2,1)<Close[1])&&(iSAR(NULL,0,0.02,0.2,2)>Close[2])) // Here is your open buy rule

     {

        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Blue);

        if(result>0)

        {

         TheStopLoss=0;

         TheTakeProfit=0;

         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;

         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;

         OrderSelect(result,SELECT_BY_TICKET);

         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);

        }

        return(0);

     }

     if((iRSI(NULL,0,14,PRICE_CLOSE,1)<iRSI(NULL,0,14,PRICE_CLOSE,2))&&(iRSI(NULL,0,14,PRICE_CLOSE,2)<65)&&(iRSI(NULL,0,14,PRICE_CLOSE,3)>65)&&(iSAR(NULL,0,0.02,0.2,1)>Close[1])&&(iSAR(NULL,0,0.02,0.2,2)<Close[1])) // Here is your open Sell rule

     {

        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Red);

        if(result>0)

        {

         TheStopLoss=0;

         TheTakeProfit=0;

         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;

         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;

         OrderSelect(result,SELECT_BY_TICKET);

         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);

        }

        return(0);

     }

  }

  

  for(int cnt=0;cnt<OrdersTotal();cnt++)

     {

      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL &&   

         OrderSymbol()==Symbol() &&

         OrderMagicNumber()==MagicNumber 

         )  

        {

         if(OrderType()==OP_BUY)  

           {

              if((iRSI(NULL,0,14,PRICE_CLOSE,1)<35)) //here is your close buy rule

              {

                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);

              }

            if(TrailingStop>0)  

              {                 

               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)

                 {

                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)

                    {

                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);

                     return(0);

                    }

                 }

              }

           }

         else 

           {

                if((iRSI(NULL,0,14,PRICE_CLOSE,1)>65)) // here is your close sell rule

                {

                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);

                }

            if(TrailingStop>0)  

              {                 

               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))

                 {

                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))

                    {

                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);

                     return(0);

                    }

                 }

              }

           }

        }

     }

   return(0);

}


int TotalOrdersCount()

{

  int result=0;

  for(int i=0;i<OrdersTotal();i++)

  {

     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);

     if (OrderMagicNumber()==MagicNumber) result++;


   }

  return (result);

}

Cid Ougaske
3064
Cid Ougaske  
Mat5284:

Pessoas, não sei se estou postando no lugar certo, se não, perdão.

Eu estou com um EA que está funcionando bem, mas com um problema que não sei como resolver de jeito nenhum!

Se eu colocar o Take curto, e ele atingir na mesma barra que abriu, o EA fecha a ordem e abre uma nova (pois ele refaz a leitura da barra e considera que tem que abrir uma nova ordem)

Algum gênio pode me ajudar a evitar isso? Pq quando ele abre uma ordem na mesma barra que ele fechou, a chance de dar loss aumenta muito!

Vou colocar o algoritmo aqui. Agradeço se alguém puder ajudar, e mesmo aqueles com idéias para resolver.


//+------------------------------------------------------------------+

//                        DO NOT DELETE THIS HEADER

//             DELETING THIS HEADER IS COPYRIGHT INFRIGMENT 

//

//                   Copyright ©2011, ForexEAdvisor.com

//                 ForexEAdvisor Strategy Builder version 0.2

//                        http://www.ForexEAdvisor.com 

//

// THIS EA CODE HAS BEEN GENERATED USING FOREXEADVISOR STRATEGY BUILDER 0.2 

// on: 12/28/2018 2:24:24 PM

// Disclaimer: This EA is provided to you "AS-IS", and ForexEAdvisor disclaims any warranty

// or liability obligations to you of any kind. 

// UNDER NO CIRCUMSTANCES WILL FOREXEADVISOR BE LIABLE TO YOU, OR ANY OTHER PERSON OR ENTITY,

// FOR ANY LOSS OF USE, REVENUE OR PROFIT, LOST OR DAMAGED DATA, OR OTHER COMMERCIAL OR

// ECONOMIC LOSS OR FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, STATUTORY, PUNITIVE,

// EXEMPLARY OR CONSEQUENTIAL DAMAGES WHATSOEVER RELATED TO YOUR USE OF THIS EA OR 

// FOREXEADVISOR STRATEGY BUILDER     

// Because software is inherently complex and may not be completely free of errors, you are 

// advised to verify this EA. Before using this EA, please read the ForexEAdvisor Strategy Builder

// license for a complete understanding of ForexEAdvisor' disclaimers.  

// USE THIS EA AT YOUR OWN RISK. 

//  

// Before adding this expert advisor to a chart, make sure there are NO

// open positions.

//                      DO NOT DELETE THIS HEADER

//             DELETING THIS HEADER IS COPYRIGHT INFRIGMENT 

//+------------------------------------------------------------------+



extern int MagicNumber=10001;

extern double Lots =0.01;

extern double StopLoss=0;

extern double TakeProfit=0;

extern int TrailingStop=0;

extern int Slippage=3;

//+------------------------------------------------------------------+

//    expert start function

//+------------------------------------------------------------------+

int start()

{

  double MyPoint=Point;

  if(Digits==3 || Digits==5) MyPoint=Point*10;

  

  double TheStopLoss=0;

  double TheTakeProfit=0;

  if( TotalOrdersCount()==0 ) 

  {

     int result=0;

     if((iRSI(NULL,0,14,PRICE_CLOSE,1)>iRSI(NULL,0,14,PRICE_CLOSE,2))&&(iRSI(NULL,0,14,PRICE_CLOSE,2)>35)&&(iRSI(NULL,0,14,PRICE_CLOSE,3)<35)&&(iSAR(NULL,0,0.02,0.2,1)<Close[1])&&(iSAR(NULL,0,0.02,0.2,2)>Close[2])) // Here is your open buy rule

     {

        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Blue);

        if(result>0)

        {

         TheStopLoss=0;

         TheTakeProfit=0;

         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;

         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;

         OrderSelect(result,SELECT_BY_TICKET);

         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);

        }

        return(0);

     }

     if((iRSI(NULL,0,14,PRICE_CLOSE,1)<iRSI(NULL,0,14,PRICE_CLOSE,2))&&(iRSI(NULL,0,14,PRICE_CLOSE,2)<65)&&(iRSI(NULL,0,14,PRICE_CLOSE,3)>65)&&(iSAR(NULL,0,0.02,0.2,1)>Close[1])&&(iSAR(NULL,0,0.02,0.2,2)<Close[1])) // Here is your open Sell rule

     {

        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Red);

        if(result>0)

        {

         TheStopLoss=0;

         TheTakeProfit=0;

         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;

         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;

         OrderSelect(result,SELECT_BY_TICKET);

         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);

        }

        return(0);

     }

  }

  

  for(int cnt=0;cnt<OrdersTotal();cnt++)

     {

      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL &&   

         OrderSymbol()==Symbol() &&

         OrderMagicNumber()==MagicNumber 

         )  

        {

         if(OrderType()==OP_BUY)  

           {

              if((iRSI(NULL,0,14,PRICE_CLOSE,1)<35)) //here is your close buy rule

              {

                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);

              }

            if(TrailingStop>0)  

              {                 

               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)

                 {

                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)

                    {

                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);

                     return(0);

                    }

                 }

              }

           }

         else 

           {

                if((iRSI(NULL,0,14,PRICE_CLOSE,1)>65)) // here is your close sell rule

                {

                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);

                }

            if(TrailingStop>0)  

              {                 

               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))

                 {

                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))

                    {

                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);

                     return(0);

                    }

                 }

              }

           }

        }

     }

   return(0);

}


int TotalOrdersCount()

{

  int result=0;

  for(int i=0;i<OrdersTotal();i++)

  {

     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);

     if (OrderMagicNumber()==MagicNumber) result++;


   }

  return (result);

}

Olá, recomento incluir o newbar() antes de executar a operação de compra/venda. Veja este tópico: https://www.mql5.com/en/forum/32686/page2

Make EA open a trade only when a new bar starts
Make EA open a trade only when a new bar starts
  • 2014.06.30
  • www.mql5.com
Hello, I made a simple EA but to speed up optimization I want to make the EA opena new trade only when the indicators are right and only when a new...
Flavio Jarabeck
4552
Flavio Jarabeck  
Cid Ougaske:

Olá, recomento incluir o newbar() antes de executar a operação de compra/venda. Veja este tópico: https://www.mql5.com/en/forum/32686/page2

Concordo com o @Cid Ougaske.

Reposte o código usando o boitão de Código-Fonte... assim mais pessoas poderão te ajudar... Analisar o código seco com formatação quilométrica vai inibir qualquer um...


;)

Flavio Jarabeck
4552
Flavio Jarabeck  

outra recomendação é jamais confiar em EA Builders... só de bater o olho no código dá pra perceber que o código gerado acha que "vai operar em um mundo perfeito"...

Jamais você terá sossego deixando um EA desses operando sozinho...

;)

Para adicionar comentários, por favor Faça o login ou registrar