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.

How to prevent; Unhandled exception type JFException
 Post subject: How to prevent; Unhandled exception type JFException Post rating: 0   New post Posted: Thu 26 Aug, 2010, 08:44 

User rating: 0
Joined: Fri 20 Aug, 2010, 14:09
Posts: 20
How to prevent the:

Unhandled exception type JFException

When I add a throws JFException it gives me:

Exception JFException is not compatible with throws clause in ActionListener.actionPerformed(ActionEvent)


 
 Post subject: Re: How to prevent; Unhandled exception type JFException Post rating: 0   New post Posted: Thu 26 Aug, 2010, 09:14 

User rating: 0
Joined: Tue 24 Aug, 2010, 20:50
Posts: 15
you'll have to put the code inside actionPerformed in the following construct:
try{
  //here goes your normal code
}catch(JFException e){
  e.printStackTrace()
  //if there is an error thrown in your code, print it
}


 
 Post subject: Re: How to prevent; Unhandled exception type JFException Post rating: 0   New post Posted: Thu 26 Aug, 2010, 11:47 

User rating: 0
Joined: Fri 20 Aug, 2010, 14:09
Posts: 20
Yeah. I tried that already but then I get this exception:

com.dukascopy.api.JFException: Incorrect thread


 
 Post subject: Re: How to prevent; Unhandled exception type JFException Post rating: 0   New post Posted: Thu 26 Aug, 2010, 11:54 

User rating: 0
Joined: Tue 24 Aug, 2010, 20:50
Posts: 15
do you get it on runtime or compile time?
if it's runtime, the try{}catch works and catches an exception...
the problem isn't in handling this exception rather than the cause of the exception.
however, to analyze that, it would be good if you could provide a little more information (e.g. some junks of code)


 
 Post subject: Re: How to prevent; Unhandled exception type JFException Post rating: 0   New post Posted: Thu 26 Aug, 2010, 15:13 

User rating: 0
Joined: Fri 20 Aug, 2010, 14:09
Posts: 20
Sure. here it is:

package jforex;

import java.util.*;

import com.dukascopy.api.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ActionTest implements IStrategy, ActionListener {
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IContext context;
   private IIndicators indicators;
   private IUserInterface userInterface;
   
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.history = context.getHistory();
      this.context = context;
      this.indicators = context.getIndicators();
      this.userInterface = context.getUserInterface();
       
       
        JPanel p = this.userInterface.getBottomTab("button panel");
        JButton buy = new JButton("buy");
        buy.addActionListener(this);
        p.add(buy);
   }
   
public void sendOrder() throws JFException  {
            Random randomGenerator = new Random();
            int randomInt = randomGenerator.nextInt(10000);
            String label = String.format("%s", Instrument.EURUSD.toString(), randomInt);
            this.engine.submitOrder(label, Instrument.EURUSD, IEngine.OrderCommand.BUY,  1000);
           
                 
}

public void actionPerformed(ActionEvent evt)  {
            this.console.getOut().println("Button pressed");

            try {
                sendOrder();
            } catch (JFException e) {
                e.printStackTrace();
                this.console.getOut().println(String.format("Error sending order: %s", e));
            }
           
        }

   public void onAccount(IAccount account) throws JFException {
   }

   public void onMessage(IMessage message) throws JFException {
   }

   public void onStop() throws JFException {
   }

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


 
 Post subject: Re: How to prevent; Unhandled exception type JFException Post rating: 0   New post Posted: Thu 26 Aug, 2010, 15:57 

User rating: 0
Joined: Tue 24 Aug, 2010, 20:50
Posts: 15
ok, so when you press the button, a new thread is started executing actionPerformed().
it seems, jforex isn't threadsafe, so having multiple threads access common elements (e.g. this.engine) has been "disabled" through throwing an exception...
there is a method, how you can get the functionality you want without adding much code. however, it's a bit of a hack...
package jforex;

import java.util.*;

import com.dukascopy.api.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ActionTest implements IStrategy, ActionListener {
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IContext context;
   private IIndicators indicators;
   private IUserInterface userInterface;

   private boolean buttonPressed=false;
   
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.history = context.getHistory();
      this.context = context;
      this.indicators = context.getIndicators();
      this.userInterface = context.getUserInterface();
       
       
        JPanel p = this.userInterface.getBottomTab("button panel");
        JButton buy = new JButton("buy");
        buy.addActionListener(this);
        p.add(buy);
   }
   
