How to code? - page 298

 
mladen:
Terrance

Did you see this section of TSD : Lessons ?

I think that a lot of questions are already answered there and that you can find very good examples for general EA writing there. As of your question : I did not test your EA so I do not know. The problem with testing someone else s EA is that one has to know the idea of it and the logic of it and a lot of times it is simply impossible to "read" those from the code itself. The code snippet that we were talking about was concerning how to detect 2 averages crosses on a closed bar and that code is doing just that and nothing but that. That is why I said that "it has nothing to do with SL or TP"

regards

Mladen

Hi Mladen, sorry to bother you again. I have gone through my codes and i notice not just my TP and SL, sometimes even my buy and sell orders are off. I have gone through the links on lesson that you gave me by codersgurus and its the same set of tutorials i had and based my codes on. I seriously can't seem to find the cause of my error in the codes.

The crosses of the MA works like it should together with the Long and Short order, however at times the Entry and Exit seems to be going haywire even when I did a basic ASK function to test it out. Please shed me some light here.

Thanks and Regards

Terrance

 

...

Terrance

First : Is your broker ECN/STP like? If yes, than you can not place stop loss and take profit along with order opening, but you must first open an order with those set to 0 and the use OrderModify() to set stop loss and / or take profit on a already opened order.

Second : Also, always check if you have anything written out in the experts tab of the terminal window (check if some errors are written there : you will probably find out then what is going on and why

Third : It could be a good idea to place a line like this :

int err=GetLastError(); Print(err);

After each Order managing function. Error descriptions you can find in the experts/include/stderror.mqh file (there are a lot of error that can happen (just see that list of possible error codes and descriptions) with orders and you must monitor each and every possible error) With known error codes (the line of code from the above), you can the concentrate on correction specific errors instead of guessing what is going on

tkuan77:
Hi Mladen, sorry to bother you again. I have gone through my codes and i notice not just my TP and SL, sometimes even my buy and sell orders are off. I have gone through the links on lesson that you gave me by codersgurus and its the same set of tutorials i had and based my codes on. I seriously can't seem to find the cause of my error in the codes.

The crosses of the MA works like it should together with the Long and Short order, however at times the Entry and Exit seems to be going haywire even when I did a basic ASK function to test it out. Please shed me some light here.

Thanks and Regards

Terrance
Files:
terminal_1.gif  62 kb
 
mladen:
Terrance

First : Is your broker ECN/STP like? If yes, than you can not place stop loss and take profit along with order opening, but you must first open an order with those set to 0 and the use OrderModify() to set stop loss and / or take profit on a already opened order.

Second : Also, always check if you have anything written out in the experts tab of the terminal window (check if some errors are written there : you will probably find out then what is going on and why

Third : It could be a good idea to place a line like this :

int err=GetLastError(); Print(err);
After each Order managing function. Error descriptions you can find in the experts/include/stderror.mqh file (there are a lot of error that can happen (just see that list of possible error codes and descriptions) with orders and you must monitor each and every possible error) With known error codes (the line of code from the above), you can the concentrate on correction specific errors instead of guessing what is going on

Hi Mladen,

Thanks for pointing out the ECN/STP thingy. I didn't know about those till you mention it. I have did the changes like you suggested, however now it only got worst for me. Not only is the Entry and Exit problem persist, my SL is missing now. This is harder then I thought.

//--- input parameters

extern double TakeProfit=1000.0;

extern double Lots=0.1;

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);

}

}

//+------------------------------------------------------------------+

//| 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

}

int isCrossed = 0;

double shortEma1 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,1);

double longEma1 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,1);

double shortEma2 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,2);

double longEma2 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,2);

double diff1 = shortEma1-longEma1;

double diff2 = shortEma2-longEma2;

mainshortEma = iMA(NULL,0,30,0,MODE_SMA,PRICE_CLOSE,0);

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

int mainisCrossed = mainCrossed (mainshortEma,mainlongEma);

if ((diff1*diff2)<0)

{

if (shortEma1>longEma1)

isCrossed = 1;

else isCrossed = 2;

}

total = OrdersTotal();

if(total < 1)

{

if(isCrossed == 1 && mainshortEma > mainlongEma)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,

"My EA",12345,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);

}

if(isCrossed == 2 && mainshortEma < mainlongEma)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,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);

}

for(cnt=0;cnt<total;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())

{

if(OrderType()==OP_BUY) // long position is opened

{

OrderClose(OrderTicket(),OrderLots(),Ask-StopLoss*Point,3,Violet);

// close position

return(0); // exit

}

else // go to short position

{

OrderClose(OrderTicket(),OrderLots(),Bid+StopLoss*Point,3,Violet);

// close position

return(0); // exit

}

}

}

return(0);

}

//+------------------------------------------------------------------+

 
mladen:
Terrance

First : Is your broker ECN/STP like? If yes, than you can not place stop loss and take profit along with order opening, but you must first open an order with those set to 0 and the use OrderModify() to set stop loss and / or take profit on a already opened order.

Second : Also, always check if you have anything written out in the experts tab of the terminal window (check if some errors are written there : you will probably find out then what is going on and why

Third : It could be a good idea to place a line like this :

int err=GetLastError(); Print(err);
After each Order managing function. Error descriptions you can find in the experts/include/stderror.mqh file (there are a lot of error that can happen (just see that list of possible error codes and descriptions) with orders and you must monitor each and every possible error) With known error codes (the line of code from the above), you can the concentrate on correction specific errors instead of guessing what is going on

Hey Mladen, I would like to thanks you for your help these few days. I manage to found the error in my code that is causing the entry and exit error.

Regards

Terrance

 

Opening more then 1 trade

Hi all, I have been trying to make my EA able to open multiple trades, but so far I am only able to make it limit to 1 trade.

My objective is simple:

When LWMA 10 crosses below LWMA 20 the system will short provided both LWMA 10 and 20 crosses below SMA 50 and vice versa for long trades.

I want to limit my EA to open no more then 2 trades, however, when I change the codes, it becomes opening 2 trades at a time instead of open 1 trade per criteria.

Do I need to set up a counter system or anything or how do I set my counter to react when 1 of my trade has already TP or SL?

Can someone please shed me some light here as to what is wrong with my codes.

Many thanks and regards

Terrance

//--- input parameters

extern double TakeProfit=500.0;

extern double Lots=0.1;

extern double StopLoss=500.0;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

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);

int isCrossed = 0;

double shortEma1 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,1);

double longEma1 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,1);

double shortEma2 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,2);

double longEma2 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,2);

double diff1 = shortEma1-longEma1;

double diff2 = shortEma2-longEma2;

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

if ((diff1*diff2)<0)

{

if (shortEma1>longEma1)

isCrossed = 1;

else isCrossed = 2;

}

total = OrdersTotal();

if(total < 1)

{

if(isCrossed == 1 && shortEma > mainshortEma && longEma > mainshortEma)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLoss*Point,Ask+TakeProfit*Point,

"My EA",12345,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);

}

if(isCrossed == 2 && shortEma < mainshortEma && longEma < mainshortEma)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*Point,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);

}

if(total == 1)

{

if(isCrossed == 1 && shortEma > mainshortEma && longEma > mainshortEma)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLoss*Point,Ask+TakeProfit*Point,

"My EA",12345,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);

}

if(isCrossed == 2 && shortEma < mainshortEma && longEma < mainshortEma)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*Point,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);

}

return(0);

}

//+------------------------------------------------------------------+

 

Questions

Mladen,

I am trying to look for Lessons under the link which you have shown me, but they are generic lessons, I am looking for information for specifically for Henderson's indicator...

Also, I have gone through the posted threads and after that I want to fnd out following things.

-In simple and easy to understand language, I want to ask is that approximately after how many bars passes, and the color changes.

In other words, maximum how many bars it takes before the color of line changes from blue to pink? Unfortunately, I dont understand fancy terminology and I would appriciated very simple reply where I can move on and start the strategy. Any chart is highly appriciated. premalmunshaw@yahoo.com is my email address.

I am trying to pay $39 for this package to be activated.

LOOKING FORWARD TO POSITIVE REPLY!

Regards,

Premal Munshaw

mladen:
Quite frankly I do not understand what are you asking

If you are asking when does it change the color, then the answer is : when the slope changes

If you are asking how it is coded to change color then the answer you should look for in this section : Lessons .
 

...

Unfortunately I do not understand your questions, so all I will do is requote myself (the calculation length refers to Henderson's filter calculation length ) :

if your calculating length is 5 then last 2 bars can change

if your calculating length is 7 then last 3 bars can change

if your calculating length is 9 then last 4 bars can change

Simpler than that is impossible to answer. And there is no simpler explanation how Henderson's filter is calculated : it would be if I would try to explain Fourier transform in terms of "1+1 = 2". Please examine the links that you have been provided

As of the rest : you got links to the math behind the filter, you got info as of how many bars are recalculated, you even got examples from the elite section news thread. As of strategy using that filter : I told a 1000 of times in 1000s of posts that recalculating indicators (SSA, Henderson's filter, centered TMA, Fourier transforms, regression analysis, ...) should not be used in "signaling mode" but in "estimating mode". If you are after a signals from Henderson's filter then you are on a completely wrong track and if it is the only reason why you consider elite section subscription, then it is better that you do not subscribe

PS: it is a bad pratice to make you email public. That way all you are going to get is a ton of spamm messages in your email box

______________________________________

Wish you all the best in your quest for a trading system

regards

Mladen

premalmunshaw:
Mladen,

I am trying to look for Lessons under the link which you have shown me, but they are generic lessons, I am looking for information for specifically for Henderson's indicator...

Also, I have gone through the posted threads and after that I want to fnd out following things.

-In simple and easy to understand language, I want to ask is that approximately after how many bars passes, and the color changes.

In other words, maximum how many bars it takes before the color of line changes from blue to pink? Unfortunately, I dont understand fancy terminology and I would appriciated very simple reply where I can move on and start the strategy. Any chart is highly appriciated. premalmunshaw@yahoo.com is my email address.

I am trying to pay $39 for this package to be activated.

LOOKING FORWARD TO POSITIVE REPLY!

Regards,

Premal Munshaw
 

Mladen,

Is there a way for me to set a counter to my trade such that when my trade has TP or SL I am able to trigger the counter jump?

Regards

Terrance

tkuan77:
Hi all, I have been trying to make my EA able to open multiple trades, but so far I am only able to make it limit to 1 trade.

My objective is simple:

When LWMA 10 crosses below LWMA 20 the system will short provided both LWMA 10 and 20 crosses below SMA 50 and vice versa for long trades.

I want to limit my EA to open no more then 2 trades, however, when I change the codes, it becomes opening 2 trades at a time instead of open 1 trade per criteria.

Do I need to set up a counter system or anything or how do I set my counter to react when 1 of my trade has already TP or SL?

Can someone please shed me some light here as to what is wrong with my codes.

Many thanks and regards

Terrance

//--- input parameters

extern double TakeProfit=500.0;

extern double Lots=0.1;

extern double StopLoss=500.0;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

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);

int isCrossed = 0;

double shortEma1 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,1);

double longEma1 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,1);

double shortEma2 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,2);

double longEma2 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,2);

double diff1 = shortEma1-longEma1;

double diff2 = shortEma2-longEma2;

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

if ((diff1*diff2)<0)

{

if (shortEma1>longEma1)

isCrossed = 1;

else isCrossed = 2;

}

total = OrdersTotal();

if(total < 1)

{

if(isCrossed == 1 && shortEma > mainshortEma && longEma > mainshortEma)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLoss*Point,Ask+TakeProfit*Point,

"My EA",12345,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);

}

if(isCrossed == 2 && shortEma < mainshortEma && longEma < mainshortEma)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*Point,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);

}

if(total == 1)

{

if(isCrossed == 1 && shortEma > mainshortEma && longEma > mainshortEma)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLoss*Point,Ask+TakeProfit*Point,

"My EA",12345,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);

}

if(isCrossed == 2 && shortEma < mainshortEma && longEma < mainshortEma)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*Point,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);

}

return(0);

}

//+------------------------------------------------------------------+
 

