Console

The IConsole interface provides an access to JForex API console. IConsole interface contains five methods, which return PrintStream class object:

  • getOut() - informational output that is displayed on a white background.
  • getErr() - error output that is displayed on a red background.
  • getWarn() - warning output that is displayed on a yellow background.
  • getInfo() - info output that is displayed on a green background.
  • getNotif() - notification output that is displayed on a blue background.

Print last ask price

For instance, to display instrument name and instrument ask price, one can do the following:

private IConsole console;
public void onStart(IContext context) throws JFException {
    this.console = context.getConsole();
}
public void onTick(Instrument instrument, ITick tick) throws JFException {
    console.getOut().println("Instrument:" + instrument + " ask price:" + tick.getAsk());
}

Print formatted numbers

Consider using PrintStream.format for elaborate decimal value logging. Consider an example strategy which logs prices differences both in absolute values and in pips:

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

    ITick tick0 = history.getLastTick(instrument);
    ITick tick10 = history.getTick(instrument, 10);
    double priceDiff = tick0.getBid() - tick10.getBid();
    double spread0 = tick0.getAsk() - tick0.getBid();
    double priceDiffPips = priceDiff / instrument.getPipValue();
    double spreadPips = spread0 / instrument.getPipValue();
    console.getOut().format(
            "0th tick=%s, 10th tick=%s \n price difference between last and 10th tick=%.5f (%.1f pips) \n last spread=%.5f (%.1f pips)"
            ,tick0, tick10, priceDiff, priceDiffPips, spread0, spreadPips).println();

}

PrintPriceDifference.java

Use different console colors

Consider using different color print streams:

public void onStart(IContext context) throws JFException {
    IConsole c = context.getConsole();
    c.getOut().println("out test");
    c.getErr().println("err test");
    c.getInfo().println("info test");
    c.getWarn().println("warn test");
    c.getNotif().println("notif test");
    System.out.println("System.out test");
}

Note that getInfo(), getWarn(), getNotif() when ran outside the jClient platform will have the same output style as getOut().

Use HTML and CSS

It is also possible to customize the style of your outputs by using plain HTML or by combining HTML with CSS

console.getOut().рrintln("<htмl><font bgcolor=\"yellow\" color=\"blue\" ><b>HTML test output</b></font>");
console.getOut().рrintln("<htмl><div style=\"background-color:red;width:200px;text-decoration:underline\">CSS test output</div>");

Note the style only applies in jClient platform.

Logging values

The following example strategy demonstrates how to log the most commonly used values and arrays of values in in JForex API:

  • a double value,
  • a time value,
  • an Object (given that it has an overridden toString method),
  • an array of objects,
  • a 2-dimensional array of double,
  • a 1-dimensional array of double.
package jforex.logging;

import java.util.Arrays;
import java.util.List;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators.MaType;
import com.dukascopy.api.util.DateUtils;

/**
 * The following strategy demonstrates how to log the most commonly used 
 * values and arrays of values in in JForex API 
 *
 */
public class LoggingValues implements IStrategy {

    private IConsole console;
    private IHistory history;
    private IIndicators indicators;

    @Override
    public void onStart(IContext context) throws JFException {
        console = context.getConsole();
        history = context.getHistory();
        indicators = context.getIndicators();

        IBar prevBar = history.getBar(Instrument.EURUSD, Period.TEN_SECS, OfferSide.BID, 1);
        long time = prevBar.getTime();

        double max = indicators.max(Instrument.EURUSD, Period.TEN_SECS, OfferSide.BID, AppliedPrice.CLOSE, 20, 1);        
        //log single double value; time value; object (given that it has an overridden toString method)
        console.getOut().format("single-value max indicator result: %.5f previous bar time=%s previous bar=%s", max, DateUtils.format(time), prevBar).println();

        //log 2-dimensional array
        double[][] bbands = indicators.bbands(Instrument.EURUSD, Period.TEN_SECS, OfferSide.BID, AppliedPrice.CLOSE, 
                20, 5, 4, MaType.SMA, Filter.NO_FILTER, 5, time, 0);
        print("2-dimensional bbands indicator result array: " + arrayToString(bbands));

        //log 1-dimensional array
        double[] maxArr = indicators.max(Instrument.EURUSD, Period.TEN_SECS, OfferSide.BID, AppliedPrice.CLOSE, 
                20, Filter.NO_FILTER, 5, time, 0);
        print("1-dimensional max indicator result array: " + arrayToString(maxArr));

        List<IBar> bars = history.getBars(Instrument.EURUSD, Period.TEN_SECS, OfferSide.BID, Filter.NO_FILTER, 5, time, 0);
        //log indexed array of objects, unindexed array of objects, list of objects
        console.getOut().format("previous bars \n as indexed array: %s \n as unindexed array: %s \n as list: %s", 
                arrayToString(bars.toArray()), Arrays.toString(bars.toArray()), bars).println();

        context.stop();
    }

    private void print(Object o){
        console.getOut().println(o);
    }

    public static String arrayToString(double[] arr) {
        StringBuilder sb = new StringBuilder();
        for (int r = 0; r < arr.length; r++) {
            sb.append(String.format("[%s] %.5f; ",r, arr[r]));
        }
        return sb.toString();
    }

    public static String arrayToString(Object[] arr) {
        StringBuilder sb = new StringBuilder();
        for (int r = 0; r < arr.length; r++) {
            sb.append(String.format("[%s] %s; ",r, arr[r]));
        }
        return sb.toString();
    }

    public static String arrayToString(double[][] arr) {
        StringBuilder sb = new StringBuilder();
        for (int r = 0; r < arr.length; r++) {
            for (int c = 0; c < arr[r].length; c++) {
                sb.append(String.format("[%s][%s] %.5f; ",r, c, arr[r][c]));
            }
            sb.append("; ");
        }
        return sb.toString();
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {}
    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
    @Override
    public void onMessage(IMessage message) throws JFException {}
    @Override
    public void onAccount(IAccount account) throws JFException {}
    @Override
    public void onStop() throws JFException {}

}

LoggingValues.java

The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.