How to trade once every trend?

 

Hi i am newbie in programming. i want to develop simple EA that trade once every trend confirmation, but in my code the EA open the trade in every bar after trend confirmation.

if(trend[0] == -1) // -1 means sell
{
     obj_Trade.Sell(LotSize,_Symbol,bid,bid+StopLoss*_Point, bid-TakeProfit*_Point);
}

if(trend[0] == 1 ) //1 means buy
{
     obj_Trade.Buy(LotSize,_Symbol,ask,ask-StopLoss*_Point, ask+TakeProfit*_Point);
}

i try to make the condition to read the previous bar if it's buy/sell but the EA didn't open any trade

if(trend[0] == -1 && trend[1] == 1)
{
     obj_Trade.Sell(LotSize,_Symbol,bid,bid+StopLoss*_Point, bid-TakeProfit*_Point);
}

if(trend[0] == 1 && trend[1] == -1)
{
     obj_Trade.Buy(LotSize,_Symbol,ask,ask-StopLoss*_Point, ask+TakeProfit*_Point);
}

Can anyone teach me how to make it trade once every trend changing?
thank you

 

You can use a bool variable that you set to TRUE after you place a trend, and you reset to FALSE once trend changes.

In this case you will trade only the first signal you have after a trand change.

 
Fabio Cavalloni #:

You can use a bool variable that you set to TRUE after you place a trend, and you reset to FALSE once trend changes.

In this case you will trade only the first signal you have after a trand change.

What will happen to that variable after restarting the terminal? (restarting the terminal while the advisor is running)

 
Vladislav Boyko #:

What will happen to that variable after restarting the terminal? (restarting the terminal while the advisor is running)

The state will be lost, in this case It's surely better to use Global Variables that are not reainitized when EA restarts...

 
Fabio Cavalloni #:

The state will be lost, in this case It's surely better to use Global Variables that are not reainitized when EA restarts...

In my opinion, it is much easier and more reliable to simply find the beginning of a trend. It will be enough to look for the beginning of a trend only when a new bar appears (provided that the current bar is ignored).

 
Mikhael Kurniawan:

i try to make the condition to read the previous bar if it's buy/sell but the EA didn't open any trade

if(trend[0] == -1 && trend[1] == 1)
{
     obj_Trade.Sell(LotSize,_Symbol,bid,bid+StopLoss*_Point, bid-TakeProfit*_Point);
}

if(trend[0] == 1 && trend[1] == -1)
{
     obj_Trade.Buy(LotSize,_Symbol,ask,ask-StopLoss*_Point, ask+TakeProfit*_Point);
}

I think these are thoughts in the right direction. But only if trend[0] is not the current bar. The signal will flicker on the current bar.

 
Very bad approaches. The easiest way is to check if there are any trades in progress. This is the simplest and takes into account orders stopped on the server.

 
Gerard William G J B M Dinh Sy #:
Very bad approaches. The easiest way is to check if there are any trades in progress. This is the simplest and takes into account orders stopped on the server.

What to do if you first launched the advisor in the middle of a trend (there are no trades)?

What if your EA is a scalper and uses very short SL/TP (due to which positions live no more than a couple of minutes)? Even if you analyze the trading history, how will it help you if you don't know when the trend started?

 
Gerard William G J B M Dinh Sy #:
Very bad approaches. The easiest way is to check if there are any trades in progress. This is the simplest and takes into account orders stopped on the server.

if(trend[0] == -1 && trend[1] == 1 && PositionsTotal()==0)
{
     obj_Trade.Sell(LotSize,_Symbol,bid,bid+StopLoss*_Point, bid-TakeProfit*_Point);
}

if(trend[0] == 1 && trend[1] == -1 && PositionsTotal()==0)
{
     obj_Trade.Buy(LotSize,_Symbol,ask,ask-StopLoss*_Point, ask+TakeProfit*_Point);
}

i added positionsTotal() to the code but when the trade close it will open trade again in the same bar. i really confuse right now

 
It is not advisable to try to solve X problems in one solution.

Taking a trade only at the beginning of a trend are two complications that must be managed separately.

the birth of a trend AND a trade for that trend
 
Mikhael Kurniawan #:
if(trend[0] == -1 && trend[1] == 1 && PositionsTotal()==0) {      obj_Trade.Sell(LotSize,_Symbol,bid,bid+StopLoss*_Point, bid-TakeProfit*_Point); } if(trend[0] == 1 && trend[1] == -1 && PositionsTotal()==0) {      obj_Trade.Buy(LotSize,_Symbol,ask,ask-StopLoss*_Point, ask+TakeProfit*_Point); }

maybe like this :

  datetime now=iTime(_Symbol,_Period,0);
  static datetime barstamp=0;
  if(now>barstamp){
      barstamp=now;
      if(trend[1] == -1 && trend[2] == 1 && PositionsTotal()==0)
      {
           obj_Trade.Sell(LotSize,_Symbol,bid,bid+StopLoss*_Point, bid-TakeProfit*_Point);
      }
      else if(trend[1] == 1 && trend[2] == -1 && PositionsTotal()==0)
      {
           obj_Trade.Buy(LotSize,_Symbol,ask,ask-StopLoss*_Point, ask+TakeProfit*_Point);
      }
  }