MQL4 - automated forex trading   /  

Forum

EA using the crossing of 3 ema lines,simple and useful

Back to topics list  | 1 2 To post a new topic, please log in or register

avatar
108
OldZ 2010.12.28 07:55 
//+------------------------------------------------------------------+
//|                                                MaCross_EA_V3.mq4 |
//|                     Revised by OldZ  2010-12-27 23:09            |
//|                     Email: oldz.cn@qq.com                        |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "OldZ" 
#property link      "http://www.metaquotes.net"

extern int         Lots=1;
extern int         MA1=5;
extern int         MA2=13;
extern int         MA3=144;
extern int         TimeFrame=0;

extern int         Trail_Stop = 600; // trail stop 60 points ( 4 digits )
extern int         Slippage = 50;     // slippage 5 points
extern int         StopLoss = 1200; //  stop loss 120 points 
extern int         TakeProfit = 2400; // take profit 240 points 

extern int         MagicNumber=998000;
datetime           TimeOpen[1];
int                Mode_MA=1;  //MODE_EMA =1
int                Mode_Price=0; // PRICE_CLOSE = 0 

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----     
   TimeOpen[0]=0;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {  
  double SL=0.0; // price of stop loss when opening an order 
  double TP=0.0; // price of take prifit when opening an order 
   
   if(MaCross(MA2,MA3,TimeFrame,1)==11) //to determine the main trend :trend up
   if(MaCross (MA1,MA2,TimeFrame,1)==1 && TimeOpen[0]!=Time[0]) // then if there is a crossing same as the main  up trend
   {
     SL=Ask-StopLoss*Point;
     SL=NormalizeDouble(SL,Digits);
     
     TP=Ask+TakeProfit*Point;
     TP=NormalizeDouble(TP,Digits);

     if (!OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP,"ea buy",MagicNumber,0,Red))
     Alert( "OrderSend Error #", GetLastError() );
     TimeOpen[0]=Time[0];
   }
   
   if(MaCross(MA2,MA3,TimeFrame,1)==-11) // to determine the main trend: trend down
   if (MaCross (MA1,MA2,TimeFrame,1)==-1 && TimeOpen[0]!=Time[0]) // then if there is a crossing same as the main down trend
   { 
     SL=Bid+StopLoss*Point;
     SL=NormalizeDouble(SL,Digits);
     
     TP=Bid-TakeProfit*Point;
     TP=NormalizeDouble(TP,Digits);
     
     if (!OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SL,TP,"ea sell",MagicNumber,0,Blue))
     Alert( "OrderSend Error #", GetLastError() );
     TimeOpen[0]=Time[0];
   }
   
   // sometimes,the market moves fast in one way.In this case,MA1 will cross MA3 earlier than MA2 cross MA3
   // when MA2 is crossing MA3,the MA1 is on the blow or above of MA3 
   // so there are 2 cases
   // case1:
   if(MaCross(MA1,MA3,TimeFrame,1)==-11)// MA1 is on the below of MA3
   if(MaCross(MA2,MA3,TimeFrame,1)==-1  && TimeOpen[0]!=Time[0]) // then MA2 crossed down MA3...
    { 
     SL=Bid+StopLoss*Point;
     SL=NormalizeDouble(SL,Digits);
     
     TP=Bid-TakeProfit*Point;
     TP=NormalizeDouble(TP,Digits);
     
     if (!OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SL,TP,"ea sell",MagicNumber,0,Blue))
     Alert( "OrderSend Error #", GetLastError() );
     TimeOpen[0]=Time[0];
   }
   
   // case 2:
   if(MaCross(MA1,MA3,TimeFrame,1)==11)// MA1 is on the above of MA3
   if(MaCross(MA2,MA3,TimeFrame,1)==1  && TimeOpen[0]!=Time[0]) //then MA2 crossed up MA3...
    {
     SL=Ask-StopLoss*Point;
     SL=NormalizeDouble(SL,Digits);
     
     TP=Ask+TakeProfit*Point;
     TP=NormalizeDouble(TP,Digits);

     if (!OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP,"ea buy",MagicNumber,0,Red))
     Alert( "OrderSend Error #", GetLastError() );
     TimeOpen[0]=Time[0];
   }
      
   TrailStop();
   
   //---- close buy order 
   if (MaCross (MA2,MA3,TimeFrame,1)==-1)
   {
     if (SearchOrder (MagicNumber,OP_BUY)>0)
     CloseOrder(SearchOrder(MagicNumber,OP_BUY));     
   }
   
   //--- close sell order  
   if (MaCross (MA2,MA3,TimeFrame,1)==1)
   {
     if (SearchOrder(MagicNumber,OP_SELL)>0)
     CloseOrder(SearchOrder(MagicNumber,OP_SELL));     
   }
   
