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.

Historical tester shows error
 Post subject: Historical tester shows error Post rating: 0   New post Posted: Mon 04 Mar, 2013, 12:40 
User avatar

User rating: 0
Joined: Mon 02 Apr, 2012, 09:22
Posts: 12
Location: United Kingdom, London
To Dukascopy Support,

I have an automated strategy based on momentum. Here is the main code.

public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (instrument != this.instrument) {
            return;
        }

        if (!isActive(order)) {
            order = null;
        } else if(order.equals(IOrder.State.CREATED)) {
            return;
        }           
       
        double[] mom1 = indicators.mom(instrument, selectedPeriod1, offerSide, appliedPrice, momPeriod, filter, 1, tick.getTime(), 0);       
       
        if (order.isLong() && (mom1[0] < -0.0005)) {
            closeOrder(order);     
        }
       
        if (!order.isLong() && (mom1[0] > 0.0005)) {
            closeOrder(order); 
        }
       
       
        if (mom1[0] > 0.0005) {
            if (order == null || !order.isLong()) {
            closeOrder(order);
            order = submitOrder(OrderCommand.BUY, instrument);
            }
        }
       
        if  (mom1[0] < -0.0005) {
            if (order == null || order.isLong()) {
            closeOrder(order);
            order = submitOrder(OrderCommand.SELL, instrument);
            } 
         }   
      }


When I test this strategy on the historical tester, it shows the following error

Quote:
Exception java.lang.NullPointerException @ jforex.Final_Test_6.onTick(Final_Test_6.java:102)


I understand that there is an error on line 102. This is code on line 102.

if (order.isLong() && (mom1[0] < -0.0005)) {
            closeOrder(order);     
        }


But I don't understand what is wrong. Kindly tell me what is the mistake.

Regards,

Terry.


 
 Post subject: Re: Historical tester shows error Post rating: 2   New post Posted: Mon 04 Mar, 2013, 15:24 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Probably your order variable is null. So accessing its isLong() throws the exception.

Also, this might be problematic:
Quote:
if (order == null || !order.isLong()) {
closeOrder(order);
order = submitOrder(OrderCommand.BUY, instrument);
}


As closeOrder(order) will be called when order=null.
Maybe that is not an issue for the API to call closeOrder(null), and it won`t throw exception, but this is bad coding.


 

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