TestOnMessageSL.java

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



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

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

/**
 ストラテジー起動時にストップロス設定付き成行オーダーを送信し、オーダー約定されたら直ぐにストップロスを更新します。
 */
public class TestOnMessageSL implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IOrder order;
    
    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();
        
        // ストップロス2pipsとリミット2pipsとスリップ20pipsでオーダーする
        order = engine.submitOrder("orderValid", Instrument.EURUSD, OrderCommand.BUY, 0.001, 0, 20, 
                price - 0.0002, price + 0.0002);
    }

    public void onMessage(IMessage message) throws JFException {
        // ストップロスをオープン価格から2pipsに更新
        if(message.getOrder() == order && message.getType() == IMessage.Type.ORDER_FILL_OK){
            order.setStopLossPrice(order.getOpenPrice() - 0.0002);  
        }
        print("<html><font color=\"gray\">"+message+"</font>");
        
    }
    
    public void print(String 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