//----
   return(0);
   
}  // End of Start function 
  
  
//+------------------------------------------------------------------+
//| MaCross function                                                 |
//+------------------------------------------------------------------+
int MaCross (int MaFast,int MaSlow,int TimeFrame,int i)
  {
//----
   double MaF[2],MaS[2];
   int Flag=0;
   MaF[0]=iMA(NULL,TimeFrame,MaFast,0,Mode_MA,Mode_Price,0+i);
   MaF[1]=iMA(NULL,TimeFrame,MaFast,0,Mode_MA,Mode_Price,1+i);
   MaS[0]=iMA(NULL,TimeFrame,MaSlow,0,Mode_MA,Mode_Price,0+i);
   MaS[1]=iMA(NULL,TimeFrame,MaSlow,0,Mode_MA,Mode_Price,1+i);
   
   MaF[0]=NormalizeDouble(MaF[0],Digits);
   MaF[1]=NormalizeDouble(MaF[1],Digits);
   MaS[0]=NormalizeDouble(MaS[0],Digits);
   MaS[1]=NormalizeDouble(MaS[1],Digits);  
   
   /* before opening order,we should determine the main trend,
     then if there is a crossing signal with the same direction as main trend
     the crossing is useful,otherwise DO NOT trade.
   */  
  
   
   // Main Trend: to determine main trend with MA2 and MA3
   if (MaF[0]>MaS[0]  &&  MaF[1]>MaS[1]) Flag=11;  // if MA2 is on the above of MA3,the main trend is UP.
   if (MaF[0]<MaS[0]  &&  MaF[1]<MaS[1]) Flag=-11; // if MA2 is on the below of MA3,the main trend is DOWN
   
    //Trading Signal:  when MA1 is crossing MA2,it trigger a trading signal
   if (MaF[0]>MaS[0]  &&  MaF[1]<MaS[1]) Flag=1;
   if (MaF[0]<MaS[0]  &&  MaF[1]>MaS[1]) Flag=-1;
   
//----
   return(Flag);
  } // End of MaCross function 
Testing (Optimization) Technique and Some Criteria for Selection of the Expert Advisor Parameters

Testing (Optimization) Technique and Some Criteria for Selection of the Expert Advisor Parameters

There is no trouble finding the Holy Grail of testing, it is however much more difficult to get rid of it. This article addresses the selection of the Expert Advisor operating parameters with automated group processing of optimisation and testing results upon maximum utilisation of the Terminal performance capabilities and minimum end user load.


avatar
108
OldZ 2010.12.28 07:57 
//+------------------------------------------------------------------+
//| SearchOrder function                                             |
//+------------------------------------------------------------------+
int SearchOrder (int MagicNumber,int Order_Type)
  {
//----
   int Ticket=-1;
   for ( int i = OrdersTotal() - 1; i >= 0; i -- )
   {
     if ( !OrderSelect( i, SELECT_BY_POS ) )
     {Print("OrderSelect(",i,",SELECT_BY_POS) - Error #",GetLastError());
       continue;
     }
     if (OrderSymbol()!=Symbol())continue;
     if (OrderMagicNumber()!=MagicNumber )continue;
     if ( OrderType()==Order_Type) Ticket=OrderTicket();
   }
//----
   return(Ticket);
} // End of SearchFunction
  
  
//+------------------------------------------------------------------+
//| CloseOrder function                                              |
//+------------------------------------------------------------------+
void CloseOrder(int Order_Ticket)
  {
//----
   if (!OrderSelect(Order_Ticket,SELECT_BY_TICKET ) )   
     Print("OrderSelect(",OrderTicket(), ",SELECT_BY_POS) - Error #",GetLastError() );  
   
   //-
   if(OrderType()==OP_BUY)
   if (!OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue))   
     Print("OrderClose(",OrderTicket(), ",SELL) - Error #",GetLastError() );   
   
   if(OrderType()==OP_SELL)
   if (!OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Blue)) 
     Print("OrderClose(",OrderTicket(), ",SELL) - Error #",GetLastError() );  
   
//---- 
  } // End of CloseOrder 

