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- blue 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 |//| https://www.metaquotes.net/ |//+------------------------------------------------------------------+#propertycopyright"Mike"#propertylink"https://www.metaquotes.net/"//Extern variablesexternintStopLoss_in_Percent=25;
//+------------------------------------------------------------------+//| expert initialization function |//+------------------------------------------------------------------+intinit(){//----//----return(0);
}//+------------------------------------------------------------------+//| expert deinitialization function |//+------------------------------------------------------------------+intdeinit(){//----//----return(0);
}//Variablesdoublebarhigh;
doublebarlow;
stringbar="nobuy";
intticket;
doublerange;
intunits;
intpips;
doublestoploss;
//Function, which checks if the current bar is new, or the same (bool --> true=1 / false=2)boolnewbar(intcurrentbars){staticintobars;
if(currentbars != obars){obars = currentbars;
return(1);
}return(0);
}//Function checks if the system is availableboolvalidsystem(doublehigh, doublelow){barhigh=High[1];
barlow=Low[1];
range=(High[1]-Low[1])*0.25; //range is the stop in double-typeif(high<High[2] && low>Low[2]){return(1);
}else{return(0);
}return(0);
}//+------------------------------------------------------------------+//| expert start function |//+------------------------------------------------------------------+intstart(){//----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" - startif(bar=="buy"){OrderClose(ticket,1, Bid,3,Yellow);
bar="nobuy";
}//CLOSE condition - endif(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 - startif(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 - startif(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 - startif(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 - startif(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- startif(bar=="buy"){OrderSelect(ticket,SELECT_BY_TICKET);
Print("Order Ticket " + OrderTicket());
switch(OrderType()){caseOP_BUY:
if(Ask <= stoploss)OrderClose(ticket,1, Bid,3,Yellow);
caseOP_SELL:
if(Bid >= stoploss)OrderClose(ticket,1, Ask,3,Yellow);
}bar="nobuy";
}//CLOSE condition 2- end}//----return(0);
}//+------------------------------------------------------------------+
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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- blue 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!