诚信为本
量力而为
当前位置:峰汇在线 > MetaTrader 5 > MT5 EA > 正文

根据之字转向指标信号开发的EA ZigZagEvgeTrofi ver.1.mq5

ZigZagEvgeTrofi ver.1 EA根据之字转向指标信号自动交易。

源码:

//+------------------------------------------------------------------+
//|                                       ZigZagEvgeTrofi ver.1.mq5 |
//|                               Copyright © 2008, Trofimov Evgeniy |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Trofimov Evgeniy"
#property link      "http://www.125808047.com/"
#property version   "1.000"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>  
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
CSymbolInfo    m_symbol;                     // symbol info object

input int      InpDepth          = 17;       // Depth
input int      InpDeviation      = 7;        // Deviation
input int      InpBackstep       = 5;        // Backstep
input double   InpLot            = 0.10;     // Volume position
input bool     InpSignalReverse  = false;    // Signal reverse
ulong          m_magic           = 837874578;// magic number
static int prevtime=0;
int Urgency=2;
ulong          m_slippage=10;                // slippage

int            handle_iCustom;               // variable for storing the handle of the iCustom indicator 

double         m_adjusted_point;             // point value adjusted for 3 or 5 points
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(!m_symbol.Name(Symbol())) 
      return(INIT_FAILED);
   RefreshRates();

   string err_text="";
   if(!CheckVolumeValue(InpLot,err_text))
     {
      Print(err_text);
      return(INIT_PARAMETERS_INCORRECT);
     }
   m_trade.SetExpertMagicNumber(m_magic);
   if(IsFillingTypeAllowed(SYMBOL_FILLING_FOK))
      m_trade.SetTypeFilling(ORDER_FILLING_FOK);
   else if(IsFillingTypeAllowed(SYMBOL_FILLING_IOC))
      m_trade.SetTypeFilling(ORDER_FILLING_IOC);
   else
      m_trade.SetTypeFilling(ORDER_FILLING_RETURN);
   m_trade.SetDeviationInPoints(m_slippage);
   int digits_adjust=1;
   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)
      digits_adjust=10;
   m_adjusted_point=m_symbol.Point()*digits_adjust;
   handle_iCustom=iCustom(m_symbol.Name(),Period(),"\\Examples\\ZigZag",InpDepth,InpDeviation,InpBackstep);
   if(handle_iCustom==INVALID_HANDLE)
     {
      PrintFormat("Failed to create handle of the iCustom indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
      Comment("www.125808047.com");
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   static datetime PrevBars=0;
   datetime time_0=iTime(0);
   if(time_0==PrevBars)
      return;
   PrevBars=time_0;
   if(!IsTradeAllowed())
      return;

   double ZigZag=0.0;
   int Signal=0;

   double arr_zigzag[];
   ArraySetAsSeries(arr_zigzag,true);
   int counter=0;
   if(iCustomGet(handle_iCustom,0,0,100,arr_zigzag))
     {
      bool find=false;
      int limit=ArraySize(arr_zigzag);
      for(int i=0;i<limit;i++)
        {
         if(arr_zigzag[i]!=0.0)
           {
            find=true;
            counter=i;
            ZigZag=arr_zigzag[i];
            break;
           }
        }
      if(!find)
         return;
     }
   else
     {
      PrevBars=iTime(1);
      return;
     }

   double high=iHigh(counter);
   double low=iLow(counter);
   if(high==0.0 || low==0.0)
     {
      PrevBars=iTime(1);
      return;
     }
   if(CompareDoubles(ZigZag,high,m_symbol.Digits()))
     {
      if(!InpSignalReverse)
         Signal=1;   // open buy
      else
         Signal=2;   // open sell
     }
   else if(CompareDoubles(ZigZag,low,m_symbol.Digits()))
     {
      if(!InpSignalReverse)
         Signal=2;   // open sell
      else
         Signal=1;   // open buy
     }
//---
   for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of current 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)
           {
            if(m_position.PositionType()==POSITION_TYPE_BUY)
              {
               if(Signal==2)
                  m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
              }
            else if(m_position.PositionType()==POSITION_TYPE_SELL)
              {
               if(Signal==1)
                  m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
              }
           }

   if(counter<=Urgency)
     {
      if(Signal==1)
         m_trade.Buy(InpLot);
      else if(Signal==2)
         m_trade.Sell(InpLot);
     }
  }
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates(void)
  {
   if(!m_symbol.RefreshRates())
     {
      Print("RefreshRates error");
      return(false);
     }
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
      return(false);
   return(true);
  }
//+------------------------------------------------------------------+
//| Check the correctness of the order volume                        |
//+------------------------------------------------------------------+
bool CheckVolumeValue(double volume,string &error_description)
  {
   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);
     }
   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);
     }
   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);
  }
//+------------------------------------------------------------------+ 
//| Checks if the specified filling mode is allowed                  | 
//+------------------------------------------------------------------+ 
bool IsFillingTypeAllowed(int fill_type)
  {
   int filling=m_symbol.TradeFillFlags();
   return((filling & fill_type)==fill_type);
  }
//+------------------------------------------------------------------+
//| Gets the information about permission to trade                   |
//+------------------------------------------------------------------+
bool IsTradeAllowed()
  {
   if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
     {
      Alert("Check if automated trading is allowed in the terminal settings!");
      return(false);
     }
   else
     {
      if(!MQLInfoInteger(MQL_TRADE_ALLOWED))
        {
         Alert("Automated trading is forbidden in the program settings for ",__FILE__);
         return(false);
        }
     }
   if(!AccountInfoInteger(ACCOUNT_TRADE_EXPERT))
     {
      Alert("Automated trading is forbidden for the account ",AccountInfoInteger(ACCOUNT_LOGIN),
            " at the trade server side");
      return(false);
     }
   if(!AccountInfoInteger(ACCOUNT_TRADE_ALLOWED))
     {
      Comment("Trading is forbidden for the account ",AccountInfoInteger(ACCOUNT_LOGIN),
              ".\n Perhaps an investor password has been used to connect to the trading account.",
              "\n Check the terminal journal for the following entry:",
              "\n\'",AccountInfoInteger(ACCOUNT_LOGIN),"\': trading has been disabled - investor mode.");
      return(false);
     }
//---
   return(true);
  }
//+------------------------------------------------------------------+ 
//| Get the High for specified bar index                             | 
//+------------------------------------------------------------------+ 
double iHigh(const int index,string symbol=NULL,ENUM_TIMEFRAMES timeframe=PERIOD_CURRENT)
  {
   if(symbol==NULL)
      symbol=m_symbol.Name();
   if(timeframe==0)
      timeframe=Period();
   double High[1];
   double high=0.0;
   int copied=CopyHigh(symbol,timeframe,index,1,High);
   if(copied>0)
      high=High[0];
   return(high);
  }
//+------------------------------------------------------------------+ 
//| Get Low for specified bar index                                  | 
//+------------------------------------------------------------------+ 
double iLow(const int index,string symbol=NULL,ENUM_TIMEFRAMES timeframe=PERIOD_CURRENT)
  {
   if(symbol==NULL)
      symbol=m_symbol.Name();
   if(timeframe==0)
      timeframe=Period();
   double Low[1];
   double low=0.0;
   int copied=CopyLow(symbol,timeframe,index,1,Low);
   if(copied>0)
      low=Low[0];
   return(low);
  }
//+------------------------------------------------------------------+ 
//| Get Time for specified bar index                                 | 
//+------------------------------------------------------------------+ 
datetime iTime(const int index,string symbol=NULL,ENUM_TIMEFRAMES timeframe=PERIOD_CURRENT)
  {
   if(symbol==NULL)
      symbol=m_symbol.Name();
   if(timeframe==0)
      timeframe=Period();
   datetime Time[1];
   datetime time=0;
   int copied=CopyTime(symbol,timeframe,index,1,Time);
   if(copied>0)
      time=Time[0];
   return(time);
  }
//+------------------------------------------------------------------+
//| Get value of buffers for the iCustom                             |
//|  the buffer numbers are the following:                           |
//+------------------------------------------------------------------+
bool iCustomGet(int handle,const int buffer,const int start_pos,const int count,double &array[])
  {
   ResetLastError();
   int copy=CopyBuffer(handle,buffer,start_pos,count,array);
   if(copy!=count)
     {
      PrintFormat("Failed to copy (%d) data from the iCustom indicator, error code %d",copy,GetLastError());
      return(false);
     }
   return(true);
  }
//+------------------------------------------------------------------+
//| Compare doubles                                                  |
//+------------------------------------------------------------------+
bool CompareDoubles(double number1,double number2,int digits)
  {
   if(NormalizeDouble(number1-number2,digits)==0)
      return(true);
   else
      return(false);
  }
//+------------------------------------------------------------------+
打赏
版权所有转载请注明标题及链接:峰汇在线 » 根据之字转向指标信号开发的EA ZigZagEvgeTrofi ver.1.mq5
上一篇:
下一篇:
分享到: 更多 (0)

如您觉得此文不错请扫码打赏!

支付宝扫一扫打赏

微信扫一扫打赏