Dukascopy
 
 
Wiki JStore Search Login

JFOREX-1748: buy/sell API problem
 Post subject: JFOREX-1748: buy/sell API problem Post rating: 0   New post Posted: Tue 11 May, 2010, 18:10 

User rating: 0
Joined: Mon 19 Apr, 2010, 19:45
Posts: 3
This seems to be a bug, but I'm new to the API so maybe it's something I'm not doing correctly.

If you run the class below once with the argument "buy" it will enter in 1 market order. (No orders should already exist for this account.) Then halt the program and run it again with the argument "sell". It will then throw a null pointer exception.

Regards,
Mark

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;
import com.dukascopy.api.system.ClientFactory;
import com.dukascopy.api.system.IClient;
import com.dukascopy.api.system.ISystemListener;

public class BuySellTest {

    private static final Logger _log = LoggerFactory.getLogger(BuySellTest.class);

    private static String jnlpUrl = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
    private static String userName = "";
    private static String password = "";

    public BuySellTest(String[] args) throws Exception {
        if (args.length != 1 || (!args[0].equals("buy") && !args[0].equals("sell"))) {
            System.out.println("Usage: BuySellTest [buy|sell]");
            System.exit(1);
        }

        String bs = args[0];

        final IClient client = ClientFactory.getDefaultInstance();

        client.setSystemListener(new ISystemListener() {
            private int lightReconnects = 3;

            public void onStart(long processId) {
                _log.info("Strategy started: " + processId);
            }

            public void onStop(long processId) {
                _log.info("Strategy stopped: " + processId);
                if (client.getStartedStrategies().size() == 0) {
                    System.exit(0);
                }
            }

            public void onConnect() {
                _log.info("Connected");
                lightReconnects = 3;
            }

            public void onDisconnect() {
                _log.warn("Disconnected");
                if (lightReconnects > 0) {
                    client.reconnect();
                    --lightReconnects;
                } else {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {}

                    try {
                        client.connect(jnlpUrl, userName, password);
                    } catch (Exception e) {
                        _log.error(e.getMessage(), e);
                    }
                }
            }
        });

        _log.info("Connecting...");
        client.connect(jnlpUrl, userName, password);

        int i = 60;
        while (i > 0 && !client.isConnected()) {
            Thread.sleep(1000);
            i--;
        }

        if (!client.isConnected()) {
            _log.error("Failed to connect Dukascopy servers");
            System.exit(1);
        }

        Set<Instrument> instruments = new HashSet<Instrument>();
        instruments.add(Instrument.EURUSD);

        _log.info("Subscribing instruments...");
        client.setSubscribedInstruments(instruments);

        _log.info("Starting strategy");
        if (bs.equals("buy")) {
            client.startStrategy(this.new BuyStrat());
        } else {
            client.startStrategy(new SellStrat());
        }

        _log.info("running...");
    }

    public static void main(String[] args) throws Exception {
        new BuySellTest(args);
    }

    public class BuyStrat implements IStrategy {

        private IEngine engine;
        private int cnt;

        public void onAccount(IAccount account) throws JFException {}

        public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar)
                throws JFException {}

        public void onMessage(IMessage message) throws JFException {}

        public void onStart(IContext context) throws JFException {
            engine = context.getEngine();
        }

        public void onStop() throws JFException {}

        public void onTick(Instrument instrument, ITick tick) throws JFException {
            List<IOrder> orders = engine.getOrders();
            cnt = orders.size();
            if (cnt > 0) return;

            String label = BuySellTest.class.getSimpleName() + System.currentTimeMillis();
            engine.submitOrder(label, instrument, IEngine.OrderCommand.SELL, 0.001, 0, 0,
                    tick.getAsk() + instrument.getPipValue() * 100, tick.getAsk()
                            - instrument.getPipValue() * 100);
        }
    }

    public class SellStrat implements IStrategy {

        private IEngine engine;

        public void onAccount(IAccount account) throws JFException {}

        public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar)
                throws JFException {}

        public void onMessage(IMessage message) throws JFException {}

        public void onStart(IContext context) throws JFException {}

        public void onStop() throws JFException {}

        public void onTick(Instrument instrument, ITick tick) throws JFException {
            List<IOrder> orders = engine.getOrders();
            for (IOrder order : orders) {
                order.close();
            }
        }
    }
}



 
 Post subject: Re: buy/sell API problem Post rating: 0   New post Posted: Tue 11 May, 2010, 22:05 

User rating: 0
Joined: Mon 19 Apr, 2010, 19:45
Posts: 3
Sorry, the code in my previous post had a bug and hid the real error. I can't edit previous post so I'm posting the corrected code below. I've added the exception as well:.
A few other important things I didn't mention:

1) If you have the JForex GUI client running while this test is running, everything works fine.
2) This problem only seems to occur with OPENED orders and not FILLED orders.
3) If you do not exit the jvm between opening and closing an order, it all works fine.

java.lang.NullPointerException
   at com.dukascopy.api.impl.connect.j.b(Unknown Source)
   at com.dukascopy.api.impl.connect.PlatformOrderImpl.close(Unknown Source)
   at com.dukascopy.api.impl.connect.PlatformOrderImpl.close(Unknown Source)
   at com.dukascopy.api.impl.connect.PlatformOrderImpl.close(Unknown Source)
   at com.dukascopy.api.impl.connect.PlatformOrderImpl.close(Unknown Source)
   at BuySellTest$SellStrat.onTick(BuySellTest.java:166)
   at com.dukascopy.api.impl.execution.t.call(Unknown Source)
   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
   at com.dukascopy.api.impl.execution.l$a.a(Unknown Source)
   at com.dukascopy.api.impl.execution.l$a.run(Unknown Source)


import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;
import com.dukascopy.api.system.ClientFactory;
import com.dukascopy.api.system.IClient;
import com.dukascopy.api.system.ISystemListener;

public class BuySellTest {

    private static final Logger _log = LoggerFactory.getLogger(BuySellTest.class);

    private static String jnlpUrl = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
    private static String userName = "";
    private static String password = "";

    public BuySellTest(String[] args) throws Exception {
        if (args.length != 1 || (!args[0].equals("buy") && !args[0].equals("sell"))) {
            System.out.println("Usage: BuySellTest [buy|sell]");
            System.exit(1);
        }

        String bs = args[0];

        final IClient client = ClientFactory.getDefaultInstance();

        client.setSystemListener(new ISystemListener() {
            private int lightReconnects = 3;

            public void onStart(long processId) {
                _log.info("Strategy started: " + processId);
            }

            public void onStop(long processId) {
                _log.info("Strategy stopped: " + processId);
                if (client.getStartedStrategies().size() == 0) {
                    System.exit(0);
                }
            }

            public void onConnect() {
                _log.info("Connected");
                lightReconnects = 3;
            }

            public void onDisconnect() {
                _log.warn("Disconnected");
                if (lightReconnects > 0) {
                    client.reconnect();
                    --lightReconnects;
                } else {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {}

                    try {
                        client.connect(jnlpUrl, userName, password);
                    } catch (Exception e) {
                        _log.error(e.getMessage(), e);
                    }
                }
            }
        });

        _log.info("Connecting...");
        client.connect(jnlpUrl, userName, password);

        int i = 60;
        while (i > 0 && !client.isConnected()) {
            Thread.sleep(1000);
            i--;
        }

        if (!client.isConnected()) {
            _log.error("Failed to connect Dukascopy servers");
            System.exit(1);
        }

        Set<Instrument> instruments = new HashSet<Instrument>();
        instruments.add(Instrument.GBPUSD);

        _log.info("Subscribing instruments...");
        client.setSubscribedInstruments(instruments);

        _log.info("Starting strategy");
        if (bs.equals("buy")) {
            client.startStrategy(this.new BuyStrat());
        } else {
            client.startStrategy(new SellStrat());
        }

        _log.info("running...");
    }

    public static void main(String[] args) throws Exception {
        new BuySellTest(args);
    }

    public class BuyStrat implements IStrategy {

        private IEngine engine;

        public void onAccount(IAccount account) throws JFException {}

        public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar)
                throws JFException {}

        public void onMessage(IMessage message) throws JFException {}

        public void onStart(IContext context) throws JFException {
            engine = context.getEngine();
        }

        public void onStop() throws JFException {}

        public void onTick(Instrument instrument, ITick tick) throws JFException {
            List<IOrder> orders = engine.getOrders();
            if (orders.size() > 0) return;

            double pip = instrument.getPipValue();
            double bid = tick.getBid();
            double ask = tick.getAsk();

            String label = BuySellTest.class.getSimpleName() + System.currentTimeMillis();
            engine.submitOrder(label, instrument, IEngine.OrderCommand.SELLSTOP, 0.001, bid
                    - (pip * 100), 0, ask, ask - (pip * 200));
            _log.info("order submitted");
        }
    }

    public class SellStrat implements IStrategy {

        private IEngine engine;

        public void onAccount(IAccount account) throws JFException {}

        public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar)
                throws JFException {}

        public void onMessage(IMessage message) throws JFException {}

        public void onStart(IContext context) throws JFException {
            engine = context.getEngine();
        }

        public void onStop() throws JFException {}

        public void onTick(Instrument instrument, ITick tick) throws JFException {
            List<IOrder> orders = engine.getOrders();
            for (IOrder order : orders) {
                _log.info("closing order: " + order.getId());
                order.close();
            }
        }
    }
}


Mark


 
 Post subject: Re: JFOREX-1748: buy/sell API problem Post rating: 0   New post Posted: Wed 12 May, 2010, 09:37 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Thanks for the good test. This will be fixed in next version


 

Jump to:  

  © 1998-2024 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