EA won't place a trade

 

Ok

Just starting out with coding here, and decided to make a very simple program just to play around with and test. All I want it to do is open a trade, yet, in forward testing, it doesn't open a trade when the criteria is met. Anyone can tell me why??

Here is the code for start

int Start()
{

double BSL = (Bid - 40*Point), BTP = (Bid + 40*Point), SSL = (Ask + 40*Point), STP = (Ask - 40*Point);
if (iMA(NULL, 0, 3, 0, MODE_EMA, 0, 1) > iMA(NULL, 0, 10, 0, MODE_EMA, 0, 1))
{
int Ticket = OrderSend(Symbol(), OP_BUY,1,Ask,10,BSL,BTP);
if (Ticket == False) {Alert("trade not placed");}
}
Alert(GetLastError());

if (iMA(NULL, 0, 3, 0, MODE_EMA, 0, 1) < iMA(NULL, 0, 10, 0, MODE_EMA, 0, 1))
{
int Ticket = OrderSend(Symbol(), OP_SELL,1,Bid,10,SSL,STP);
if (Ticket == False) {Alert("trade not placed");}
}
Alert(GetLastError());
return(0);

} 
 
double BSL = (Bid - 40*Point), BTP = (Bid + 40*Point), SSL = (Ask + 40*Point), STP = (Ask - 40*Point);

40*Point may be too close to the current price. With a 5 Digit broker, that is only 4 Pips

It helps if you Print the error and let us know what it is.

int Ticket = OrderSend(Symbol(), OP_BUY,1,Ask,10,BSL,BTP);
if (Ticket == False) {Alert("trade not placed");}

Why are you comparing the integer Ticket to false? It is an integer, not a boolean.

OrderSend returns -1 if it fails and that does not equate to false.

 

Awesome thanks for the help. Made the changes, but still nothing.

No Alerts, nothing in the journal, no trades. Nothing

I put an alert in the init, which says "Test initialized" to test whether the EA is even operating. That alert pops up, but nothing else happens after that. Autotrading is enabled, little smiley face at the top and everything. but no trading

 

Another question, when I try to compile, it won't allow me unless I have this statement "return(0)" at the end of start, instead of just "return()"

I've seen other example EAs that only had the return(). Has this got something to do with Metquotes trying to merge MQL4 $ 5???

 

Couple of things.

As the previous poster noted 40*Point may be too small if you're on a 5 digit broker, you can check with the MarketInfo function to get the points allowed and make sure your stop doesn't fall within this range.

Additionally, when sending values used in a Order function you must normalize the double variables to ensure they meet the amount of digits required by your broker, or else the trade will not succeed.

Finally, the OrderSend function returns -1 if it fails or the ticket if it succeeds.

double BSL     = NormalizeDouble((Bid - 40*Point),Digits);
double BTP     = NormalizeDouble((Bid + 40*Point),Digits);
double SSL     = NormalizeDouble((Ask + 40*Point),Digits);
double STP     = NormalizeDouble((Ask - 40*Point),Digits);
int    MinStop = MarketInfo(Symbol(), MODE_STOPLEVEL);

if(MinStop > 40) {
   Alert("Minimum stop distance allowed is: ",MinStop);
   return(0);
}

if (iMA(NULL, 0, 3, 0, MODE_EMA, 0, 1) > iMA(NULL, 0, 10, 0, MODE_EMA, 0, 1)) {
   if(OrderSend(Symbol(), OP_BUY,1,Ask,10,BSL,BTP); < 0) {
      Alert("trade not placed");
   }
}
Alert(GetLastError());

if (iMA(NULL, 0, 3, 0, MODE_EMA, 0, 1) < iMA(NULL, 0, 10, 0, MODE_EMA, 0, 1)) {
   if(OrderSend(Symbol(), OP_SELL,1,Bid,10,SSL,STP) < 0) {
      Alert("trade not placed");
   }
}
Alert(GetLastError());
return(0);
 
Cerberus:

Another question, when I try to compile, it won't allow me unless I have this statement "return(0)" at the end of start, instead of just "return()"

I've seen other example EAs that only had the return(). Has this got something to do with Metquotes trying to merge MQL4 $ 5???


what version of MT4 are you using?
 
Cerberus:

Another question, when I try to compile, it won't allow me unless I have this statement "return(0)" at the end of start, instead of just "return()"

I've seen other example EAs that only had the return(). Has this got something to do with Metquotes trying to merge MQL4 $ 5???

when the function is void, meaning it doesn't return a value, you can use just "return" to exit from that function.

Start() is an int (hence the int when defining Start), so you must return an int value, which is why you might see some functions just return and others return(0)

 
Cerberus:

Ok

Just starting out with coding here, and decided to make a very simple program just to play around with and test. All I want it to do is open a trade, yet, in forward testing, it doesn't open a trade when the criteria is met. Anyone can tell me why??

Here is the code for start

Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.

 
fantomfx:

what version of MT4 are you using?



Thanks. I've implemented the changes and going to test them now.

I'm using metaeditor build 914

 
angevoyageur:
Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.



Ok will do
 
fantomfx:

Couple of things.

As the previous poster noted 40*Point may be too small if you're on a 5 digit broker, you can check with the MarketInfo function to get the points allowed and make sure your stop doesn't fall within this range.

Additionally, when sending values used in a Order function you must normalize the double variables to ensure they meet the amount of digits required by your broker, or else the trade will not succeed.

Finally, the OrderSend function returns -1 if it fails or the ticket if it succeeds.



Well I've made all these changes, and run it. Still no trading, no alerts or anything. If it makes any difference I'm running it on a demo account with Axitrader. Testing this on the 1 min EUR/USD