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.

Get number of ticks processed in current TickBar period
 Post subject: Get number of ticks processed in current TickBar period Post rating: 0   New post Posted: Wed 24 Aug, 2011, 18:59 

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

can you please provide a strategy for usage with TickBar charts which displays (at the right side of current period) the number of ticks already processed in current period ?


Best regards
AbsoluteReturner


 
 Post subject: Re: Get number of ticks processed in current TickBar period Post rating: 0   New post Posted: Thu 25 Aug, 2011, 09:40 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Here is an example strategy that shows the number of ticks in the current bar.

import java.awt.Color;
import java.util.*;

import com.dukascopy.api.*;
import com.dukascopy.api.drawings.IChartObjectFactory;
import com.dukascopy.api.drawings.ILabelChartObject;

public class TickCountStrategy implements IStrategy {
    private IConsole console;
    private IHistory history;
    private IChart chart;

    private ILabelChartObject label;
    int tickCount;
    double price;
   
    @Configurable("Period")
    public Period strategyPeriod = Period.ONE_HOUR;

    @Configurable("Instrument")
    public Instrument strategyInstrument = Instrument.EURUSD;

    double previousValue = 0;

    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.history = context.getHistory();
       
        this.chart = context.getChart(strategyInstrument);
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
        print("Stopped!");
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (instrument.equals(strategyInstrument)) {
            if(label != null){
                tickCount++;
                label.setText(tickCount+"");
                price = Math.max(tick.getBid(), price);
                label.setPrice(0, Math.max(tick.getBid(), price));
            }
        }
    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
       
        if (instrument.equals(strategyInstrument) && period.equals(strategyPeriod)) {
            IBar bar = history.getBar(strategyInstrument, strategyPeriod, OfferSide.BID, 0);
            drawLabel(bar.getTime(), bar.getOpen());
        }
    }

    public void print(Object o) {
        this.console.getOut().println(o.toString());
    }
   
    private void drawLabel(long time, double price) {       
        IChartObjectFactory factory = chart.getChartObjectFactory();
        String key = ("label" + UUID.randomUUID().toString().replace('-', '0'));
        this.label = factory.createLabel(key, time, price);
        label.setText("0");
        label.setColor(Color.BLACK);
        chart.addToMainChart(label);
       
        this.price = price;
        tickCount = 0;
    }
   
   
   
}


Attachments:
TickCountStrategy.java [2.3 KiB]
Downloaded 342 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: Get number of ticks processed in current TickBar period Post rating: 0   New post Posted: Thu 25 Aug, 2011, 21:30 

User rating: 1
Joined: Tue 12 Jul, 2011, 20:43
Posts: 51
Location: Germany,
Thank you for this example.

But my question was how to do this for a chart based on TickBars, e.g. 50 ticks per period.
It seems more complicated because I don't know how to identify the period (which doesn't has a constant time duration) in the onBar() method.

Regards
AR


 
 Post subject: Re: Get number of ticks processed in current TickBar period Post rating: 0   New post Posted: Fri 26 Aug, 2011, 13:50 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Here is a strategy that puts and updates labels on tick bar chart. Labels contain the number of ticks in current tick bar.

package jforex.strategies;

import java.awt.Color;
import java.util.*;

import com.dukascopy.api.*;
import com.dukascopy.api.bar.ITickBar;
import com.dukascopy.api.drawings.IChartObjectFactory;
import com.dukascopy.api.drawings.ILabelChartObject;
import com.dukascopy.api.listener.ITickBarFeedListener;


public class TickCountStrategy implements IStrategy {
    private IConsole console;
    private IHistory history;
    private IChart chart;

    private ILabelChartObject label;
    int tickCount;
    double price;

    @Configurable("Instrument")
    public Instrument strategyInstrument = Instrument.EURUSD;
   
    @Configurable("Tick bar size")
    public int tickBarSize = 10;

    double previousValue = 0;

    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.history = context.getHistory();
       
