Could you anybody help me?

 

Hello,


I wrote a script which close immediately opened position and delete pending position:

But it doesn't run well. It delete pending position, but don't close opened position. What will be the problem?


//+------------------------------------------------------------------+
//| close_immediately.mq4 |
//| Copyright © 2008, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#include <stderror.mqh>
#include <stdlib.mqh>

//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
//----
int total,ticket,lots,cmd,error;
double price;

total=OrdersTotal();

for(int i=0; i<total; i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
ticket=OrderTicket();
//Print(ticket);
lots=OrderLots();
cmd=OrderType();

if(cmd==OP_BUY)
{
price=Bid;
}
else
{
price=Ask;
}

if (cmd==OP_SELL || cmd==OP_BUY)
{
OrderClose(ticket,lots,price,3000,Red);
}
else
{
OrderDelete(OrderTicket());
}

error=GetLastError();
if (error!=0)
{
Print("Error: ", error,". Description: ",ErrorDescription(error));
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+

 

Try changing this line:


for(int i=0; i<total; i++)


to:


for(int i=total-1; i >= 0; i--)

 
may be the slippage= 3000 is too larger for OrderClose(), 3 is enough.
 

I tried this code too. When I installed MT4, this script already exists in MT4 scripts.

But this script either not working. I saw into the log message:

2008.09.26 15:15:40 Script close EURUSD,M1: removed
2008.09.26 15:15:40 Script close EURUSD,M1: loaded successfully


//+------------------------------------------------------------------+
//| close.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"


//+------------------------------------------------------------------+
//| script "close first market order if it is first in the list" |
//+------------------------------------------------------------------+
int start()
{
bool result;
double price;
int cmd,error;
//----
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();
//---- first order is buy or sell
if(cmd==OP_BUY || cmd==OP_SELL)
{
while(true)
{
if(cmd==OP_BUY) price=Bid;
else price=Ask;
result=OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
}
}
else Print( "Error when order select ", GetLastError());
//----
return(0);
}
//+------------------------------------------------------------------+

 
I found the mistake. The script was not working because when I opened the position the lot size was 1.5. I declared the lots variable "int" type
Reason: