Drawing objects in the chart window on JForex platform (Dukascopy)

We often want to draw various types of objects (lines, texts) in the chart window – for instance to draw a horizontal line at the specified price level or display the current spread. Then, the best choice is to write a short strategy, which will draw such objects for us.

At the beginnig we have to get the chart, where we want to draw the object. If we want the EURUSD chart, we should write:

IChart chart = context.getChart(Instrument.EURUSD);

This method return the arbitrarily selected chart to the specified instrument. If you have several opened charts with the same instrument, method return one of them, but always the same. Unfortunately we can not decide which chart will be returned.

You can also return all the charts of the specified instrument by the method:

IContext.getCharts(Instrument.EURUSD)

or even all the opened charts on the platform independent of the instrument by the method:

IContext.getCharts()

Now we can call on the chart object method:

IChart.getChartObjectFactory()

which return object of IChartObjectFactory on which we can call method which create the selected object on the chart. For example, to draw a horizontal line in the onTick method just write:

IChartObject bidLine = chart.getChartObjectFactory().createHorizontalLine("HorizontalLine", tick.getBid());

as a parameters, depending on the type of object, we put (key_name,  chart coordinates (time, price))

Now we can also change the line color:

bidLine.setColor(Color.GRAY);

We can call many other methods the IChartObject. Full description is at IChartObject

Below, I present a code of a strategy, which draws horizontal lines for ask and bid prices and shows the current spread (the difference between ask and bid prices) in chart with choosen instrument:

package fxcraft;
import java.util.*;
import java.text.*;
import java.awt.*;
import com.dukascopy.api.*;

public class FxCraftAskBidLines implements IStrategy {
private IContext context;

@Configurable("Instrument:") 
public Instrument instrumentThis = Instrument.EURUSD; 
@Configurable("spread is visible:")
public boolean spreadVisible = true;
@Configurable("lines are visible:")
public boolean linesVisible = true;

public void onStart(IContext context) throws JFException {
this.context = context;
}

public void onAccount(IAccount account) throws JFException {
}

public void onMessage(IMessage message) throws JFException {
}

public void onStop() throws JFException {
IChart chart = context.getChart(instrumentThis);
if(chart != null){
chart.remove("BidLine");
chart.remove("AskLine");
chart.remove("SpreadABL");
}
}

public void onTick(Instrument instrument, ITick tick) throws JFException {
if(instrument!=instrumentThis) return;
IChart chart = context.getChart(instrument);
if (chart != null){
long nowTime = tick.getTime();
double tickBid = tick.getBid();
double tickAsk = tick.getAsk();
double pipValue = instrument.getPipValue();
double spread = NormalizeDouble((tickAsk - tickBid)/pipValue, 1);
long timeInterval = chart.getSelectedPeriod().getInterval();
if(timeInterval<=0) timeInterval=5000;

if(linesVisible){
IChartObject bidLine = chart.getChartObjectFactory().createHorizontalLine("BidLine", tickBid);
bidLine.setVisibleInWorkspaceTree(false);
bidLine.setColor(Color.GRAY);
chart.add(bidLine);

IChartObject askLine = chart.getChartObjectFactory().createHorizontalLine("AskLine", tickAsk);
askLine.setVisibleInWorkspaceTree(false);
askLine.setColor(Color.GRAY);
chart.add(askLine);
}else{
IChart chart1 = context.getChart(instrumentThis);
if(chart1 != null){
chart1.remove("BidLine");
chart1.remove("AskLine");
}
}
if(spreadVisible){
IChartObject spreadNumber = chart.getChartObjectFactory().createLabel("SpreadABL", nowTime + timeInterval, tickBid);
spreadNumber.setVisibleInWorkspaceTree(false);
spreadNumber.setColor(Color.BLUE);
spreadNumber.setText(Double.toString(spread), new Font(Font.SANS_SERIF, Font.BOLD, 12));
chart.add(spreadNumber);
}else{
IChart chart2 = context.getChart(instrumentThis);
if(chart2 != null){
chart2.remove("SpreadABL");
}
}
}
}

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

double NormalizeDouble(double operand, int miejcsa) {
String wzor = "0.0";
for(int i=1;i<miejcsa;i++){
if(i==miejcsa-1) wzor += "#";
else wzor += "0";
}
DecimalFormat applydeci = new DecimalFormat(wzor);
Double result = new Double(applydeci.format(operand).replace(',', '.')).doubleValue();
return(result);
}
}

Share with your friends:

Send us your comments

Name (required)

Email (required)

Message

  • Facebook