Topic: Question about Bar Closing indicator

I've come accross some strategies with FSB that open a trade at the end of the bar.

Looking back at historical prices, you can just find the closing price of a bar. But how does it work in live trading?

Does the indicator somehow tell you when the bar is about to close during live trading?

Is there an mt4 indicator for this? I'd like to experiment with scripting some EAs to trade the strategies that FSB uncovers, but I have no idea how to script the EA to enter at the end of the bar without cheating by looking ahead at the closing price.

Thanks,
Ed

Re: Question about Bar Closing indicator

No answer yet?

This seems like an important point. I don't see how you could reliably enter at bar closing. The problem here is that you cannot know what the last tick of a bar is. Depending on liquidity, the last tick might come milliseconds or even minutes before the end of the bar.

Re: Question about Bar Closing indicator

Bar Closing will enter the position up to 5 secs before the bar closes. It assumes the price at that time is the price that the bar will actually close at and calculate the indicators accordingly. Unless something dramatic happens during the last few seconds this price will be very close to the actual bar closing price.

Since Forex is 24 hours there shouldn't be much difference between  closing and opening prices except at the weekend.

Use of Bar Closing might be a good way to enter a trade over the weekend if you are that way inclined.

Re: Question about Bar Closing indicator

FST enters 3 seconds before bar closing. This period is adjustable from Trade Settings window.

Re: Question about Bar Closing indicator

Ah, I see.

I figured it was a timing issue and it tried to enter close to the end time for the bar.

Anyone know how to script this functionality in mt4? is there an mt4 indicator for this?

Re: Question about Bar Closing indicator

I found some old source code of an expert where I tested bar opening and bar closing in MQL4

bool bIsFirstTick;// It is used to catch the Bar Opening.
datetime dtCurrentBarOpeningTime;

int init()
{
   bIsFirstTick = false;
   dtCurrentBarOpeningTime = Time[0];
   return(0);
}

int start()
{
   // Is this the first tick for the bar
   bIsFirstTick = (dtCurrentBarOpeningTime != Time[0]);
   if(bIsFirstTick)
   {
      dtCurrentBarOpeningTime = Time[0];
   }

...
...



///
/// Exit at a Bar Closing price (almost).
///
/// MetaTrader does not provide an onBarClose event so we are not able to close a position
/// exactly at Bar Closing. We workaround the problem by closing the position within a time span
/// near to the bar closing time. In the cases when there is no any ticks within this period,
/// we close the position att he next bar opening price.
///
int ClosePositionsAtBarClosing(bool bCloseLong, bool bCloseShort, datetime dtClosingTimeSpan)
{
   int  iOrders = OrdersTotal();
   bool bIsOpen = false;

   for(int iOrder = 0; iOrder < iOrders; iOrder++)
   {
      OrderSelect(iOrder, SELECT_BY_POS, MODE_TRADES);
      
      if((OrderType() == OP_BUY || OrderType() == OP_SELL) && OrderSymbol() == Symbol())
      {  // There is an open position for this symbol.

         datetime dtOpeningTime     = Time[0] - TimeSeconds(Time[0]); // The opening time of current bar
         datetime dtClosingTime     = dtOpeningTime + Period() * 60;  // The closing time of current bars
         datetime dtCurrentTickTime = TimeCurrent() ;                 // The time of current tick
         
         if(dtCurrentTickTime > dtClosingTime - dtClosingTimeSpan ||  bIsFirstTick)
         {  // The current tick is within the closing time span or this is the first tick of the bar.

            // Code

         }
       }
    }
}

The full code is attached.
Use at your own risk. Not supported!

Post's attachments

FSB__Bar_Opening_Bar_Closing_1.mq4 15.36 kb, 13 downloads since 2012-04-25 

You don't have the permssions to download the attachments of this post.

Re: Question about Bar Closing indicator

Popov,

Thank you so much. That code is very helpful.