| / | Forum |
|
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 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. |
|
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 |
|
OldZ
2010.12.28 07:59
|
|
OldZ
2010.12.28 08:01
|
|
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.
|
|
ammodawg
2010.12.28 08:51
here is the 2nd
|
|
ammodawg
2010.12.28 08:51
sorry...here it is
|
|
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!
|
|
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". |
|
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:
|
|
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". |