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

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



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

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

/**
 ストラテジー起動時にストップロス付き成行オーダーを送信し、約定後直ぐにストップロスを更新します。
 */
@RequiresFullAccess
public class TestWaitForUpdateSL implements IStrategy {
    private IEngine engine;
    private IConsole console;
        
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        context.setSubscribedInstruments(java.util.Collections.singleton(Instrument.EURUSD), true);
        
        double price = context.getHistory().getLastTick(Instrument.EURUSD).getBid();
        
        // ストラテジー起動時にストップロス付き成行オーダーを送信し、約定後直ぐにストップロスを更新します。
        IOrder order = engine.submitOrder("orderValid", Instrument.EURUSD, OrderCommand.BUY, 0.001, 0, 20, 
                price - 0.0005, price + 0.0005);
        order.waitForUpdate(2000); // オーダー状態OPENED待ちの為の2秒待ち
        order.waitForUpdate(2000); // オーダー状態FILLED待ちの為の2秒待ち
        print(String.format("約定後: state=%s, オープン価格=%.5f, 約定時間=%s, ストップロス=%.5f",
                order.getState(),order.getOpenPrice(), DateUtils.format(order.getFillTime()), order.getStopLossPrice()));
        
        // オープン価格から5pipsの所にストップロス調整
        order.setStopLossPrice(order.getOpenPrice() - 0.0005);
        order.waitForUpdate(2000); // ストップロス価格更新待ちの為の2秒待ち
        print(String.format("ストップロス更新後: state=%s, オープン価格=%.5f, 約定時間=%s, ストップロス=%.5f",
                order.getState(),order.getOpenPrice(), DateUtils.format(order.getFillTime()), order.getStopLossPrice()));
         
    }

    public void onMessage(IMessage message) throws JFException {  
        print("<html><font color=\"gray\">"+message+"</font>");
    }
    
    public void print(Object message) {
        console.getOut().println(message);
    }

    // ストラテジー停止時に全オーダークローズ
    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