MY first meta 4 program

 

Hello, my name is Andy and I'm a computer engineering student. This is my first meta4 expert adviser program, can anyone tell me whats wrong with it.


Function.

Its an EMA 9.33 program, its supposed to open only one position at a time and this happens on signal cross.


//+------------------------------------------------------------------+
//| SparkCross.mq4 |
//| Copyright © 2009, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "SPARKANDY"
#property link "http://www.metaquotes.net "
//---- input parameters
extern double TakeProfit = 20;
extern double StopLoss = 30;
extern double Lots = 1;
extern double TrailingStop = 50;
extern int ShortEma = 9;
extern int LongEma = 33;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int Crossed (double line1, double line2)
{
static int last_direction = 0;
static int current_direction = 0;
//Don't work in the first load, wait for the first cross!
static bool first_time = true;
if(first_time == true)
{
first_time = false;
return (0);
}
//----
if(line1 > line2)
current_direction = 1; //up
if(line1 < line2)
current_direction = 2; //down
//----
if(current_direction != last_direction) //changed
{
last_direction = current_direction;
return(last_direction);
}
else
{
return (0); //not changed
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int cnt, ticket, total, cmd,error;
double SEma, LEma, price;
bool result;


//----
if(Bars < 100)
{
Print("bars less than 100");
return(0);
}
//----
//if(TakeProfit < 10)
//{
//Print("TakeProfit less than 10");
//return(0); // check TakeProfit
//}
//----
SEma = iMA(NULL, 0, ShortEma, 0, MODE_EMA, PRICE_HIGH, 0);
LEma = iMA(NULL, 0, LongEma, 0, MODE_EMA, PRICE_CLOSE, 0);
//----
static int isCrossed = 0;
isCrossed = Crossed (LEma, SEma);
//----
total = OrdersTotal();


//----

if (isCrossed == 1) // CLOSE ALL OPENED POSITIONS AND EXECUTE A BUY ORDER
{

//----
//CLOSE FIRST OPENED POSITION ON THE LIST

if (total>0)
{
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();
//---- first order is buy or sell
if(cmd==OP_BUY || cmd==OP_SELL)
{
while(true)
{
if(cmd==OP_BUY) price=Bid;
else price=Ask;
result=OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
}
}
else Print( "Error when order select ", GetLastError());

}
//----

PlaySound("Alert.wav");
Print("BUY!!!");

ticket=OrderSend(Symbol(),OP_BUY,1,Ask,3,0,0,"",16384,0,Blue);//BUY

if(ticket<0)
{
Print("OrderSend failed with error #",GetLastError());
return(0);
}

}

//----
else if (isCrossed == 2) // CLOSE ALL OPENED POSITIONS AND EXECUTE A SELL ORDER
{


//----
//CLOSE FIRST OPENED POSITION ON THE LIST

if (total>0)
{
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();
//---- first order is buy or sell
if(cmd==OP_BUY || cmd==OP_SELL)
{
while(true)
{
if(cmd==OP_BUY) price=Bid;
else price=Ask;
result=OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
}
}
else Print( "Error when order select ", GetLastError());
}

//----



ticket=OrderSend(Symbol(),OP_SELL,1,Bid,3,0,0,"",16384,0,Green); //SELL


if(ticket<0)
{
Print("OrderSend failed with error #",GetLastError());
return(0);
}

}


//----
return(0);
}
//+------------------------------------------------------------------+

 

Hi Andy


You may get a better response if you make it easier for people to help. You are asking people to read your code and figure out what each part is supposed to do and that's a fair effort, especially when it's not formatted. I offer the following suggestions.

1. When you post code, clikc on the SRC button on the toolbar at the top of the comment editor - it colour codes and indents the code so it's easier to read.

2. You have said what you want it to do, but what actually happens? What happens when you run it. Does it crash? Does it open an order every tick? Does it open no orders? etc.

3. Say what you've tried in your own debgging efforts. Print statements in the code in places can give good clues.


Cheers

Jellybean

 

If you use the close price you will get false signals so try this as a quick fix.

SEma = iMA(NULL, 0, ShortEma, 0, MODE_EMA, PRICE_HIGH, 1);
LEma = iMA(NULL, 0, LongEma, 0, MODE_EMA, PRICE_CLOSE, 1);

Also a common problem is not to think about the first trade in terms of initialisation.