2013年9月23日月曜日

Client action on order fill and close

Client action on order fill and close
オーダー記入とクローズする上でのクライアントアクション

Consider that from the program you wish to do some extra logging on both order fill and close.
オーダー記入とクローズの両方において、拡張的ロギングが必要ならば、プログラム上で行うことを検討して欲しい。
And also you wish to set order stop loss:
また、あなたはストップロスオーダーを望むだろう:
client.startStrategy(new StrategyPublicMethods(new StrategyPublicMethods.ClientActions() {
                       
    @Override
    public void onOrderFill(IOrder order) {
        LOGGER.info("Order filled, execute here some logic on client side, say set stop loss if the order is long");
        if(order.isLong()){
            try {
                order.setStopLossPrice(order.getOpenPrice() - order.getInstrument().getPipValue() * 10);
            } catch (JFException e) {
                e.printStackTrace();
            }
        }
    }
                       
    @Override
    public void onOrderClose(IOrder order) {
        LOGGER.info("Order closed, execute here some logic on client side");                                  
    }
}));
Consider introducing an interface in a strategy which would serve as an order fill and close listener.
オーダー記入とクローズの助けとして、ストラテジの中でインタフェイスの導入を検討して欲しい。
Implementation of the interface would get passed from the IClient program:
インタフェイスの実装はIClientプログラムから渡されるだろう。
public class StrategyPublicMethods implements IStrategy {
       
    private IConsole console;
    private IEngine engine;
    private StrategyPublicMethods.ClientActions clientActions;
   
    public interface ClientActions {
        void onOrderClose(IOrder order);
        void onOrderFill(IOrder order);
    }
   
    //for the launch from standalone
    public StrategyPublicMethods (StrategyPublicMethods.ClientActions clientActions){
        this.clientActions = clientActions;
    }

    //...
}
Then on every order fill and close execute the logic that has been passed from the IClient program:
そして、全てのオーダー記入とクローズ時、IClientプログラムから渡されたロジックが実行される。 
@Override
public void onMessage(IMessage message) throws JFException {
    if(message.getType() == IMessage.Type.ORDER_FILL_OK){
        clientActions.onOrderFill(message.getOrder());
    }
    if(message.getType() == IMessage.Type.ORDER_CLOSE_OK){
        clientActions.onOrderClose(message.getOrder());
    }
}

2013年9月21日土曜日

Program communicating with a strategy

Program communicating with a strategy
ストラテジとしてのプログラム・コミュニケイティング

By defining a listener (in the means of public interface) within a strategy one can implement some client side logic on strategy events.
ストラテジの中でリスナー (パブリックインターフェイスの意味で)を定義することにより、一つのストラテジ・イベントにいくつかのクライアント側のロジックを実装することができる。



2013年9月18日水曜日

Change chart theme

Change chart theme
チャートテーマの変更

Note: Available with JForex-API 2.7.9
注意:JForex-API 2.7.9にて有効
IChartTheme represents a chart theme, it can be retreived and set to a chart by using the getTheme and setTheme methods of the IClientChartPresentationManager interface.
IChartThemeはチャートテーマを表示するものであり、IClientChartPresentationManagerインタフェイスのgetThemeメソッドとsetThemeメソッドの使用により取り出され、チャートにセットすることができる。
Consider changing chart's background and tick/candle colors:
チャートの背景やティック/ローソク足の色の変更を検討して欲しい。
chartPresentationManager.setTheme(
    chartPresentationManager.getTheme()
        .setName("Custom tick/candle color theme")
        .setColor(ColoredElement.BACKGROUND, new Color(254, 244, 214))
        .setColor(ColoredElement.CANDLE_BEAR, Color.CYAN)
        .setColor(ColoredElement.CANDLE_BULL, Color.ORANGE)
        .setColor(ColoredElement.ASK, Color.YELLOW.darker())
        .setColor(ColoredElement.BID, Color.PINK.darker())
);
One can retrieve a predefined theme by calling the IClientChartPresentationManager.getPredefinedTheme method.
IClientChartPresentationManager.getPredefinedThemeメソッドのコールにより、あらかじめ定義されたテーマを取り出すことができる。
Consider setting a predefined theme to a chart:
あらかじめ定義されたテーマをチャートにセッティングすることを検討して欲しい。
chartPresentationManager.setTheme(chartPresentationManager.getPredefinedTheme(IChartTheme.Predefined.BLUE_BLACK_ON_GRAY));
For full usage example look up the cmbChartTheme combo box in:
全ての使用方法の例をcmbChartThemeコンボボックスで検索できる。 

2013年9月15日日曜日

Add chart objects

Add chart objects
チャートオブジェクトの追加

One can not only plot chart objects from within a strategy, but also from the program running IClient.
ストラテジの中からチャートオブジェクトをプロットできるだけではなく、IClient実行中のプログラムからも可能である。
Consider creating a button panel which receives an IChart from IClientGUI.getChart():
IClientGUI.getChart()からIChartを受信するボタンパネルの作成を検討して欲しい。 

@SuppressWarnings("serial")
private class ChartObjectPanel extends JPanel {
   
    private final IChart chart;
    private ChartObjectPanel(IChart chart){
        this.chart = chart;
        addButtons();
    }
   
    private void addButtons(){          
        JButton btnVLine = new JButton("Add VLine");
        btnVLine.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                //draw the line at the time of the last drawn feed element on the chart
                ITimedData[] chartData = chart.getLastRequestedData();
                long time = chartData[chartData.length - 1].getTime();
                IChartObject obj = chart.getChartObjectFactory().createVerticalLine("vLine", time);
                obj.setColor(Color.RED);
                chart.add(obj);
            }});
        add(btnVLine);          
        JButton btnHLine = new JButton("Add HLine");
        btnHLine.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {  
                //draw the line in the chart price axis midpoint
                double price = chart.getMinPrice() + (chart.getMaxPrice() - chart.getMinPrice()) / 2;
                System.out.println(String.format("%.5f", price));                  
                IChartObject obj = chart.getChartObjectFactory().createHorizontalLine("hLine", price);
                obj.setColor(Color.GREEN);
                chart.add(obj);
            }});
        add(btnHLine);
    }
   
}

Handle IContext.openChart

Handle IContext.openChart
IContext.openChartのハンドル

Consider a program which opens a chart whenever the strategy calls IContext.openChart and closes a chart whenever the strategy call IContext.closeChart:
ストラテジがIContext.openChartをコールするごとにチャートを開くか、ストラテジがIContext.closeChartをコールするごとにチャートを閉じるか、ということをプログラムとして検討して欲しい。

client.addClientGUIListener(new IClientGUIListener() {  
    @Override
    public void onOpenChart(final IClientGUI clientGUI) {
        LOGGER.info("Chart opened from a startegy " + clientGUI.getChart().getFeedDescriptor());
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ChartFrame frame = new ChartFrame(clientGUI, client.getSubscribedInstruments());
                chartFrameMap.put(clientGUI.getChart(), frame);
                //Handle manual close - we need to call IClient.closeChart for strategy to know that the chart is no more there
                frame.addWindowListener(new WindowAdapter(){
                    public void windowClosing(WindowEvent e) {
                        LOGGER.info("Chart manually closed, removing the chart from the strategy context");
                        client.closeChart(clientGUI.getChart());
                        updateOnClose(clientGUI.getChart());
                    }
                });
            }
        });
    }
 
    @Override
    public void onCloseChart(IChart chart) {
        LOGGER.info("Chart closed from a startegy " + chart.getFeedDescriptor());
        //we need to take care of closing the frame ourselves in gui
        ChartFrame frame = chartFrameMap.get(chart);
        frame.dispose();
        updateOnClose(chart);
    }
 
    private void updateOnClose(IChart chart){
        chartFrameMap.remove(chart);
        if(chartFrameMap.isEmpty()){
            LOGGER.info("All charts closed, stopping the program");
            System.exit(0);
        }
    }
});

