MQL4 - automated forex trading   /  

Forum

1 Trade per Bar

Back to topics list To post a new topic, please log in or register

avatar
25
golden188 2007.09.25 00:50 
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?
article

Interview with Jimmy Tirtawangsa (fireflies)

Gorez does a good job by predicting that GBPJPY will go south for many weeks.


avatar
123
janklimo 2007.09.25 02:26 
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

avatar
1
NTrader 2007.09.29 17:03 
@ 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.

avatar
123
janklimo 2007.09.29 18:41 
NTrader wrote:
@ 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.

avatar
25
golden188 2008.02.26 22:04 
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?

avatar
279
devilian1899 2008.02.26 22:16 
Nevermind.... :)

avatar
2462
phy 2008.02.26 22:26 

golden188:

WHEN is the line BarsCount = Bars executed?

WHEN does the expression Bars > BarsCount evaluate as True?



avatar
25
golden188 2008.02.28 04:02 

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.

Back to topics list  

To add comments, please log in or register