Logic not working!!!

 

Hi,

I am trying to code an EA which looks to see if the last bar had a close near the centre and if it did buy or sell on a breakout of the high or low of the same bar but the opening of orders does not happen when I expect. Any ideas? Thanks very much. Code follows.

//+------------------------------------------------------------------+
//| CentralClose.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| https://www.metaquotes.net// |
//+------------------------------------------------------------------+

extern double TakeProfit = 40;
extern double Lots = 0.1;
extern double StopLoss = 40;
extern double LastClose, LastHigh, LastLow, SigRangeHi, SigRangeLo;
extern int OrderSig=0;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+


int start()
{

int ticket, total, OrderSig;

// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars

if(Bars<100)
{
Print("bars less than 100");
return(0);
}

if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}

// to simplify the coding and speed up access
// data are put into internal variables
LastClose=Close[0];
LastHigh=High[0];
LastLow=Low[0];
SigRangeHi=(LastLow+((LastHigh-LastLow)/2)+(20*Point));
SigRangeLo=(LastLow+((LastHigh-LastLow)/2)-(20*Point));



total=OrdersTotal();

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

if(total>0)
{
Print("Orders are already open");
return(0);

}

if (LastClose>SigRangeLo && LastClose<SigRangeHi)
{
OrderSig=1;
}

// check for order placement possibility
if(OrderSig==1)
{
{if (Bid <= LastLow)
OrderSend(Symbol(),OP_SELL,Lots,Bid,0,Bid+StopLoss*Point,Bid-TakeProfit*Point,"My order #",16384,0,Green);
Alert (GetLastError());
}
}
if(OrderSig==1)
{
{if (Ask >= LastHigh)
OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Ask-StopLoss*Point,Ask+TakeProfit*Point,"My order #",16384,0,Green);
Alert (GetLastError());
}
}
OrderSig=0;
return(0);
}
// the end.