2013年9月14日土曜日

Open a chart from IClient

Open a chart from IClient
IClientからチャートを開く。

Note: Available with JForex-API 2.7.1
注意:JForex-API 2.7.1として有効。
Consider opening multiple charts - for each instrument in an array instrArr:
複数チャートを開く場合を検討 - instrArr配列の中でfor each命令ループを使用。
for(Instrument instrument : instrArr){
    IFeedDescriptor feedDescriptor = new TicksFeedDescriptor(instrument);
    feedDescriptor.setOfferSide(OfferSide.BID);// need to set due to platform requirements
    IChart chart = client.openChart(feedDescriptor);
    final IClientGUI clientGUI = client.getClientGUI(chart);
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            ChartFrame frame = new ChartFrame(clientGUI, client.getSubscribedInstruments());
            chartFrameMap.put(clientGUI.getChart(), frame);
            //Handle manual close - we need to call IClient.closeChart for strategy to know that the chart is no more there
            frame.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    LOGGER.info("Chart manually closed, removing the chart from the strategy context");
                    client.closeChart(clientGUI.getChart());
                    chartFrameMap.remove(clientGUI.getChart());
                    if(chartFrameMap.isEmpty()){
                        LOGGER.info("All charts closed, stopping the program");
                        System.exit(0);
                    }
                }
            });
        }
    });
}

2013年9月13日金曜日

Work with charts

Work with charts
チャートとしての機能

There are two ways how one can open a chart:
チャートを開くことができる方法が2つある:
IClient.openChart is used to open a chart without running a strategy.
IClient.openChartはストラテジを実行せずチャートを開くのに使われる。
IClient.addClientGUIListener adds a listener for the program to handle IContext.openChart and IContext.closeChart events.
IClient.addClientGUIListenerはプログラムにリスナを追加し、IContext.openChartとIContext.closeChartイベントをハンドルする。

2013年9月8日日曜日

Stopping strategy

Stopping strategy
ストラテジの停止

One retrieves strategy process id from IClient.startStrategy, which afterwards can be used to stop the strategy by using the IClient.stopStrategy method.
IClient.startStrategyからストラテジのプロセスIDを取得した後、IClient.stopStrategyメソッドを使うことで、ストラテジの停止が可能である。
Consider a program which starts an anonymous strategy and checks every second if the user has typed in the console "stop", if so then the program stops the strategy.
プログラムでストラテジを停止するならば、匿名のストラテジを開始して、毎秒ユーザがコンソールに"stop"とタイプするかをチェックするプログラムを検討してほしい。

final long strategyId = client.startStrategy(new IStrategy(){
    public Instrument instrument = Instrument.EURUSD;
    private IConsole console;

    public void onStart(IContext context) throws JFException {      
        console = context.getConsole();  
    }
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if ( instrument == this.instrument){
            console.getOut().println(" bar: " + period  + " " + askBar);
        }
    }
    public void onTick(Instrument instrument, ITick tick) throws JFException {    }
    public void onMessage(IMessage message) throws JFException {    }
    public void onAccount(IAccount account) throws JFException {    }
    public void onStop() throws JFException {    }
});
//now it's running

