I write a advisor program based on two-EMA-Crossing, but it can not work. The program can not give any signals and open any positions. There are some wrong with it , but I dont know what the problem is. Please help me check it, thanks!
- Flexible MetaTrader 5 trading system with all order types
- MetaTrader 5 Built-in Trading Strategy Tester
- MQL5 Wizard: Development of trading robots for MetaTrader 5
well, for a start PerShort, etc are not defined. I guess some code is missing. can u complete?
Sorry, I didnt paste the definition of the extern variables. The complete program is following, thanks again.
#property copyright "Copyright@2005, wannale" #property link "http://www.metaquotes.net " //---- input parameters extern double Lots = 0.1; extern int PerShort=5; extern int PerShiftShort=-2; extern int PerLong=13; extern int PerShiftLong=0; extern int SlipPage=3; extern int TakeProfit=30; extern int StopLoss=20; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { double mapre1,mapre2,macur1,macur2; int total,cnt,ticket; if(Bars<100) { Print("bars less than 100"); return(0); } // Go to Trading, calculating MA mapre1=iMA(NULL,0,PerShort,PerShiftShort,MODE_EMA,PRICE_CLOSE,-1); mapre2=iMA(NULL,0,PerLong,PerShiftLong,MODE_EMA,PRICE_CLOSE,-1); macur1=iMA(NULL,0,PerShort,PerShiftShort,MODE_EMA,PRICE_CLOSE,0); macur2=iMA(NULL,0,PerLong,PerShiftLong,MODE_EMA,PRICE_CLOSE,0); total=OrdersTotal(); if (total < 1) { //if (AccountFreeMargin()<(1000*Lots)) // { // Print("We have no money. Free Margin = ", AccountFreeMargin()); // return(0); // } // check for long position (BUY) possibility if ( mapre1<mapre2 && macur1>macur2 && macur1>mapre1) { ticket=OrderSend(Symbol(),OP_BUY,Lots,Close,SlipPage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"am crossing",16677,0,Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice()); } else Print("Error opening BUY order : ",GetLastError()); return(0); } // check for short position (SELL) possibility if (mapre1>mapre2 && macur1<macur2 && macur1<mapre1) { ticket=OrderSend(Symbol(),OP_SELL,Lots,Close,SlipPage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"am crossing",16677,0,Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice()); } else Print("Error opening SELL order : ",GetLastError()); return(0); } } // it is important to enter the market correctly, // but it is more important to exit it correctly... for (cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderType()<=1 && // check for opened position OrderSymbol()==Symbol()) // check for symbol { if(OrderType()==0) // long position is opened { // should it be closed? if (mapre1>mapre2 && macur1<macur2 && macur1<mapre1) { OrderClose(OrderTicket(),OrderLots(),Close,SlipPage,Violet); // close position return(0); // exit } } } else // go to short position { // should it be closed? if(mapre1<mapre2 && macur1>macur2 && macur1>mapre1) { OrderClose(OrderTicket(),OrderLots(),Close,SlipPage,Violet); // close position return(0); // exit } } } //---- //---- return(0); }
here u go...
There were three errors...
1) a problem with the OrderSend instruction.. it gave an error in the journal.. I changed Close to Ask, Bid w/o slippage.. it works but if u want it to close,play around...
2) the MA bar parameters needs to be positive.. I changed -1 to 1
3) the PerShiftShort parameter was negative, I assume it needs to be zero or positive.. i have not checked.
All changes have // HDB ...
When u debug,
1) look at your journal for errors.. this gives good clues as to what is hapening
2) put in print() statements to CHECK that the values are what u think they should be.
The ma were zero so this told me where the problem was
3) Now u can use the backtester to see if the expert works.. u dont have to wait for a demo cahrt to do something!
Good coding,
There were three errors...
1) a problem with the OrderSend instruction.. it gave an error in the journal.. I changed Close to Ask, Bid w/o slippage.. it works but if u want it to close,play around...
2) the MA bar parameters needs to be positive.. I changed -1 to 1
3) the PerShiftShort parameter was negative, I assume it needs to be zero or positive.. i have not checked.
All changes have // HDB ...
When u debug,
1) look at your journal for errors.. this gives good clues as to what is hapening
2) put in print() statements to CHECK that the values are what u think they should be.
The ma were zero so this told me where the problem was
3) Now u can use the backtester to see if the expert works.. u dont have to wait for a demo cahrt to do something!
Good coding,
#property copyright "Copyright@2005, wannale" #property link "http://www.metaquotes.net " //---- input parameters extern double Lots = 0.1; extern int PerShort=5; extern int PerShiftShort=0; // HDB CHANGED -2 to 0 extern int PerLong=13; extern int PerShiftLong=0; extern int SlipPage=3; extern int TakeProfit=30; extern int StopLoss=20; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { double mapre1,mapre2,macur1,macur2; int total,cnt,ticket; if(Bars<100) { Print("bars less than 100"); return(0); } // Go to Trading, calculating MA mapre1=iMA(NULL,0,PerShort,PerShiftShort,MODE_EMA,PRICE_CLOSE,1); // HDB CHANGED -1 to 1 mapre2=iMA(NULL,0,PerLong,PerShiftLong,MODE_EMA,PRICE_CLOSE,1); // HDB CHANGED -1 to 1 macur1=iMA(NULL,0,PerShort,PerShiftShort,MODE_EMA,PRICE_CLOSE,0); macur2=iMA(NULL,0,PerLong,PerShiftLong,MODE_EMA,PRICE_CLOSE,0); total=OrdersTotal(); if (total < 1) { //if (AccountFreeMargin()<(1000*Lots)) // { // Print("We have no money. Free Margin = ", AccountFreeMargin()); // return(0); // } // check for long position (BUY) possibility // Print("ma 1, 2: ",macur1, " ",macur2, " ",mapre1," ",mapre2); // HDB ADDED for tests if ( mapre1<mapre2 && macur1>macur2 && macur1>mapre1) { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Ask-StopLoss*Point,Ask+TakeProfit*Point,"am crossing",16677,0,Green); // HDB CHANGED Close TO Ask and SlipPage to 0 if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice()); } else Print("Error opening BUY order : ",GetLastError()); return(0); } // check for short position (SELL) possibility if (mapre1>mapre2 && macur1<macur2 && macur1<mapre1) { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,Bid+StopLoss*Point,Bid-TakeProfit*Point,"am crossing",16677,0,Red); // HDB CHANGED Close TO Bid and SlipPage to 0 if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice()); } else Print("Error opening SELL order : ",GetLastError()); return(0); } } // it is important to enter the market correctly, // but it is more important to exit it correctly... for (cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderType()<=1 && // check for opened position OrderSymbol()==Symbol()) // check for symbol { if(OrderType()==0) // long position is opened { // should it be closed? if (mapre1>mapre2 && macur1<macur2 && macur1<mapre1) { OrderClose(OrderTicket(),OrderLots(),Close,SlipPage,Violet); // close position return(0); // exit } } } else // go to short position { // should it be closed? if(mapre1<mapre2 && macur1>macur2 && macur1>mapre1) { OrderClose(OrderTicket(),OrderLots(),Close,SlipPage,Violet); // close position return(0); // exit } } } //---- //---- return(0); }
Thanks , hdb. Now I know where are the problems.
By the way, where can I get the backtester and how to use it?
By the way, where can I get the backtester and how to use it?
oh, it is. I had used it, Thanks.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register