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.

Strategy with graphical objects blocks JForex platform
 Post subject: Strategy with graphical objects blocks JForex platform Post rating: 0   New post Posted: Wed 21 Dec, 2011, 21:25 

User rating: 1
Joined: Tue 12 Jul, 2011, 20:43
Posts: 51
Location: Germany,
Hello Support,

I want to create a strategy with graphical objects on a bottom tab to support manual scalping on a EUR/USD 66 tick chart. Parts of this strategy I already completed, and it worked well about six weeks ago. Today I wanted to continue my work on this strategy. First I started it again - without any code changes - and now it blocks the whole JForex platform so that the only possibility is to cancel the platform with the task manager (operating system is windows 7).

I created a short version of my strategy with the same behaviour to figure out the problem:
package jForex;

import com.dukascopy.api.*;
import com.dukascopy.api.bar.ITickBar;
import com.dukascopy.api.listener.ITickBarFeedListener;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.BorderFactory;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import java.awt.Color;
import java.awt.BasicStroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.HashSet;
import java.util.HashMap;

public class Strategy implements IStrategy
{
    private IContext clsContext = null;
    private IAccount clsAccount = null;
    private IConsole clsConsole = null;
    private IEngine clsEngine = null;
    private IHistory clsHistory = null;
    private IChart clsTickBarChart = null;
    private IOrder clsOrder = null;
    private IUserInterface clsUserInterface = null;
    private HashMap clsHashMapTimePeriodCharts = new HashMap<String,IChart>();
    private final JButton clsButtonChangeTradeDirection = new JButton("Long <-> Short");
    private final JButton clsButtonOpenPosition = new JButton("   O p e n   ");
    private final JButton clsButtonCancelPosition = new JButton(" C a n c e l ");
    private final JButton clsButtonClosePosition = new JButton("  C l o s e  ");
    private final JCheckBox clsCheckboxLimitOrStop = new JCheckBox("Limit / Stop", false);
    private final JCheckBox clsCheckboxTakeProfit = new JCheckBox("Take Profit", true);
    private final JCheckBox clsCheckboxCloseHalfPosition = new JCheckBox("Close ½", false);
    private final JComboBox clsComboboxStopLosses = new JComboBox(new String [] { "Manual Stop", "Bar Trailing Stop", "Tick Trailing Stop" } );
    private double clsdStartEquity = Double.NaN;
   
    @Configurable("Risk per Trade in %") public double cfgdRiskPerTradeInPercent = 0.3;
    @Configurable("Maximum Loss in %")   public double cfgdMaxLossInPercent = 2.5;
    @Configurable("Maximum Leverage")    public double cfgdMaxLeverage = 20.0;
    @Configurable("TickBar Size")        public int    cfgiTickBarSize = 66;
   
   public void onStart(IContext context) throws JFException
   {
        clsContext = context;
        clsAccount = clsContext.getAccount();
        clsConsole = clsContext.getConsole();
        clsEngine = clsContext.getEngine();
        clsHistory = clsContext.getHistory();
        clsUserInterface = clsContext.getUserInterface();
       
        clsContext.setSubscribedInstruments(new HashSet<Instrument>(Arrays.asList(Instrument.EURUSD)));
       
        clsdStartEquity = clsAccount.getEquity();
       
        String strChartName = "";
        for (IChart chart : clsContext.getCharts(Instrument.EURUSD))
        {
            if (chart.getDataType() == DataType.TICK_BAR)
                clsTickBarChart = chart;
            if (chart.getDataType() == DataType.TIME_PERIOD_AGGREGATION)
            {
               strChartName = chart.getSelectedPeriod().getNumOfUnits() + " " + chart.getSelectedPeriod().getUnit().toString();
               if (chart.getSelectedPeriod().getNumOfUnits() > 1)
                   strChartName += "s";
               clsHashMapTimePeriodCharts.put(strChartName, chart);
            }
        }

        if (clsTickBarChart == null)
            throw new JFException("No TickBarChart available");
       
        ITick tick = clsHistory.getLastTick(Instrument.EURUSD);
       
        JPanel jPanelBottomTab = clsUserInterface.getBottomTab("Scalping");
        JPanel jPanelTradeManagement = new JPanel();
        jPanelTradeManagement.setBorder(BorderFactory.createTitledBorder(new LineBorder(Color.lightGray, 2), " Trade Management ", TitledBorder.CENTER, TitledBorder.TOP));
        jPanelTradeManagement.add(new JLabel(" "));
        jPanelTradeManagement.add(clsCheckboxLimitOrStop);
        jPanelTradeManagement.add(new JLabel(""));
        jPanelTradeManagement.add(clsButtonChangeTradeDirection);
        jPanelTradeManagement.add(new JLabel("  "));
        jPanelTradeManagement.add(clsButtonOpenPosition);
        jPanelTradeManagement.add(new JLabel("  "));
        jPanelTradeManagement.add(clsButtonCancelPosition);
        jPanelTradeManagement.add(new JLabel("  "));
        jPanelTradeManagement.add(clsCheckboxTakeProfit);
        jPanelTradeManagement.add(new JLabel(" "));
        jPanelTradeManagement.add(clsComboboxStopLosses);
        jPanelTradeManagement.add(new JLabel("  "));
        jPanelTradeManagement.add(clsCheckboxCloseHalfPosition);
        jPanelTradeManagement.add(new JLabel(""));
        jPanelTradeManagement.add(clsButtonClosePosition);
        jPanelTradeManagement.add(new JLabel("  "));
        jPanelBottomTab.add(jPanelTradeManagement);
       
        }
   
   public void onMessage(IMessage message) throws JFException
   {
   }

   public void onTick(Instrument instrument, ITick tick) throws JFException
   {
   }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException
    {
    }
   
    public void onAccount(IAccount account) throws JFException
    {
    }
   
    public void onStop() throws JFException
    {
        if (clsEngine != null && !clsEngine.getOrders(Instrument.EURUSD).isEmpty())
            for (IOrder order : clsEngine.getOrders())
                order.close();
       
        for (IChart chart : clsContext.getCharts(Instrument.EURUSD))
            if (chart.getDataType() == DataType.TICK_BAR || chart.getDataType() == DataType.TIME_PERIOD_AGGREGATION)
                chart.removeAll();
       
        if (clsUserInterface != null)
            clsUserInterface.removeBottomTab("Scalping");
    }
}

If I deactivate the lines related to creation of bottom tab objects (from line 82 to line 102) the strategy starts immediately. Otherwise, either it takes about a half minute to start and show the bottom tab objects, or it only prints out the message "strategy is started ..." in the message tab and then the whole JForex platform is blocked.

I assume there are any changes in JForex client or API during last 6 weeks which causes this problem.
I'm using demo version 2.13.37 and API version 2.6.38.1.

Plese help me to find a solution.


Thanks and regards

AbsoluteReturner


 
 Post subject: Re: Strategy with graphical objects blocks JForex platform Post rating: 0   New post Posted: Thu 22 Dec, 2011, 09:30 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
We were unable to reproduce this problem.
Could you please check what you receive on Java console? See:
viewtopic.php?f=81&t=39073


 
 Post subject: Re: Strategy with graphical objects blocks JForex platform Post rating: 0   New post Posted: Thu 22 Dec, 2011, 14:14 

User rating: 3
Joined: Tue 17 May, 2011, 16:51
Posts: 38
Location: Sweden, Jonkoping
Check out: viewtopic.php?f=65&t=43153


 
 Post subject: Re: Strategy with graphical objects blocks JForex platform Post rating: 0   New post Posted: Thu 22 Dec, 2011, 15:57 

User rating: 1
Joined: Tue 12 Jul, 2011, 20:43
Posts: 51
Location: Germany,
I activated JavaConsole and then I tested the two workarounds suggested from Cashbone.

The "@RequiresFullAccess" works (although it seems that the strategy runs slower now than 6 weeks ago).

The alternative JavaConsole implementation produces following NullPointerException:
Java Web Start 1.6.0_29
Verwendung der JRE-Version 1.6.0_29-b11 Java HotSpot(TM) Client VM
----------------------------------------------------
...
----------------------------------------------------
22.12.2011 15:36:04.972 SCHWERWIEGEND                            a.c ] Uncaught exception in [AWT-EventQueue-0] thread: null
java.lang.NullPointerException
   at com.dukascopy.dds2.greed.gui.component.chart.aa.m(Unknown Source)
   at com.dukascopy.dds2.greed.gui.component.chart.aa.mousePressed(Unknown Source)
   at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
   at java.awt.Component.processMouseEvent(Unknown Source)
   at javax.swing.JComponent.processMouseEvent(Unknown Source)
   at java.awt.Component.processEvent(Unknown Source)
   at java.awt.Container.processEvent(Unknown Source)
   at java.awt.Component.dispatchEventImpl(Unknown Source)
   at java.awt.Container.dispatchEventImpl(Unknown Source)
   at java.awt.Component.dispatchEvent(Unknown Source)
   at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
   at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
   at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
   at java.awt.Container.dispatchEventImpl(Unknown Source)
   at java.awt.Window.dispatchEventImpl(Unknown Source)
   at java.awt.Component.dispatchEvent(Unknown Source)
   at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
   at java.awt.EventQueue.access$000(Unknown Source)
   at java.awt.EventQueue$1.run(Unknown Source)
   at java.awt.EventQueue$1.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
   at java.awt.EventQueue$2.run(Unknown Source)
   at java.awt.EventQueue$2.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)
22.12.2011 15:36:05.116 SCHWERWIEGEND                            a.c ] Uncaught exception in [AWT-EventQueue-0] thread: null
java.lang.NullPointerException
   at com.dukascopy.dds2.greed.gui.component.chart.aa.m(Unknown Source)
   at com.dukascopy.dds2.greed.gui.component.chart.aa.mouseReleased(Unknown Source)
   at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
   at java.awt.Component.processMouseEvent(Unknown Source)
   at javax.swing.JComponent.processMouseEvent(Unknown Source)
   at java.awt.Component.processEvent(Unknown Source)
   at java.awt.Container.processEvent(Unknown Source)
   at java.awt.Component.dispatchEventImpl(Unknown Source)
   at java.awt.Container.dispatchEventImpl(Unknown Source)
   at java.awt.Component.dispatchEvent(Unknown Source)
   at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
   at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
   at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
   at java.awt.Container.dispatchEventImpl(Unknown Source)
   at java.awt.Window.dispatchEventImpl(Unknown Source)
   at java.awt.Component.dispatchEvent(Unknown Source)
   at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
   at java.awt.EventQueue.access$000(Unknown Source)
   at java.awt.EventQueue$1.run(Unknown Source)
   at java.awt.EventQueue$1.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
   at java.awt.EventQueue$2.run(Unknown Source)
   at java.awt.EventQueue$2.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)


Anyway, thanks a lot and merry christmas !

Regards
AR


 
 Post subject: Re: Strategy with graphical objects blocks JForex platform Post rating: 0   New post Posted: Tue 27 Dec, 2011, 09:44 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
AbsoluteReturner wrote:
The alternative JavaConsole implementation produces following NullPointerException:
The exception does not seem to be related to the alternative JavaConsole implementation.


 
 Post subject: Re: Strategy with graphical objects blocks JForex platform Post rating: 0   New post Posted: Thu 29 Dec, 2011, 01:41 

User rating: 1
Joined: Tue 12 Jul, 2011, 20:43
Posts: 51
Location: Germany,
Hello Support,

On 22th Dec. I wrote that "@RequiresFullAccess" solved the blocking problem, but I also wrote that the processing speed of the strategy seems to be slower as before now.
Concretely, bid and ask price inside the onTick() function of the strategy mostely are not equal to the current bid / ask values as shown in the platform but equal to previous prices.
I noticed this because the strategy shows the bid / ask spread by horizontal lines.

Please have a look at the screenshots "TickBarChart_Bid.jpg" and "TickBarChart_Ask.jpg", it show the difference between current platform bid / ask price and tick.getBid() / tick.getAsk() result inside the onTick() function.
Attachment:
TickBarChart_Bid.jpg [23.34 KiB]
Downloaded 356 times

Attachment:
TickBarChart_Ask.jpg [21.95 KiB]
Downloaded 336 times


I created another short version of my strategy with this behaviour which produced theese two screenshots:
package jForex;

import com.dukascopy.api.*;
import com.dukascopy.api.drawings.IHorizontalLineChartObject;
import java.awt.Color;
import java.util.Arrays;
import java.util.HashSet;
import java.util.HashMap;

@RequiresFullAccess
public class Strategy implements IStrategy
{
    private IContext clsContext = null;
    private IHistory clsHistory = null;
    private IChart clsTickBarChart = null;
    private IHorizontalLineChartObject clsHLineBid = null, clsHLineAsk = null;
   
   public void onStart(IContext context) throws JFException
   {
        clsContext = context;
        clsHistory = clsContext.getHistory();
       
        clsContext.setSubscribedInstruments(new HashSet<Instrument>(Arrays.asList(Instrument.EURUSD)));
       
        for (IChart chart : clsContext.getCharts(Instrument.EURUSD))
        {
            if (chart.getDataType() == DataType.TICK_BAR)
                clsTickBarChart = chart;
        }
        if (clsTickBarChart == null)
            throw new JFException("No TickBarChart available");
       
        ITick tick = clsHistory.getLastTick(Instrument.EURUSD);
        clsHLineBid = addHLineToTickBarChart("HLineBid", tick.getBid(), Color.red.darker(), tick.getBid() + " ", false);
        clsHLineAsk = addHLineToTickBarChart("HLineAsk", tick.getAsk(), Color.green.darker(), tick.getAsk() + " ", false);
    }
   
   public void onMessage(IMessage message) throws JFException
   {
   }

   public void onTick(Instrument instrument, ITick tick) throws JFException
    {
        if (instrument != Instrument.EURUSD)
            return;
       
        if (clsHLineBid != null)
        {
            clsHLineBid.setPrice(0, tick.getBid());
            clsHLineBid.setText(tick.getBid() + " ");
        }
       
        if (clsHLineAsk != null)
        {
            clsHLineAsk.setPrice(0, tick.getAsk());
            clsHLineAsk.setText(tick.getAsk() + " ");
        }
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException
    {
    }
   
    public void onAccount(IAccount account) throws JFException
    {
    }
   
    public void onStop() throws JFException
    {
        for (IChart chart : clsContext.getCharts(Instrument.EURUSD))
            if (chart.getDataType() == DataType.TICK_BAR)
                chart.removeAll();
    }
   
    private IHorizontalLineChartObject addHLineToTickBarChart(String sKey, double dValue, Color color, String sText, boolean bEnabled)
    {       
        if (sKey == null || sKey.trim().length() == 0 || Double.isNaN(dValue))
            return null;
       
        IHorizontalLineChartObject hLine = clsTickBarChart.getChartObjectFactory().createHorizontalLine(sKey, dValue);
        hLine.setColor(color == null ? Color.gray : color);
        hLine.setText(sText == null ? " " : sText);
       
        if (bEnabled == true)
            clsTickBarChart.addToMainChartUnlocked(hLine);
        else
            clsTickBarChart.addToMainChart(hLine);
       
        return hLine;
    }
}


Is there a way to get the real current bid and ask prices without delay inside the onTick() function for my scalping strategy ?
Is there a special approach needed to get theese values for a TickBar Chart (66 Ticks per period) ?

Regards
AbsoluteReturner


 
 Post subject: Re: Strategy with graphical objects blocks JForex platform Post rating: 0   New post Posted: Thu 29 Dec, 2011, 12:59 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
We were not able to reproduce the behavior by using your strategy. We tested both with BID and ASK prices. We tested with 3 tick, 10 tick and 66 tick bars. Does the problem persist?


 
 Post subject: Re: Strategy with graphical objects blocks JForex platform Post rating: 0   New post Posted: Fri 30 Dec, 2011, 11:04 

User rating: 1
Joined: Tue 12 Jul, 2011, 20:43
Posts: 51
Location: Germany,
Yes, the problem still persist - sometime prices of the HLineBid / HLineAsk are equal to platform bid / ask prices, but mostly they are not but equal to prices of previous tick.
In contrast, about two month ago (when I created the first version of this strategy) this behaviour occured very seldom.

Please note again that I updated JRE to version 1.6.0_29-b11 some weeks ago and using JForex demo version 2.13.37 with API version 2.5.38.1 now.


 
 Post subject: Re: Strategy with graphical objects blocks JForex platform Post rating: 0   New post Posted: Fri 30 Dec, 2011, 15:55 

User rating: 1
Joined: Tue 12 Jul, 2011, 20:43
Posts: 51
Location: Germany,
The Bid and Ask price delay problem seems to be solved.

I inserted "clsTickBarChart.repaint();" inside the onTick() function.
Surpisingly this solution works not only by insertion of this statement at the end of the onTick() function (after line 57) but also before updating the price values of HLineBid and HLineAsk (after line 46).


Regards
AR


 

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