OrdersTotal(); equals zero, even when it doesn't . . .

 

I wrote this function for my program, and am back testing it. Strangely, Orders Total is always equal to zero so that my Take profit doesn't function, even when there are orders open! Can anyone see why Orders Total is always equal to zero when trades are open?


int ManualTakeProfit()


{

Print("Now in the manual take profit function.OrdersTotal:",OrdersTotal(),".");
// {

for(cnt=0;cnt<OrdersTotal();cnt++) // for(cnt=0;cnt<OrdersTotal();cnt++) // cant understand why this doesn't work!
{
Print("There are orders.");
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
//mode=OrderType();
if (OrderSymbol()==Symbol() && OrderMagicNumber() == Magic)
{
Print("A first trade made by this program has been found.");
if (OrderProfit()>0)
{
_AProfit=OrderOpenPrice()-OrderClosePrice();
_Profit=MathAbs(_AProfit);
Print("The trade has made a profit. The profit is ",OrderProfit()," or ",_Profit,".");
Print("Now comparing Profit:",_Profit," to _MyTakeProfit:(",_MyTakeProfit,")*Point:",Point,".");

if (_Profit>=_MyTakeProfit*Point)
{
Print("The trade has made enough profit! Profit(",_Profit,")>=Takeprofit(",_MyTakeProfit,"*",Point,".Take Profit will close the order now.");
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Blue);
Print("My take Profit function has taken profit. Going Home Now.");
return(0);
}
if (_Profit<_MyTakeProfit*Point)
{
Print("The trade has not yet made enough profit. Profit(",_Profit,")<Takeprofit(",_MyTakeProfit,"*",Point,". Going Home.");
return(0);
}
}
if (OrderProfit()<=0)
{
Print("The trade has not yet made a profit. Profit:",OrderProfit(),"Going Home.");
return(0);
}

//LastPrice=OrderOpenPrice();
// if (mode==OP_BUY) { myOrderType="Buy Only"; }
Print("This should never be read. Going Home.");
return(0); //if (mode==OP_SELL) { myOrderType="Sell Only"; }
}
return(0);
Print("All trades were made by another program. Going Home.");
}
Print("Since there were no trades, Going Home.");
return(0);
}
 
Show what is in logs journal.
 
Roger:
Show what is in logs journal.

I attached the log. There were 28 trades.

Files:
20090928.zip  2951 kb
 
Beeepel wrote >>

I attached the log. There were 28 trades.

When you open a order you was sending the same magic number like you are select in your code?

>if (OrderSymbol()==Symbol() && OrderMagicNumber() == Magic)

 
Beeepel:

I wrote this function for my program, and am back testing it. Strangely, Orders Total is always equal to zero so that my Take profit doesn't function, even when there are orders open! Can anyone see why Orders Total is always equal to zero when trades are open?


int ManualTakeProfit()



for(cnt=0;cnt<OrdersTotal();cnt++) // for(cnt=0;cnt<OrdersTotal();cnt++) // cant understand why this doesn't work!
{ ... }

On the moment that you close an order what was order[cnt+1] becomes order[cnt] and you'll skip the one

for(cnt=OrdersTotal()-1; cnt>=0; cnt--) {...}

Also you should be checking the return code from OrderClose and printing any errors.

Reason: