Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Determining if bar in calculate method is live bar
 Post subject: Determining if bar in calculate method is live bar Post rating: 0   New post Posted: Tue 30 Aug, 2011, 09:04 

User rating: 1
Joined: Tue 30 Aug, 2011, 08:54
Posts: 19
Location: AustraliaAustralia
I use the below code in the calculate() method of a custom indicator to check if we have been asked to calculate a value for the currently forming bar.

The indicator does not display any values for the currently forming bar, so..
- it returns no values if startIndex is the currently forming bar; and
- it resets the value of endIndex to the 2nd last bar (ie. the bar before the currently forming bar) if endIndex refers to the currently forming bar.

Unfortunately I rely on the IIndicatorContect.getHistory() method to do this. This is not available in production, only in demo.

How can I re-write this code to exclude calculating values for the currently forming candle in production?

..
        this.barHistory = indiContext.getHistory();
        ..
       // avoid processing in progress candle
        int futureCandlesAvoided = 0;
        try
        {
           long inProgressBarTime = barHistory.getStartTimeOfCurrentBar(this.instrument, this.period);
          if (inputBars[startIndex].getTime() >= inProgressBarTime)
             return new IndicatorResult(0, 0); // do not calculate in progress bar
          if (inputBars[startIndex].getTime() < inProgressBarTime && inputBars[endIndex].getTime() >= inProgressBarTime)
          {
             // move endIndex so that we do not calculate in progress bar
                  int count;
             for (count = startIndex; count <= endIndex; count++)
             {
                if (inputBars[count].getTime() == inProgressBarTime)
                {
                   break;
                }
             }
             futureCandlesAvoided = endIndex - (count-1);
            endIndex = count-1;
          }
        }
        catch (Exception e)
        {
           ...
        }
        ..
        indiRes = new IndicatorResult (startIndex, endIndex-startIndex + 1 + futureCandlesAvoided);
        ..
        return indiRes;


 
 Post subject: Re: Determining if bar in calculate method is live bar Post rating: 0   New post Posted: Wed 31 Aug, 2011, 09:07 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The only way is to compute inProgressBarTime is to use System.currentTimeMillis() and period interval of the bar.

    public IndicatorResult calculate(int startIndex, int endIndex) {

        // calculate the number of values that are to be calculated
        int length = endIndex - startIndex - getLookback();
       
        // the last output value should end at inProgressBarTime
        long inProgressBarTime = System.currentTimeMillis() - period.getInterval();
        int i, j;
       
        // find correct endIndex that is before inProgressBarTime
        while (endIndex >= startIndex){           
            if(inputs[0][endIndex].getTime() < inProgressBarTime) {
                break;
            }
            endIndex--;
        }
        // find startIndex so that required number of values is produced and the output ends at inProgressBarTime
        startIndex = Math.max(startIndex, endIndex - length);
       
        for(int k = 0; k <= endIndex - startIndex; k++){
            outputs[0][k] = 1;
        }

        return new IndicatorResult(startIndex, endIndex - startIndex + 1);
    }


Attachments:
Indicator.java [3.69 KiB]
Downloaded 314 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 

Jump to:  

cron
  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com