Need help for a code I've written

To add comments, please log in or register
korhan.x
15
korhan.x  

Hi everone,


I've started writing my first EA. It contains (although somewhat simple, yet still effective) money management, stop-trailing features and a simple trading criteria I added for testing purposes. (2 MA crosses). However, when I test the code, for the first 2 order it works ok, but later, it enters a meaningless loop for opening and closing orders. It opens an order and closes it immediately. I looked at the code but couldn't find what might result this. I'd really appreciate if someone can check it and help me to fix it. Code is quite clear as I added commentary notes to several parts, and it's simple...


Thanks already for your help. I added the EA.

Files:
firstea.mq4 21 kb
Nikolay Panev
1129
Nikolay Panev  
korhan.x:

Hi everone,


I've started writing my first EA. It contains (although somewhat simple, yet still effective) money management, stop-trailing features and a simple trading criteria I added for testing purposes. (2 MA crosses). However, when I test the code, for the first 2 order it works ok, but later, it enters a meaningless loop for opening and closing orders. It opens an order and closes it immediately. I looked at the code but couldn't find what might result this. I'd really appreciate if someone can check it and help me to fix it. Code is quite clear as I added commentary notes to several parts, and it's simple...


Thanks already for your help. I added the EA.


Hi, you can easily fix this by checking the trading criteria only at the beginning of a new bar. Otherwise several crosses may occur on a single bar and several trades will be opened and closed as in your case.

korhan.x
15
korhan.x  
robofx.org:

Hi, you can easily fix this by checking the trading criteria only at the beginning of a new bar. Otherwise several crosses may occur on a single bar and several trades will be opened and closed as in your case.

Thank you for your response.


Yeah, I'll try this approach. But I've got a question in my mind. In trade criteria, I used flags to open and close trades. And it should open order only if the condition is satisfied. And as I said, for the first 2 trades, it works well. I mean, it doesn't open and close several orders, it opens one order and until stop loss or take profit is satisfied, it remains open.If there was an error in the trading criteria, it should have opened erroneous orders from the beginning. Anyway, later on, something happens (I think, in stop loss values or take profit values, there is an error. Like stop loss value is equal to open order value, because it closes the trade as soon as it opens.


Did you check the code part?

Nikolay Panev
1129
Nikolay Panev  
korhan.x:

Thank you for your response.


Yeah, I'll try this approach. But I've got a question in my mind. In trade criteria, I used flags to open and close trades. And it should open order only if the condition is satisfied. And as I said, for the first 2 trades, it works well. I mean, it doesn't open and close several orders, it opens one order and until stop loss or take profit is satisfied, it remains open.If there was an error in the trading criteria, it should have opened erroneous orders from the beginning. Anyway, later on, something happens (I think, in stop loss values or take profit values, there is an error. Like stop loss value is equal to open order value, because it closes the trade as soon as it opens.


Did you check the code part?

It is not possible the stop loss and open price to be equal. There's a minimum distance that stop loss value should keep. It can be checked with MarketInfo(Symbol(),MODE_STOPLEVEL); function.

You can change the trading criteria like this:

MA_1_t=iMA(NULL,0,Period_MA_1,0,MODE_LWMA,PRICE_TYPICAL,1); 
MA_2_t=iMA(NULL,0,Period_MA_2,0,MODE_LWMA,PRICE_TYPICAL,1);  

if(Volume[0]<10)
{
   if (MA_1_t > MA_2_t + Rastvor*Point)         
   {                                          
      OPN_B=true;                              
      CLS_S=true;                               
   }
   if (MA_1_t < MA_2_t - Rastvor*Point)         
   {                                          
      OPN_S = true;                               
      CLS_B = true;                               
   }
}


See if it will work.

Good luck!

korhan.x
15
korhan.x  

Thanks a lot, it works in that way. I'll start implementing my own trade strategy from now on, bit by bit. I'll share it if it gives good results, and keep the code as readable as I can for anyone who's willing to add it.

Good day to you,

Korhan

To add comments, please log in or register