Especialistas: Expert 2MA Bunny Cross - página 2

 

hmm, e tenho erros de compilação init fails nas linhas 281 e 287. E, como consequência, não consigo executar nada=(

 
Александр Князев:

hmm, e tenho erros de compilação init fails nas linhas 281 e 287. E, como consequência, não consigo executar nada=(

Observe cuidadosamente O QUE e ONDE você faz o download. No momento, a versão 1.003 é baixada da parte russa do KodoBase e é compilada sem erros.

 

cảm ơn bạn về code

nhưng nó không hoạt động

khi nén lại n ó báo lỗi

bạn có thể chỉ tôi cách khắc phục không ?

xin cảm ơn.


 
Le Nam :

cảm ơn bạn về code.

nhưng nó không hoạt động

khi nén lại n ó báo lỗi

bạn có thể chỉ tôi cách khắc phục không ?

xin cảm ơn.


A versão 1.003 está disponível no fórum russo: https: //www.mql5.com/ru/code/16783

 

Hi,

Obrigado pelo seu EA, eu o modifiquei para alterar os períodos de MA, ele é bem lucrativo.

Abaixo o código modificado

//+------------------------------------------------------------------+
//| 2MA Bunny Cross Expert (edição de barabashkakvn).mq5
//|Direitos autorais © Ron Thompson |
//|http://www.lightpatch.com/forex/ || http: //www. lightpatch.com/forex/
//+------------------------------------------------------------------+
#property copyright "Ron Thompson"
#property link      "http://www.lightpatch.com/forex"
#property version   "1.003"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>  
#include <Trade\AccountInfo.mqh>
#include <Expert\Money\MoneyFixedMargin.mqh>
CPositionInfo  m_position;                   // objeto de posição comercial
CTrade         m_trade;                      // objeto de negociação
CSymbolInfo    m_symbol;                     // objeto de informação de símbolo
CAccountInfo   m_account;                    // invólucro de informações da conta
CMoneyFixedMargin *m_money;
//--- parâmetros de entrada
input double   InpLots     = 0;              // Lotes (ou "Lotes">0 e "Risco"==0 ou "Lotes"==0 e "Risco">0)
input double   Risk        = 5;              // Risco (ou "Lotes">0 e "Risco"==0 ou "Lotes"==0 e "Risco">0)
input int PeriodMAS=20;                      // Period of the fast MA
input int PeriodMAF=5;                       


input ulong    m_magic=319906800;      // número mágico
//---
ulong          m_slippage=10;                // derrapagem
int    handle_iMA_5;                         // variável para armazenar o identificador do indicador iMA 
int    handle_iMA_20;                        // variável para armazenar o identificador do indicador iMA 
//+------------------------------------------------------------------+
//| Função de inicialização especializada|
//+------------------------------------------------------------------+
int OnInit()
  {
   if(!m_symbol.Name(Symbol())) // define o nome do símbolo
      return(INIT_FAILED);
   RefreshRates();
//---
   m_trade.SetExpertMagicNumber(m_magic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetDeviationInPoints(m_slippage);
//--- sintonia para 3 ou 5 dígitos
   int digits_adjust=1;
   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)
      digits_adjust=10;
//---
   if(!LotsOrRisk(InpLots,Risk,digits_adjust))
      return(INIT_PARAMETERS_INCORRECT);
//---
   if(m_money!=NULL)
      delete m_money;
   m_money=new CMoneyFixedMargin;
   if(m_money!=NULL)
     {
      if(!m_money.Init(GetPointer(m_symbol),Period(),m_symbol.Point()*digits_adjust))
         return(INIT_FAILED);
      m_money.Percent(Risk);
     }
   else
     {
      Print(__FUNCTION__,", ERROR: Object CMoneyFixedMargin is NULL");
      return(INIT_FAILED);
     }
//--- criar identificador do indicador iMA
   handle_iMA_5=iMA(m_symbol.Name(),Period(),PeriodMAF,0,MODE_SMA,PRICE_WEIGHTED);
//--- se o identificador não for criado 
   if(handle_iMA_5==INVALID_HANDLE)
     {
      //--- informar sobre a falha e emitir o código de erro 
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
      //--- o indicador é interrompido antecipadamente 
      return(INIT_FAILED);
     }
//--- criar identificador do indicador iMA
   handle_iMA_20=iMA(m_symbol.Name(),Period(),PeriodMAS,3,MODE_SMA,PRICE_WEIGHTED);
//--- se o identificador não for criado 
   if(handle_iMA_20==INVALID_HANDLE)
     {
      //--- informar sobre a falha e emitir o código de erro 
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
      //--- o indicador é interrompido antecipadamente 
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Função de desinicialização de especialista|
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   if(m_money!=NULL)
      delete m_money;
  }
//+------------------------------------------------------------------+
//| Função de tique de especialista|
//+------------------------------------------------------------------+
void OnTick()
  {
//--- trabalhamos somente no momento do nascimento da nova barra
   static datetime PrevBars=0;
   datetime time_0=iTime(m_symbol.Name(),Period(),0);
   if(time_0==PrevBars)
      return;
   PrevBars=time_0;
//---

   double ArrayMA5[];
   ArraySetAsSeries(ArrayMA5,true);
   double ArrayMA20[];
   ArraySetAsSeries(ArrayMA20,true);
   if(!iMAGetArray(handle_iMA_5,0,3,ArrayMA5) || !iMAGetArray(handle_iMA_20,0,3,ArrayMA20) || !RefreshRates())
     {
      PrevBars=0;
      return;
     }
//---
   bool signal_buy=(ArrayMA5[2]<ArrayMA20[2] && ArrayMA5[1]>ArrayMA20[1]);
   bool signal_sell=(ArrayMA5[2]>ArrayMA20[2] && ArrayMA5[1]<ArrayMA20[1]);
//---
   if(signal_buy)
     {
      ClosePositions(POSITION_TYPE_SELL);
      OpenBuy(0.0,0.0);
     }
   if(signal_sell)
     {
      ClosePositions(POSITION_TYPE_BUY);
      OpenSell(0.0,0.0);
     }
//---

  }
//+------------------------------------------------------------------+
//| Função TradeTransaction|
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//---

  }
//+------------------------------------------------------------------+
//| Obter o valor dos buffers para o iMA na matriz
//+------------------------------------------------------------------+
bool iMAGetArray(const int handle_iMA,const int start_pos,const int count,double &arr_buffer[])
  {
//---
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      Print("This a no dynamic array!");
      return(false);
     }
   ArrayFree(arr_buffer);
   int       buffer_num=0;          // número do buffer do indicador 
//--- redefinir o código de erro 
   ResetLastError();
//--- preencher uma parte da matriz iMABuffer com valores do buffer do indicador que tem índice 0 
   int copied=CopyBuffer(handle_iMA,buffer_num,start_pos,count,arr_buffer);
   if(copied<0)
     {
      //--- se a cópia falhar, informe o código de erro 
      PrintFormat("Failed to copy data from the iMA indicator, error code %d",GetLastError());
      //--- sair com resultado zero - isso significa que o indicador é considerado como não calculado 
      return(false);
     }
   else if(copied<count)
     {
      PrintFormat("Moving Average indicator: %d elements from %d were copied",copied,count);
      DebugBreak();
      return(false);
     }
//---
   return(result);
  }
//+------------------------------------------------------------------+
//| Posições de fechamento|
//+------------------------------------------------------------------+
void ClosePositions(const ENUM_POSITION_TYPE pos_type)
  {
   for(int i=PositionsTotal()-1;i>=0;i--) // retorna o número de posições atuais
      if(m_position.SelectByIndex(i))     // seleciona a posição por índice para acesso posterior às suas propriedades
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
            if(m_position.PositionType()==pos_type) // obtém o tipo de posição
               m_trade.PositionClose(m_position.Ticket()); // fechar uma posição com o símbolo especificado
  }
//+------------------------------------------------------------------+
//| Atualiza os dados das cotações dos símbolos|
//+------------------------------------------------------------------+
bool RefreshRates(void)
  {
//--- taxas de atualização
   if(!m_symbol.RefreshRates())
     {
      Print("RefreshRates error");
      return(false);
     }
//--- proteção contra o valor de retorno "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
      return(false);
//---
   return(true);
  }
//+------------------------------------------------------------------+
//| Verificar a exatidão do volume da posição
//+------------------------------------------------------------------+
bool CheckVolumeValue(double volume,string &error_description)
  {
//--- volume mínimo permitido para operações comerciais
   double min_volume=m_symbol.LotsMin();
   if(volume<min_volume)
     {
      error_description=StringFormat("Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f",min_volume);
      return(false);
     }
//--- volume máximo permitido de operações comerciais
   double max_volume=m_symbol.LotsMax();
   if(volume>max_volume)
     {
      error_description=StringFormat("Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f",max_volume);
      return(false);
     }
//--- obter a etapa mínima de alteração de volume
   double volume_step=m_symbol.LotsStep();
   int ratio=(int)MathRound(volume/volume_step);
   if(MathAbs(ratio*volume_step-volume)>0.0000001)
     {
      error_description=StringFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",
                                     volume_step,ratio*volume_step);
      return(false);
     }
   error_description="Correct volume value";
   return(true);
  }
