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.

SwingUtilities.invokeAndWait
 Post subject: SwingUtilities.invokeAndWait Post rating: 0   New post Posted: Wed 21 Dec, 2011, 14:06 

User rating: 0
Joined: Tue 22 Nov, 2011, 12:47
Posts: 130
Location: United States,
hi Support,

I have no experience in Swing programming.

Could you please provide best practices on when to use SwingUtilities.invokeAndWait ?

Is it mandatory for every Chart Object update?

For example - I have a label which updates on every tick. Is it a must?


 
 Post subject: Re: SwingUtilities.invokeAndWait Post rating: 0   New post Posted: Thu 22 Dec, 2011, 14:54 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
There is no need to use SwingUtilities.invokeAndWait.
In order to update a label on chart, you can keep a reference to label object in your strategy and update its time and price values. Moreover, if you create a new label with the same key it will replace the existing label.
Here is a strategy that creates and updates a label.
package jforex;

import com.dukascopy.api.*;
import com.dukascopy.api.drawings.IChartObjectFactory;
import com.dukascopy.api.drawings.ILabelChartObject;
import java.awt.Color;
import java.awt.Font;

public class Strategy implements IStrategy {
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IContext context;
   private IIndicators indicators;
   private IUserInterface userInterface;
    private IChart chart;
    @Configurable("Instrument")
    private Instrument     selectedInstrument     = Instrument.EURUSD;
    private Period         selectedPeriod;
   
   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();
        this.chart = context.getChart(selectedInstrument);
        this.selectedPeriod = chart.getSelectedPeriod();
   }

   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(!instrument.equals(selectedInstrument)) {
            return;
        }
        drawLabel(tick.getTime(), tick.getAsk());
   }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
   
    private ILabelChartObject label = null;
   
    private void drawLabel(long time, double price) {
        long timeDelta = selectedPeriod.getInterval() / 2;
        if(label == null) {
           IChartObjectFactory factory = chart.getChartObjectFactory();
           label = factory.createLabel("label", time + timeDelta, price);
            label.setOpacity(1);
            label.setColor(Color.BLUE);
        } else {
           label.setPrice(0, price);
           label.setTime(0, time + timeDelta);
        }       
        label.setText(String.valueOf(price), Font.BOLD);
        chart.addToMainChart(label);
    }
}


Attachments:
SimpleLabel.java [2.1 KiB]
Downloaded 267 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: SwingUtilities.invokeAndWait Post rating: 0   New post Posted: Fri 23 Dec, 2011, 08:56 

User rating: 0
Joined: Tue 22 Nov, 2011, 12:47
Posts: 130
Location: United States,
thanks Support!

I noticed a few indicators in this forum posted by users that include the usage of SwingUtilities.invokeAndWait and was thinking that this is related to performance issue (not loosing ticks etc.)

Can you please confirm that drawing does not cause any lost ticks?


 
 Post subject: Re: SwingUtilities.invokeAndWait Post rating: 0   New post Posted: Tue 27 Dec, 2011, 11:38 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
cb888trader wrote:
Can you please confirm that drawing does not cause any lost ticks?
Drawing does not cause skipping of ticks, since it does not take an excessive amount of time (for more details on skipping ticks, see: Strategy Api/Other aspects/onTick execution policy in https://www.dukascopy.com/wiki/).


 

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