What's wrong? Please go through it

 

Hello,

as i figured out all my problems in the beginning i got stuck with that here.

This ea should enter on the break of an inner bar (doesn't matter if long or short) with exiting on the open of the nex bar. Sl is 25% of the range of the ib.

If i cut the last part of the code (Close condition 2 - see below- bold part), the ea works wonderful, with exiting on the bar after the position-opening-bar (the second bar after the inner bar). But if the code is there, somehow nothing works, the ea even doesn't open position, although the code, for opening a position is BEFORE the "Close condition 2-part". Additionally i get the error that the ticket number is invalid, which emphasizes once more that the opening part gets somehow leaped.

I hope you can help me, i'm completely baffled.

Thank you very much in advance!

//+------------------------------------------------------------------+

//| IB.mq4 |

//| Mike |

//| Forex Trading Software: Forex Trading Platform MetaTrader 4 |

//+------------------------------------------------------------------+

#property copyright "Mike"

#property link "http://www.metaquotes.net"

//Extern variables

extern int StopLoss_in_Percent=25;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//Variables

double barhigh;

double barlow;

string bar="nobuy";

int ticket;

double range;

int units;

int pips;

double stoploss;

//Function, which checks if the current bar is new, or the same (bool --> true=1 / false=2)

bool newbar(int currentbars)

{

static int obars;

if(currentbars != obars)

{

obars = currentbars;

return(1);

}

return(0);

}

//Function checks if the system is available

bool validsystem(double high, double low)

{

barhigh=High[1];

barlow=Low[1];

range=(High[1]-Low[1])*0.25; //range is the stop in double-type

if(highLow[2])

{

return(1);

}

else

{

return(0);

}

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

//----

if(newbar(Bars) == 1)//we only execute when a new bar has been created

{

//CLOSE condition if no stoploss occured (= close on open of the bar after the "position-one-bar" - start

if(bar=="buy")

{

OrderClose(ticket,1, Bid,3,Yellow);

bar="nobuy";

}

//CLOSE condition - end

if(validsystem(High[1], Low[1]) == 1) //if the last bar was our system(IB) --> we activate now our buy trigger

{

bar="buy";

//Buy condition on first tic - start

if(bar == "buy" && Bid > barhigh && OrdersTotal() == 0)

{

ticket = OrderSend(Symbol(), OP_BUY,1,Ask,3,0,0,"my order",123,0,Green);

Print("last error " + GetLastError(), "LastError");

stoploss = Ask-range;

Print("Sl = " + stoploss);

}

//Buy condition on first tic - end

//Sell condition first tic - start

if(bar == "buy" && Bid < barlow && OrdersTotal() == 0)

{

ticket = OrderSend(Symbol(), OP_SELL,1,Bid,3,0,0,"my order",123,0,Red);

Print("last error " + GetLastError(), "LastError");

stoploss = Bid+range;

Print("Sl = " + stoploss);

}

//Sell condition first tic - end

}

}

else //if not a new bar

{

//Buy condition normal - start

if(bar == "buy" && Bid > barhigh && OrdersTotal() == 0)

{

ticket = OrderSend(Symbol(), OP_BUY,1,Ask,3,0,0,"my order",123,0,Green);

Print("last error " + GetLastError(), "LastError");

stoploss = Ask-range;

Print("Sl = " + stoploss);

}

//Buy condition normal - end

//Sell condition normal - start

if(bar == "buy" && Bid < barlow && OrdersTotal() == 0)

{

ticket = OrderSend(Symbol(), OP_SELL,1,Bid,3,0,0,"my order",123,0,Red);

Print("last error " + GetLastError(), "LastError");

stoploss = Bid+range;

Print("Sl = " + stoploss);

}

//Sell condition normal - end

//CLOSE condition 2 (= stop loss close)- I didn't include sl into the OrderSend function, because

// i wanted the sl not at a certain pip, but on a relational basis- start

if(bar=="buy")

{

OrderSelect(ticket,SELECT_BY_TICKET);

Print("Order Ticket " + OrderTicket());

switch(OrderType())

{

case OP_BUY:

if(Ask <= stoploss)

OrderClose(ticket,1, Bid,3,Yellow);

case OP_SELL:

if(Bid >= stoploss)

OrderClose(ticket,1, Ask,3,Yellow);

}

bar="nobuy";

}

//CLOSE condition - end

}

//----

return(0);

}

//+------------------------------------------------------------------+
 

I am not sure that this will resolve all your problems, but you have to use a "break" statement after the case OP_BUY is done, otherwize, the following code is also executed.

MQL4:

The keyword case and the constant are just labels, and if operators are executed for some case variant, the program will further execute the operators of all following variants until break operator occurs.