Hello,
I trade off a 40 tick chart. I'm trying to code a strategy that displays the number of ticks remaining in the current bar.
I've tried to do this by keeping a counter for each new bar, which is then subtracted from on each tick. However, I've found that the start time of each bar changes even when a new bar has not been displayed. This results in the counter being reset on a random basis, often 2 or 3 times per bar.
Is anyone able to help me code this functionality?
Here is my current code...
package jforex;
import com.dukascopy.api.*;
import java.awt.*;
public class TickCounter implements IStrategy {
private IContext context;
private Instrument[] instruments;
private int[] subscribed;
int tickCount;
long barTime;
public void onStart(IContext context) throws JFException {
this.context = context;
instruments = Instrument.values();
subscribed = new int[instruments.length];
}
public void onAccount(IAccount account) throws JFException {
}
public void onMessage(IMessage message) throws JFException {
}
public void onStop() throws JFException {
// garbage collect
for (int i = 0; i < subscribed.length; i++){
if (subscribed[i] == 1){
IChart chart = context.getChart(instruments[i]);
chart.remove("Ticks");
}
}
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) {
if (askBar.getTime() != barTime)
{
barTime = askBar.getTime();
tickCount = 40;
}
}
public void onTick(Instrument instrument, ITick tick) throws JFException {
IChart chart = context.getChart(instrument);
if (chart != null){
subscribed[instrument.ordinal()] = 1;
double _ask = tick.getAsk();
long _time = tick.getTime();
long _time_shift = 2 * chart.getSelectedPeriod().getInterval();
tickCount--;
IChartObject ticks = chart.get("Ticks");
if (ticks != null) {
ticks.setText(Double.toString(tickCount));
ticks.move(_time + _time_shift, _ask);
}
else {
ticks = chart.draw("Ticks", IChart.Type.LABEL, 0, 0);
ticks.setColor(Color.WHITE);
ticks.setText(Double.toString(tickCount), new Font(Font.SANS_SERIF, Font.BOLD, 12));
ticks.move(_time + _time_shift, _ask);
}
}
}
}