GetBarsExample.java

注意事項:
サンプルソースコードには実際にオーダーを発注するものがあります。
サンプルソースコードのストラテジーを起動する場合は、注意して下さい。



// Copyright (c) 2009 Dukascopy (Suisse) SA. All Rights Reserved.
package jforex.data;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.text.SimpleDateFormat;

import com.dukascopy.api.*;

// 10秒足のBidチャートから、過去10本目のバーから最新のバーまでのヒストリカルデータを取得します。

public class GetBarsExample implements IStrategy {
    private IContext context;
    private IConsole console;
    private IHistory history;

    public void onStart(IContext context) throws JFException {
        this.context = context;
        console = context.getConsole();
        history = context.getHistory();
        context.setSubscribedInstruments(java.util.Collections.singleton(Instrument.EURUSD), true);
    }

    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 {
        console.getOut().println("バーをロードし、ファイルへ保存中です。");

        long prevBarTime = history.getPreviousBarStart(
                                            Period.TEN_SECS,
                                            tick.getTime()
                                           );
        List<IBar> bars = history.getBars(
                                           instrument,                                                     // 通貨ペア
                                           Period.TEN_SECS,                                                // 時間軸
                                           OfferSide.BID,                                                  // オファーサイド
                                           history.getTimeForNBarsBack(Period.TEN_SECS, prevBarTime, 10),  // 取得開始時刻
                                           prevBarTime                                                     // 取得終了時刻
                                          );

        File dirFile = context.getFilesDir();
        if (!dirFile.exists()) {
            console.getErr().println("エラー:Strategiesフォルダ内にfilesフォルダを作成して下さい。");
            context.stop();
        }

        File file = new File(dirFile, "last10bars.txt");
        console.getOut().println("ファイル書き出し中 " + file);

        try {
            PrintWriter pw = new PrintWriter(file);
            SimpleDateFormat temp_sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");

            for (IBar bar : bars) {
                pw.println(
                           temp_sdf.format(bar.getTime()) + "," +
                           bar.getTime()  + "," + 
                           bar.getOpen()  + "," + 
                           bar.getClose() + "," +
                           bar.getHigh()  + "," +
                           bar.getLow()   + "," +
                           bar.getVolume()
                          );
            }
            pw.close();
        } catch (IOException e) {
            e.printStackTrace(console.getErr());
        }
        console.getOut().println("ファイル保存完了。");
        context.stop();
    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
}



取得したデータ



スポンサーリンク

スポンサーリンク
検索
リファレンスツリー


Copyright ©2016 JForexAPIで自動売買させ隊! All Rights Reserved.


Top

inserted by FC2 system