Dificuldade em Codificar Martingale.

 

Olá! Estou desenvolvendo um pequeno EA, baseado em alguns indicadores que tenho aqui. Quero adicionar a função Martingale nele, porém, do jeito que está, ele simplesmente abre lotes de forma crescente, e não faz o martingale apenas após o loss.

Aqui está o resultado do meu codigo > Screenshot_4


//+------------------------------------------------------------------+
//|                                                        FX_EA.mq4 |
//|                                                  Henrique Araújo |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Henrique Araújo"
#property link      ""
#property version   "1.00"
#property strict


extern string Note="PZ_DayTrading EA";
extern double LotSize=0.01;
double MgSize = LotSize;
extern int TakeProfit=30;
extern int StopLoss=100;
extern int Slippage=3;
extern int MagicNumber= 333456;
extern string Note2 = "Martingale";
extern bool UseMartingale = false;
extern double Martingale_Ad = 0.02;



double FL03_buy_signal=0;
double FL03_sell_signal=0;
double joker_filter=0;
double broom_sell_signal=0;
double broom_buy_signal=0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void getindi()
  {
   FL03_buy_signal=iCustom(NULL,0,"FL03",0,1);
   FL03_sell_signal=iCustom(NULL,0,"FL03",1,1);
   joker_filter=iCustom(NULL,0,"JokerFilter",0,1);
   broom_sell_signal=iCustom(NULL,0,"broom",1,1);
   broom_buy_signal=iCustom(NULL,0,"broom",2,1);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void openbuy()
  {
   OrderSend(_Symbol,OP_BUY,LotSize,Ask,Slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"FX_EA by Henrique",MagicNumber,0,clrAqua);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void opensell()
  {
   OrderSend(_Symbol,OP_SELL,LotSize,Bid,Slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"FX_EA by Henrique",MagicNumber,0,clrMagenta);
  }




bool mgbuy=true;
bool confmgbuy=false;
double TP_buy;
double SL_buy;

bool mgsell=true;
bool confmgsell=false;
double TP_sell;
double SL_sell;
void entry_order()
  {   
    if(UseMartingale==true){
    if(FL03_buy_signal!=0  && broom_buy_signal>0 &&joker_filter==1.0 && OrdersTotal()==0 && mgbuy==true)
        {
         openbuy();
         TP_buy = Bid+TakeProfit*Point;
         SL_buy = Bid-StopLoss*Point;
         confmgbuy=true;
         mgbuy=false;
        }
        if(confmgbuy=true){
        if(Bid>TP_buy){
        LotSize=MgSize;
        confmgbuy=false;
        mgbuy=true;
        }
        if(Bid<SL_buy){
        LotSize=LotSize+Martingale_Ad;
        confmgbuy=false;
        mgbuy=true;
        }
        }
        
      if(FL03_sell_signal!=0 && broom_sell_signal<0 &&joker_filter==2147483647.0 && OrdersTotal()==0 && mgsell==true)
        {
         opensell();
         TP_sell = Bid+TakeProfit*Point;
         SL_sell = Bid-StopLoss*Point;
         confmgsell=true;
         mgsell=false;
        }
        if(confmgsell=true){
        if(Bid<TP_sell){
        LotSize=MgSize;
        confmgsell=false;
        mgsell=true;
        }
        if(Bid>SL_sell){
        LotSize=LotSize+Martingale_Ad;
        confmgsell=false;
        mgsell=true;
        }
    
    }
   
}
}
  


int last_bars;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   getindi();
   if(last_bars!=Bars)
     {
      entry_order();
      last_bars=Bars;
     }

  //Comment(joker_filter);
  }
//+------------------------------------------------------------------+
Screenshot
Screenshot
  • prnt.sc
Capturado com Lightshot
 
Henrique Araújo:

Olá! Estou desenvolvendo um pequeno EA, baseado em alguns indicadores que tenho aqui. Quero adicionar a função Martingale nele, porém, do jeito que está, ele simplesmente abre lotes de forma crescente, e não faz o martingale apenas após o loss.

Aqui está o resultado do meu codigo > Screenshot_4


Olá Henrique,

LOL.... erro crasso que todos desenvolvedores comentem. Eu tô rindo porque a gente fica puto qdo. descobre.  

Obs. Eu só li as instruções e parei nesse erro. 



if(confmgbuy=true) ==>  if(confmgbuy==true) 
if(confmgsell=true) ==> if(confmgsell==true)

 
Rogerio Giannetti Torres:

Olá Henrique,

LOL.... erro crasso que todos desenvolvedores comentem. Eu tô rindo porque a gente fica puto qdo. descobre.  

Obs. Eu só li as instruções e parei nesse erro. 


 Olá Rogerio!! Obrigado por responder... Realmente isso me passou despercebido... Mas mesmo corrigindo este pequeno erro o EA continua brindo lotes crescentes e não volta pro lote inicial, deve ser algum erro na logica que não estou identificando...


Este é meu codigo corrigido:

//+------------------------------------------------------------------+
//|                                                        FX_EA.mq4 |
//|                                                  Henrique Araújo |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Henrique Araújo"
#property link      ""
#property version   "1.00"
#property strict


extern string Note="PZ_DayTrading EA";
extern double LotSize=0.01;
double MgSize = LotSize;
extern int TakeProfit=30;
extern int StopLoss=100;
extern int Slippage=3;
extern int MagicNumber= 333456;
extern string Note2 = "Martingale";
extern bool UseMartingale = false;
extern double Martingale_Ad = 0.02;



double FL03_buy_signal=0;
double FL03_sell_signal=0;
double joker_filter=0;
double broom_sell_signal=0;
double broom_buy_signal=0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void getindi()
  {
   FL03_buy_signal=iCustom(NULL,0,"FL03",0,1);
   FL03_sell_signal=iCustom(NULL,0,"FL03",1,1);
   joker_filter=iCustom(NULL,0,"JokerFilter",0,1);
   broom_sell_signal=iCustom(NULL,0,"broom",1,1);
   broom_buy_signal=iCustom(NULL,0,"broom",2,1);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void openbuy()
  {
   OrderSend(_Symbol,OP_BUY,LotSize,Ask,Slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"FX_EA by Henrique",MagicNumber,0,clrAqua);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void opensell()
  {
   OrderSend(_Symbol,OP_SELL,LotSize,Bid,Slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"FX_EA by Henrique",MagicNumber,0,clrMagenta);
  }




bool mgbuy=true;
bool confmgbuy=false;
double TP_buy;
double SL_buy;

bool mgsell=true;
bool confmgsell=false;
double TP_sell;
double SL_sell;
void entry_order()
  {   
    if(UseMartingale==true){
    if(FL03_buy_signal!=0  && broom_buy_signal>0 &&joker_filter==1.0 && OrdersTotal()==0 && mgbuy==true)
        {
         openbuy();
         TP_buy = Bid+TakeProfit*Point;
         SL_buy = Bid-StopLoss*Point;
         confmgbuy=true;
         mgbuy=false;
        }
        if(confmgbuy==true){
        if(Bid>TP_buy){
        LotSize=MgSize;
        confmgbuy=false;
        mgbuy=true;
        }
        if(Bid<SL_buy){
        LotSize=LotSize+Martingale_Ad;
        confmgbuy=false;
        mgbuy=true;
        }
        }
        
      if(FL03_sell_signal!=0 && broom_sell_signal<0 &&joker_filter==2147483647.0 && OrdersTotal()==0 && mgsell==true)
        {
         opensell();
         TP_sell = Bid+TakeProfit*Point;
         SL_sell = Bid-StopLoss*Point;
         confmgsell=true;
         mgsell=false;
        }
        if(confmgsell==true){
        if(Bid<TP_sell){
        LotSize=MgSize;
        confmgsell=false;
        mgsell=true;
        }
        if(Bid>SL_sell){
        LotSize=LotSize+Martingale_Ad;
        confmgsell=false;
        mgsell=true;
        }
    
    }
   
}
}
  


int last_bars;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   getindi();
   if(last_bars!=Bars)
     {
      entry_order();
      last_bars=Bars;
     }

  //Comment(joker_filter);
  }
//+------------------------------------------------------------------+

Resultado: Screenshot


PS: Estou usando como base este codigo:

extern double LotSize = 0.01;
double mgsize = LotSize;
extern double Mg = 1.44;
bool compra=true;
bool venda=true;
int C;
int V;
double TP;
double SL;
bool config_compra=false;
bool config_venda=false;
double TP_venda;
double SL_venda;
void OnTick(){


if(Close[1] > Open[1] && compra==true && OrdersTotal()==0){
C = OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Bid-200*Point,Bid+200*Point,0);
TP=Bid+200*Point;
SL=Bid-200*Point;
config_compra=true;
compra=false;
}

if(config_compra==true){
if(Bid>TP){
LotSize=mgsize;
config_compra=false;
compra=true;
}

if(Bid<SL){
LotSize=LotSize+Mg;
config_compra=false;
compra=true;
}
}

if(Close[1] < Open[1] && venda==true && OrdersTotal()==0){
V = OrderSend(Symbol(),OP_SELL,LotSize,Ask,3,Bid+200*Point,Bid-200*Point,0);
TP_venda=Bid+200*Point;
SL_venda=Bid-200*Point;
config_venda=true;
venda=false;
}

if(config_venda==true){
if(Bid>TP_venda){
LotSize=mgsize;
config_venda=false;
venda=true;
}

if(Bid<SL_venda){
LotSize=LotSize+Mg;
config_venda=false;
venda=true;
}

}






}

Veja o resultado do codigo acima: Screenshot_2

É isso que o meu EA deveria fazer, porem, ao codificar esta mesma logica no meu EA, não funciona do mesmo jeito. Não estou conseguindo identificar o erro. Help me.

Screenshot
Screenshot
  • prnt.sc
Capturado com Lightshot
 
Henrique Araújo:


Henrique, puxei trecho abaixo para explicar uma situação de compra com lote=0,03 que fechou com TP.

Pois bem, sair com com TP não significa que  if(Bid>TP_buy) foi executado, pois o preço pode cair até if(Bid<SL_buy) e depois subir, fechando no TP. OK!?


        if(confmgbuy==true){
        if(Bid>TP_buy){
        LotSize=MgSize;
        confmgbuy=false;
        mgbuy=true;
        }
        if(Bid<SL_buy){
        LotSize=LotSize+Martingale_Ad;
        confmgbuy=false;
        mgbuy=true;
        }
        }