OrderHistoryAsync6MonthsAllInstr.java

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



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

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

import com.dukascopy.api.*;

/**
 過去6ヶ月の全通貨ペアの全オーダーヒストリーを非同期取得します。 
全オーダーをローカルキャッシュに保存後にストラテジー停止します。
 */
@RequiresFullAccess
public class OrderHistoryAsync6MonthsAllInstr implements IStrategy {
   
    private IConsole console;
    private IHistory history;
    private IContext context;

    private Set<Instrument> instruments = new HashSet<Instrument>(Arrays.asList(Instrument.values()));

    private Map<Instrument, List<IOrder>> histOrderMap = new HashMap<Instrument, List<IOrder>>();
    private List<Instrument> loadingFinishedInstruments = new ArrayList<Instrument>();

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

    @Override
    public void onStart(IContext context) throws JFException {
        console = context.getConsole();
        history = context.getHistory();
        this.context = context;

        context.setSubscribedInstruments(instruments, true);
        instruments = context.getSubscribedInstruments();

        long to   = System.currentTimeMillis();
        long from = to - 6 * Period.MONTHLY.getInterval();

        console.getOut().println(sdf.format(from) + " 〜 " + sdf.format(to) + 
                                "期間のオーダーヒストリーをロード中。"
                                );
       
        for (final Instrument instrument : instruments) {
           
            history.readOrdersHistory(
                  instrument,
                  from,
                  to,
                  new LoadingOrdersListener() {

                      @Override
                      public void newOrder(Instrument instrument, IOrder orderData) {
                          List<IOrder> histOrderList = histOrderMap.get(instrument);
                          if (histOrderList == null) {
                              histOrderList = new ArrayList<IOrder>();
                              histOrderMap.put(instrument, histOrderList);
                          }
                          histOrderList.add(orderData);
      
                          }
                  },

                  new LoadingProgressListener() {
      
                      @Override
                      public void dataLoaded(long start, long end, long currentPosition, String information) {
                      }
      
                      @Override
                      public void loadingFinished(boolean allDataLoaded, long start, long end, long currentPosition) {
                          if (allDataLoaded) {
                              loadingFinishedInstruments.add(instrument);
                              console.getInfo().println(instrument + "のオーダーヒストリーのロード完了。" );
                          }
                      }
      
                      @Override
                      public boolean stopJob() {
                          return false;
                      }
                  }
            );

        }
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (instrument != Instrument.EURUSD) { // EURUSDティック受信時にロード完了チェックを行う
            return;
        }
        if (!loadingFinishedInstruments.containsAll(instruments)) {
            console.getOut().println("未だロード完了していません。");
            return;
        }

        console.getOut().println("全通貨ペアのロード完了しました。");

        for (Map.Entry<Instrument, List<IOrder>> entry : histOrderMap.entrySet()) {
            console.getOut().println("オーダーヒストリー情報: " + entry);
        }
        context.stop();

    }

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

}






スポンサーリンク

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


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


Top

inserted by FC2 system