//+------------------------------------------------------------------+
//| Lotes ou risco em porcentagem para um negócio a partir de uma margem livre
//+------------------------------------------------------------------+
bool LotsOrRisk(const double lots,const double risk,const int digits_adjust)
  {
   if(lots<0.0 && risk<0.0)
     {
      Print(__FUNCTION__,", ERROR: Parameter (\"lots\" or \"risk\") can't be less than zero");
      return(false);
     }
   if(lots==0.0 && risk==0.0)
     {
      Print(__FUNCTION__,", ERROR: Trade is impossible: You have set \"lots\" == 0.0 and \"risk\" == 0.0");
      return(false);
     }
   if(lots>0.0 && risk>0.0)
     {
      Print(__FUNCTION__,", ERROR: Trade is impossible: You have set \"lots\" > 0.0 and \"risk\" > 0.0");
      return(false);
     }
   if(lots>0.0)
     {
      string err_text="";
      if(!CheckVolumeValue(lots,err_text))
        {
         Print(__FUNCTION__,", ERROR: ",err_text);
         return(false);
        }
     }
   else if(risk>0.0)
     {
      if(m_money!=NULL)
         delete m_money;
      m_money=new CMoneyFixedMargin;
      if(m_money!=NULL)
        {
         if(!m_money.Init(GetPointer(m_symbol),Period(),m_symbol.Point()*digits_adjust))
            return(false);
         m_money.Percent(risk);
        }
      else
        {
         Print(__FUNCTION__,", ERROR: Object CMoneyFixedMargin is NULL");
         return(false);
        }
     }
//---
   return(true);
  }
