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 */ }
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 )
|
- docs.mql4.com
whroeder1:
That is not code to test a MA cross. Test for a cross:
Thanks for your corrections whroeder1
|
|
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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...