why does my simple mql4 program not work as expected?

 

Hello,

I used to be a computerprogrammer but I am new in MQL4.

I wrote my first program, but several things do not work, here is the program:

int start()
  { int ticket=0;
 
    if (Close[0] < Close[1] && Close[1] > Close[2] && Close[2] > Close[3]) 
       //  Go short
          
          {    ticket=OrderSend(Symbol(),OP_SELL,1,Bid,3,High[0],((High[0] - Low[0])*2),"My short order",16384,0,Green);
                if (ticket < 0) MessageBox ("something went wrong with the order!");   
               ObjectCreate("Down", OBJ_ARROW, 0, TimeCurrent(), Close[0]);
               PlaySound("goshort!.wav");  

            Sleep(120000);

          }

           

    if (Close[0] > Close[1] && Close[1] < Close[2] && Close[2] < Close[3])
        //  Go long
       {     ticket=OrderSend(Symbol(),OP_BUY,1,Ask,3,Low[0],((High[0] - Low[0])*2),"My long order",16384,0,Green);
             if (ticket < 0) MessageBox ("something went wrong with the order!");
            ObjectCreate("Down", OBJ_ARROW, 0, TimeCurrent(), Close[0]);
            PlaySound("golong!.wav");   

           Sleep(120000);

       }

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

====

Problems:

1. the order is never being executed succesfully, why not?

2. the arrow is never being drawn, why not?

3. the strategy loses 100% of the trades when I test it with strategy tester (how is that even possible) ?

4. why is the order not being executed (in demo mode) but can i still backtest it?

5. I swear that sometimes I hear two times goshort or golong in less than two minutes, that is rare, but I swear it happened, and that should not be possible according to the code!



I hope somebody can help me....


Thanks in advance


Michael

 

Well, one obvious problem is Close[0]. There may be others, but start with that.

[0] is the current bar, so think about it, when it closes the new bar becomes the [0] bar......

So here's the first thing you need to do...

use

(Close[1] < Close[2] && Close[2] > Close[3] && Close[3] > Close[4])

 
  1. What are Function return values ? How do I use them ? - MQL4 forum

  2. Play video
    Please edit your post.
    For large amounts of code, attach it.


 
skaboy:

Well, one obvious problem is Close[0]. There may be others, but start with that.

[0] is the current bar, so think about it, when it closes the new bar becomes the [0] bar......

So here's the first thing you need to do...

use

(Close[1] < Close[2] && Close[2] > Close[3] && Close[3] > Close[4])

or....
(Open[0] < Open[1] && Open[1] > Open[2] && Open[2] > Open[3])
 
miguelholandes:

Hello,

I used to be a computerprogrammer but I am new in MQL4.

I wrote my first program, but several things do not work, here is the program:


====

Problems:

1. the order is never being executed succesfully, why not?                       // ECN broker ??     make use in your code   function  GetLastError()

2. the arrow is never being drawn, why not?                                          // if you have already object with name "Down"  then you can't make new one with this name

                                                                                                         // make unique names maybe   "Down" + OrderTicket()

3. the strategy loses 100% of the trades when I test it with strategy tester (how is that even possible) ?     // check the trades   if you open every tick a trade ........  not enough money

4. why is the order not being executed (in demo mode) but can i still backtest it?    //ECN ??

5. I swear that sometimes I hear two times goshort or golong in less than two minutes, that is rare, but I swear it happened, and that should not be possible according to the code! 

                                                // Explain why should it not be possible according to the code   ???


Michael



 
miguelholandes:

Hello,

I used to be a computerprogrammer but I am new in MQL4.

I wrote my first program, but several things do not work, here is the program:

<CODE REMOVED>

====

Problems:

1. the order is never being executed succesfully, why not?

2. the arrow is never being drawn, why not?

3. the strategy loses 100% of the trades when I test it with strategy tester (how is that even possible) ?

4. why is the order not being executed (in demo mode) but can i still backtest it?

5. I swear that sometimes I hear two times goshort or golong in less than two minutes, that is rare, but I swear it happened, and that should not be possible according to the code!

Please edit your post . . .    please use the   SRC   button to post code: How to use the   SRC   button. 

 

1.  your point number 3. contradicts this,  if 100% of the trades fail then orders must be being executed successfully.  Did you mean that they work in the Strategy tester but not on Demo/Live ?  is your Broker an ECN type Broker ?  if you are not sure execute this simple test :  https://www.mql5.com/en/forum/143043/page2#746319

2.  you are using the same name for the 2 arrow Objects,  if you draw the first the second will remove the first,  and generate an error,  and replace it.

3.  if the SL is so close to the open price it could happen.  Do you know what Spread is ?

4.  see answer to 1.  

5.  if you are hearing the sound then your OrderSend() is being attempted . . . your OrderSend() is failing,  why ?

Don't use Sleep() to set the delay between subsequent trades,  it does not work in the Strategy Tester,  it's just a bad idea . . .  test the time using TimeCurrent() and don't place another trade until enough time has elapsed . . .  doing this instead of Sleep()  will work in the Strategy Tester.