EA Help

Hi guys, I am still quite new to all this EA programming. I have done up a trail EA on my own but I keep encountering some errors. My EA would only open 1 trade and after the first trade, it stops opening trade even after the conditions are met. Please advise if anyone knows what is wrong with my codes. :frowning:

Criteria:

  1. when SMA short crosses SMA Long upwards, only look at long trade. Open trade when LWMA short crosses LWMA long upwards.

  2. when SMA short crosses SMA Long downwards, only look at short trade. Open trade when LWMA short crosses LWMA long downwards.

  3. only open 1 trade at a time.

  4. SL and TP = 100pips

extern double TakeProfit=100.0;
extern double Lots=0.01;
extern double StopLoss=100.0;
//±-----------------------------------------------------------------+
//| expert initialization function |
//±-----------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
int mainCrossed (double mainline1 , double mainline2)
{
static int mainlast_direction = 0;
static int maincurrent_dirction = 0;
if(mainline1>mainline2)maincurrent_dirction = 1; //main up
if(mainline1<mainline2)maincurrent_dirction = 2; //main down
if(maincurrent_dirction != mainlast_direction) //main changed
{
mainlast_direction = maincurrent_dirction;
return (mainlast_direction);
}
else
{
return (0);
}
}

int Crossed (double line1 , double line2)
{
static int last_direction = 0;
static int current_dirction = 0;
if(line1>line2)current_dirction = 1; //up
if(line1<line2)current_dirction = 2; //down
if(current_dirction != last_direction) //changed
{
last_direction = current_dirction;
return (last_direction);
}
else
{
return (0);
}
}
//±-----------------------------------------------------------------+
//| expert start function |
//±-----------------------------------------------------------------+
int start()
{
//----
int cnt, ticket, total;
double shortEma, longEma, mainshortEma, mainlongEma;
if(Bars<100)
{
Print(“bars less than 100”);
return(0);
}
if(TakeProfit<10)
{
Print(“TakeProfit less than 10”);
return(0); // check TakeProfit
}
shortEma = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,0);
longEma = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,0);

mainshortEma = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0);
mainlongEma = iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);

int mainisCrossed = mainCrossed (mainshortEma,mainlongEma);

int isCrossed = Crossed (shortEma,longEma);

total = OrdersTotal();
if(total < 1)
{
if(mainisCrossed == 1)
{

        if(isCrossed == 1)
        {
           ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,
           "My EA",12345,0,Green);
           if(ticket&gt;0)
           {
              if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
              Print("BUY order opened : ",OrderOpenPrice());
           }
           else Print("Error opening BUY order : ",GetLastError());
           return(0);
        }

if(mainisCrossed == 2)
{
if(isCrossed == 2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLossPoint,
Bid-TakeProfit
Point,“My EA”,12345,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);
}
return(0);
Print(“total for mainisCrossed == 2 at end is”,total);
}
}
return(0);
}

Thanks in advance guys. :slight_smile:

Regards
Terrance

  1. Direction
  2. Crossed (always true until lines touch)
  3. Just crossed

3 concepts you must abstract separately.

Your maincrossed method says only return a direction when a change in direction occurs otherwise return 0. That means it only does its job for one tick. This is not what you wish for according to your spec.

Your cross method says only return a direction when a change in direction occurs. This means at the point the lines cross over that tick will yield a direction. This appears to follow your wishes.

The issue is these two events have to occur in the same tick to take effect i.e a change in direction in both the maincross and cross.

First time you run your code, your methods use variables set to 0. So as soon as LA1>LA2 or LA1<LA2 and LB1>LB2 or LB1<LB2, the comparison with those 0s constitute a ‘change in direction’. If the lines cross to the same direction (e.g u run the code within a trend) the code will execute straight away first time.

While in the trade, the cross overs can occur again before stop-out / take-prof so they get missed.

After the first trade, u need both crosses to happen to the same direction in the very same tick to get another trade unless u have maincross only return a direction and never 0. In which case the change of direction in the cross will trigger another trade in that tick if the new direction agrees with the one held by maincross.

Hope this makes sense, been up 18 hours :stuck_out_tongue:

Hi, sorry if i do not get what you mean, however I have made some adjustments based on what you have told me. Now the code just opens a Buy and Sell order at the same time whenever I have no trade open. please help. :frowning:

//±-----------------------------------------------------------------+
//| My_First_EA.mq4 |
//| zzz|
//|
//±-----------------------------------------------------------------+
#property copyright “zzz”

//— input parameters
extern double TakeProfit=1000.0;
extern double Lots=0.01;
extern double StopLoss=1500.0;
//±-----------------------------------------------------------------+
//| expert initialization function |
//±-----------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
int mainCrossed (double mainline1 , double mainline2)
{
static int mainlast_direction = 0;
static int maincurrent_dirction = 0;
if(mainline1>mainline2)maincurrent_dirction = 1; //main up
if(mainline1<mainline2)maincurrent_dirction = 2; //main down
if(maincurrent_dirction != mainlast_direction) //main changed
{
mainlast_direction = maincurrent_dirction;
return (mainlast_direction);
}
else
{
return (0);
}
}

