Endless loop?

 

This script is simply supposed to add up all lots taken, determine their position, close all orders and enter a new order in the opposite direction of the same total size.  There would be problems if you have orders of two different directions open at once, but since I do not trade this way, it is no problem for me.  The script starts fine, it addds up the total lots well, and closes all open positions.  Then, however, nothing happens.  If I activate another script, such as buy, I get a pop up window which says, do you really want to stop Reverse.mqh and run Buy.mqh?  Which leads me to believe there must be an endless loop somewhere in the code, but I can't spot it.

//+------------------------------------------------------------------+
//|                                                      Reverse.mqh |
//+------------------------------------------------------------------+
 
#include <stdlib.mqh>
#include <WinUser32.mqh>
 
int start()
{
 
int total=OrdersTotal();
int ordertype;
int ticket;
int BuyOrSell=2;
double priceClose;
double TotalLots = 0;
 
for(int i=total-1;i>=0;i--)
{
   if (OrderSelect(i,SELECT_BY_POS))
   {
      TotalLots = TotalLots +OrderLots();
   }
}   
 
if (total==0) {Alert("No trades open!");return;}
 
if(MessageBox("Stop and reverse "+TotalLots+" lots?    ",
   "Script",MB_YESNO|MB_ICONQUESTION)!=IDYES) return(1);
 
 
for(int j=total-1;j>=0;i--)
{
   if (OrderSelect(j,SELECT_BY_POS))
   {
      ordertype=OrderType();
      ticket=OrderTicket();
      switch(ordertype)
      {
         case 0:
            // if order was a buy, then close at bid 
            priceClose=MarketInfo(OrderSymbol(),MODE_BID);
            OrderClose(ticket,OrderLots(),priceClose,3,White);
            BuyOrSell = 1;
            break;    
         case 1:
            // if order was a sell, then close at ask
            priceClose=MarketInfo(OrderSymbol(),MODE_ASK);
            OrderClose(ticket,OrderLots(),priceClose,3,White);
            BuyOrSell = 0;
            break;    
         default:
            OrderDelete(ticket);  
            break;
      }    
   }
}   
 
//if previous orders were buys, then sell
if(BuyOrSell == 1)
{
   ticket=OrderSend(Symbol(),OP_SELL,TotalLots,Bid,3,0,0,NULL,0,0,Red);
}
 
//if previous orders were sells, then buy
if(BuyOrSell == 0)
{
   ticket=OrderSend(Symbol(),OP_BUY,TotalLots,Ask,3,0,0,NULL,0,0,Green);
}
 
 
OrderPrint();
return(0);
}
 

Hello inkexit

Firstly:

see file naming conventions: Creating and Using Programs and most importantly: File System

 
inkexit:

This script is simply supposed to add up all lots taken, determine their position, close all orders and enter a new order in the opposite direction of the same total size. There would be problems if you have orders of two different directions open at once, but since I do not trade this way, it is no problem for me. The script starts fine, it addds up the total lots well, and closes all open positions. Then, however, nothing happens. If I activate another script, such as buy, I get a pop up window which says, do you really want to stop Reverse.mqh and run Buy.mqh? Which leads me to believe there must be an endless loop somewhere in the code, but I can't spot it.

As you said, it's and endless loop:

copy/paste is not a good habit

you have a typo here. "i--" should be "j--"

for(int j=total-1;j>=0;i--)
 //CHANGE THIS LINE, should be for(int j=total-1;j>=0;j--)
 {
   if (OrderSelect(j,SELECT_BY_POS))
   {
      ordertype=OrderType();
Reason: