MT4 Stubbornly Refusing to Trade

 

Hello everyone,

I have written my first baby of an EA... if you could call it that. More like an embryo. I am trying to get it to buy a small amount on a MB Trading demo MT4 account. Here is the code so far:

int init()

{

start(); // I had to put a call to start() here because otherwise it wouldn't execute anything in start(). Is that normal?

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

return(0);

}

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

//| expert start function |

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

int start()

{

double stl = Ask-25*10*Point; // set stoploss, it's a 5 digit broker so I add a 10 multiplier

double tp = Ask+25*10*Point; // set takeprofit

OrderSend(Symbol(),OP_BUY,10,Ask,3,stl,tp,"My order",16384,0,Green);

Alert(GetLastError());

return(0);

}

// END OF PROGRAM

As you can see, I have it displaying the last error code. It comes back with error number 130, which means that the stop loss or take profit are invalid or "unnormalized." Now, I have displayed the stoploss and take profit in testing, and the values come back completely normal. I am at a loss why MT4 would keep refusing them.

Would anyone know what might be going wrong here? I'm almost sure it's something just staring me in the face, and I can't see it...

Thanks in advance,

Dean.

 
incog13:

Hello everyone,

I have written my first baby of an EA... if you could call it that. More like an embryo. I am trying to get it to buy a small amount on a MB Trading demo MT4 account. Here is the code so far:

int init()

{

start(); // I had to put a call to start() here because otherwise it wouldn't execute anything in start(). Is that normal?

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

return(0);

}

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

//| expert start function |

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

int start()

{

double stl = Ask-25*10*Point; // set stoploss, it's a 5 digit broker so I add a 10 multiplier

double tp = Ask+25*10*Point; // set takeprofit

OrderSend(Symbol(),OP_BUY,10,Ask,3,stl,tp,"My order",16384,0,Green);

Alert(GetLastError());

return(0);

}

// END OF PROGRAM

As you can see, I have it displaying the last error code. It comes back with error number 130, which means that the stop loss or take profit are invalid or "unnormalized." Now, I have displayed the stoploss and take profit in testing, and the values come back completely normal. I am at a loss why MT4 would keep refusing them.

Would anyone know what might be going wrong here? I'm almost sure it's something just staring me in the face, and I can't see it...

Thanks in advance,

Dean.


Use the

Too hard to read this. No it's not normal to have to put start() in init(), BUY at Bid Sell at Ask. Search for 130 error on site. Did you use the wizard to create the program? You could use one of the EA's that are shipped with MT4 in the experts directory and remove code and add your.

 
danjp:


Use the

Too hard to read this. No it's not normal to have to put start() in init(), BUY at Bid Sell at Ask. Search for 130 error on site. Did you use the wizard to create the program? You could use one of the EA's that are shipped with MT4 in the experts directory and remove code and add your.


Thanks danjp, I haven't thought of that. I will see if I can find an expert that came with MT4 that uses the OrderSend() function. Having said that, does everything look normal to you, aside from what you noted above?

 
incog13:

Hello everyone,

I have written my first baby of an EA... if you could call it that. More like an embryo. I am trying to get it to buy a small amount on a MB Trading demo MT4 account. Here is the code so far:

As you can see, I have it displaying the last error code. It comes back with error number 130, which means that the stop loss or take profit are invalid or "unnormalized." Now, I have displayed the stoploss and take profit in testing, and the values come back completely normal. I am at a loss why MT4 would keep refusing them.


MBT is an ECN Broker, you have to send the Order with SL & TP sset to 0.0 and then do an OrderModify() to set the SL & TP . . . plenty of info here: ECN

Also you need to take note of the return value from the OrderSend so you know if it worked or not, if not you can then print this info to the log.

Did you try to run this EA while the markets were closed ? start() is called each time there is a tick . . . there are no ticks while the markets are closed. You should learn to test your code in the Strategy Tester then you can test it at any time even when the Markets are closed . . .

A Buy is opened at Ask . . . but a Buy is closed at TP or SL as a result of a Sell and sells happen at Bid . . . so your SL & TP for a Buy should be in relation to Bid, if you don't do this, or don't allow for the Spread, you may end up with other errors due to your SL or TP being too close to the current price.

Only Alert with the last error if you need to . . . . use the return value from the OrderSend() to determine if there is a problem or not . . .

 

You CAN NOT call start from init, as init MUST return in 2 seconds or less.

You were running on the weekends, no ticks. Use the Strategy tester to test.

Everything RaptorUK said.

Use SRC

 
RaptorUK:

MBT is an ECN Broker, you have to send the Order with SL & TP sset to 0.0 and then do an OrderModify() to set the SL & TP . . . plenty of info here: ECN

Also you need to take note of the return value from the OrderSend so you know if it worked or not, if not you can then print this info to the log.

Did you try to run this EA while the markets were closed ? start() is called each time there is a tick . . . there are no ticks while the markets are closed. You should learn to test your code in the Strategy Tester then you can test it at any time even when the Markets are closed . . .

A Buy is opened at Ask . . . but a Buy is closed at TP or SL as a result of a Sell and sells happen at Bid . . . so your SL & TP for a Buy should be in relation to Bid, if you don't do this, or don't allow for the Spread, you may end up with other errors due to your SL or TP being too close to the current price.

Only Alert with the last error if you need to . . . . use the return value from the OrderSend() to determine if there is a problem or not . . .


Thanks, changing the SL and TP to zero did the trick. As it happens, I was running it while I was getting ticks yesterday, and also today I am getting a price feed, but it still won't run start() unless I call it from init(). That's weird, but look below for the next thing that I'll try.
 
WHRoeder:

You CAN NOT call start from init, as init MUST return in 2 seconds or less.

You were running on the weekends, no ticks. Use the Strategy tester to test.

Everything RaptorUK said.

Use SRC


I will try running the EA from that link you provided, and will report back with the results.
 
incog13:

Thanks, changing the SL and TP to zero did the trick. As it happens, I was running it while I was getting ticks yesterday, and also today I am getting a price feed, but it still won't run start() unless I call it from init(). That's weird, but look below for the next thing that I'll try.
Do you have a smiley face in the top right corner of your chart with the EA name ? I'm guessing not . . . did you click the Expert Advisors ICON on the tool bar so that it goes from a Red X to a Green > ?
 
Learn how to use the Strategy Tester . . . you will be very glad of it.
 
incog13:

I will try running the EA from that link you provided, and will report back with the results.

WHRoeder:

You CAN NOT call start from init, as init MUST return in 2 seconds or less.

You were running on the weekends, no ticks. Use the Strategy tester to test.

Everything RaptorUK said.

Use SRC


Ok, I pasted in the EA from the link above and added a single Alert() call inside start() so that I would know if it got there. Here is the modified code below:

#import "user32.dll"
   int PostMessageA(int hWnd,int Msg,int wParam,int lParam);
   int RegisterWindowMessageA(string lpString);
#import

int init(){
   int hwnd=WindowHandle(Symbol(), Period());
   int msg = RegisterWindowMessageA("MetaTrader4_Internal_Message");
   PostMessageA(hwnd, msg, 2, 1); // enqueue a fake tick and let init() return
   return(0);
}

int start(){
   while (!IsStopped()){
      // do your stuff here
      Alert("I executed start()");
      Sleep(1000);
   }
   return(0);
}

One more thing to note - it gave me two warnings when I ran it in the form of text messages.

"Do you permit to call function 'RegisterWindowMessageA' from 'user32.dll'?"

I clicked Yes.

"Do you permit to call function 'PostMessageA' from 'user32.dll'?"

I clicked Yes.

Then it loaded and initialized the EA, according to the messages under the 'Experts' tab. However, no alert popped up, so that's why it seems that it did not reach start().

What would be my next step in troubleshooting this?

Thanks for your time,

Dean.

 
See my question above . . .
Reason: