OrderTotal() stuck at 1 yet no open or prending orders since last closed

 
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#include "include/stdlib.mqh"
//--- input parameters
extern int num_bars_long=2;
extern int abs_volatility_long=50;
extern int buystop=20;
extern int trail_stop_long=40;

extern int num_bars_short=2;
extern int abs_volatility_short=50;
extern int sellstop=20;
extern int trail_stop_short=40;
extern int long_short=0;
extern double Lots=1;

extern int Slippage=5;
extern double Magic = 123456;
bool New_Bar=false; // Flag of a new bar
//--- variables
int i,cnt;
int count=0;
int ticket = 0;
int total = 0;
double _sl, _tp;
double stop_loss_long =0;
double stop_loss_short =0;
bool isMP_Long = false;
bool isMP_Short = false;
//double buy_level = Close[0] + buystop*Point;
double buy_level = 0;
//double sell_level =Close[0] - sellstop*Point;
double sell_level =0;
bool buy_level_valid=false;
bool sell_level_valid = false;
bool isbuy = false;
bool isshort = false;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{


if(ticket==0)
{
stop_loss_long=0;
isMP_Long=False;
stop_loss_short=0;
isMP_Short=False;
}

//Print("Strategy is alive !");

if ( ((Close[num_bars_long]-Close[0]) >= abs_volatility_long*Point) && (!isbuy) )
{
isbuy=true;
Print("isbuy is true");
}

if ( ((Close[0] - Close[num_bars_short]) >= abs_volatility_long*Point) && (!isshort) )
{
isshort=true;
Print("isshort is true");
}


if ( (Close[0]<Close[1]) && (!buy_level_valid) )
{
buy_level = Close[0] + buystop*Point;
//buy_level = Ask + buystop*Point;
buy_level_valid = true;
Print("buy_level_valid is true, buy_level is "+DoubleToStr(buy_level, 5));
}
else if ( (Close[0]<Close[1]) && (buy_level_valid) )
{
buy_level = MathMin(buy_level,Close[0] + buystop*Point);
//buy_level = MathMin(buy_level,Ask + buystop*Point);
Print("buy_level updated to "+DoubleToStr(buy_level, 5));
}

if ( (Close[0]>Close[1]) && (!sell_level_valid) )
{
sell_level = Close[0] - sellstop*Point;
//sell_level = Bid - sellstop*Point;
sell_level_valid = true;
//Print("sell_level_valid is true, sell_level is "+DoubleToStr(sell_level, 5));
}
else if ( (Close[0]>Close[1]) && (sell_level_valid) )
{
sell_level = MathMax(sell_level,Close[0] - sellstop*Point);
//sell_level = MathMax(sell_level,Bid - sellstop*Point);
//Print("sell_level updated to "+DoubleToStr(sell_level, 5));
}


//Detect Close of Bar
Fun_New_Bar(); // Function call
if (New_Bar==true) // If bar is new..
{
isbuy=false;
isshort=false;
buy_level_valid=false;
sell_level_valid=false;
Print("New Bar! buy_level_valid is ",buy_level_valid, " sell_level_valid is ",sell_level_valid );
}

/*
for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--) if(
OrderSelect(iPos, SELECT_BY_POS) // Only my orders w/
&& OrderMagicNumber() == Magic // my magic number
&& OrderSymbol() == "EURUSD.fx" // and my pair.
){ count++; }
//if (count == 0) Print("Total Open Orders: ",total);
Print("Total Open Orders: ",count);
*/

total=OrdersTotal();
/*
if(OrderSelect(1, SELECT_BY_TICKET)==true)
Print("Profit for the order 1 ",OrderProfit());
else
Print("OrderSelect returned the error of ",GetLastError());
*/
Print("Total Open Orders: ",total, " ",OrderSymbol());

if(total<1)
{
// no opened orders identified
/*
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
*/

////////
// LONG
////////
if (isbuy && ((long_short==0) || (long_short==1)) )
{
_sl = buy_level-trail_stop_long*Point;
_tp = buy_level+trail_stop_long*Point;

Print("OrderSend BUY STOP: curren openprice is buy_level="+DoubleToStr(buy_level, 5)+" SL="+_sl+" tp="+_tp);
ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,buy_level,Slippage,_sl,_tp,"Long",Magic,0,Blue);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY STOP order opened : ",OrderOpenPrice(), " ticket,",ticket);
}
else Print("Error opening BUY STOP order : ",GetLastError());
return(0);
}

////////
// SHORT
////////
if (isshort && ((long_short==0) || (long_short==2)) )
{
_sl = sell_level+trail_stop_short*Point;
_tp = sell_level-trail_stop_short*Point;
Print("OrderSend SELL STOP: price="+DoubleToStr(sell_level, 5)+" SL="+_sl+" tp="+_tp);
ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,sell_level,Slippage,_sl,_tp,"Short",Magic,0,Blue);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice(), " ticket,",ticket);
}
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_SELLSTOP && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUYSTOP) // long position is opened
{
// should it be closed?
// check for trailing stop
if(trail_stop_long>0)
{
if(Bid-OrderOpenPrice()>Point*trail_stop_long)
{
if(OrderStopLoss()<Bid-Point*trail_stop_long)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*trail_stop_long,OrderTakeProfit(),0,Green);
Print("Order Modified Trailing stop to = : ",(Bid-Point*trail_stop_long), " ticket,",ticket);
return(0);
}
}
}
}
else // go to short position
{
// should it be closed?

// check for trailing stop
}
}//OrderType
}//for
//Print("I get here ! ");
//----
//----
return(0);
}
//+------------------------------------------------------------------+
void Fun_New_Bar() // Funct. detecting ..
{ // .. a new bar
static datetime New_Time=0; // Time of the current bar
New_Bar=false; // No new bar
if(New_Time!=Time[0]) // Compare time
{
New_Time=Time[0]; // Now time is so
New_Bar=true; // A new bar detected
}
}
 

Before posting please read some of the other threads . . . then you would have seen numerous requests like this one:

Please use this to post code . . . it makes it easier to read.

 
Where is this code located ? is it in a custom function ? or in start() ?
 
mefTrader:

one trade is placed and "total" which is OrdersTotal() remains at 1 even though the trade has exited - I have restarted MT4 and it is still 1.

Closing the terminal does not close trades. so restarting is irrelevant

It's one because you have an open order. open terminal (control-t) trade tab.

 
RaptorUK:

Before posting please read some of the other threads . . . then you would have seen numerous requests like this one:

Please use this to post code . . . it makes it easier to read.


I have updated the code above


I have read the numerous posts you are referring to - they seem to be stuck at 0 not 1. I have read them thoroughly and could not rectify my probelm


I looked at the Trade Window and the 1st trade seems to already closed out - Right click on the order and the option to close out the trade is greyed out

 
mefTrader:

I have updated the code above


I have read the numerous posts you are referring to - they seem to be stuck at 0 not 1. I have read them thoroughly and could not rectify my probelm


I looked at the Trade Window and the 1st trade seems to already closed out - Right click on the order and the option to close out the trade is greyed out





 
mefTrader:

I have read the numerous posts you are referring to - they seem to be stuck at 0 not 1. I have read them thoroughly and could not rectify my probelm

The numerous posts I referred to was posting your code using the SRC button so it is formatted as code and easier to read . If your code is to big to paste into the box that appears when you click the SRC button then please add it as a file.
 
mefTrader:

I looked at the Trade Window and the 1st trade seems to already closed out - Right click on the order and the option to close out the trade is greyed out

It's not closed . . . . it's not even Open yet . . . its a OP_BUYSTOP or Buy Stop . . . it's a Pending order . . . you don't close an order that isn't open yet . . . if you want to get rid of it you delete it . . . OrderDelete()
 
RaptorUK:
It's not closed . . . . it's not even Open yet . . . its a OP_BUYSTOP or Buy Stop . . . it's a Pending order . . . you don't close an order that isn't open yet . . . if you want to get rid of it you delete it . . . OrderDelete()

Okay thanks for that.
Reason: