Your code sets close flags to true, but not to to false.
void CheckForClose() { bool Close_Long; bool Close_Short;
Try this
void CheckForClose() { bool Close_Long=0; // or Close_Long=false; bool Close_Short=0; // or Close_Short=false;
for( int z = Orders_total - 1; z >= 0; z -- ) // checking for orders { if(Orders_total>0) // If there is orders opened check there magic number
The IF is useless (always true.) You can't get to the IF when Orders_total is zero because the for won't iterate.Reject_entry=1; .. if(Orders_total<1 || Reject_entry==0)
Reject_entry is a bool, initialize it to true, set it to false, not integers
if (Orders_total<1 makes this EA incompatible with any other EA including itself on other charts.Orders_total = 0; for( int z = OrdersTotal() - 1; z >= 0; z -- ) if( OrderSelect( z, SELECT_BY_POS ) && (OrderMagicNumber()==Buy_MagicNumber || OrderMagicNumber() == Sell_MagicNumber) && OrderSymbol() == Symbol() ) Orders_total++; if(Orders_total<1) // no opened orders identified or open positions was not generated by this expert
- You MUST count down when closing orders. Use true/false not 0/1 or just get rid of the bools entirely
void CheckForClose(){ for( int z = OrdersTotal() - 1; z >= 0; z -- ) if( OrderSelect( z, SELECT_BY_POS ) && (OrderMagicNumber()==Buy_MagicNumber || OrderMagicNumber() == Sell_MagicNumber) && OrderSymbol() == Symbol() && MathAbs(OrderClosePrice() - OrderOpenPrice()) > 80*point){ if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),3,Red) ) Alert("OrderClose(",OrderTicket(),") failed: ",GetLastError(); } }
- There's no reason to have two magic numbers. Filter on one, then look at the type.
- EA's should adjust to 5 digit brokers TP, SL, AND slippage) On ECN brokers you open the order and then set the tp/sl
//++++ These are adjusted for 5 digit brokers. int pips2points; // slippage 3 pips 3=points 30=points double pips2dbl; // Stoploss 15 pips 0.0015 0.00150 int Digits.pips; // DoubleToStr(dbl/pips2dbl, Digits.pips) int init(){ if (Digits == 5 || Digits == 3){ // Adjust for five (5) digit brokers. pips2dbl = Point*10; pips2points = 10; Digits.pips = 1; } else { pips2dbl = Point; pips2points = 1; Digits.pips = 0; } // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
WHRoeder
Thank you a lot, you really studied my code and found all errors I‘ve done. That is helpful, once I’m not programmer, and nobody see what I build. I’m sure it will correct my problem. Tomorrow I going to do those changes that you wrote above.
Thank you again.
int start() { double Compra; double Venda; int Orders_total, ticket, cnt; bool time_ok=false; //---- initial data checks----// if(Bars<100) { Print("bars less than 100"); return(0); } //----Indicator Reference----// Compra= iCustom(Symbol(), 0, "Q Against God Wishes V2", EMAPeriod, AngleTreshold, StartEMAShift, EndEMAShift, Per_Trend_Direct, Per_Fast_Trend, Per_Slow_Trend, e_period, e_type_data, e_random_line, 0, 1); Venda= iCustom(Symbol(), 0, "Q Against God Wishes V2", EMAPeriod, AngleTreshold, StartEMAShift, EndEMAShift, Per_Trend_Direct, Per_Fast_Trend, Per_Slow_Trend, e_period, e_type_data, e_random_line, 1, 1); //---- Checking for orders ----// time_ok=false; if( barTime < Time[0] ) { barTime = Time[0]; // keep the new bar open time time_ok=true; // flag to enter only once per bar } Orders_total = 0; for( int z = OrdersTotal() - 1; z >= 0; z -- ) { if( OrderSelect( z, SELECT_BY_POS ) && (OrderMagicNumber()==Buy_MagicNumber || OrderMagicNumber() == Sell_MagicNumber) && OrderSymbol()== Symbol() ) { Orders_total++; } } //-------------------------------------------------------------------------------- if(Orders_total<1 && time_ok==true) // no opened orders identified or open positions was not generated by this expert { if(Compra==1) { ticket=OrderSend(Symbol(), OP_BUY, Lots, Ask, 1.5, Bid-(StopLoss*Point), Bid+ (TakeProfit*Point), "Q Against God Wishes", Buy_MagicNumber, 0, Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice()); } else Print("Error opening BUY order : ",GetLastError()); return(0); } //---- check for short position (SELL) possibility ----// if(Venda==-1) { ticket=OrderSend(Symbol(), OP_SELL, Lots, Bid, 1.5, Ask+(StopLoss*Point), Ask-(TakeProfit*Point), "Q Against God Wishes", Sell_MagicNumber, 0, Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice()); } else Print("Error opening SELL order : ",GetLastError()); return(0); } }
I did all the changes needed, but the problem still there, cutting the trades early. I decided to remove the “CheckForClose()”, once I want to close at the tp or sl.
Any idea what’s happening?

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I really don’t understand why my expert buy and sell the same symbol in 2 or 3 seconds, there is no reason for that in my formula. But, for sure I’m doing a mistake, I only don’t know where.
Can you help me out with it?
Thanks