//+------------------------------------------------------------------+ //| testEA3.mq4 | //| rem | //| | //+------------------------------------------------------------------+ #property copyright "rem" #property link "" extern double Lots = 1; int start() { int cnt, ticket, total; total=OrdersTotal(); if(total<1) { // no opened orders identified if(AccountFreeMargin()<(1000*Lots)) { Print("We have no money. Free Margin = ", AccountFreeMargin()); return(0); } // check for long position (BUY) possibility if(Close[1] > Close[2]) { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"testEA3",16384,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); } // check for short position (SELL) possibility if(Close[1] < Close[2]) { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,0,"testEA3",16384,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); } // it is important to enter the market correctly, // but it is more important to exit it correctly... for(cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderType()<=OP_SELL && // check for opened position OrderSymbol()==Symbol()) // check for symbol { if(OrderType()==OP_BUY) // long position is opened { // should it be Closed? if(Close[1] < Close[2]) { OrderClose(OrderTicket(),OrderLots(),Bid,0,Violet); // Close position // + open a new one ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,0,"testEA2",16384,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); // exit } } else // go to short position { // should it be Closed? if(Close[1] > Close[2]) { OrderClose(OrderTicket(),OrderLots(),Ask,0,Violet); // Close position // + open a new one ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"testEA2",16384,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); // exit } } } } return(0); } // the end.
Sample
Analysis
Market Information Used:
Series array that contains close prices for each bar
Indicator Curves created:
Indicators Used:
Custom Indicators Used:
Order Management characteristics:
Checks for the total of open orders
It automatically opens orders when conditions are reached
It Closes Orders by itself
Other Features: