Error 130 Please help

 

I'm new, so if it has been discused before, just point me to right direction.
I wrote an EA like martingale, and back tested it, works just fine, no errors, but when I add it to the chart (demo account) it gives me error 130
No matter what I do, I get the same error, I even cleared the code and made it as simple as beloew just to place a simple buy order, but it still error 130.
Here is the code:

int ticket = 0;

int start()
{
Print("Buying at ", Ask, " - s/l at: ", Ask - 200 * Point, " - t/p at: ", Ask + 200 * Point);
ticket = OrderSend(Symbol(), OP_BUY, 1, Ask, 5, Ask - 200 * Point, Ask + 200 * Point);
if(ticket < 0)
Print("OrderSend failed with error #",GetLastError());
return(0);
}


Please Please help.
Try this code, see if you get the same error.
I'm using a demo account on MT4 with FXCM

 

Try using NormalizedDouble with Digits. For example:

#include <stdlib.mqh> //this is needed for NormalizedDouble

//
// YOUR CODE HERE
//

double test = NormalizedDouble(Ask+200*MarketInfo(Symbol(), MODE_POINT), MarketInfo(Symbol(), MODE_DIGIT));

//test is now a normalized double

Since that's likely not your problem, could you tell me what the output of the Print statement at the top is on the chart?

 
I scouted engcomp's thread for ya, and it looks like his solution was to do OrderSend without stop loss or take profit, then use OrderModify to set stop loss. I'm playing Dragon Age so I'm a little distracted, but I think that's the gist of it. Maybe I'm wrong.
 
ubuntuboy:
I scouted engcomp's thread for ya, and it looks like his solution was to do OrderSend without stop loss or take profit, then use OrderModify to set stop loss. I'm playing Dragon Age so I'm a little distracted, but I think that's the gist of it. Maybe I'm wrong.

I tried the link I gave you and it failed, so I pulled my reply.

You are right, the problem is with some brokers who do not allow sl and tp with market orders.

You have to place a naked order first, then modify it with your sl and tp. As simple as that.

 
ubuntuboy:

Try using NormalizedDouble with Digits. For example:

Since that's likely not your problem, could you tell me what the output of the Print statement at the top is on the chart?


Thanks for the replies guys.
Here is what I get:
2010.06.23 06:40:26 test EURUSD,M1: Buying at 1.2237 - s/l at: 1.2217 - t/p at: 1.2257

and then of course:
2010.06.23 06:40:26 test EURUSD,M1: OrderSend failed with error #130


I thought about sending the order without sl & tp, and then modify and add them. But I'm affraid if modify fails, I can loose my capital. I'd rather not open the trade than have a trade with no sl.

Please try the code and let me know if you get the same error.
If it is the broker, then I will change it. It's just demo account anyway.

 

Do you guys know which brokers allow sl and tp with market orders?

 

Try using OrderModify but do a check. Have something like:

bool wootwoot = OrderModify(blahblah);
if (!wootwoot) {
    Alert("OOPSIES");
    OrderClose(blahblah);
}

If you specify a valid stop loss I suspect you WILL get true. Use this code in the strategy tester first. Open up your journal in Notepad, do a find on "OOPSIES", and if you get a hit... OOPSIES.

 
ubuntuboy:
I scouted engcomp's thread for ya, and it looks like his solution was to do OrderSend without stop loss or take profit, then use OrderModify to set stop loss. I'm playing Dragon Age so I'm a little distracted, but I think that's the gist of it. Maybe I'm wrong.

Here are the code snippets I use...

int OpenLong(double volume=0.1)
{
  ticket = OrderSend( Symbol(), OP_BUY, volume, Ask, slip, 0, 0, "myRVIea", EA_magic, 0, Blue );
 
  if(ticket>0)
  {
    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
    {
       OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(sl*MyPoint),OrderOpenPrice()+(tp*MyPoint),0,CLR_NONE);
       return(0);
    }
    else
      {
        Print("OpenLong(),OrderSelect() - returned an error : ",GetLastError()); 
        return(-1);
      }   
  }
  else 
  {
    Print("Error opening Buy order : ",GetLastError()); 
    return(-1);
  }
}
  
int OpenShort(double volume=0.1)
{
  ticket = OrderSend( Symbol(), OP_SELL, volume, Bid, slip, 0, 0, "myRVIea", EA_magic, 0, Red );

  if(ticket>0)
  {
    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
      {
          OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(sl*MyPoint),OrderOpenPrice()-(tp*MyPoint),0,CLR_NONE);
          return(0);
      }
    else
      {
        Print("OpenShort(),OrderSelect() - returned an error: ",GetLastError()); 
        return(-1);
      }    
  }
  else 
  {
    Print("Error opening Sell order : ",GetLastError()); 
    return(-1);
  }
}
Hope this helps?
 
engcomp:

Here are the code snippets I use [...]

This doesn't affect me personally, but I'm going to broaden out the discussion on the grounds of building reliable code. What do you do if the OrderModify() fails after placing the initial order? You've then got an unprotected order in the market with no s/l. The three options are basically (a) keep re-trying the s/l; (b) give up and close the order; or (c) have the EA keeps its own separate record of what the s/l is supposed to be, and close the order manually if that level is breached. (C) is required anyway if you want a "phantom" stop which is different to what it is reported to the broker, but requires that the EA can remember the s/l across restarts, which has its own complications. What are people's preferences?

(The problem with brokers who require a separate OrderModify() is that this removes one of the key things which makes MT4 easier to write code for than other platforms: the ability to place an order and its s/l as a single transaction which either succeeds or fails as a whole. It puts MT4 code back into the same position as most other trading platforms, where you have to do much more error-checking, and of a more thorny variety.)
 
jjc:
This doesn't affect me personally, but I'm going to broaden out the discussion on the grounds of building reliable code. What do you do if the OrderModify() fails after placing the initial order? You've then got an unprotected order in the market with no s/l. The three options are basically (a) keep re-trying the s/l; (b) give up and close the order; or (c) have the EA keeps its own separate record of what the s/l is supposed to me, and close the order manually if that level is breached. (C) is required anyway if you want a "phantom" stop which is different to what it is reported to the broker, but requires that the EA can remember the s/l across restarts, which has its own complications. What are people's preferences?

(The problem with brokers who require a separate OrderModify() is that this removes one of the key things which makes MT4 easier to write code for than other platforms: the ability to place an order and its s/l as a single transaction which either succeeds or fails as a whole. It puts MT4 code back into the same position as most other trading platforms, where you have to do much more error-checking, and of a more thorny variety.)

jjc, thank you for a fantastic contribution. Why doesn't it affect you personally? Your broker must allow you placing sl and tp with the order.

The questions you raise are VERY important because the scenario of an unprotected position just when my MT4 says "no connection" is a real possibility.

All of the solutions you suggest won't work in this case. You won't even know if your position IS unprotected.

You have certainly "broadened the discussion". Thank you, Helmut

 
engcomp:

Why doesn't it affect you personally?

Multiple reasons applying to different systems:

  • Use of brokers who don't use the Boston Technologies bridge, and who therefore do allow an s/l on market orders
  • Entry using pending orders rather than market orders, and therefore able to place the s/l with the opening order
  • Systems which trade baskets of orders and implement their own s/l rather than placing one into the market.

By little more than coincidence, we don't have any systems running on BT brokers which enter with a market order and need an s/l (or t/p) in the market.

But it's the sort of thing which has been a pig to deal with in the past, particularly on platforms other than MT4. The logic, error-checking, and potential clean-up get really nasty if you're using a pedantic platform which requires you to place an entry order, then a separate stop-loss order, then a separate take-profit order, and then combine the s/l and t/p into an OCO group. Unwinding that sequence if it fails part-way through isn't fun, particularly because you then need to cater for partial failure in unwinding the position, or orders getting filled while you're trying to cancel them...

engcomp:

The questions you raise are VERY important because the scenario of an unprotected position just when my MT4 says "no connection" is a real possibility.

This can also potentially be caused by things like a temporary problem at the broker's server which doesn't lead to a disconnection (e.g. server overload which eventually returns with error #128).

There are also scenarios such as the price moving between your OrderSend() and OrderModify() such that your intended s/l is no longer valid. Obviously only likely to happen if you're using a very tight stop, but you at least need to consider the possibility of it happening.

Reason: