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.

JFException when calling submitOrder() via own Button
 Post subject: JFException when calling submitOrder() via own Button Post rating: 0   Post Posted: Wed 18 Apr, 2012, 13:53 
User avatar

User rating: 1
Joined: Thu 15 Mar, 2012, 16:30
Posts: 20
Hello,
i want to create my own BUY/SELL Button in a BottomTab but when i click my Button i get an Exception @ submitOrder() Function.
If i call submitOrder() in the onStart() Function it works just fine.

Here is a simple Strategy that shows the Problem.


Attachments:
TickCountingComponent.java [2 KiB]
Downloaded 306 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.
 
 Post subject: Re: JFException when calling submitOrder() via own Button Post rating: 0   Post Posted: Fri 20 Apr, 2012, 14:49 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Orders must be placed from the context of functions of IStrategy interface or functions that are called by them.
Orders cannot be placed from separate threads including user interface elements.

package jforex.guitests;

import javax.swing.*;

import com.dukascopy.api.*;
import java.awt.Container;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TickCountingComponent implements IStrategy {
    private IUserInterface userInterface;
    private IContext context;

    private final String tabName = "TickCounter";
    boolean buy = false;

    public void onStart(IContext context) throws JFException {
        this.userInterface = context.getUserInterface();
        this.context = context;

        JPanel tab = userInterface.getBottomTab(tabName);
       
        addComponentsToPane(tab);
       
        //Buy();    // works fine
    }

    private void addComponentsToPane(Container pane) {

        pane.setLayout(null);

        final JButton btn = new JButton("BUY");

        pane.add(btn);

        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                buy = true;
                //Buy();    // throws Exception @ engine.submitOrder();
            }
        });

        //customized size and placement of the button
        Insets insets = pane.getInsets();
        btn.setBounds(25 + insets.left, 5 + insets.top, 100, 25);
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if(buy) {
            Buy();
            buy = false;
        }
    }

    public void onStop() throws JFException {
        userInterface.removeBottomTab(tabName);    //remove tab
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

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

    private void Buy()
    {
        try{ context.getEngine().submitOrder("longEntry", Instrument.EURUSD, IEngine.OrderCommand.BUY, 0.001);}
        catch (JFException e)
        {
            context.getConsole().getOut().println("   -> JFException @ engine.submitOrder();");

            e.printStackTrace();
        }
    }
}


Attachments:
TickCountingComponent.java [2.12 KiB]
Downloaded 291 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.
 
 Post subject: Re: JFException when calling submitOrder() via own Button Post rating: 0   Post Posted: Sun 22 Apr, 2012, 00:46 
User avatar

User rating: 1
Joined: Thu 15 Mar, 2012, 16:30
Posts: 20
As i feared. I am now using a boolean Flag.
Is there any method to send a simulated Tick to JForex so that the onTick() Method gets called immediately from the System?
Otherwiese i have to wait until next Tick and that could result to a different Price...

Thanks for your Help so far.


 
 Post subject: Re: JFException when calling submitOrder() via own Button Post rating: 0   Post Posted: Tue 24 Apr, 2012, 10:20 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
package jforex.guitests;

import javax.swing.*;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import java.awt.Container;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;

public class TickCountingComponent implements IStrategy {

    private IUserInterface userInterface;
    private IContext context;
    private final String tabName = "TickCounter";
    boolean buy = false;
    private IEngine engine;
    private IHistory history;

    public void onStart(IContext context) throws JFException {
        this.userInterface = context.getUserInterface();
        this.context = context;
        this.engine = engine;
        this.history = context.getHistory();

        JPanel tab = userInterface.getBottomTab(tabName);

        addComponentsToPane(tab);
    }

    private void addComponentsToPane(Container pane) {

        pane.setLayout(null);

        final JButton btn = new JButton("BUY");

        pane.add(btn);

        btn.addActionListener(new BuyAction(context, Instrument.EURUSD));

        //customized size and placement of the button
        Insets insets = pane.getInsets();
        btn.setBounds(25 + insets.left, 5 + insets.top, 100, 25);
    }
   
    class BuyAction implements ActionListener, Callable<Object> {
       
        final IContext context;
        Instrument instrument;
       
        BuyAction(IContext context, Instrument instrument) {
            this.context = context;
            this.instrument = instrument;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                context.executeTask(this);
            } catch (Exception ex) {
                context.getConsole().getErr().println(Thread.currentThread().getName() + " " + ex);
            }
        }
         
        @Override
        public Object call() throws Exception {
            return context.getEngine().submitOrder("longEntry", instrument, IEngine.OrderCommand.BUY, 0.001);
        }
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }

    public void onStop() throws JFException {
        userInterface.removeBottomTab(tabName);    //remove tab
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

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


}


Also see: https://www.dukascopy.com/wiki/#Threading


Attachments:
TickCountingComponent.java [2.49 KiB]
Downloaded 295 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:  

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