Yet still Error 130 ???

 

I've read the limitations on stoploss levels for opening orders and I can succesfully open orders from an EA, but I have a script which I want to drop on a chart to reverse the single existing position to opposite side and it still fails to work. Can anyone try and help me to resolve the issue ? Thank you,


//+------------------------------------------------------------------+
//|                                                       REVERT.mq4 |

int NumberOfTries = 1;
int RetryTime = 1;
int ticket = 0;
int StopLoss = 400;
int TakeProfit = 400;

int start() {
   int Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL);// Min. distance
   int FreezeLevel=MarketInfo(Symbol(), MODE_FREEZELEVEL);
   Print("Min dist="+Min_Dist+" digits="+Digits+" point:"+Point);
   if (StopLoss < Min_Dist)                   // If it is less than allowed
   {
    StopLoss=Min_Dist;                      // Set the allowed
    Print(" Increased the distance of SL = ",StopLoss," pt");
   }

	int OrdersPerSymbol, cnt = 0;
	for (cnt = OrdersTotal(); cnt >= 0; cnt--) {
		OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
		if (OrderSymbol() == Symbol()) {
			OrdersPerSymbol++;
			break;
		}
	}
	
	int type = OrderType();
	int oldTicket = OrderTicket();
	Print("Found order of type " + type);
	int tries, err = 0;
	double SL=0;
	double TP = 0;
	switch (type) {
	case OP_BUY:
		if (!IsTradeContextBusy() && IsTradeAllowed()) {
			while (tries < NumberOfTries) {
				RefreshRates();
				SL = NormalizeDouble(Ask + StopLoss * Point, Digits) ;
				TP = NormalizeDouble(Ask - TakeProfit * Point, Digits);
				ticket = OrderSend(Symbol(), OP_SELL, OrderLots() * 2, Bid, 3,SL,TP);
				if (ticket <= 0) {
					Print("Error Occured : " + GetLastError());
					Print(Symbol() + " Sell @ " + Bid + " SL @ " + SL + " TP @" + TP + " ticket =" + ticket);
					tries++;
				} else {
					tries = NumberOfTries;
					Print("Order opened : " + Symbol() + " Sell @ " + Bid + " SL @ " + SL + " TP @" + TP + " ticket =" + ticket);
					bool ret = OrderCloseBy(oldTicket,ticket, CLR_NONE);
				}
				Sleep(RetryTime * 500);
			}
		}
		else {
		 Print("Cant trade "+IsTradeContextBusy()+" "+IsTradeAllowed());
		}
		err = ticket;
		break;
	case OP_SELL:
		if (!IsTradeContextBusy() && IsTradeAllowed()) {
			while (tries < NumberOfTries) {
				RefreshRates();
				SL = NormalizeDouble(Bid - StopLoss * Point, Digits);
				TP = NormalizeDouble(Bid + TakeProfit * Point, Digits);
				ticket = OrderSend(Symbol(), OP_BUY, OrderLots() * 2, Ask, 3, SL, TP);
				if (ticket <= 0) {
					Print("Error Occured : " + GetLastError());
					Print(Symbol() + " Buy @ " + Ask + " SL @ " + SL + " TP @" + TP + " ticket =" + ticket);
					tries++;
				} else {
					tries = NumberOfTries;
					Print("Order opened : " + Symbol() + " Buy @ " + Ask + " SL @ " + SL + " TP @" + TP + " ticket =" + ticket);
					OrderCloseBy(oldTicket,ticket, CLR_NONE);
				}
				Sleep(RetryTime * 500);
			}
		}
		err = ticket;
		break;
	}
	return (0);
}

It just fails on error 130 what ever I try to do with it.

 

I note you print the output of "Min_Dist". What is it?


CB

 
edein:

It just fails on error 130 what ever I try to do with it.


for (cnt = OrdersTotal(); cnt >= 0; cnt--) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol()) {
Order positions are just like bars, they start at zero (High[0]) You're count is off buy one. 
The first OrderS
elect selects an invalid position, so OrdersPerSymbol, OrderLots() may be invalid.
ticket = OrderSend(Symbol(), OP_BUY, OrderLots() * 2, Ask, 3, SL, TP);
Change to:
for (cnt = OrdersTotal()-1; cnt >= 0; cnt--) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol()) {
Also the slip of 3 is invalid on Five (5) Digit brokers.

 
Thank you WHRoeder, the slippage was the main culprit.