//+------------------------------------------------------------------+
//| TrailStop function                                               |
//  refer to  http://book.mql4.com/trading/ordermodify                |
//+------------------------------------------------------------------+
void  TrailStop()
{
string Text="";                     
bool Modify=false;

  for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()<=1 )
        {                                      
         int Tip=OrderType(); 
         double SL=OrderStopLoss();            // SL of the selected order  
         double TP    =OrderTakeProfit();   // TP of the selected order
         double Price =OrderOpenPrice();     // Price of the selected order 
         int    Ticket=OrderTicket();        // Ticket of the selected order. 
         double TS=Trail_Stop;               // Initial value
         int Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL);//Min. distance 
         if (TS<Min_Dist) TS=Min_Dist;     // If less than allowed // New value of TS
          
            switch(Tip)                         // By order type  Tip ==0
              {
               case 0 :                         // Order Buy  
                  if (NormalizeDouble(SL,Digits)< NormalizeDouble(Bid-TS*Point,Digits)) // If it is lower than we want 
                    {
                     SL=Bid-TS*Point;           // then modify SL price 
                     Text="Buy Trail Stop ";        // Text for Buy order trail stop
                     Modify=true;              // To be modified 
                    }  break;                  // Exit 'switch'  
               case 1 :                         // Order Sell,Tip ==1
                  if (NormalizeDouble(SL,Digits)> NormalizeDouble(Ask+TS*Point,Digits) // If it is higher than we want                     
                     || NormalizeDouble(SL,Digits)==0)//or equal to zero,it means we did not set stop less when open order 
                    {
                     SL=Ask+TS*Point;           // then modify it 
                     Text="Sell Trail Stop ";             // Text for Sell
                     Modify=true;               // To be modified 
                    } break;                    // Exit 'switch' 
              }                                 // End of 'switch' 
          
            //--------------------------------------------------- 5 --
           
            if(Modify == true)
            {
        //    Alert ("Modification ",Text,Ticket,". Awaiting response..");
            bool Ans=OrderModify(Ticket,Price,SL,TP,0,Blue);//Modify it!
            }
            //--------------------------------------------------- 6 --
            if (Ans==true)                     // Got it! :)
              {
         //      Alert ("Order ",Text,Ticket," is modified:)");
               break;                           // From modification cycle.
              }
            //--------------------------------------------------- 7 --
            if(Ans==false)
            {
            int Error=GetLastError();          // Failed :(
            switch(Error)                       // Overcomable errors  
              {
               case 130:Alert("Wrong stops. Retrying.");
                  RefreshRates();               // Update data
                  continue;                     // At the next iteration
                  
              case 136:Alert("No prices. Waiting for a new tick..");
                  while(RefreshRates()==false)  // To the new tick
                     Sleep(1);                  // Cycle delay
                  continue;                     // At the next iteration 
                  
               case 146:Alert("Trading subsystem is busy. Retrying ");
                 Sleep(500);                   // Simple solution
                  RefreshRates();               // Update data
                  continue;                     // At the next iteration 
                  
                 // Critical errors
               case 2 : Alert("Common error."); break;    // Exit 'switch'
               case 5 : Alert("Old version of the client terminal."); break; // Exit 'switch'
               case 64: Alert("Account is blocked.");  break;  // Exit 'switch' 
               case 133:Alert("Trading is prohibited"); break;  // Exit 'switch'
             //  default: Alert("Occurred error ",Error);  break; //Other errors    Exit 'switch'               
               
                default:  break; //Other errors    Exit 'switch'    
       
              }  // End switch                                     
           }      // End if Ans==false                               
        
         } // End for 
         
      }  // End if 

} // Een TrailStop function 

avatar
108
OldZ 2010.12.28 07:59 


avatar
108
OldZ 2010.12.28 08:01 

Attached files:
  MaCross_EA_V3.mq4 (12.62 KB)

avatar
6
ammodawg 2010.12.28 08:51 
Can any one put these two indicators together into one EA. When both indicators show green=long, when both are red = short.
Attached files:
  TrendLord.ex4 (3.08 KB)

avatar
6
ammodawg 2010.12.28 08:51 
here is the 2nd
Attached files:
  TrendLord_1.ex4 (3.08 KB)

avatar
6
ammodawg 2010.12.28 08:51 
sorry...here it is
Attached files:
  Fisher_Yur4ik.mq4 (2.05 KB)

avatar
6
ammodawg 2010.12.28 08:52 
I've been trading it on the M30, H1, and H4 succefully -- manually. But if someone could put it on an EA that would be cool!

avatar
135
serpentsnoir 2010.12.28 16:17 
OldZ:


Hey there, did you try the EA with "Every Tick"?. I have had poor test-to-reality experiences when I back test using "Control Points".

avatar
9
seanwood 2010.12.28 17:55 

Your advisor's back test is extreme bad and I want to known how you get such report.

If I use control point, my advisor also get good result:

商品EURUSD (Euro vs US Dollar)
时间周期1 小时图 2010.07.08 07:00 - 2010.12.28 14:00 (2010.07.01 - 2011.01.01)
复盘模型控制点(基于最近的小一级时段内的12个控制点的分形插值计算)
参数STDPeriod=50; SDLPeriod=100; Stoploss=80; ShockWave=12; ShockWaveTP=4; ShockWaveTP2=12; OneLots=5; MaxLots=10;
经测试过的柱数3481用于复盘的即时价数量61394复盘模型的质量n/a
输入图表错误1704
起始资金10000.00
总净盈利90934.00总获利424738.00总亏损-333804.00
盈利比1.27预期盈利531.78
绝对亏损1679.50最大亏损47908.50 (45.85%)相对亏损55.31% (12750.00)
交易单总计171卖单 (获利百分比)87 (49.43%)买单 (获利百分比)84 (50.00%)
盈利交易(占总百分比)85 (49.71%)亏损交易(占总百分比)86 (50.29%)
最大:获利交易18602.00亏损交易-4060.00
平均:获利交易4996.92亏损交易-3881.44
最大:连续获利金额8 (39202.50)连续亏损金额6 (-23998.00)
最多:连续获利次数39202.50 (8)连续亏损次数-23998.00 (6)
平均:连续获利2连续亏损2


avatar
108
OldZ 2011.01.06 06:28 
serpentsnoir:

Hey there, did you try the EA with "Every Tick"?. I have had poor test-to-reality experiences when I back test using "Control Points".

I think it depends on your codes and your broker limitation instead of "Every Tick".
Back to topics list   | 1 2  

To add comments, please log in or register