Need Help On MA Crossover Expert Advisor

 

Hi,

I'm totaly new to MQL 4 but wanted to create an Expert Advisor so I used a programm,

it works ok but i'm not fully statisfied, the EA should open orders on the Crossover aof the MA's ( just 2 MA's) long and short.

Now the problem is, that the EA can just open ONE order at the same time and thast not what I want, I want that he opens a order evrytime the MA's cross but I just can not figure out how I can do THIS.

I'm adding a part of the EA where I think the mistake could be.

So PLEAS if you can help me answer to that post.


Please tell me also if this isn't the part where the Mistake is.


sorry if the english is to bad i hope you can understand my problem.


int start() {
   if(Bars<30) {
      Print("NOT ENOUGH DATA: Less Bars than 30");
      return(0);
   }

   if(!manageEntriesAndPositions()) {
      return(0);
   }
   
   // LONG: (EMA(10) Crosses Above EMA(40))

   if(TradeLong) {
      bool LongEntryCondition = ((iMA(NULL, 0, pEMA_1, 0, MODE_EMA, PRICE_CLOSE, 2) < iMA(NULL, 0, pEMA_2, 0, MODE_EMA, PRICE_CLOSE, 2)) 
      && (iMA(NULL, 0, pEMA_1, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(NULL, 0, pEMA_2, 0, MODE_EMA, PRICE_CLOSE, 1)));
      if(LongEntryCondition == true) 
      {
         openPosition(1);
      }
   }
   
   
   // SHORT: (EMA(10) Crosses Below EMA(40))

   if(TradeShort) {
      bool ShortEntryCondition = ((iMA(NULL, 0, pEMA_3, 0, MODE_EMA, PRICE_CLOSE, 2) > iMA(NULL, 0, pEMA_4, 0, MODE_EMA, PRICE_CLOSE, 2))
      && (iMA(NULL, 0, pEMA_3, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(NULL, 0, pEMA_4, 0, MODE_EMA, PRICE_CLOSE, 1)));
      if(ShortEntryCondition == true) 
      {
         openPosition(-1);
      }
   }

   return(0);
}
 
pinkybrain:

Hi,

I'm totaly new to MQL 4 but wanted to create an Expert Advisor so I used a programm,

it works ok but i'm not fully statisfied, the EA should open orders on the Crossover aof the MA's ( just 2 MA's) long and short.

Now the problem is, that the EA can just open ONE order at the same time and thast not what I want, I want that he opens a order evrytime the MA's cross but I just can not figure out how I can do THIS.

I'm adding a part of the EA where I think the mistake could be.

What does the   manageEntriesAndPositions()  function do ?


You should read this thread:  https://www.mql5.com/en/forum/139865
 
RaptorUK:
What does the   manageEntriesAndPositions()  function do ?


You should read this thread:  https://www.mql5.com/en/forum/139865


Hi, like i said the code was created by a program wait i will add the complete MQL4 Code


I'm pretty sure more than 80% of this code is not needed for what i want to have.


I just want a MA crossover with trailing stop wich opens an order on every crossover.


I have to Attach the file because the code is that long.


Pleas help.

Files:
 
RaptorUK:
What does the   manageEntriesAndPositions()  function do ?


You should read this thread:  https://www.mql5.com/en/forum/139865

Oh I think I get it i shouldn't posts questions about codes wich are createt with a EA builder?
 
pinkybrain:

Oh I think I get it i shouldn't posts questions about codes wich are createt with a EA builder?
You can . . . but from what I have seen so far this kind of tool produces bad code with lots of problems.  If you want to create code that works correctly you need to learn to code,  correctly,  the Book is free to read.
 
RaptorUK:
You can . . . but from what I have seen so far this kind of tool produces bad code with lots of problems.  If you want to create code that works correctly you need to learn to code,  correctly,  the Book is free to read.


Ok, I get it.

But i thought it should be comparably simple for someone to find the part where it's said that there no order should be opened if alleredy one is running.

And that would realy help me a lot.

But it's possible that it isn't as easy to find as I imagine so if that's the case I'm sorry.

 
RaptorUK:
You can . . . but from what I have seen so far this kind of tool produces bad code with lots of problems. 

For example,

  • slippage is not adjusted to compensate for 4/5 digit brokers
  • Orders are deleted using a loop that counts up,  this will not delete all orders
  • the code does not check return values and report errors from many function calls, for example:   OrderClose(),  OrderDelete() 
 
RaptorUK:

For example,

  • slippage is not adjusted to compensate for 4/5 digit brokers
  • Orders are deleted using a loop that counts up,  this will not delete all orders
  • the code does not check return values and report errors from many function calls, for example:   OrderClose(),  OrderDelete() 


Ok I get this thats what I meant with " I think 80% of this code are not neccesarry"

But the mentioned problems are not responsible that the ea just can open one order or am I wrong?

 
pinkybrain:

Ok, I get it.

But i thought it should be comparably simple for someone to find the part where it's said that there no order should be opened if alleredy one is running.

To change code properly while understanding all the consequences of the changes being made means understanding all the code,  in this case you have 1300 lines of code,  to understand all that code takes more than a few minutes . . .


Try this at your own risk . . . .  try it in the Strategy Tester first  ! ! 


Find this part of the code and comment out the lines indicated . . . 

void openPosition(int tradeDirection) {
   if(tradeDirection == 0) return(0);
   
   if(checkTradeClosedThisMinute()) {
      return(0);
   }        

   if(EnterOnlyOncePerBar && checkTradeClosedThisBar()) {
      return(0);
   }
   //---------------------------------------
   // get order price
   double openPrice = NormalizeDouble(getTradeOpenPrice(tradeDirection), Digits);
   
   //---------------------------------------
   // get order type
   int orderType;
   if(tradeDirection == 1) {
//      if(getMarketPosition() != 0) return;     //<----  comment out this line

      orderType = OP_BUY;

   } else {
//      if(getMarketPosition() != 0) return;     //<----  comment out this line

      orderType = OP_SELL;

   }
Reason: