This forum is in read-only mode now. You may discuss your questions on forums of MQL4.community and MQL5.community
my advisor can not work?Back to topics list |
|
wannaleo
2005.07.13 19:02
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!
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);
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<macur2 && macur1>macur2)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,SlipPage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"am crossing",16384,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>macur2 && macur1<macur2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,SlipPage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"am crossing",16384,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()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if (mapre1>macur2 && macur1<macur2)
{
OrderClose(OrderTicket(),OrderLots(),Bid,SlipPage,Violet); // close position
return(0); // exit
}
}
}
else // go to short position
{
// should it be closed?
if(mapre1<macur2 && macur1>macur2)
{
OrderClose(OrderTicket(),OrderLots(),Ask,SlipPage,Violet); // close position
return(0); // exit
}
}
}
//----
//----
return(0);
} |
|
hdb
2005.07.14 01:56
well, for a start PerShort, etc are not defined. I guess some code is missing. can u complete?
|
|
wannaleo
2005.07.14 02:56
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);
}
|
|
hdb
2005.07.14 14:26
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,
#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);
}
|
|
wannaleo
2005.07.15 01:59
Thanks , hdb. Now I know where are the problems.
By the way, where can I get the backtester and how to use it? |
|
hdb
2005.07.15 04:20
yw... the back tester is part of MT 4.. under menu 'View' item 'Strategy Tester'
enjoy! |
|
wannaleo
2005.07.16 00:24
oh, it is. I had used it, Thanks.
|
Download MetaTrader 5 (450 Kb, web installer) — a new terminal for financial markets
