1 Trade per Bar

 
I am working on an EA and one problem I have come across is that sometimes, a trade will open, and quickly hit it's limit and I will get the profit. However, new trades will open in the same bar and I will lose money on those. How can I limit one trade per bar posted on the chart? For example, if a trade closes in the current bar, I don't want another trade to be able to open until the new bar is posted. Can someone give me that language?
 
int BarsCount = 0;
 
int start()
{
 
  if (Bars > BarsCount)
  {
    //your code to be executed only once per bar goes here
     
    BarsCount = Bars;
  }
 
  return(0);
}
Define a global variable BarsCount. The integer Bars stores the number of bars in the current chart. Once a new bar is added on the chart, your code is executed but then you set BarsCount = Bars, so it will be executed again only when a new bar appears. BarsCount is initially set to 0, so your code is executed the very first time you load the indicator.

Jan
 
@ janklimo: And if I would evaluate the situation to enter long or short only at the end of the bar? I mean the close of the bar.
 
NTrader:
@ janklimo: And if I would evaluate the situation to enter long or short only at the end of the bar? I mean the close of the bar.
The open of the new bar comes only one tick after the close of the old bar, so using the new bar to do the evaluation is the most precise, in my opinion. But if you really want to do the operation just before the bar close, check whether the current time is greater of equal to the next bar open time minus a couple of seconds.
 
I'm sorry if I come off as an idiot, but what does this code mean? I don't see how this could limit the EA to only opening 1 trade per bar. How does that work? Also, what does BarsCount=Bars mean? Since you are saying Barscount=Bars, how could Bars > BarsCount work? Don't you say later that they are equal?
 
Nevermind.... :)
 

golden188:

WHEN is the line BarsCount = Bars executed?

WHEN does the expression Bars > BarsCount evaluate as True?

 

First off, I should say that I am basing my EA off of the MACD Sample EA that was available (and still may be) on this site. I guess I just don't understand where it should go in this code:

extern double TakeProfit = 50;
extern double Lots = 0.1;
extern double TrailingStop = 30;
extern double MACDOpenLevel=3; 
extern double MACDCloseLevel=2;
extern double MATrendPeriod=26;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+ 
int start()
{
double MacdCurrent, MacdPrevious, SignalCurrent;
double SignalPrevious, MaCurrent, MaPrevious; 
int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external 
// variables (Lots, StopLoss, TakeProfit, 
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars 
if(Bars<100)
{
Print("bars less than 100");
return(0); 
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10"); 
return(0); // check TakeProfit
}
// to simplify the coding and speed up access
// data are put into internal variables 
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1); 
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); 
MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);

total=OrdersTotal();
if(total<1) 
{
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots)) 
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0); 
}
// check for long position (BUY) possibility 
if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&
MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious) 
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
if(ticket>0) 
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError()); 
return(0); 
}

what i have assumed is that it goes where the language "if Bars<100" currently is. However, I wasn't sure... phy, i appreciate your patience.

 
please am having thesame issue , my ea opens trade on every candle  once its above and belw 50ma please can yu guys help me with the programming language 
 
peter samuel:
please am having thesame issue , my ea opens trade on every candle  once its above and belw 50ma please can yu guys help me with the programming language 

Help you with what?

Show your code and describe precisely what your problem is and somebody may be able to help you.

 
Jan Klimo: Define a global variable BarsCount.

For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          New candle - MQL4 programming forum

Reason: