package charts.test;
import java.util.*;
import javax.swing.SwingConstants;
import com.dukascopy.api.*;
import com.dukascopy.api.drawings.*;
public class InteractiveRectangleDrawer implements IStrategy {
private IConsole console;
private IHistory history;
private IChart chart;
private Instrument instrument = Instrument.EURUSD;
private Period period = Period.TEN_SECS;
public void onStart(IContext context) throws JFException {
this.console = context.getConsole();
this.history = context.getHistory();
chart = context.getChart(instrument);
}
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 { }
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if (chart == null || !period.equals(this.period) || !instrument.equals(this.instrument))
return;
IBar prevBar = history.getBar(instrument, this.period, OfferSide.BID, 2);
InteractiveRectangle rectangle = new InteractiveRectangle(
"click herev",
prevBar.getTime(),
prevBar.getClose(),
bidBar.getTime(),
bidBar.getClose()
);
rectangle.addToChart();
}
private String getKey(String str) {
return str + UUID.randomUUID().toString().replace('-', '0');
}
private void print(String message) {
console.getOut().println(message);
}
class InteractiveRectangle {
private ILabelChartObject label;
private IRectangleChartObject rectangle;
public InteractiveRectangle( String caption, long time1, double price1, long time2, double price2 ) {
IChartObjectFactory factory = chart.getChartObjectFactory();
rectangle = factory.createRectangle(
getKey("rectangle"),
time1,
price1,
time2,
price2
);
label = factory.createLabel(
getKey("label"),
getLabelTime(),
getLabelPrice()
);
label.setText(caption, SwingConstants.LEFT);
rectangle.setChartObjectListener(new ChartObjectAdapter() {
@Override
public void deleted(ChartObjectEvent e) {
print("オブジェクト削除:キー = " + rectangle.getKey());
chart.remove(label);
}
@Override
public void selected(ChartObjectEvent e) {
print("オブジェクト選択:キー = " + rectangle.getKey());
}
@Override
public void moved(ChartObjectEvent e) {
label.setPrice(0, getLabelPrice());
label.setTime(0, getLabelTime());
}
});
}
public void addToChart() {
chart.addToMainChartUnlocked(rectangle);
chart.addToMainChartUnlocked(label);
}
private long getLabelTime(){
return (rectangle.getTime(1) - rectangle.getTime(0))/2 + rectangle.getTime(0);
}
private double getLabelPrice(){
return (rectangle.getPrice(1) - rectangle.getPrice(0))/2 + rectangle.getPrice(0);
}
}
}