int Crossed (double line1 , double line2)
{
static int last_direction = 0;
static int current_dirction = 0;
if(line1>line2)current_dirction = 1; //up
if(line1<line2)current_dirction = 2; //down
if(current_dirction != last_direction) //changed
{
last_direction = current_dirction;
return (last_direction);
}
else
{
return (0);
}
}
//±-----------------------------------------------------------------+
//| expert start function |
//±-----------------------------------------------------------------+
int start()
{
//----
int cnt, ticket, total;
double shortEma, longEma, mainshortEma, mainlongEma;
if(Bars<100)
{
Print(“bars less than 100”);
return(0);
}
if(TakeProfit<10)
{
Print(“TakeProfit less than 10”);
return(0); // check TakeProfit
}
shortEma = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,0);
longEma = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,0);

mainshortEma = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0);
mainlongEma = iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);

int mainisCrossed = mainCrossed (mainshortEma,mainlongEma);

int isCrossed = Crossed (shortEma,longEma);

total = OrdersTotal();
if(total < 1)
{
if(mainisCrossed == 1 && isCrossed == 1)
Print("1 an 1 and total is ",total);
{
Print("1.2 an 1.2 and total is ",total);

           ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,
           "My EA",12345,0,Green);
           
           Print("buy success 1.3 an 1.3 and total is ",total);
        }
        
   
        if(mainisCrossed == 2 && isCrossed == 2)
        Print("2 an 2 and total is ",total);
        {
           Print("2.2 an 2.2 and total is ",total);
           
           ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,
           Bid-TakeProfit*Point,"My EA",12345,0,Red);
           
           Print("sell success 2.3 an 2.3 and total is ",total);
        }

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

Thanks and regards
Terrance

if(something)
doSomething();
{
doSomethingElse();
}

in this example (as you have done with your print() statements) doSomethingElse will always be executed regardless
because only the doSomething() belongs to the if statement and this posession ends at the ; if you have an if statement not followed by a code block {}, then only the statement directly after the if statement is binded to it. if u use a code block {}, the entire code block is binded to the if statement.

if(something)
{
doSomething();
doSomethingElse();
}

in this example doSomething() and doSomethingElse() can only occur when the if statement is true.

.
.
.
if(something)
doSomething();
.
.
.
if(something)
{
doSomethingElse();
}
.
.
.
in this example doSomething() and doSomethingElse() will both only be called when the if statement is true but are in their own code sections and can be surround by other code before, between and after the if statements and still both execute as they need to.

Hi, sorry for being slow as all this is still mew to me, but I am still a bit unsure because I have tried the methods that you have mentioned above, however I keep getting back the same issue, like the system will only open one trade and no more trades will be opened afte, that’s why I’m very lost. Please help me here.

Thanks.
Terrance

Hi I did some editing and decided to focus on short trade first just to make things simpler. However, the situation still remains the same. After executing 1 trade, it stop opening new trades. my criteria are there should only be 1 trade open at any point of time and trade will only open when LWMA fast crossed over LWMA slow and SMA fast crossed over SMA slow, both crossed downwards.

My TP is 100pips and SL is 150pips

Please help. :frowning:

//±-----------------------------------------------------------------+
#property copyright “zzz”

//— input parameters
extern double TakeProfit=1000.0;
extern double Lots=0.01;
extern double StopLoss=1500.0;
//±-----------------------------------------------------------------+
//| expert initialization function |
//±-----------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
int mainCrossed (double mainline1 , double mainline2)
{
static int mainlast_direction = 0;
static int maincurrent_dirction = 0;
if(mainline1>mainline2)maincurrent_dirction = 1; //main up
if(mainline1<mainline2)maincurrent_dirction = 2; //main down
if(maincurrent_dirction != mainlast_direction) //main changed
{
mainlast_direction = maincurrent_dirction;
return (mainlast_direction);
}
else
{
return (0);
}
}

int Crossed (double line1 , double line2)
{
static int last_direction = 0;
static int current_dirction = 0;
if(line1>line2)current_dirction = 1; //up
if(line1<line2)current_dirction = 2; //down
if(current_dirction != last_direction) //changed
{
last_direction = current_dirction;
return (last_direction);
}
else
{
return (0);
}
}
//±-----------------------------------------------------------------+
//| expert start function |
//±-----------------------------------------------------------------+
int start()
{
//----
int cnt, ticket, total;
double shortEma, longEma, mainshortEma, mainlongEma;
if(Bars<100)
{
Print(“bars less than 100”);
return(0);
}
if(TakeProfit<10)
{
Print(“TakeProfit less than 10”);
return(0); // check TakeProfit
}
shortEma = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,0);
longEma = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,0);

mainshortEma = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0);
mainlongEma = iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);

int mainisCrossed = mainCrossed (mainshortEma,mainlongEma);

int isCrossed = Crossed (shortEma,longEma);

total = OrdersTotal();
if(total < 1)
{
if(mainisCrossed == 2 && isCrossed == 2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLossPoint,
Bid-TakeProfit
Point,“My EA”,12345,0,Red);

           return(0);
        }
        return(0);

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

Thanks and regards
Terrance