if (OrderMagicNumber() != 00001) //If the search reveals an order with MN different from 00001 or finds no orders at all -> Go to OpenBuySell
{
OpenBuySell();
}
if find no orders at all the for dont enter..
You need also somethng like if (OrdersTotal()<1) openbuy or sell or something ...
to have just one order open at a time, you want to use
int total = OrdersTotal();
then if total == 0 make an order. that is, if there are no orders make an order. If total = 1 ( >0 ) then no action will be taken.
As for the code you have presented, it is from an EA you have downloaded and hacked from the internet. It was probably working fine till you messed with it.
If you are just starting out, you want to avoid using functions like that - just keep everything as simple as you can. Read the documentation and learn.
You can have a trade condition recognised, eg if on the previous bar MA1 > MA2 && on the current bar MA1 < MA2 (MA crossover) then open a trade, and then you can have a similar setup in order to close the order, and use orderclose.
I dont have time tonight to put up a full example.
double MA1=iMA(NULL,0,10,0,1,0,0); double MA2=iMA(NULL,0,10,0,1,0,1); double MA3=iMA(NULL,0,4,0,1,0,0); double MA4=iMA(NULL,0,4,0,1,0,1); string BUY="false"; string SELL="false"; if(MA1 < MA3 && MA2 > MA4 )BUY="true"; if(MA1 > MA3 && MA2 < MA4 )SELL="true";
above is an example of a simple MA crossover entry condition
if (BUY=="true" && total == 0) // total == 0 means if there are no orders present { Ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP,EAName,Magic,0,Blue); }
above is a simple example of making the order if the Buy condition is met, and there are no orders on.
then if the order is on, and the close conditions are met, this code from an EA which closes all open orders should point you in the right direction.
//+------------------------------------------------------------------+ //| close-all-orders.mq4 | //| Copyright © 2005, Matias Romeo. | //| Custom Metatrader Systems. | //+------------------------------------------------------------------+ #property copyright "Copyright © 2005, Matias Romeo." #property link "mailto:matiasDOTromeoATgmail.com" int start() { int total = OrdersTotal(); for(int i=total-1; i>=0; i-- ) { OrderSelect(i, SELECT_BY_POS); int type = OrderType(); bool result = false; switch(type) { //Close opened long positions case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); break; //Close opened short positions case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); break; //Close pending orders case OP_BUYLIMIT : case OP_BUYSTOP : case OP_SELLLIMIT : case OP_SELLSTOP : result = OrderDelete( OrderTicket() ); } if(result == false) { Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() ); Sleep(3000); } } return(0); }
string BUY="false"; string SELL="false"; if(MA1 < MA3 && MA2 > MA4 )BUY="true"; if(MA1 > MA3 && MA2 < MA4 )SELL="true"; if (BUY=="true" && total == 0)
bool cross = (MA3-MA1) * (MA2-MA4), buy = MA3 > MA1; if (cross && total == 0){ if (buy) OrderSend(OP_BUY... else OrderSend(OP_SELL... }
int total = OrdersTotal(); for(int i=total-1; i>=0; i-- ) { OrderSelect(i, SELECT_BY_POS); int type = OrderType();
In the presence of multiple order (multiple charts) you MUST count down when modifying/closing/deleteing. Always test return codes. No magic number/symbol means the EA incompatable with other EAs (including itself on other charts) and manual trading.for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if ( OrderSelect(pos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == magic.number // my magic number && OrderSymbol() == Symbol() ){ // and my pair.
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Trip
On 5 digit brokers, you must adjust tp, sl, AND SLIPPAGE//++++ These are adjusted for 5 digit brokers. double pips2points, // slippage 3 pips 3=points 30=points pips2dbl; // Stoploss 15 pips 0.0015 0.00150 int Digits.pips; // DoubleToStr(dbl/pips2dbl, Digits.pips) int init(){ if (Digits == 5 || Digits == 3){ // Adjust for five (5) digit brokers. pips2dbl = Point*10; pips2points = 10; Digits.pips = 1; } else { pips2dbl = Point; pips2points = 1; Digits.pips = 0; } // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
- int total = OrdersTotal();OrdersTotal() is meaning less with multiple charts/multiple EAs. Do the orderSelect loop and count YOUR open orders on YOUR chart.
then if total == 0 make an order. that is, if there are no orders make an order. If total = 1 ( >0 ) then no action will be taken.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello guys,
Im trying to build my first EA. I got some Visual Basic courses at highschool (years ago), but that hasn't helped me much. I'm very motivated to learn though.
I've been spending a lot of hours on the underneath code (yes, you can laugh) but things aren't working as they should. So it's time to call for some little help :)
Compiling the underneath code, I get no errors. But when attaching the EA to the chart, nothing happens.
I'm pretty sure something is wrong in the int start() part. What I want to do is open a buy or sell order at a certain condition (Moving Averages), hold it, and close the order when another specific condition (Signal line MACD) is met.
Since I don't want it to open orders constantly (when the condition is valid over a longer time) I work with MagicNumbers. An active Buy order gets MN 00001 and I dont want the EA to buy more lots until the order with MN 00001 is closed. Same for Sell orders (using MN 00002)
Help would be greatly appreciated!
Thx in advance,
StormOpZee
***