LoggingValues.java

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



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

import java.util.Arrays;
import java.util.List;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators.MaType;
import com.dukascopy.api.util.DateUtils;

/**
 JForexAPIで良く使用される値と配列のログ出力例
 */
public class LoggingValues implements IStrategy {
    
    private IConsole console;
    private IHistory history;
    private IIndicators indicators;

    @Override
    public void onStart(IContext context) throws JFException {
        console = context.getConsole();
        history = context.getHistory();
        indicators = context.getIndicators();
        
        // 直前のバー情報取得
        IBar prevBar = history.getBar(
                                      Instrument.EURUSD,
                                      Period.TEN_SECS,
                                      OfferSide.BID,
                                      1                    // シフト
                       );

        // 直前バーの時間取得
        long time = prevBar.getTime();


        // maxインジケータの算出(単一結果)
        double max = indicators.max(
                                     Instrument.EURUSD,
                                     Period.TEN_SECS,
                                     OfferSide.BID,
                                     AppliedPrice.CLOSE,
                                     20,                  // タイムピリオド
                                     1                    // シフト
                      );
                                     
        // 単一のdouble変数の値をログ出力
        console.getOut().format("単一の値(maxインジケータの結果): %.5f 直前のバー時間=%s 直前のバー情報=%s",
                                max,
                                DateUtils.format(time),
                                prevBar
        ).println();
        
        // ボリンジャーバンドの算出(2次元配列結果)
        double[][] bbands = indicators.bbands(
                                               Instrument.EURUSD,
                                               Period.TEN_SECS,
                                               OfferSide.BID,
                                               AppliedPrice.CLOSE,
                                               20,                 // タイムピリオド
                                               5,                  // 標準偏差の値(+σ)
                                               4,                  // 標準偏差の値(-σ)
                                               MaType.SMA,         // MAタイプ
                                               Filter.NO_FILTER,   // フィルター
                                               5,                  // 基準時刻から取得開始するバーオフセット(前方向)
                                               time,               // 基準時刻
                                               0                   // 基準時刻から取得終了するバーオフセット(後方向)
                             );

        // 2次元配列のログ出力
        print("2次元配列のログ出力(ボリンジャーバンドインジケータ算出結果): " + arrayToString(bbands) );
        
        // maxインジケータの算出(1次元配列結果)
        double[] maxArr = indicators.max(
                                          Instrument.EURUSD,
                                          Period.TEN_SECS,
                                          OfferSide.BID,
                                          AppliedPrice.CLOSE,
                                          20,                 // タイムピリオド
                                          Filter.NO_FILTER,   // フィルター
                                          5,                  // 基準時刻から取得開始するバーオフセット(前方向)
                                          time,               // 基準時刻
                                          0                   // 基準時刻から取得終了するバーオフセット(後方向)
        );

        // 1次元配列のログ出力
        print("1次元配列のログ出力(maxインジケータ算出結果): " + arrayToString(maxArr));
        
        // バー履歴のリスト取得
        List<IBar> bars = history.getBars(
                                          Instrument.EURUSD,
                                          Period.TEN_SECS,
                                          OfferSide.BID,
                                          Filter.NO_FILTER,   // フィルター
                                          5,                  // 基準時刻から取得開始するバーオフセット(前方向)
                                          time,               // 基準時刻
                                          0                   // 基準時刻から取得終了するバーオフセット(後方向)
        );

        // オブジェクト配列のインデックス化、非インデックス、リストの出力
        console.getOut().format("―――\n" +
                                "オブジェクト配列(バー履歴取得結果)\n" +
                                "インデックス配列  : %s \n" + 
                                "非インデックス配列: %s\n" +
                                "リスト            : %s", 
                                arrayToString(bars.toArray()),
                                Arrays.toString(bars.toArray()),
                                bars
        ).println();

        context.stop();
    }
    
    private void print(Object o){
        console.getOut().println(o);
    }
    
    public static String arrayToString(double[] arr) {
        StringBuilder sb = new StringBuilder();
        for (int r = 0; r < arr.length; r++) {
            sb.append(String.format("[%s] %.5f; ",r, arr[r]));
        }
        return sb.toString();
    }

    public static String arrayToString(Object[] arr) {
        StringBuilder sb = new StringBuilder();
        for (int r = 0; r < arr.length; r++) {
            sb.append(String.format("[%s] %s; ",r, arr[r]));
        }
        return sb.toString();
    }

    public static String arrayToString(double[][] arr) {
        StringBuilder sb = new StringBuilder();
        for (int r = 0; r < arr.length; r++) {
            for (int c = 0; c < arr[r].length; c++) {
                sb.append(String.format("[%s][%s] %.5f; ",r, c, arr[r][c]));
            }
            sb.append("; ");
        }
        return sb.toString();
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {}
    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
    @Override
    public void onMessage(IMessage message) throws JFException {}
    @Override
    public void onAccount(IAccount account) throws JFException {}
    @Override
    public void onStop() throws JFException {}

}






スポンサーリンク

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


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


Top

inserted by FC2 system