# how to open 1 position per direction trend...

269

Pls I have few difficulties implementing this program line...

I want assistance on if ma1 crosses ma2 ---open buy,don't open again till another cross for

Sell...

```Now this is my code
If(ma1 > ma2 && bid < OrderOpenprice())

My question is a help to return only 1buy per direction.
Thanks..```
17

For example, you can count the number of the opened buy orders and do not open a new position if there is at least 1 opened order:

```int CountOpenedOrders(const int cOrderType)
{
int order_count = 0;
int total_order = OrdersTotal();

for (int i = total_order - 1; i >= 0; i--)
{
{
if ((Symbol() == OrderSymbol()) && (MagicNumber == OrderMagicNumber()))
{
if (cOrderType == OrderType())
{
order_count++;
}
}
}
}

return (order_count);
}
```

So:

```if((ma1 > ma2) && (bid < OrderOpenprice()) && (CountOpenedOrders(OP_BUY) == 0))
{
}```
It will open just 1 buy position until you will close it (and I suppose you will close it before opening a sell position). You can do the same for sell orders by passing OP_SELL as parameter.

If you want, you can also set the maximum number of opened orders as an input to make it more flexible. In this case, it's better to check the last order open time in order to open at maximum one order for each bar (otherwise it would potentially open one order for each tick that is a mess):

```input int i_MaximumOpenedOrders = 1;

/* ... */

datetime GetLastOrderOpenTime(void)
{
datetime last_open_time = 0;

int total_order = OrdersTotal();
for (int i = total_order - 1; i >= 0; i--)
{
{
if ((Symbol() == OrderSymbol()) && (0 == OrderMagicNumber()))
{
if (OrderOpenTime() > last_open_time)
{
last_open_time = OrderOpenTime();
}
}
}
}

return (last_open_time);
}

/* ... */

if((ma1 > ma2) && (bid < OrderOpenprice()) && (CountOpenedOrders(OP_BUY) < i_MaximumOpenedOrders) && (GetLastOrderOpenTime() < Time[0]))
{
}

```
269

Thank you all I really appreciate your assistance... I will work with your examples..thanks
15934

`If(ma1 > ma2 && bid < OrderOpenprice())`
That is not code to test a MA cross. Test for a cross:
```double ma1prev = iMA(... 2);
double ma1curr = iMA(... 1);
double ma2prev = iMA(... 2);
double ma2curr = iMA(... 1);
bool wasUp   = ma1prev > ma2prev;
bool  isUp   = ma1curr > ma2curr;
bool isCross =isUp != wasUp;
```
269

pls I want to update that this is not an EA with order send(....) function rather its an EA with
only Alert (....) function that its action is stored on if(....) values..before Alert..

pls my intention wasn't MAs but parabolic sar...
pls I want a value to get parabolic second dot...
now if second dot appear Alert (....) only once on the second dot.and return
now I do this....

```double parabolic(....);
if(parabolic < open[2])
```

the problem here is  I want the alert only on (second dot ) and return till another second dot tick reoccur.

Thanks

15934

 chudanever: my intention wasn't MAs but parabolic sar... I want the alert only on (second dot ) Then why did you initially bring up MAs?So substitute iSAR for MA(1) and open for MA(2)learn to code it, or pay (Freelance) someone to code it. We're not going to code it for you. We are willing to help you when you post your attempt (using SRC) and the nature of your problem.
iSAR - Technical Indicators - MQL4 Reference
• docs.mql4.com
iSAR - Technical Indicators - MQL4 Reference
269

whroeder1:
That is not code to test a MA cross. Test for a cross: