extern double Lots=20; extern int StopLoss=20; double Ma1; extern int Ma1Period=6; double Ma2; extern int Ma2Period=3; #define MAX_ORDERS 1 //This controls the total number of that can be placed at any one time. int static ticketbuy, ticketsell; void start() //This process repeats itself after every tick (change in price). { Ma1=iMA("GER30",PERIOD_M5,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defining the moving average Ma2=iMA("GER30",PERIOD_M5,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defining the moving average if(OrdersTotal()<MAX_ORDERS) //Total number of orders //Buy Criteria { if(Ma1<Ma2) { ticketbuy = OrderSend("GER30",OP_BUY,Lots,Ask+0.00001,10,StopLoss,0,"test_1",0,0,Green); } // Sell Criteria else { ticketsell = OrderSend("GER30",OP_SELL,Lots,Bid+0.00001,10,StopLoss,0,"test_1",0,0,Red); } //The section below is with regards to closing a live trade for (int i=0;i<MAX_ORDERS;i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true) continue; else break; if (OrderType() == OP_BUY) { if (Ma1>Ma2) { bool result1; result1 = OrderClose(i,Lots,Bid,10,Blue); break; } } if (OrderType() == OP_SELL) { if (Ma1<Ma2) { bool result2; result2 = OrderClose(i,Lots,Bid,10,Blue); break; } } } } } Thanks for looking at this.
for (int i=OrdersTotal()-1;i>=;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true) if (MagicNumber==OrderMagicNumber()) if (Symbol()==OrderSymbol()) if (OrderType() == OP_BUY) { if (Ma1>Ma2) { bool result1 = OrderClose(OrderTicket(),OrderLots(),Bid,10,Blue); } } . . . //SELL the same }
Thanks for your help. Could you please explain what is a 'MagicNumber' and how do I define it with respect to the rest of the code? Thanks again.
int MagicNumber=OrderMagicNumber();
- Check your return codes (OrderSelect and OrderClose) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
- You must count down when closing/deleting in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
- You create orders with the magic number, then use an OrderSelect loop to find only those orders. order accounting - MQL4 forum
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello,
I am attempting to write an EA which opens a trade using moving averages. I can get my EA to open trades but I cannot get it to close live trades. What is wrong with my code??? Here it is:
extern double Lots=20;
extern int StopLoss=20;
double Ma1;
extern int Ma1Period=6;
double Ma2;
extern int Ma2Period=3;
#define MAX_ORDERS 1 //This controls the total number of that can be placed at any one time.
int static ticketbuy, ticketsell;
void start() //This process repeats itself after every tick (change in price).
{
Ma1=iMA("GER30",PERIOD_M5,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defining the moving average
Ma2=iMA("GER30",PERIOD_M5,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defining the moving average
if(OrdersTotal()<MAX_ORDERS) //Total number of orders
//Buy Criteria
{
if(Ma1<Ma2)
{
ticketbuy = OrderSend("GER30",OP_BUY,Lots,Ask+0.00001,10,StopLoss,0,"test_1",0,0,Green);
}
// Sell Criteria
else
{
ticketsell = OrderSend("GER30",OP_SELL,Lots,Bid+0.00001,10,StopLoss,0,"test_1",0,0,Red);
}
//The section below is with regards to closing a live trade
for (int i=0;i<MAX_ORDERS;i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true) continue;
else break;
if (OrderType() == OP_BUY)
{
if (Ma1>Ma2)
{
bool result1;
result1 = OrderClose(i,Lots,Bid,10,Blue);
break;
}
}
if (OrderType() == OP_SELL)
{
if (Ma1<Ma2)
{
bool result2;
result2 = OrderClose(i,Lots,Bid,10,Blue);
break;
}
}
}
}
}
Thanks for looking at this.