My first EA, it wont open trades, any help?

 

Hello, i just started studying MQL4 programing, i am working on my first EA, but it wont open any trades. It is a Scalper for the USDJPY, and the premise is simple,

it should launch an order if the price is 15 points above an 8 period MA (it shoul buy)

or 15 points below an 8 period MA, it should sell, i know i must have done something wrong, any help? leave the code:

Thanks in advance...

//+------------------------------------------------------------------+
//|                                         Moving Average Scalp.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property strict

extern double TakeProfit = 60;
extern double StopLoss = 20;
extern int MA = 8;
extern int MAShift = 0;
extern int MAMethod = 0;
extern int MAApliedTo = 0;
extern double LotSize = 0.01;
extern int MagicNumber = 123;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Start function                                           |
//+------------------------------------------------------------------+
int start()
  {
//---
  double Media = iMA (NULL,0,MA, MAShift,MAMethod, MAApliedTo,1); 

  double PriceBid = Bid;

  double PriceAsk = Ask;    

   //para comprar   

   if (PriceAsk>Media+(15))

      if(OrdersTotal()==0)

      OrderSend (Symbol (),OP_BUY,LotSize,Ask,6,Ask-(StopLoss*.010),Ask+(TakeProfit*.010),NULL,MagicNumber,0,Green); 

   //para venta   

   if (PriceBid<Media-(15))

       if(OrdersTotal()==0)

       OrderSend (Symbol (),OP_SELL,LotSize,Bid,6,Bid+(StopLoss*.010),Bid-(TakeProfit*.010),NULL,MagicNumber,0,Red);
       

       return (0);
  }
//+------------------------------------------------------------------+

 

Hey Luis, it looks like you made a bunch of errors but I understand this language is driving me crazy too... what helped me is watching some of the instructional videos on youtube. I'll be glad to help you write this program but I need to know a little bit more. The first question I have is do you want to open a position any time the price is 15 points higher then the moving average? or only when the price moves from less then to more then 15 Points? (which is a cross and the why you have to exspress that is not as easy as you would think).


Here is a link to a Video that really helped me understand some of the concepts of the MQL Language, keep in mind that this is a older video and the language has been updated. I suggest you get aquainted with the reference guide containded within the MQL4 Editor.

https://youtu.be/t-TmsV0hV8Y 


If you have any questions feel free to send me a message


Kurt Edson

 
Kurt Edson:

Hey Luis, it looks like you made a bunch of errors but I understand this language is driving me crazy too... what helped me is watching some of the instructional videos on youtube. I'll be glad to help you write this program but I need to know a little bit more. The first question I have is do you want to open a position any time the price is 15 points higher then the moving average? or only when the price moves from less then to more then 15 Points? (which is a cross and the why you have to exspress that is not as easy as you would think).


Here is a link to a Video that really helped me understand some of the concepts of the MQL Language, keep in mind that this is a older video and the language has been updated. I suggest you get aquainted with the reference guide containded within the MQL4 Editor.

https://youtu.be/t-TmsV0hV8Y 


If you have any questions feel free to send me a message


Kurt Edson

Hi Kurt, thanks for replying, actually the only thing i want is for the EA to launch an order when the price is at a 15 point distance from the MA, since i have a second condition wich is there must be no other opened trades, i figure it doesnt really matter if it touched the MA and went far from it again, does this answers your question? english is not my first language so i really don`t know ... Thanks!

 
Luis Garcia:

it should launch an order if the price is 15 points above an 8 period MA (it shoul buy)

or 15 points below an 8 period MA, it should sell, i know i must have done something wrong, any help? leave the code:

In future, please use the code button (Alt + S) when posting code. Also avoid excessive use of blank lines, then others can read your code easier.

I have edited your post this time.

I have also deleted a couple of irrelevant posts.

  double PriceBid = Bid;

  double PriceAsk = Ask;    

You don't need these variables, just use Ask and Bid, it makes your code simpler.


if (PriceAsk>Media+(15))

Now I don't know what symbol you may be using this with, but if it is something like GBPUSD and the MA value is 1.3000

do you think that the Ask will ever be 16.3000 ?

 
Keith Watford:

In future, please use the code button (Alt + S) when posting code. Also avoid excessive use of blank lines, then others can read your code easier.

I have edited your post this time.

I have also deleted a couple of irrelevant posts.

You don't need these variables, just use Ask and Bid, it makes your code simpler.


Now I don't know what symbol you may be using this with, but if it is something like GBPUSD and the MA value is 1.3000

do you think that the Ask will ever be 16.3000 ?

Hi Keith, thanks so much for your response, actually it is meant for the USDJPY, I understand what you are saying, i modified the code, it is still wont take trades, trying to figure out what i did wrong, thanks for your help.

 
Luis Garcia:

Hi Keith, thanks so much for your response, actually it is meant for the USDJPY, I understand what you are saying, i modified the code, it is still wont take trades, trying to figure out what i did wrong, thanks for your help.

You are using

#property strict

so you should be getting warned by the compiler that the OrderSend() return should be checked.

There is a reason for this warning, so check it. Print all details should the OrderSend() return -1, then you may be able to pinpoint the problem.

 
You're trying to use OnStart() in an Expert but it's a script event. Use OnTick() instead.
 
mt4ski:
You're trying to use OnStart() in an Expert but it's a script event. Use OnTick() instead.

Wrong.

He is using start.

 
Keith Watford:

Wrong.

He is using start.

Still, it is exclusively a script event processed by OnStart().

The HTML editor doesn't work so here's the quote from MQL4 reference without formatting:

"The Start event is a special event for script activation after it is loaded. This event is processed by OnStart handler. The Start event is not sent to Expert Advisors or custom indicators."

 

Hi guys, i fixed it, thanks to all of you for beeing such great suport, I changed the parameters for the distance beetween the price and the MA as Keith suggested but it wasn´t making any orders, what i had to do was:


Add a Res=OrderSend, I´m trying to paste the code but it wont do it, so i will just paste a screen shot for the sake of sharing



All i did was adding that Res parameter and putting the Res= in front of OrderSend (got the idea from one of the expertes mt4 has built in)

 
mt4ski:
You're trying to use OnStart() in an Expert but it's a script event. Use OnTick() instead.
Keith Watford:

Wrong.

He is using start.

mt4ski:

Still, it is exclusively a script event processed by OnStart().

The HTML editor doesn't work so here's the quote from MQL4 reference without formatting:

"The Start event is a special event for script activation after it is loaded. This event is processed by OnStart handler. The Start event is not sent to Expert Advisors or custom indicators."

start (not Start) predates OnTick. Unfortunately a lot of MQL4 tutorials have not been updated and so still use start. Many beginners with MQL4 will use start because of this.

The compiler will still compile start Ok.

Reason: