Will not exit on next bar after position taken

 

I am trying to write an EA that:

Buys on the open when the last bar is an Outside bar and the low of 2 bars ago was the lowest low of the previous 8 bars.

exit when on the next bars open.

Sells on the open when the last bar is an Outside bar and the high of 2 bars ago was the highest high of the previous 8 bars

Here is the code thus far --- it seems to exit on the next tick rather than wait for the open of the next bar --- any help please.

// External Variables
extern bool CheckOncePerBar = true;
extern double LotSize = 0.01;
extern int MagicNumber = 123;
extern int Slippage = 5;
extern int MAPeriods = 8;


// Global Variables
datetime CurrentTimeStamp;
int BuyTicket;
int SellTicket;


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
CurrentTimeStamp = Time[0];

}



//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}



//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
// Execute on bar open
if(CheckOncePerBar == true)
{
int BarShift = 1;
if(CurrentTimeStamp != Time[0])
{
CurrentTimeStamp = Time[0];
bool NewBar = true;
}
else NewBar = false;
}
else
{
NewBar = true;
BarShift = 0;
}

//
int LowestShift = iLowest(NULL,0,MODE_LOW,MAPeriods,BarShift+1);
double LL = Low[LowestShift];
int HighestShift = iHighest(NULL,0,MODE_HIGH,MAPeriods,BarShift+1);
double HH = High[HighestShift];

// Begin trade block

{

// Buy Order
if(High[BarShift] > High[BarShift+1] && Low[BarShift] < Low[BarShift+1]
&& Low[BarShift] < LL && BuyTicket == 0 && NewBar == true)
{
// Buy order code
double OpenPrice = Ask;
BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,Slippage,0,0,"Buy Order",MagicNumber,0,Green);


SellTicket = 0;
}

// Sell Order
if(High[BarShift] > High[BarShift+1] && Low[BarShift] < Low[BarShift+1]
&& High[BarShift] > HH && SellTicket == 0 && NewBar == true)
{
// Sell order code
OpenPrice = Bid;
SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,OpenPrice,Slippage,0,0,"Sell Order",MagicNumber,0,Red);


BuyTicket = 0;
}

// Exit LONG

OrderSelect(BuyTicket,SELECT_BY_TICKET);
if(OrderCloseTime() == 0 && BuyTicket > 0 && NewBar == true)
{
double CloseLots = OrderLots();
double ClosePrice = Bid;

bool Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,Slippage,Red);
}

// Exit SHORT
OrderSelect(SellTicket,SELECT_BY_TICKET);
if(OrderCloseTime() == 0 && SellTicket> 0 && NewBar == true)
{
CloseLots = OrderLots();
ClosePrice = Ask;

Closed = OrderClose(SellTicket,CloseLots,ClosePrice,Slippage,Green);
}


} // End trade block







}
//+------------------------------------------------------------------+

 

  1. put Print() statements in the code to find out what it's doing
  2. OrderSelect(BuyTicket,SELECT_BY_TICKET);
    if(OrderCloseTime() == 0 && BuyTicket > 0 && NewBar == true)
    
    Always check if orderSelect succeeded. If orderSelect succeeded then OrderCloseTime() will always be zero.