        context.subscribeToTickBarFeed(Instrument.EURUSD, OfferSide.BID,  TickBarSize.valueOf(tickBarSize), new ITickBarFeedListener() {
            @Override
            public void onBar(Instrument instrument, OfferSide offerSide, TickBarSize size, ITickBar bar) {
                console.getOut().println("On Tick Bar " + " " + instrument + " " + offerSide + " " + tickBarSize + " " + bar);
                label = null;
            }
        });
       
        this.chart = context.getChart(strategyInstrument);
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
        print("Stopped!");
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (instrument.equals(strategyInstrument)) {
            if(label == null){
                price = tick.getBid();
                tickCount = 1;
                label = drawLabel(tick.getTime(), tick.getBid());
            } else {
                price = Math.max(tick.getBid(), price);
                label.setPrice(0, Math.max(tick.getBid(), price));
                tickCount++;
                label.setText(tickCount+"");
                label.setTime(0, tick.getTime());
            }         
        }
    }

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

    public void print(Object o) {
        this.console.getOut().println(o.toString());
    }
   
    private ILabelChartObject drawLabel(long time, double price) {       
        IChartObjectFactory factory = chart.getChartObjectFactory();
        String key = ("label" + UUID.randomUUID().toString().replace('-', '0'));
        ILabelChartObject l = factory.createLabel(key, time, price);
        l.setText("1");
        l.setColor(Color.BLACK);
        chart.addToMainChart(l);
        return l;
    }
   
   
   
}


In order to show tick bars on chart a new custom period should be defined:
Tools > Preferences > Period > Ticks > choose tick count in the bar > press ">>" button > press "OK".
It is now possible to choose the new period in the chart.


Attachments:
TickCountStrategy.java [2.78 KiB]
Downloaded 343 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: Get number of ticks processed in current TickBar period Post rating: 0   New post Posted: Mon 29 Aug, 2011, 16:42 

User rating: 1
Joined: Tue 12 Jul, 2011, 20:43
Posts: 51
Location: Germany,
I compiled and started this strategy on a 60 ticks chart but no label with tickCount value was shown on the main chart.

Therefore I inserted the following statement in the onTick() method:
console.getOut().println(tickCount+"");
and the tickCount value was printed correctly (every new tickCount value in a new line).

So I can say, the main problem (identify TickBars periods and count the ticks of every period) is solved, but display of tick value on the main chart does not yet work.

Best Regards
AR


 
 Post subject: Re: Get number of ticks processed in current TickBar period Post rating: 0   New post Posted: Tue 30 Aug, 2011, 08:33 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Could you, please, take a screenshot with the console output of the strategy and the chart.


 
 Post subject: Re: Get number of ticks processed in current TickBar period Post rating: 0   New post Posted: Tue 30 Aug, 2011, 16:16 

User rating: 1
Joined: Tue 12 Jul, 2011, 20:43
Posts: 51
Location: Germany,
In the meanwhile I found out that the labels are displayed in the 1 hour chart instead - one label for every new TickBars period.
I have open the follwing EUR/USD charts: H1, M5, M1, 100T.

Regards
AR


 
 Post subject: Re: Get number of ticks processed in current TickBar period Post rating: 0   New post Posted: Wed 31 Aug, 2011, 10:04 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Here is a strategy that will add the numbers to an open tick bar chart.


Attachments:
TickCountStrategy.java [3.42 KiB]
Downloaded 364 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: Get number of ticks processed in current TickBar period Post rating: 0   New post Posted: Wed 31 Aug, 2011, 16:21 

User rating: 1
Joined: Tue 12 Jul, 2011, 20:43
Posts: 51
Location: Germany,
Generally it works now, but I inserted the following two code lines at line 38 (in the onBar() method of ITickBarFeedListener, before the statement: label = null;) to force deletion of old labels:
...
if (chart != null && label != null)
    chart.remove(label);
...

Regards
AR


 

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