//+------------------------------------------------------------------+
//| Abrir posição de compra|
//+------------------------------------------------------------------+
void OpenBuy(double sl,double tp)
  {
   sl=m_symbol.NormalizePrice(sl);
   tp=m_symbol.NormalizePrice(tp);

   double check_open_long_lot=0.0;
   if(Risk>0.0)
     {
      check_open_long_lot=m_money.CheckOpenLong(m_symbol.Ask(),sl);
      Print("sl=",DoubleToString(sl,m_symbol.Digits()),
            ", CheckOpenLong: ",DoubleToString(check_open_long_lot,2),
            ", Balance: ",    DoubleToString(m_account.Balance(),2),
            ", Equity: ",     DoubleToString(m_account.Equity(),2),
            ", FreeMargin: ", DoubleToString(m_account.FreeMargin(),2));
      if(check_open_long_lot==0.0)
        {
         Print(__FUNCTION__,", ERROR: method CheckOpenLong returned the value of \"0.0\"");
         return;
        }
     }
   else
      check_open_long_lot=InpLots;
//--- verificar o volume antes do OrderSend para evitar o erro "not enough money" (CTrade)
   double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),check_open_long_lot,m_symbol.Ask(),ORDER_TYPE_BUY);

   if(check_volume_lot!=0.0)
     {
      if(check_volume_lot>=check_open_long_lot)
        {
         if(m_trade.Buy(check_open_long_lot,NULL,m_symbol.Ask(),sl,tp))
           {
            if(m_trade.ResultDeal()==0)
              {
               Print("#1 Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
                     ", description of result: ",m_trade.ResultRetcodeDescription());
               PrintResult(m_trade,m_symbol);
              }
            else
              {
               Print("#2 Buy -> true. Result Retcode: ",m_trade.ResultRetcode(),
                     ", description of result: ",m_trade.ResultRetcodeDescription());
               PrintResult(m_trade,m_symbol);
              }
           }
         else
           {
            Print("#3 Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
                  ", description of result: ",m_trade.ResultRetcodeDescription());
            PrintResult(m_trade,m_symbol);
           }
        }
      else
        {
         string text="";
         if(Risk>0.0)
            text="< method CheckOpenLong ("+DoubleToString(check_open_long_lot,2)+")";
         else
            text="< Lots ("+DoubleToString(InpLots,2)+")";
         Print(__FUNCTION__,", ERROR: method CheckVolume (",DoubleToString(check_volume_lot,2),") ",
               text);
         return;
        }
     }
   else
     {
      Print(__FUNCTION__,", ERROR: method CheckVolume returned the value of \"0.0\"");
      return;
     }
