2013年9月15日日曜日

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インスタンスへ追加される。