...

Try this function :

int countOpenedOrders(int type)

{

int openedOrders = 0;

for(int i=0; i < OrdersTotal(); i++)

{

if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) break;

if(OrderSymbol() != Symbol()) continue;

if(OrderMagicNumber() != MagicNumber) continue;

if(OrderType() == type) openedOrders++;

}

return(openedOrders);

}

[/PHP]

To count opened buy orders, call it like this :

int openedBuys = countOpened(OP_BUY);

to count opened sell orders, call it like this :

[PHP]int openedSells = countOpened(OP_SELL);
and then you can check :
if openedBuys==0 open buy

if openedSells==0 open sell

tkuan77:
Mladen,

Is there a way for me to set a counter to my trade such that when my trade has TP or SL I am able to trigger the counter jump?

Regards

Terrance
 

Hi Mladen,

I have come up with another way by using "Bars" function. When a trade is triggered, I will set a integer count with the bars function (e.g. a buy trade is triggered in bar 122, hence the count will be 122) and when my criteria has being triggered again, I will call the bar function again and compared it with the count and the trade will only trigger if the new count is higher then the previous which is 122 in this case.

However, I am still triggering 2 trades together instead of 1 per criteria. Why is this so?

Please help

Thanks and regards

Terrance

//--- input parameters

extern double TakeProfit=530.0;

extern double Lots=0.1;

extern double StopLoss=520.0;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

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,25,0,MODE_LWMA,PRICE_CLOSE,0);

int isCrossed = 0;

double shortEma1 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,1);

double longEma1 = iMA(NULL,0,25,0,MODE_LWMA,PRICE_CLOSE,1);

double shortEma2 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,2);

double longEma2 = iMA(NULL,0,25,0,MODE_LWMA,PRICE_CLOSE,2);

double diff1 = shortEma1-longEma1;

double diff2 = shortEma2-longEma2;

mainshortEma = iMA(NULL,0,113,0,MODE_EMA,PRICE_CLOSE,0);

if ((diff1*diff2)<0)

{

if (shortEma1>longEma1)

isCrossed = 1;

else isCrossed = 2;

}

int BarCounter, BarCounter2;

total = OrdersTotal();

if(total < 1)

{

if(isCrossed == 1 && shortEma > mainshortEma && longEma > mainshortEma)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLoss*Point,Ask+TakeProfit*Point,

"My EA",12345,0,Green);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

Print("BUY order opened : ",OrderOpenPrice());

BarCounter = Bars;

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

}

if(isCrossed == 2 && shortEma < mainshortEma && longEma < mainshortEma)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*Point,Bid-TakeProfit*Point,

"My EA",12345,0,Red);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

Print("SELL order opened : ",OrderOpenPrice());

BarCounter = Bars;

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

}

return(0);

}

if(total == 1)

{

BarCounter2 = Bars;

if(isCrossed == 1 && shortEma > mainshortEma && longEma > mainshortEma && BarCounter2 > BarCounter)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLoss*Point,Ask+TakeProfit*Point,

"My EA",12345,0,Green);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

Print("BUY order opened : ",OrderOpenPrice());

BarCounter = BarCounter2;

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

}

if(isCrossed == 2 && shortEma < mainshortEma && longEma BarCounter)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*Point,Bid-TakeProfit*Point,

"My EA",12345,0,Red);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

Print("SELL order opened : ",OrderOpenPrice());

BarCounter = BarCounter2;

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

}

return(0);

}

return(0);

}

mladen:
Try this function :
int countOpenedOrders(int type)

{

int openedOrders = 0;

for(int i=0; i < OrdersTotal(); i++)

{

if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) break;

if(OrderSymbol() != Symbol()) continue;

if(OrderMagicNumber() != MagicNumber) continue;

if(OrderType() == type) openedOrders++;

}

return(openedOrders);

}

[/PHP]

To count opened buy orders, call it like this :

int openedBuys = countOpened(OP_BUY);

to count opened sell orders, call it like this :

[PHP]int openedSells = countOpened(OP_SELL);
and then you can check :
if openedBuys==0 open buy

if openedSells==0 open sell