トップ  >  リファレンス  >  サンプルソースコード  >  ストラテジーAPI  >  TestOnMessageResubmit.java
TestOnMessageResubmit.java

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



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

import java.util.Map;
import java.util.HashMap;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;

/**
 このストラテジーは起動時に約定拒否されるような取引数量で成行オーダーします。
取引数量を増やして再オーダーしてオーダー約定されるか、またはmaxOrderResubmitCountで指定した回数分再オーダーするまで継続します

注意:意図的に約定拒否されるようマイナスロットでオーダーしていますが、
      APIのバージョンによってはマイナスロットオーダーするとストラテジーが停止します。
 */
public class TestOnMessageResubmit implements IStrategy {
    private IEngine engine;
    private IConsole console; 
    
    @Configurable("再オーダー送信回数上限")
    public int maxOrderResubmitCount = 5;
    
    private Map<IOrder,Integer> resubmitAttempts = new HashMap<IOrder,Integer>();
    
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        context.setSubscribedInstruments(java.util.Collections.singleton(Instrument.EURUSD), true);
        
        IOrder order = engine.submitOrder("order", Instrument.EURUSD, OrderCommand.BUY, - 0.002);
        resubmitAttempts.put(order, 0);
    }

    public void onMessage(IMessage message) throws JFException {

        IOrder order = message.getOrder();
        
        if(message.getType() == IMessage.Type.ORDER_SUBMIT_REJECTED){
            console.getOut().println(message);
            Integer attempts = resubmitAttempts.get(order);
            if(attempts == null){
                console.getWarn().println("Rejected order was not created by this strategy.");
            } else if (attempts > maxOrderResubmitCount){
                console.getWarn().println("約定拒否の再オーダー送信回数上限を超えました");
            } else {
                resubmitAttempts.remove(order);
                IOrder newOrder = engine.submitOrder(order.getLabel(), order.getInstrument(),
                                                     order.getOrderCommand(), order.getAmount() + 0.001);
                resubmitAttempts.put(newOrder, ++attempts);
                console.getOut().println("再送信オーダー: " + newOrder +
                                         " 残送信回数: " +(maxOrderResubmitCount - attempts + 1));
            }
        }        
    }

    // ストラテジー停止時に全オーダークローズ
    public void onStop() throws JFException {
        for (IOrder order : engine.getOrders()) {
            order.close();
        }
    }    

    public void onAccount(IAccount account) throws JFException {    }

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

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


}



スポンサーリンク

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


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


Top

inserted by FC2 system