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.

Issue with infos plotted on chart
 Post subject: Issue with infos plotted on chart Post rating: 0   New post Posted: Mon 07 Oct, 2013, 04:52 
User avatar

User rating: 0
Joined: Tue 24 Jul, 2012, 12:38
Posts: 41
Dear All,

I coded a small tool to close all positions when a global trailing stop is reached. The tool plot on chart some infos like the higest/lowest profit, the current profit, etc ...

However I noticed that the infos are updated on chart only if I move the mouse on chart. As long as I am not active on chart, the infos are not updated. At least the current account P/L should continuously updating.

Attached is the code. Could someone look at it and tell me if something is wrong, missing or even if something isn't necessary in order to work correctly ?

Thank you very much.
Eric


Attachments:
GlobalTrailingStop.java [5.53 KiB]
Downloaded 118 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: Issue with infos plotted on chart Post rating: 1   New post Posted: Mon 07 Oct, 2013, 10:21 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
I thin a chart.repaint(); will do trick, but you have to test...

public void onTick(Instrument instrument, ITick tick) throws JFException {  
       
    for (int i = 0; i < charts.size(); i++) {
        if (charts.get(i).getInstrument().equals( instrument)) {
            IOhlcChartObject ohlc = ohlcArray.get(i);
            prepareInfo(ohlc, instrument);
            charts.get(i).repaint(); // <- add this!
        }
    }
}


 
 Post subject: Re: Issue with infos plotted on chart Post rating: 0   New post Posted: Mon 07 Oct, 2013, 10:26 
User avatar

User rating: 0
Joined: Tue 24 Jul, 2012, 12:38
Posts: 41
tcsabina wrote:
I thin a chart.repaint(); will do trick, but you have to test...

public void onTick(Instrument instrument, ITick tick) throws JFException {  
       
    for (int i = 0; i < charts.size(); i++) {
        if (charts.get(i).getInstrument().equals( instrument)) {
            IOhlcChartObject ohlc = ohlcArray.get(i);
            prepareInfo(ohlc, instrument);
            charts.get(i).repaint(); // <- add this!
        }
    }
}


Thank you for your help. Will test this asap.


 
 Post subject: Re: Issue with infos plotted on chart Post rating: 2   New post Posted: Mon 07 Oct, 2013, 10:40 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Now, after reviewing my comment I must say that it is a stupid idea to call chart repaint in onTick(). As onTick() is going to repaint the chart of the tick's instrument, isn't it?

Also, you might try Widget class for you purpose. It is almost the same as the OhlcInformer, but more user friendly (no need to hide the predefined values, for example).

Anyway, we will see...


 
 Post subject: Re: Issue with infos plotted on chart Post rating: 0   New post Posted: Mon 07 Oct, 2013, 10:48 
User avatar

User rating: 0
Joined: Tue 24 Jul, 2012, 12:38
Posts: 41
tcsabina wrote:
Now, after reviewing my comment I must say that it is a stupid idea to call chart repaint in onTick(). As onTick() is going to repaint the chart of the tick's instrument, isn't it?

Also, you might try Widget class for you purpose. It is almost the same as the OhlcInformer, but more user friendly (no need to hide the predefined values, for example).

Anyway, we will see...


Thank you again!


 
 Post subject: Re: Issue with infos plotted on chart Post rating: 1   New post Posted: Mon 07 Oct, 2013, 11:45 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
tcsabina wrote:
Also, you might try Widget class for you purpose. It is almost the same as the OhlcInformer, but more user friendly (no need to hide the predefined values, for example).
Exactly. Consider a chart widget where you can see that it does get updated as soon as a new label value is set:
package jforex.charts.widget;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IChart;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IMessage;
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.drawings.ICustomWidgetChartObject;
import com.dukascopy.api.util.DateUtils;

@RequiresFullAccess
public class WidgetExecutor implements IStrategy {

    private JLabel label;   
    private ScheduledExecutorService executor;
    private ICustomWidgetChartObject chartWidget;
    private IChart chart;
   
