Here are few practical examples in strategies context!
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.IUserInterface;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;
public class FullMarketDepth implements IStrategy {
private static final String TAB_NAME = "Full Market Depth";
private IContext context;
private IUserInterface userInterface;
private MarketDepthTableModel tableModel;
private JTable table;
public void onStart(IContext context) throws JFException {
this.context = context;
userInterface = context.getUserInterface();
placeControlsOnTab(context);
}
public void onAccount(IAccount account) throws JFException {
}
public void onMessage(IMessage message) throws JFException {
}
public void onStop() throws JFException {
userInterface.removeBottomTab(TAB_NAME);
}
public void onTick(Instrument instrument, final ITick tick) throws JFException {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
setVolumes(tick.getAsks(), tick.getAskVolumes(), tick.getBids(), tick.getBidVolumes());
}
});
} catch (Exception e) {
context.getConsole().getOut().println(e);
}
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
}
private void placeControlsOnTab(IContext context) {
JPanel mainPanel = userInterface.getBottomTab(TAB_NAME);
//mainPanel.setLayout(new BorderLayout());
tableModel = new MarketDepthTableModel();
table = new JTable(tableModel);
mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
}
public void setVolumes(double[] asks, double[] askVols, double[] bids, double[] bidVols) {
double[][] data = new double[asks.length > bids.length ? asks.length : bids.length][4];
for (int i = 0; i < asks.length; i++) {
data[i][3] = askVols[i] / 1000000;
data[i][2] = asks[i];
}
for (int i = 0; i < bids.length; i++) {
data[i][0] = bidVols[i] / 1000000;
data[i][1] = bids[i];
}
tableModel.setData(data);
}
}
class MarketDepthTableModel extends AbstractTableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
private double[][] data = new double[0][0];
public void setData(double[][] data) {
this.data = data;
fireTableDataChanged();
}
public int getRowCount() {
return data.length;
}
public int getColumnCount() {
return 4;
}
public Object getValueAt(int row, int column) {
if (data[row][column] == 0.0) {
return "";
} else {
return Double.toString(data[row][column]);
}
}
public String getColumnName(int column) {
switch (column) {
case 0:
return "Vol";
case 1:
return "Bid";
case 2:
return "Ask";
case 3:
return "Vol";
default:
return "";
}
}
}
//////////////////////////////////////////////////////////
import java.util.*;
import com.dukascopy.api.*;
public class MarketDepth_V1 implements IStrategy {
private IConsole console;
private IContext context;
public void onStart(IContext context) throws JFException {
context.getEngine();
this.console = context.getConsole();
context.getHistory();
this.context = context;
context.getIndicators();
context.getUserInterface();
Set<Instrument> instruments = new HashSet<Instrument>();
instruments.add(Instrument.EURUSD);
this.context.setSubscribedInstruments(instruments);
}
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.EURUSD.equals(instrument)) {
return;
}
console.getOut().print("ask ");
for(double volume : tick.getAskVolumes()) {
console.getOut().print(volume+ ", ");
}
console.getOut().println();
console.getOut().print("bid ");
for(double volume : tick.getBidVolumes()) {
console.getOut().print(volume+ ", ");
}
console.getOut().println();
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
}
}
////////////////////////////
package jforex.indicators;
import com.dukascopy.api.indicators.*;
import java.awt.*;
import com.dukascopy.api.*;
/**
* Created by: chriz aka Indiana Pips
* Review: Mar 12, 2011
* email: puntasabbioni/AT/gmail.com
*/
public class AskBidVolumeIndiPrice2 implements IIndicator {
private IndicatorInfo indicatorInfo;
private InputParameterInfo[] inputParameterInfos;
private OptInputParameterInfo[] optInputParameterInfos;
private OutputParameterInfo[] outputParameterInfos;
private double[][] iBidVolumes, iAskVolumes;
private double[][] outputs = new double[3][];
private IIndicatorContext context;
public void onStart(IIndicatorContext context) {
this.context = context;
indicatorInfo = new IndicatorInfo("AskBidVolumeIndiPrice", "Ask+Bid Volume Indi Price", "AskBid", false, false, true, 2, 0, 3);
inputParameterInfos = new InputParameterInfo[] { new InputParameterInfo("Input data Ask", InputParameterInfo.Type.PRICE) {
{
setOfferSide(OfferSide.ASK);
}
}, new InputParameterInfo("Input data Bid", InputParameterInfo.Type.PRICE) {
{
setOfferSide(OfferSide.BID);
}
} };
optInputParameterInfos = new OptInputParameterInfo[] { new OptInputParameterInfo("MA Period", OptInputParameterInfo.Type.OTHER,
new IntegerRangeDescription(100, 2, 1000, 1)) };
outputParameterInfos = new OutputParameterInfo[] {
new OutputParameterInfo("Ask+Bid", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.HISTOGRAM) {
{
setColor(Color.BLUE);
}
},
new OutputParameterInfo("Ask Vol > Bid Vol ", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.HISTOGRAM) {
{
setColor(Color.GREEN);
}
},
new OutputParameterInfo("Bid Vol > Ask Vol ", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.HISTOGRAM) {
{
setColor(Color.RED);
}
} };
}
public IndicatorResult calculate(int startIndex, int endIndex) {
double currentAskVolume = 0, currentBidVolume = 0;
if (startIndex - getLookback() < 0) {
startIndex -= startIndex - getLookback();
}
if (startIndex > endIndex) {
return new IndicatorResult(0, 0);
}
int i, j;
for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
if(i >= iAskVolumes[4].length || i >= iBidVolumes[4].length){ //not enough data for calculation on the given candle
continue;
}
//InputParameterInfo.Type.PRICE consists of 5 arrays open, close, high, low, volume -> volume is with index 4
currentAskVolume = iAskVolumes[4][i];
currentBidVolume = iBidVolumes[4][i];
outputs[0][j] = (currentAskVolume + currentBidVolume);
if (currentAskVolume >= currentBidVolume) {
outputs[1][j] = -(currentAskVolume - currentBidVolume);
outputs[2][j] = Double.NaN;
} else if (currentAskVolume < currentBidVolume) {
outputs[2][j] = -(currentBidVolume - currentAskVolume);
outputs[1][j] = Double.NaN;
}
}
return new IndicatorResult(startIndex, j);
}
public IndicatorInfo getIndicatorInfo() {
return indicatorInfo;
}
public InputParameterInfo getInputParameterInfo(int index) {
if (index <= inputParameterInfos.length) {
return inputParameterInfos[index];
}
return null;
}
public int getLookback() {
return 5;
}
public int getLookforward() {
return 0;
}
public OptInputParameterInfo getOptInputParameterInfo(int index) {
if (index <= optInputParameterInfos.length) {
return optInputParameterInfos[index];
}
return null;
}
public OutputParameterInfo getOutputParameterInfo(int index) {
if (index <= outputParameterInfos.length) {
return outputParameterInfos[index];
}
return null;
}
public void setInputParameter(int index, Object array) {
switch (index) {
case 0:
iAskVolumes = (double[][]) array;
break;
case 1:
iBidVolumes = (double[][]) array;
break;
default:
throw new ArrayIndexOutOfBoundsException(" setInputParameter(). Invalid index: " + index);
}
}
public void setOptInputParameter(int index, Object value) {
}
public void setOutputParameter(int index, Object array) {
outputs[index] = (double[]) array;
}
private void print(String sss) {
context.getConsole().getOut().println(sss);
}
}
////////////////////////////////////////////////////////////////