ManageSL.java

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



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

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

/**
このサンプルでは以下の順序で特定のオーダーのストップロスを管理します。

    1.オーダー作成時にストップロスを追加する
    2.既存のオーダーにストップロスを追加する
    3.ストップロスを削除する
    4.ストップロスを変更します。

    オーダーが変化したり、ストラテジー停止によるオーダークローズされる時にログ出力します
 */
public class ManageSL implements IStrategy {
    
    @Configurable("通貨ペア")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("時間軸")
    public Period period = Period.TEN_SECS;
    @Configurable("オーダーコマンド")
    public OrderCommand cmd = OrderCommand.BUY;
    @Configurable("ストップロス(pips)")
    public int StopLossInPips = 5;
    
    private IConsole console;
    private IHistory history;
    private IEngine engine;
    private IOrder order;

    @Override
    public void onStart(IContext context) throws JFException {
        console = context.getConsole();
        history = context.getHistory();
        engine = context.getEngine();
        
        context.setSubscribedInstruments(java.util.Collections.singleton(instrument), true);
        double lastBidPrice = history.getLastTick(instrument).getBid();

        double amount = 0.001;
        int slippage = 5;
        double stopLossPrice = getSLPrice(lastBidPrice);
        double takeProfitPrice = 0; // リミット設定しない
        
        //1) 最後に受信したBid価格で新規オーダーする(ストップロス設定付き)
        order = engine.submitOrder("order1", instrument, cmd, amount, lastBidPrice, slippage, stopLossPrice, takeProfitPrice);
        
    }
    
    private double getSLPrice (double price){
        return cmd.isLong() 
            ? price - instrument.getPipValue() * StopLossInPips
            : price + instrument.getPipValue() * StopLossInPips;
    }
    
    private void print(Object o){
        console.getOut().println(o);
    }

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

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if(instrument != this.instrument || period != this.period){
            return;
        }
        
        // オーダー状態がFILLEDかOPENEDの時のみストップロス変更可能
        if(order.getState() == IOrder.State.FILLED || order.getState() == IOrder.State.OPENED){
            
            //2) ストップロス設定されていなかったら、設定する
            if (doubleEquals(order.getStopLossPrice(),0)){
                order.setStopLossPrice(getSLPrice(order.getOpenPrice()));
                return;
            }
            
            //3) ストップロス価格がStopLossInPipsの2倍以上ズレたら、ストップロスを削除する
            if (Math.abs(order.getOpenPrice() - order.getStopLossPrice()) > StopLossInPips * instrument.getPipValue() * 2){
                order.setStopLossPrice(0);
                return;
            }
            
            //4) 現在のストップロスを1pipズラす
            if (order.isLong()){
                order.setStopLossPrice(order.getStopLossPrice() - instrument.getPipValue());
            } else {
                order.setStopLossPrice(order.getStopLossPrice() + instrument.getPipValue());
            }
        }

    }
    
    //we need such function since floating point values are not exact
    private boolean doubleEquals(double d1, double d2){
        //0.1 pip is the smallest increment we work with in JForex
        return Math.abs(d1-d2) < instrument.getPipValue() / 10;
    }

    @Override
    public void onMessage(IMessage message) throws JFException {        
        //print only orders related to our order change
        if(message.getOrder() != null && message.getOrder() == order)
            print(message);
    }

    @Override
    public void onAccount(IAccount account) throws JFException {}

    //close all active orders on strategy stop
    @Override
    public void onStop() throws JFException {
        for(IOrder o : engine.getOrders()){
            o.close();
        }
    }

}







スポンサーリンク

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


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


Top

inserted by FC2 system