    @Override
    public void onStart(final IContext context) throws JFException {
        final Instrument instrument = Instrument.EURUSD;
        chart = context.getChart(instrument);
        if (chart == null) {
                context.getConsole().getErr().println("No chart opened!");
            context.stop();
        }
        ICustomWidgetChartObject obj = chart.getChartObjectFactory().createChartWidget();
        obj.setText("Current time");
       
        obj.setFillOpacity(0.1f); //use 0f for transparent chart widget
        obj.setColor(Color.GREEN.darker());
       
        JPanel panel = obj.getContentPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        label = new JLabel();
        label.setAlignmentX(Component.CENTER_ALIGNMENT);
       
        panel.add(label);
        panel.setSize(new Dimension(150, 100));
        chart.add(obj);

        executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleAtFixedRate(new Runnable(){

            @Override
            public void run() {
                label.setText(DateUtils.format(System.currentTimeMillis()));
            }
            }, 0, 200, TimeUnit.MILLISECONDS);
    }
   
    public void onStop() throws JFException {
       executor.shutdown(); //essential to stop the execution process which executed in a separate thread
       chart.remove(chartWidget);
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {}
    public void onMessage(IMessage message) throws JFException {}
    public void onAccount(IAccount account) throws JFException {}

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


Attachments:
WidgetExecutor.java [2.65 KiB]
Downloaded 110 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: Issue with infos plotted on chart Post rating: 0   New post Posted: Mon 07 Oct, 2013, 11:56 
User avatar

User rating: 0
Joined: Tue 24 Jul, 2012, 12:38
Posts: 41
API Support wrote:
Exactly. Consider a chart widget where you can see that it does get updated as soon as a new label value is set


Thank you Support. Very much appreciated.

What am I supposed to do with this ? Implement this in my code or implemente my code in the widget code ? I am very new to java coding !!!


 
 Post subject: Re: Issue with infos plotted on chart Post rating: 0   New post Posted: Mon 07 Oct, 2013, 12:12 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Eric wrote:
Implement this in my code or implemente my code in the widget code ?
If you are new to java then we find it more feasible for you to use our example as the starting point and move your logic piece by piece to it. Essentially in the case of a chart widget you work with a JPanel which is a standard Java Swing component.
In case it does not suffice with this and wiki example, see the Java Swing Tutorial. And if you don't use an IDE for writing your strategy, we would strongly advise you to use one, since not only there it is much more convenient to write your strategy but also you will be able to see the Swing component javadocs.


 
 Post subject: Re: Issue with infos plotted on chart Post rating: 0   New post Posted: Mon 07 Oct, 2013, 12:14 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Only replace IOhlcChartObject instances in your strategy with the mentioned ICustomWidgetChartObject class, and use that one (the Widget objects) to show your messages on the chart, instead of an OhlCInformer.
There are some extra steps necessary (not just a simple replace), as clearUserMessages() for instance is a function of the IOhlcChartObject class only, but I think you can figure it out...


 
 Post subject: Re: Issue with infos plotted on chart Post rating: 0   New post Posted: Mon 07 Oct, 2013, 12:32 
User avatar

User rating: 0
Joined: Tue 24 Jul, 2012, 12:38
Posts: 41
Thank you both for your answers.


 
 Post subject: Re: Issue with infos plotted on chart Post rating: 0   New post Posted: Thu 26 Dec, 2013, 21:16 
User avatar

User rating: 96
Joined: Mon 09 Sep, 2013, 07:09
Posts: 287
Location: Ukraine, SHostka
API Support wrote:
tcsabina wrote:
Also, you might try Widget class for you purpose. It is almost the same as the OhlcInformer, but more user friendly (no need to hide the predefined values, for example).
Exactly. Consider a chart widget where you can see that it does get updated as soon as a new label value is set:
package jforex.charts.widget;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IChart;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IMessage;
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.drawings.ICustomWidgetChartObject;
import com.dukascopy.api.util.DateUtils;

public class WidgetExecutor implements IStrategy {

    private JLabel label;   
    private ScheduledExecutorService executor;
    private ICustomWidgetChartObject chartWidget;
    private IChart chart;
   
    @Override
    public void onStart(final IContext context) throws JFException {
        final Instrument instrument = Instrument.EURUSD;
        chart = context.getChart(instrument);
        if (chart == null) {
                context.getConsole().getErr().println("No chart opened!");
            context.stop();
        }
        ICustomWidgetChartObject obj = chart.getChartObjectFactory().createChartWidget();
        obj.setText("Current time");
       
        obj.setFillOpacity(0.1f); //use 0f for transparent chart widget
        obj.setColor(Color.GREEN.darker());
       
        JPanel panel = obj.getContentPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        label = new JLabel();
        label.setAlignmentX(Component.CENTER_ALIGNMENT);
       
        panel.add(label);
        panel.setSize(new Dimension(150, 100));
        chart.add(obj);

        executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleAtFixedRate(new Runnable(){

            @Override
            public void run() {
                label.setText(DateUtils.format(System.currentTimeMillis()));
            }
            }, 0, 200, TimeUnit.MILLISECONDS);
    }
   
    public void onStop() throws JFException {
       executor.shutdown(); //essential to stop the execution process which executed in a separate thread
       chart.remove(chartWidget);
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {}
    public void onMessage(IMessage message) throws JFException {}
    public void onAccount(IAccount account) throws JFException {}

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
}
Your code throws a SecurityException on my machine:
java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "modifyThread")
   at java.security.AccessControlContext.checkPermission(AccessControlContext.java:372)
   at java.security.AccessController.checkPermission(AccessController.java:559)
   at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
   at java.util.concurrent.ThreadPoolExecutor.checkShutdownAccess(ThreadPoolExecutor.java:733)
   at java.util.concurrent.ThreadPoolExecutor.shutdown(ThreadPoolExecutor.java:1390)
   at java.util.concurrent.ScheduledThreadPoolExecutor.shutdown(ScheduledThreadPoolExecutor.java:759)
   at java.util.concurrent.Executors$DelegatedExecutorService.shutdown(Executors.java:629)
   at jforex.WidgetExecutor.onStop(WidgetExecutor.java:68)
   at com.dukascopy.api.impl.execution.v.call(Unknown Source)
   at com.dukascopy.api.impl.execution.v.call(Unknown Source)
   at com.dukascopy.api.impl.connect.ag.onStop(Unknown Source)
   at com.dukascopy.api.impl.connect.JForexTaskManager$c.call(Unknown Source)
   at com.dukascopy.api.impl.connect.JForexTaskManager$c.call(Unknown Source)
   at com.dukascopy.api.impl.execution.k.call(Unknown Source)
   at java.util.concurrent.FutureTask.run(FutureTask.java:262)
   at com.dukascopy.api.impl.execution.g$a.f(Unknown Source)
   at com.dukascopy.api.impl.execution.g$a.run(Unknown Source)
   at java.lang.Thread.run(Thread.java:744)
Why?


 
 Post subject: Re: Issue with infos plotted on chart Post rating: 0   New post Posted: Tue 07 Jan, 2014, 11:18 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
We had forgotten to add @RequiresFullAccess.


 
 Post subject: Re: Issue with infos plotted on chart Post rating: 0   New post Posted: Tue 07 Jan, 2014, 12:49 
User avatar

User rating: 96
Joined: Mon 09 Sep, 2013, 07:09
Posts: 287
Location: Ukraine, SHostka
API Support wrote:
We had forgotten to add @RequiresFullAccess.
Now I've got this exception:
java.lang.NullPointerException
   at com.dukascopy.charts.chartbuilder.t.remove(Unknown Source)
   at com.dukascopy.charts.f.b.remove(Unknown Source)
   at jforex.WidgetExecutor.onStop(WidgetExecutor.java:69)
   at com.dukascopy.api.impl.execution.v.call(Unknown Source)
   at com.dukascopy.api.impl.execution.v.call(Unknown Source)
   at com.dukascopy.api.impl.connect.ag.onStop(Unknown Source)
   at com.dukascopy.api.impl.connect.JForexTaskManager$c.call(Unknown Source)
   at com.dukascopy.api.impl.connect.JForexTaskManager$c.call(Unknown Source)
   at com.dukascopy.api.impl.execution.k.call(Unknown Source)
   at java.util.concurrent.FutureTask.run(FutureTask.java:262)
   at com.dukascopy.api.impl.execution.g$a.f(Unknown Source)
   at com.dukascopy.api.impl.execution.g$a.run(Unknown Source)
   at java.lang.Thread.run(Thread.java:744)


 
 Post subject: Re: Issue with infos plotted on chart Post rating: 1   New post Posted: Tue 07 Jan, 2014, 13:07 
User avatar

User rating: 96
Joined: Mon 09 Sep, 2013, 07:09
Posts: 287
Location: Ukraine, SHostka
hebasto wrote:
API Support wrote:
We had forgotten to add @RequiresFullAccess.
Now I've got this exception:
java.lang.NullPointerException
   at com.dukascopy.charts.chartbuilder.t.remove(Unknown Source)
   at com.dukascopy.charts.f.b.remove(Unknown Source)
   at jforex.WidgetExecutor.onStop(WidgetExecutor.java:69)
   at com.dukascopy.api.impl.execution.v.call(Unknown Source)
   at com.dukascopy.api.impl.execution.v.call(Unknown Source)
   at com.dukascopy.api.impl.connect.ag.onStop(Unknown Source)
   at com.dukascopy.api.impl.connect.JForexTaskManager$c.call(Unknown Source)
   at com.dukascopy.api.impl.connect.JForexTaskManager$c.call(Unknown Source)
   at com.dukascopy.api.impl.execution.k.call(Unknown Source)
   at java.util.concurrent.FutureTask.run(FutureTask.java:262)
   at com.dukascopy.api.impl.execution.g$a.f(Unknown Source)
   at com.dukascopy.api.impl.execution.g$a.run(Unknown Source)
   at java.lang.Thread.run(Thread.java:744)

The bug is fixed. Correct strategy is attached.
package jforex;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IChart;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IMessage;
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.drawings.ICustomWidgetChartObject;
import com.dukascopy.api.util.DateUtils;
import com.dukascopy.api.RequiresFullAccess;

@RequiresFullAccess
public class WidgetExecutor implements IStrategy {

    private JLabel label;   
    private ScheduledExecutorService executor;
    private ICustomWidgetChartObject chartWidget;
    private IChart chart;
   
    @Override
    public void onStart(final IContext context) throws JFException {
        final Instrument instrument = Instrument.EURUSD;
        chart = context.getChart(instrument);
        if (chart == null) {
                context.getConsole().getErr().println("No chart opened!");
            context.stop();
        }
        chartWidget = chart.getChartObjectFactory().createChartWidget();
        chartWidget.setText("Current time");
       
        chartWidget.setFillOpacity(0.1f); //use 0f for transparent chart widget
        chartWidget.setColor(Color.GREEN.darker());
       
        JPanel panel = chartWidget.getContentPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        label = new JLabel();
        label.setAlignmentX(Component.CENTER_ALIGNMENT);
       
        panel.add(label);
        panel.setSize(new Dimension(150, 100));
        chart.add(chartWidget);

        executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleAtFixedRate(new Runnable(){

            @Override
            public void run() {
                label.setText(DateUtils.format(System.currentTimeMillis()));
            }
            }, 0, 200, TimeUnit.MILLISECONDS);
    }
   
    public void onStop() throws JFException {
        executor.shutdown(); //essential to stop the execution process which executed in a separate thread
        chart.remove(chartWidget);
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {}
    public void onMessage(IMessage message) throws JFException {}
    public void onAccount(IAccount account) throws JFException {}
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
}


Attachments:
WidgetExecutor.java [2.73 KiB]
Downloaded 97 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:  

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