how to open 1 position per direction trend...

 

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())
"Open buy"

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

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 (OrderSelect (i, SELECT_BY_POS, MODE_TRADES))
        {
            if ((Symbol() == OrderSymbol()) && (MagicNumber == OrderMagicNumber()))
            {
                if (cOrderType == OrderType())
                {
                    order_count++;
                }
            }
        }
    }

    return (order_count);
}

So:

if((ma1 > ma2) && (bid < OrderOpenprice()) && (CountOpenedOrders(OP_BUY) == 0))
{
    /* Open buy */
}
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 (OrderSelect (i, SELECT_BY_POS, MODE_TRADES))
        {
            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]))
{
    /* Open buy */
}

 
Thank you all I really appreciate your assistance... I will work with your examples..thanks
 
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;
 

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])
Alert("second dot")


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

Thanks

 
chudanever: my intention wasn't MAs but parabolic sar... I want the alert only on (second dot )
  1. Then why did you initially bring up MAs?
  2. So substitute iSAR for MA(1) and open for MA(2)
  3. 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
iSAR - Technical Indicators - MQL4 Reference
  • docs.mql4.com
iSAR - Technical Indicators - MQL4 Reference
 

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


Thanks for your corrections whroeder1

 
whroeder1:

  1. Then why did you initially bring up MAs?
  2. So substitute iSAR for MA(1) and open for MA(2)
  3. 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




    Whroeder I know about all that...but I want to get only one alert after each trick


    On second dot....pls does.   SAR(NULL,0,0.02,0.2,shift)>Close[0]) return(0);

    Shift i=1

    Return second dot on the parabolic sat...just clue...I can code it...sir.