public void sendOrder() throws JFException  {
            Random randomGenerator = new Random();
            int randomInt = randomGenerator.nextInt(10000);
            String label = String.format("%s", Instrument.EURUSD.toString(), randomInt);
            this.engine.submitOrder(label, Instrument.EURUSD, IEngine.OrderCommand.BUY,  1000);
           
                 
}

public void actionPerformed(ActionEvent evt)  {
            this.console.getOut().println("Button pressed");

            buttonPressed=true;
           
        }

   public void onAccount(IAccount account) throws JFException {
   }

   public void onMessage(IMessage message) throws JFException {
   }

   public void onStop() throws JFException {
   }

   public void onTick(Instrument instrument, ITick tick) throws JFException {
      if(buttonPressed){
         sendOrder();
         buttonPressed=false;
      }
   }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
}


so, what you do is, you have a boolean variable in your class, that specifies, whether a button has been pressed. in actionPerformed(), you set it to true. so now, each tie you press the button, this boolean will be true.
next, we need something in the original thread, that polls the value of the variable. the best place for that should be in onTick() because that method gets called most often. so in onTick() we check, whether a button has been pressed. if it has been pressed, we send the order and set buttonPressed back to false (so not to send another order in the next tick)
this method is very simple and has a few caveats: e.g. you can't send a second order unless there has been a tick inbetween and you may have some slight synchronization issues (e.g. pressing the button in exactly that moment when submitting the order won't have any effect.
however, there are ways to avoid those problems, but they require a bit more code...


 
 Post subject: Re: How to prevent; Unhandled exception type JFException Post rating: 0   New post Posted: Thu 26 Aug, 2010, 16:16 

User rating: 0
Joined: Fri 20 Aug, 2010, 14:09
Posts: 20
thx.

How to do it properly then? Since like in the example waiting on a tick for sending a market order does not seem a wise thing to do. ;-)


 
 Post subject: Re: How to prevent; Unhandled exception type JFException Post rating: 0   New post Posted: Thu 26 Aug, 2010, 17:52 

User rating: 0
Joined: Tue 24 Aug, 2010, 20:50
Posts: 15
take a look at this: https://www.dukascopy.com/wiki/index.php/Threading
that seems to be, how threading is implemented in jforex... not pretty but one can work with that.
you could make a Callable that has a constant polling (e.g. every 100 ms) in its call() method. this class then could also implement ActionListener and use the method described in my last post.
i haven't tried it yet and, unfortunately, don't have time to do so at the moment, but theoretically it should work.


 
 Post subject: Re: How to prevent; Unhandled exception type JFException Post rating: 0   New post Posted: Fri 27 Aug, 2010, 12:08 

User rating: 0
Joined: Fri 20 Aug, 2010, 14:09
Posts: 20
This seems to work:
package jforex;

import java.util.*;

import com.dukascopy.api.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import javax.swing.*;

public class ActionTest implements IStrategy, ActionListener {
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IContext context;
   private IIndicators indicators;
   private IUserInterface userInterface;
   
    private Future<Object> future;
   
    private BuyTask task;
   
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.history = context.getHistory();
      this.context = context;
      this.indicators = context.getIndicators();
      this.userInterface = context.getUserInterface();
       
       
        JPanel p = this.userInterface.getBottomTab("button panel");
        JButton buy = new JButton("buy");
        buy.addActionListener(this);
        p.add(buy);
        task = new BuyTask(Instrument.EURUSD, 40);
   }

    public void actionPerformed(ActionEvent evt)  {
        this.console.getOut().println("Button pressed");
        try {
        future = context.executeTask(task);
        } catch (Exception e) {
            e.printStackTrace();
            this.console.getOut().println(String.format("Error: %s", e));
        }
    }

   public void onAccount(IAccount account) throws JFException {
   }

   public void onMessage(IMessage message) throws JFException {
   }

   public void onStop() throws JFException {
        this.userInterface.removeBottomTab("button panel");
   }

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

    private class BuyTask implements Callable<Object>{
        private Instrument instrument;
        private double stopLossPips;       
       
        public BuyTask(Instrument instrument, double stopLossPips){
            this.instrument = instrument;
            this.stopLossPips = stopLossPips;
        }
                       
        public Object call() throws Exception {
            double stopLossPrice = history.getLastTick(this.instrument).getBid() - this.stopLossPips * this.instrument.getPipValue();
           
            try {
               return engine.submitOrder("Buy_order", this.instrument, IEngine.OrderCommand.BUY, 0.001, 0, 20, stopLossPrice, 0);
            } catch (JFException e) {
               console.getErr().println(e.getMessage());
               return null;
            }
        }
    }
}


 

Jump to:  

cron
  © 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