//every second check if "stop" had been typed in the console - if so - then stop the strategy
Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {              
        Scanner s = new Scanner(System.in);                
        while(true){
            while(s.hasNext()){
                String str = s.next();
                if(str.equalsIgnoreCase("stop")){
                    System.out.println("Strategy stop by console command.");
                    client.stopStrategy(strategyId);
                    break;
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    });
thread.start();
MainStopFromConsole.java

2013年9月7日土曜日

Running strategies

Running strategies
ストラテジの実行

One runs a strategy by passing it to IClient.startStrategy, for instance:
インスタンスとして、IClient.startStrategyへストラテジを一つ渡して実行する。
client.startStrategy(new MA_Play());

One might also run multiple strategies.
複数のストラテジを実行するかもしれない。
For instance, one might run two instances of the same strategy but with different parameters:
インスタンスとして、2つの同じだがパラメータの異なるストラテジを実行するかもしれない。
StrategySimple strategy1 = new StrategySimple();
StrategySimple strategy2 = new StrategySimple();
strategy1.amount = 0.01;
strategy1.stopLossPips = 20;
strategy1.takeProfitPips = 10;
strategy2.amount = 0.02;
strategy2.stopLossPips = 60;
strategy2.takeProfitPips = 60;
     
client.startStrategy(strategy1);
client.startStrategy(strategy2);

2013年9月6日金曜日

Subscribing to instruments

Subscribing to instruments
インスツルメンツへのサブスクライブ

In contrary to JForex client, in Standalone API one always has to explicitly subscribe to all instruments that his strategy is going to use.
JForexクライアントに反し、スタンドアロンAPIの一つは、常に明示的に、ストラテジの使用予定として、全インスツルメントをサブスクラブする必要がある。

This can be done by using the setSubscribedInstruments method.
これは、setSubscribedInstrumentsメソッドを用いて行うことができる。

Note that the subscription is asynchronous, thus if the in it is advised to subscribe in the following way:
サブスクリプションが非同期であることに注意すべき、それゆえ、その中にあるならば、それは以下のようにしてサブスクライブするよう助言される:

2013年9月4日水曜日

System listener

System listener
システムリスナ

ISystemListener interface allows the user to execute some business logic on system connects and disconnects as well as on start and stop of every strategy.
ISystemListenerインタフェイスは、全ストラテジの開始や終了と同様、システムの接続や切断を行う際に、幾つかのビジネスロジックの実行を可能にする。 

System listener gets added to IClient by using the setSystemListener method, for instance:
システムリスナは、setSystemListenerメソッドを使うことで、IClientインスタンスへ追加される。

2013年9月3日火曜日

IClient functionality

IClient functionality
IClientの機能性

IClient interface is used for working with live data and ITesterClient inherits its functionality for working with historical data.
IClientインタフェイスはライブデータを操作するために使用され、ITesterClientは履歴データを操作するために、その機能を継承している。
One gets an instance of IClient by using ClientFactory.getDefaultInstance().
ClientFactory.getDefaultInstance()を使うことでIClientのインスタンスを取得する。
One connects to dukascopy servers by using the connect method, for example:
例題のように、connectメソッドを使うことで、dukascopyサーバに接続する。
client.connect("https://www.dukascopy.com/client/demo/jclient/jforex.jnlp", "username", "password");

2013年9月2日月曜日

JForex SDK

JForex理解度向上の為、SDKドキュメントを翻訳。
JForex SDK
http://www.dukascopy.com/wiki/#JForex_SDK

JForex Software Development Kit (JForex SDK) allows the user to use JForex API without JForex client.
JForexソフトウェア開発キット (JForex SDK)は、JForexクライアントを使うことなく、JForex APIの使用を可能にする。
Most prominently it enables the user to run his strategies from a custom java application.
最も顕著に有効なのは、カスタムJavaアプリケーションから自身のストラテジの実行を可能にすることだ。
One uses the IClient interface for working with live data and ITesterClient for working with historical data.
まず、ライブデータを操作するにはIClientインターフェイス過去データを扱うにはITesterClientのどちらか一つを使用する
Also JForex SDK provides interfaces for working with charts outside JForex client, find examples in sections Work with charts and Backtesting in GUI mode.
またJForex SDKは、JForexクライアント外部でのチャート操作として機能するインタフェイスを提供しており、Work with chartsセクションやBacktesting in GUI modeセクションで例題を見つけて欲しい。
Go to Use in Eclipse or Use in NetBeans to download the SDK with examples and get started.
例題としてSDKをダウンロードするのに、Use in Eclipse、又はUse in NetBeansに向かい、すぐに始めて欲しい。