トップ  >  リファレンス  >  サンプルソースコード  >  SDK-バックテスト  >  TesterMainCustomReport.java
TesterMainCustomReport.java

注意事項:
サンプルソースコードには実際にオーダーを発注するものがあります。
サンプルソースコードのストラテジーを起動する場合は、必ずデモ口座で行ってください。



// Copyright (c) 2009 Dukascopy (Suisse) SA. All Rights Reserved.

package singlejartest;

import com.dukascopy.api.*;
import com.dukascopy.api.system.ISystemListener;
import com.dukascopy.api.system.ITesterClient;
import com.dukascopy.api.system.TesterFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.Future;

//import jforex.news.Main;

// デューカスコピーテスター(ストラテジーレポートのカスタマイズ)の初期化・起動するストラテジー


@RequiresFullAccess
public class TesterMainCustomReport {
    private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

    // url of the DEMO jnlp
    private static String jnlpUrl = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
    // user name
    private static String userName = "username";
    // password
    private static String password = "password";
    
    public static void insertStringInFile(File inFile, int lineno, String lineToBeInserted) throws Exception {
        // 一時ファイル
        File outFile = new File("$$$$$$$$.tmp");

        // 入力
        FileInputStream fis = new FileInputStream(inFile);
        BufferedReader  in  = new BufferedReader(new InputStreamReader(fis));

        // 出力
        FileOutputStream fos = new FileOutputStream(outFile);
        PrintWriter      out = new PrintWriter(fos);

        String thisLine = "";
        int i = 1;
        while ((thisLine = in.readLine()) != null) {
            if (i == lineno)
                out.println(lineToBeInserted);
            out.println(thisLine);
            i++;
        }
        out.flush();
        out.close();
        in.close();

        inFile.delete();
        outFile.renameTo(inFile);
    }

    public static void main(String[] args) throws Exception {
        //  IClientインターフェースのインスタンスを取得
        final ITesterClient client = TesterFactory.getDefaultInstance();
        //  システムイベントを受け取るリスナーをセット
        client.setSystemListener(new ISystemListener() {
            @Override
            public void onStart(long processId) {
                LOGGER.info("ストラテジー起動: " + processId);
            }

            @Override
            public void onStop(long processId) {
                LOGGER.info("ストラテジー停止: " + processId);
                File reportFile = new File("C:\\temp\\report2.html");
                try {
                    client.createReport(processId, reportFile);
                    insertStringInFile(reportFile, 90, "__________INSERTED LINE__________1.2345");
                } catch (Exception e) {
                    LOGGER.error(e.getMessage(), e);
                }
                if (client.getStartedStrategies().size() == 0) {
                    System.exit(0);
                }
            }

            @Override
            public void onConnect() {
                LOGGER.info("接続完了");
            }

            @Override
            public void onDisconnect() {
                // テスターは切断されません
            }
        });

        LOGGER.info("接続中…");
        // jnlp, user , passwordでサーバーに接続します
        // データをダウンロードするにはサーバー接続する必要があります
        client.connect(jnlpUrl, userName, password);

        // 接続待ち
        int i = 10; // 最大10秒待ち
        while (i > 0 && !client.isConnected()) {
            Thread.sleep(1000);
            i--;
        }
        if (!client.isConnected()) {
            LOGGER.error("デューカスコピーのサーバーに接続出来ませんでした");
            System.exit(1);
        }

        // custom historical data
        String dateFromStr = "05/25/2011 00:00:00";
        String dateToStr   = "05/26/2011 00:00:00";

        final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

        Date dateFrom = dateFormat.parse(dateFromStr);
        Date dateTo = dateFormat.parse(dateToStr);

        client.setDataInterval(Period.THIRTY_MINS, OfferSide.BID, ITesterClient.InterpolationMethod.CLOSE_TICK, dateFrom.getTime(),
                dateTo.getTime());

        // テストする通貨ペアをセット
        Set instruments = new HashSet();
        instruments.add(Instrument.EURUSD);
        LOGGER.info("通貨ペア登録…");
        client.setSubscribedInstruments(instruments);
        // 初期資金設定
        client.setInitialDeposit(Instrument.EURUSD.getSecondaryCurrency(), 50000);
        // データロード
        LOGGER.info("データのダウンロード中");
        Future future = client.downloadData(null);
        // ダウンロード完了待ち
        future.get();
        // ストラテジー開始
        LOGGER.info("ストラテジー開始");

        client.startStrategy(new MA_Play(), new LoadingProgressListener() {

            @Override
            public void dataLoaded(long startTime, long endTime, long currentTime, String information) {
                LOGGER.info(information);
            }

            @Override
            public void loadingFinished(boolean allDataLoaded, long startTime, long endTime, long currentTime) {
            }

            @Override
            public boolean stopJob() {
                return false;
            }
        });
        // 動作中

    }
}






C:\temp\report2.htmlに作成されたレポート結果




スポンサーリンク

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


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


Top

inserted by FC2 system