Dukascopy Support Board
http://www.dukascopy.com/swiss/english/forex/jforex/forum/

history.getBar() returns incorrect values in some cases
http://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=85&t=56483
Page 1 of 1

Author:  NonikNone [ Fri 04 May, 2018, 12:48 ]
Post subject:  history.getBar() returns incorrect values in some cases

IBar previousBar = history.getBar(Instrument.EURUSD, Period.ONE_HOUR, OfferSide.BID, 1);  
writeToFile(previousBar.getClose(), time);


When the new bar is created, if the end of previous bar was volatile, previousBar.getClose() returns incorrect value.
And keeps returning same value in the following ticks, but if I restart the strategy, it is starting to log the correct value (the same with the chart).


You can reproduce the case by logging close and open prices for 1-2 days and compare logged close prices with historical data with following code.

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if (!instrument.equals(Instrument.EURUSD)) return;         
        if(period.equals(Period.ONE_HOUR)){         
            newBar = history.getBar(Instrument.EURUSD, Period.ONE_HOUR, OfferSide.BID, 0); //I also tried getBars method with bidBar.getTime parameter but same thing happens.
            previousBar = history.getBar(Instrument.EURUSD, Period.ONE_HOUR, OfferSide.BID, 1);         
            writeToLog(previousBar.getClose() + " " + newBar.getOpen() + "");
    }
 
    public void writeToLog(String text){
        try{
            FileWriter fileOpenWidthAppendMode = new FileWriter("C:/Users/Ugur/Documents/Visual Studio 2015/Projects/Unicorn/Unicorn/log_test.txt", true);
            BufferedWriter contentToWrite = new BufferedWriter(fileOpenWidthAppendMode);
            contentToWrite.append(text + "\r\n");
            contentToWrite.close();
        }catch(Exception e){
                 
        }       
    }

  Page 1 of 1