1 Trade per Bar

To add comments, please log in or register
Paul Goldsmith
84
Paul Goldsmith  
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?
Jan Klimo
116
Jan Klimo  
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
Ntrader
30
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.
Jan Klimo
116
Jan Klimo  
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.
Paul Goldsmith
84
Paul Goldsmith  
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?
Erman Ardianto
1144
Erman Ardianto  
Nevermind.... :)
Ray
2653
Ray  

golden188:

WHEN is the line BarsCount = Bars executed?

WHEN does the expression Bars > BarsCount evaluate as True?

Paul Goldsmith
84
Paul Goldsmith  

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.

To add comments, please log in or register