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

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



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

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

/**
 このストラテジーは最初にポジションを持ち、2回部分クローズを施行します。
 最終的には全クローズを行います。
*/
public class PartialCloseByPrice implements IStrategy {
    
    private IOrder order;
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private Instrument instrument = Instrument.EURUSD;
    

    public void onStart(IContext context) throws JFException {
        engine = context.getEngine();
        console = context.getConsole();
        history = context.getHistory();
        context.setSubscribedInstruments(java.util.Collections.singleton(instrument), true);
        
        double lastBid = history.getLastTick(instrument).getBid();
        
        order = engine.submitOrder("orderPartialClose", instrument, OrderCommand.BUY, 0.01);
        order.waitForUpdate(2000, IOrder.State.FILLED); 
        
        // このようなプライスが提示される事は無いので、ORDER_CLOSE_REJECTEDエラーになります。
        order.close(0.003, lastBid + 100 * instrument.getPipValue(), 5);
        order.waitForUpdate(2000); // オーダー状態遷移待ち
        
        // JForexのポジティブスリップによって約定され、メッセージタイプはORDER_CLOSE_OKになります
        order.close(0.003, lastBid - 100 * instrument.getPipValue(), 5);
        order.waitForUpdate(2000); // オーダー状態遷移待ち
        
        order.close(); // 全クローズ
        context.stop();// ストラテジー停止
    }

    public void onMessage(IMessage message) throws JFException {
        console.getOut().format("%s\n%s クローズ価格=%.5f, 取引数量=%.3f, リクエストした取引数量=%.3f, クローズ時間=%s, オーダー状態=%s", 
                message, order.getLabel(), order.getClosePrice(), order.getAmount(), order.getRequestedAmount(), 
                    DateUtils.format(order.getCloseTime()), order.getState()).println();

    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {}
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) 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