//---
  }
//+------------------------------------------------------------------+
//| Abrir posição de venda|
//+------------------------------------------------------------------+
void OpenSell(double sl,double tp)
  {
   sl=m_symbol.NormalizePrice(sl);
   tp=m_symbol.NormalizePrice(tp);

   double check_open_short_lot=0.0;
   if(Risk>0.0)
     {
      check_open_short_lot=m_money.CheckOpenShort(m_symbol.Bid(),sl);
      Print("sl=",DoubleToString(sl,m_symbol.Digits()),
            ", CheckOpenLong: ",DoubleToString(check_open_short_lot,2),
            ", Balance: ",    DoubleToString(m_account.Balance(),2),
            ", Equity: ",     DoubleToString(m_account.Equity(),2),
            ", FreeMargin: ", DoubleToString(m_account.FreeMargin(),2));
      if(check_open_short_lot==0.0)
        {
         Print(__FUNCTION__,", ERROR: method CheckOpenShort returned the value of \"0.0\"");
         return;
        }
     }
   else
      check_open_short_lot=InpLots;
//--- verificar o volume antes do OrderSend para evitar o erro "not enough money" (CTrade)
   double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),check_open_short_lot,m_symbol.Bid(),ORDER_TYPE_SELL);

   if(check_volume_lot!=0.0)
     {
      if(check_volume_lot>=check_open_short_lot)
        {
         if(m_trade.Sell(check_open_short_lot,NULL,m_symbol.Bid(),sl,tp))
           {
            if(m_trade.ResultDeal()==0)
              {
               Print("#1 Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),
                     ", description of result: ",m_trade.ResultRetcodeDescription());
               PrintResult(m_trade,m_symbol);
              }
            else
              {
               Print("#2 Sell -> true. Result Retcode: ",m_trade.ResultRetcode(),
                     ", description of result: ",m_trade.ResultRetcodeDescription());
               PrintResult(m_trade,m_symbol);
              }
           }
         else
           {
            Print("#3 Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),
                  ", description of result: ",m_trade.ResultRetcodeDescription());
            PrintResult(m_trade,m_symbol);
           }
        }
      else
        {
         string text="";
         if(Risk>0.0)
            text="< method CheckOpenShort ("+DoubleToString(check_open_short_lot,2)+")";
         else
            text="< Lots ("+DoubleToString(InpLots,2)+")";
         Print(__FUNCTION__,", ERROR: method CheckVolume (",DoubleToString(InpLots,2),") ",
               text);
         return;
        }
     }
   else
     {
      Print(__FUNCTION__,", ERROR: method CheckVolume returned the value of \"0.0\"");
      return;
     }
//---
  }
//+------------------------------------------------------------------+
//| Imprimir resultado do CTrade|
//+------------------------------------------------------------------+
void PrintResult(CTrade &trade,CSymbolInfo &symbol)
  {
   Print("Code of request result: "+IntegerToString(trade.ResultRetcode()));
   Print("code of request result: "+trade.ResultRetcodeDescription());
   Print("deal ticket: "+IntegerToString(trade.ResultDeal()));
   Print("order ticket: "+IntegerToString(trade.ResultOrder()));
   Print("volume of deal or order: "+DoubleToString(trade.ResultVolume(),2));
   Print("price, confirmed by broker: "+DoubleToString(trade.ResultPrice(),symbol.Digits()));
   Print("current bid price: "+DoubleToString(trade.ResultBid(),symbol.Digits()));
   Print("current ask price: "+DoubleToString(trade.ResultAsk(),symbol.Digits()));
   Print("broker comment: "+trade.ResultComment());
//int d=0;
  }
//+------------------------------------------------------------------+