how to avoid big ups and downs in current bar?

 
I want my ea does not buy/sell in big movements such as on news events..
 
You can try using a volatility-indicator but those are usually slow for current bars. So my suggestion is that you limit the trades via Time. Ex> if(TimeCurrent()>Last_Trade_Time + TimeHour()*4){...Its OK now to place a new order;}
 

If you are entering using stop orders (or to a lesser extent limit orders), it's not so easy since you can have the situation where the market is quiet for a while then the spread rises instantly, and the next tick can be a huge distance away, getting you into a trade without warning.

If you are entering using market orders, it is a lot more practical to filter out signals that occur at news events. One filter to use is the spread. You can add a condition to your entry signal that the Ask minus the Bid is less than a certain amount. This will filter out the move immediately after major news, where the spread stays high for a while.

ubzen mentioned the volatility indicator but is right that this is going to be slow to react, even if you use values of the indicator on a 1 minute chart. However, you effectively have access to a tick chart if you write the code yourself rather than using an indicator. This is not so difficult to do, but you'll need to work out exactly what would tell you that there was a news event occurring in order to use it.

But if you are trading on a relatively large scale (not scalping) the combination of these two should do it. First adapt your code so that all entries are at market (to avoid entering on an violent move that catches a limit order or a stop order in a single tick). Any limit order or stop order you can change to a condition to check if a limit order or a stop order would have been triggered (combined with your new conditions to avoid news moves) and a command to enter at market. Secondly, you need to identify a threshold of spread that would tell you that there is a news event and use this information to filter your signals. You could, for example, avoid entering for a chosen time after a sudden increase in spread. Thirdly you could work out what sort of move you would identify as being caused by news and translate this into a condition based on volatility on a sufficiently short term chart (might as well use the 1 minute). You could come up with the parameters by tinkering with the parameters of a volatility indicator and seeing what values it has reached at previous news events.

A bit of work to implement, but should serve the purpose.

 
Elroch:

If you are entering using stop orders (or to a lesser extent limit orders), it's not so easy since you can have the situation where the market is quiet for a while then the spread rises instantly, and the next tick can be a huge distance away, getting you into a trade without warning.

If you are entering using market orders, it is a lot more practical to filter out signals that occur at news events. One filter to use is the spread. You can add a condition to your entry signal that the Ask minus the Bid is less than a certain amount. This will filter out the move immediately after major news, where the spread stays high for a while.

ubzen mentioned the volatility indicator but is right that this is going to be slow to react, even if you use values of the indicator on a 1 minute chart. However, you effectively have access to a tick chart if you write the code yourself rather than using an indicator. This is not so difficult to do, but you'll need to work out exactly what would tell you that there was a news event occurring in order to use it.

But if you are trading on a relatively large scale (not scalping) the combination of these two should do it. First adapt your code so that all entries are at market (to avoid entering on an violent move that catches a limit order or a stop order in a single tick). Any limit order or stop order you can change to a condition to check if a limit order or a stop order would have been triggered (combined with your new conditions to avoid news moves) and a command to enter at market. Secondly, you need to identify a threshold of spread that would tell you that there is a news event and use this information to filter your signals. You could, for example, avoid entering for a chosen time after a sudden increase in spread. Thirdly you could work out what sort of move you would identify as being caused by news and translate this into a condition based on volatility on a sufficiently short term chart (might as well use the 1 minute). You could come up with the parameters by tinkering with the parameters of a volatility indicator and seeing what values it has reached at previous news events.

A bit of work to implement, but should serve the purpose.



I am entering instant order and already using spread to avoid big spreads.(maybe its useful for news events as you are saying).but yesterday there was a problem.evening it went deep suddenly and more than 30 pips in a one bar.
 
ubzen:
You can try using a volatility-indicator but those are usually slow for current bars. So my suggestion is that you limit the trades via Time. Ex> if(TimeCurrent()>Last_Trade_Time + TimeHour()*4){...Its OK now to place a new order;}


ı want to work with "High[0] and low[0]"s.

H_L=MathAbs(High[i]-Low[i])/Point;//Height of the bar

 
double height =(High[0]-Low[0])/Point;
double heightPrevious =(High[1]-Low[1])/Point;
if(height>250 || heightPrevious>250){Spread=0;}
if (SellValue>0  && Spread<25) {
 OpenSell=1;


}
 
if  (BuyValue>0  && Spread<25) {
 OpenBuy=1;
  

}
 
maybe it works...
 

ı want to work with "High[0] and low[0]"s.H_L=MathAbs(High[i]-Low[i])/Point;//Height of the bar. One drawback is that the EA might buy-again before the threshold is reached. If it's really news time you're afraid of then look into writing a News indicator and avoid News times altogether. Otherwise, Elroch and your own suggestions for volatility will work but nothing is perfect.

Added: just seen your second post. Bar[0] information don't work in backtester. Your only means of testing this would be with live-demo. The static value of 250 don't work much for me. But thats the beauty of defining thresholds, all men for themselves.

 
vieri3225:

I am entering instant order and already using spread to avoid big spreads.(maybe its useful for news events as you are saying).but yesterday there was a problem.evening it went deep suddenly and more than 30 pips in a one bar.

If you want to identify big moves within a 1 minute bar you need to go down to the tick level. While tick charts aren't available in MT4, the fact that the start function executes every tick means that you can quite easily write some code to check the range of the market in a shorter period ( X seconds, say). This is another way to identify sharp moves and avoid trades based on them. It's very simple to determine the range of the last X seconds:

int timeThreshold = 25; // time of interest in seconds
int loop = 0;
int bids[60];
int asks[60];
int times[60];

int start()
  {
  //use a loop of 60 ticks for populating the time and price arrays
  loop++; 
  loop=MathMod(loop,60);
  times[loop]=TimeCurrent();
  bids[loop]=Bid;
  asks[loop]=Ask;
  int recentMax=Ask;
  int recentMin=Bid;
  
  for(int i=0; i<60; i++)
  {
  if(TimeCurrent()-times[i] < timeThreshold)
    {
        recentMax=MathMax(recentMax, asks[i]);
        recentMin=MathMin(recentMin, bids[i]);
    }
  }
  double recentRange=recentMax-recentMin;

 // Then you can use recentRange in your conditions for trading

// ...

Backtesting this would be subject to significant errors due to the way the software simulates ticks based on the 1 minute bars.

 
Elroch:

If you want to identify big moves within a 1 minute bar you need to go down to the tick level. While tick charts aren't available in MT4, the fact that the start function executes every tick means that you can quite easily write some code to check the range of the market in a shorter period ( X seconds, say). This is another way to identify sharp moves and avoid trades based on them. It's very simple to determine the range of the last X seconds:


Backtesting this would be subject to significant errors due to the way the software simulates ticks based on the 1 minute bars.


hmm.ok I will think it.

but if I want to select the price(x1) which is 2 seconds before the current(x0).and compare it current.and !(x0-x1>30)

 

You can use the first part of my code to store recent ticks and their time stamps in two arrays, then replace the loop and the rest of the code with something else. You have to be a bit careful because there might be no ticks in the last 2 seconds. For example you could compare the most recent price that was at least 2 seconds ago with the code below. Since a loop of 60 ticks is obviously excessive if your looking at the last 2 seconds, I've reduced it for efficiency. 60 ticks is probably more than adequate for 2 seconds even though ticks can sometimes be quite close together. On reflection you would need a bigger loop than 60 for longer times like 25s in my earlier post, as the tick frequency can get quite high in a news spike. This code is not perfect for tiny times like 2s as the time stamps are only accurate to 1s, so you can't find which ticks are more recent when they are in the same second. You can fix this by taking account of the order of the data in the array, but I'm not going to bother.

int loop = 0;
int bids[60];
int asks[60];
int times[60];
int secondsBack=2; // looking for a price 2s ago or just over
double recentBid, recentAsk;

int start()
  {
  //uses a loop of 60 ticks for populating the time and price arrays
  loop++; 
  loop=MathMod(loop,60);
  times[loop]=TimeCurrent();
  bids[loop]=Bid;
  asks[loop]=Ask;
   
  int secondsBack=1000; //just a large number to get started 
  for(int i=0; i<60; i++)
  {
  timeNow=TimeCurrent(); 
  if((timeNow-times[i] < secondsBack) && (timeNow-times[i] >= 2))
    {   
        secondsBack=timeNow-times[i];
        recentBid=bids[i];
        recentAsk=asks[i];
    }
   }
//Now recentBid and recentAsk can be compared to the current bid and ask as you wish

// ...
   
//----
   return(0);
  }
 
Elroch:

If you want to identify big moves within a 1 minute bar you need to go down to the tick level. While tick charts aren't available in MT4, the fact that the start function executes every tick means that you can quite easily write some code to check the range of the market in a shorter period ( X seconds, say). This is another way to identify sharp moves and avoid trades based on them. It's very simple to determine the range of the last X seconds:


Backtesting this would be subject to significant errors due to the way the software simulates ticks based on the 1 minute bars.


Hello, im a newbie in coding for experts. im looking at doing the same thing as my e.a managing itself during high volatile areas, but jus for the sake of understanding your piece of code, what is the relevance of the code below . plus, if i would like to test this on a larger time frame, does it mean that i will have to change the loop to be larger than 60? im running  1 hour chart. thank you

loop=MathMod(loop,60);
Reason: