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.

Can IHistory.getLastTick return null?
 Post subject: Can IHistory.getLastTick return null? Post rating: 0   New post Posted: Tue 10 Jun, 2014, 21:13 
User avatar

User rating: 11
Joined: Tue 27 Mar, 2012, 17:47
Posts: 111
Location: GermanyGermany
Hello Support,

when the client is disconnected I get a strange behaviour with the mehtod getLastTick from IHistory, see here :
https://www.dukascopy.com/client/javadoc ... trument%29
I've implemented a try-catch-block for disconnects, catching a possible JForex exception.
But when testing this with a hard disconnect I do not see the log output from the catch block.
So the question is if getLastTick can also return null wihtout throwing when no connection is given?!

Thanks for help,
Juergen


 
 Post subject: Re: Can IHistory.getLastTick return null? Post rating: 0   New post Posted: Thu 12 Jun, 2014, 08:25 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Could you please provide an example strategy for the case that you described?


 
 Post subject: Re: Can IHistory.getLastTick return null? Post rating: 0   New post Posted: Fri 13 Jun, 2014, 07:54 
User avatar

User rating: 11
Joined: Tue 27 Mar, 2012, 17:47
Posts: 111
Location: GermanyGermany
Here is the code snippet:

public InstrumentPriceEngine(final JForexPlatform jforexPlatform,
                                 final Instrument instrument) {
        history = checkNotNull(jforexPlatform.getHistory());
        this.instrument = checkNotNull(instrument);

        lastValidTick = new EmptyTick();
    }

    public synchronized ITick getLastTick() {
        final ITick tempTick = getLastTickFromHistory();
        if (tempTick != null)
            lastValidTick = tempTick;
        return lastValidTick;
    }

    private ITick getLastTickFromHistory() {
        ITick tick = null;
        try {
            tick = history.getLastTick(instrument);
        } catch (JFException e) {
            logger.warn("Could not receive last tick for " + instrument.toString() + ". Error message: " + e.getMessage());
        }
        return tick;
    }


The InstrumentPriceEngine is the class constructor. The field lastValidTick is the tick container and the policy is that if an error occures within IHistory always the last known price is returned.
Therefore in getLastTick a temporary variable saves the result to the real call.
This is the revised code with the null check added. I tested it with internet disconnection and it works as expected. Without testing for null, the returned tick maybe null and is dangerous.
I was just wondering why in the documentation of IHistory.getLastTick it is not stated that the method can also return null?

Thanks,
Juergen


 
 Post subject: Re: Can IHistory.getLastTick return null? Post rating: 0   New post Posted: Fri 13 Jun, 2014, 12:22 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
FXjuxe wrote:
So the question is if getLastTick can also return null wihtout throwing when no connection is given?!
This in fact is what happens - the last tick is null if there is no connection, because this implies that the platform has not received the last tick. In other words - if there is no connection, the platform does not have the last tick, IHistory.getLastTick returning the "last tick received before the disconnect" would be wrong in many regards.

Regarding the example you provided, it would be much simpler for you to fetch the tick in the onTick method and store it in a field, instead of using the IHistory.getLastTick method.

FXjuxe wrote:
I was just wondering why in the documentation of IHistory.getLastTick it is not stated that the method can also return null?
We will update the javadocs.


 

Jump to:  

  © 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