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

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



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

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


// このストラテジーはリミットとストップロスの設定を行います。
// ポジションがクローズされると、直ぐに以下のいずれかのオーダーが行われます。
//  ・リミットでクローズされると同じ方向のオーダーが行われます。
//  ・ストップロスでクローズされると反対方向のオーダーが行われ、違う距離のリミットが設定されます。
//  ・手動でクローズされると、ストラテジーが停止します。
//
// ストラテジーを停止するとポジションをクローズします。


public class SimpleTpSlStrategy implements IStrategy {
    // パラメータ設定
    @Configurable("通貨ペア")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("ロット")
    public double amount = 0.001;
    @Configurable("ストップロス")
    public int slPips = 10;
    @Configurable("リミット(ストップロスクローズ時)")
    public int tpPipsOnLoss = 10;
    @Configurable("リミット(リミットクローズ時)")
    public int tpPipsOnProfit = 5;

    private IEngine engine;
    private IHistory history;
    private IConsole console;
    private IContext context;
    private IOrder order;

    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.history = context.getHistory();
        this.console = context.getConsole();
        this.context = context;
        // 通貨ペアの登録
        context.setSubscribedInstruments(java.util.Collections.singleton(instrument));
        // ヒストリーから日足データをフェッチ
        IBar prevDailyBar = history.getBar(instrument, Period.DAILY, OfferSide.ASK, 1);
        // 初回オーダーのエントリー方向設定
        OrderCommand orderCmd = prevDailyBar.getClose() > prevDailyBar.getOpen() 
                ? OrderCommand.BUY 
                : OrderCommand.SELL;
        // 新規オーダー(ロットとリミット設定)
        submitOrder(amount, orderCmd, tpPipsOnLoss);
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
        if (message.getType() != Type.ORDER_CLOSE_OK
                || !message.getOrder().equals(order) // ポジションクローズでフィルタリング
            ) {
            return;
        }
        console.getInfo().format("%s リミット/ストップによるクローズ %.1f pips", order.getLabel(), order.getProfitLossInPips()).println();
        if (message.getReasons().contains(IMessage.Reason.ORDER_CLOSED_BY_TP)) {
            // リミットクローズの場合、同じ方向でオーダー
            submitOrder(amount, order.getOrderCommand(), tpPipsOnProfit);
        } else if (message.getReasons().contains(IMessage.Reason.ORDER_CLOSED_BY_SL)) {
            //  ストップロスクローズの場合、違う方向でオーダーし異なるリミットを設定する
            OrderCommand orderCmd = order.isLong() ? OrderCommand.SELL : OrderCommand.BUY;
            submitOrder(amount, orderCmd, tpPipsOnLoss);
        } else {
            // 手動でポジションクローズした場合、ストラテジーを停止
            console.getOut().println("ストラテジー以外の方法でクローズされました。ストラテジーを停止します。");
            context.stop();
        }
    }

    private void submitOrder(double amount, OrderCommand orderCmd, double tpPips) throws JFException {
        double slPrice, tpPrice;
        ITick lastTick = history.getLastTick(instrument);
        // ストップロスとリミットの価格を算出します。
        if (orderCmd == OrderCommand.BUY) {
            slPrice = lastTick.getAsk() - slPips * instrument.getPipValue();
            tpPrice = lastTick.getAsk() + tpPips * instrument.getPipValue();
        } else {
            slPrice = lastTick.getBid() + slPips * instrument.getPipValue();
            tpPrice = lastTick.getBid() - tpPips * instrument.getPipValue();
        }
        // 現在の市場価格でオーダー
        order = engine.submitOrder( orderCmd.toString() + System.currentTimeMillis(), instrument, orderCmd, amount,
                                    0, 20, slPrice, tpPrice);
    }
    
    public void onStop() throws JFException {
        if(order.getState() == IOrder.State.FILLED || order.getState() == IOrder.State.OPENED){
            order.close();
        }
    }

    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