トップ  >  リファレンス  >  サンプルソースコード  >  ストラテジーAPI(インジケータ)  >  CustomIndicatorPlotAndLogValues2.java
CustomIndicatorPlotAndLogValues2.java

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



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

import java.awt.Color;
import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.MaType;
import com.dukascopy.api.indicators.IIndicator;
import com.dukascopy.api.indicators.OutputParameterInfo.DrawingStyle;

/**
 このストラテジーは複数出力するカスタムインジケータを使用します。
カスタムインジケータの出力結果を解析し、ログ出力します。
また、複数出力を処理してチャートにプロットします。

あと、カスタムインジケータを複数の方法で算出します。
 ・シフトによる算出(算出結果は1つ)
 ・バーインターバルによる算出(算出結果は複数)
 */
@RequiresFullAccess
public class CustomIndicatorPlotAndLogValues2 implements IStrategy {
    private IConsole console;
    private IIndicators indicators;
    private IChart chart;

    @Configurable("インジケータ.jfxファイルのフルパス")
    public File indicatorJfxFile = new File("C:/temp/AwesomeOscillatorCustom.jfx");
    @Configurable("通貨ペア")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("時間軸")
    public Period period = Period.TEN_SECS;
    @Configurable("バーインターバルカウント")
    public int candleCount = 20;
    @Configurable("シフト数")
    public int shift = 2;
    
    // インジケータのoptInputParameterInfosのインプット設定
    @Configurable("長期MA期間")
    public int FasterMATimePeriod = 5;
    @Configurable("長期MAタイプ")
    public MaType FasterMaType = MaType.SMA;
    @Configurable("短期MA期間")
    public int SlowerMATimePeriod = 10;
    @Configurable("短期MAタイプ")
    public MaType SlowerMAType = MaType.SMA;
    
    // インジケータのoutputParameterInfosによる出力
    private double[] ZeroOutput;
    private double[] PositiveOutput;
    private double[] NegativeOutput;
    
    private double ZeroByShift;
    private double PositiveByShift;
    private double NegativeByShift;
    
    private int     outCount = 3;
    private String indicatorName = "AwesomeCustom";
    Object[] optionalInputArray;

    DecimalFormat df = new DecimalFormat("0.00000##");

    @SuppressWarnings("serial")
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss") {
        {
            setTimeZone(TimeZone.getTimeZone("GMT"));
        }
    };

    public void onStart(IContext context) throws JFException {
        this.console    = context.getConsole();
        this.indicators = context.getIndicators();
        this.chart      = context.getChart(instrument);
        
        // indicatorJfxFileストラテジーのカスタムインジケータ登録
        this.indicators.registerCustomIndicator(indicatorJfxFile);        

        // MaType列挙のint値を取得します
        optionalInputArray = new Object[] { 
                                            FasterMATimePeriod,
                                            FasterMaType.ordinal(),
                                            SlowerMATimePeriod,
                                            SlowerMAType.ordinal()
                                           };

        IIndicator awesomeCustom = indicators.getIndicator(indicatorName);
        
        // チャートにインジケータ追加
        chart.addIndicator(
                           awesomeCustom,
                           optionalInputArray,
                           new Color[] { Color.BLACK, Color.BLUE.darker(), Color.MAGENTA.brighter() },
                           new DrawingStyle[] { DrawingStyle.DASHDOT_LINE, DrawingStyle.HISTOGRAM ,DrawingStyle.HISTOGRAM },
                           new int[] { 1, 1, 1 }
        );
    }

    private void print(Object o) {
        console.getOut().println(o);
    }

    public void onAccount(IAccount account) throws JFException {    }
    public void onMessage(IMessage message) throws JFException {    }

    public void onStop() throws JFException {    
        chart.removeAll();
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    
        if ( instrument != this.instrument || period != this.period )
            return;


        // カスタムインジケータ算出(バーインターバル)
        Object[] resultArr = indicators.calculateIndicator(
                                    this.instrument,
                                    this.period,
                                    new OfferSide[] { OfferSide.BID },
                                    indicatorName,
                                    new IIndicators.AppliedPrice[] { IIndicators.AppliedPrice.CLOSE },
                                    optionalInputArray,
                                    Filter.NO_FILTER,
                                    candleCount,
                                    bidBar.getTime(),
                                    0
                             );
        
        // カスタムインジケータ算出(シフト)
        Object[] resultByShift = indicators.calculateIndicator(
                                    this.instrument,
                                    this.period,
                                    new OfferSide[] { OfferSide.BID },
                                    indicatorName,
                                    new IIndicators.AppliedPrice[] { IIndicators.AppliedPrice.CLOSE },
                                    optionalInputArray,
                                    shift
                                 );

        
        // 全ての出力を保持する配列
        double[][] outputArrays   = new double[outCount][];
        double[]   outputsByShift = new double[outCount];
        
        for (int i = 0; i < outCount; i++) {
            outputArrays[i] = (double[]) resultArr[i];
            outputsByShift[i] = Double.valueOf(resultByShift[i].toString());
        }
        
        // 便宜上、分かり易い名称の変数に出力データをコピー
        ZeroOutput      = outputArrays[0];
        PositiveOutput  = outputArrays[1];
        NegativeOutput  = outputArrays[2];
        
        ZeroByShift     = outputsByShift[0];
        PositiveByShift = outputsByShift[1];
        NegativeByShift = outputsByShift[2];        

        print( sdf.format(bidBar.getTime()) + "から直近" +candleCount+"本の算出結果:");
        print( "ゼロ出力      : " + arrayToStringLast(ZeroOutput));
        print( "ポジティブ出力: " + getMaxInfo(PositiveOutput) + arrayToStringLast(PositiveOutput));
        print( "ネガティブ出力: " + getMinInfo(NegativeOutput) + arrayToStringLast(NegativeOutput));
        print( String.format("shift=%sでの算出結果: ゼロ=%s ポジティブ=%s ネガティブ=%s",
                shift, ZeroByShift, df.format(PositiveByShift), df.format(NegativeByShift)));
    } 
    
    // 配列内データのMAX値を取得してログ出力する
    private String getMaxInfo(double[] arr){
        double value = Double.MIN_VALUE;
        int    index  = -1;

        for ( int i = 0; i < arr.length; i++ ) {
            if (arr[i] > value) {
                index = i;
                value = arr[i];
            }
        }
        return "Max値: " + df.format(value) + " インデックス: " + index + "; ";
    }
    
    // 配列内データのMIN値を取得してログ出力する
    private String getMinInfo(double[] arr){
        double value = Double.MAX_VALUE;
        int index = -1;

        for ( int i = 0; i < arr.length; i++ ) {
            if (arr[i] < value) {
                index = i;
                value = arr[i];
            }
        }
        return "Min値 " + df.format(value) + " インデックス: " + index + "; ";
    }

    // double配列をstring変換してログ出力
    private String arrayToStringLast(double[] arr) {
        String str = "";

        for ( int r = 0; r < arr.length; r++ ) {
            str += " [" + r + "]" + df.format(arr[r]) + ",";
        }
        return str;
    }
}


indicatorJfxFileで指定したパスにAwesomeOscillatorCustom.jfxファイルを置く必要があります。
AwesomeOscillatorCustom.jfxはAwesomeOscillatorCustom.javaファイルをコンパイルして生成します。
また、ストラテジー起動する前にチャートを開いておく必要があります。






スポンサーリンク

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


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


Top

inserted by FC2 system