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

0 件のコメント:

コメントを投稿