EA opens 2 trades in one candle.

 

Hi,


I've programmed a really simple expert advisor. The EA opens a long position when the slow stochastics closes below the value 86. 

 
extern double lot = 0.1;
extern double tp = 60; //for EURUSD M5
extern double sl = 1000; //for EURUSD M5


 
int start()
  { 
     double r1 = iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_MAIN,1);
     double r2 = iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_MAIN,2);
     
     {
 
     if (r2>86 && r1<86 && OrdersTotal()<1) 
     {  
     OrderSend(Symbol(),OP_SELL,lot,Bid,0,Bid+sl*Point,Bid-tp*Point,"Stoch signal",0,0,clrRed);
     }
          
       }
    return(0);    
     }


The EA does it's job, but sometimes it opens a new position when the TP is hit inside the first candle. (check the image http://prntscr.com/4vrtlr )

How should I add to my code to repair this problem. Is there a parameter who stores the amount of orders inside a candle?


Thanks in advance,

Thierry

 

you have to code that yourself.

The TP is triggered while the condition to open the trade is still true: wait a) for some time b) for a trade in the other direction c) ...

 
xtractalpha: The EA opens a long position when the slow stochastics closes below the value 86. 

The EA does it's job, but sometimes it opens a new position when the TP is hit inside the first candle. (check the image http://prntscr.com/4vrtlr )

How should I add to my code to repair this problem. Is there a parameter who stores the amount of orders inside a candle?

extern double tp = 60; //for EURUSD M5
extern double sl = 1000; //for EURUSD M5

OrderSend(Symbol(),OP_SELL,lot,Bid,0,Bid+sl*Point,Bid-tp*Point,"Stoch signal",0,0,clrRed);
  1. nope
  2. The stops of a sell are relative to the Ask. So your actual TP is tp - spread. 60 - 20 = 4 pips.
  3. If you don't want to open in the same bar remember the bar time when you open (global/static) and check for new/same bar.
 
     if (r2>86 && r1<86 && OrdersTotal()<1) 
     {  
     OrderSend(Symbol(),OP_SELL,lot,Ask,0,Ask+sl*Point,Ask-tp*Point,"Stoch signal",0,0,clrRed);
     Sleep(300000); //milliseconds
     }


Gooly & WHRoder thanks for your help!

You are right about the Ask price, I've edited it in my EA. 

I've added a sleep function after my "ordersend" function. That should do the job too (In my eyes).


Thanks

 
F7 > Common > Allow live trading

Something like that.

 

Thank you, I feel so stupid.

Reason: