BreakEvenHLine.java

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



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

import java.awt.Color;

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

/**
 このストラテジーは起動時にポジションを持ちます。
 このストラテジーは起動時にポジションを持ちます。
 含み益が5pipsに到達すると、ストップロスをエントリー価格に移動します。

 チャート上にブレークイーブン価格のラインを描画します、このラインをユーザーが手動で移動させてストップロス価格を調整する事が出来ます。
*/
public class BreakEvenHLine implements IStrategy {
    
    @Configurable("通貨ペア")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("オーダーコマンド")
    public OrderCommand orderCommand = OrderCommand.BUY;
    @Configurable("ロット")
    double amount = 0.001;
    @Configurable("スリップページ")
    public int slippage = 20;
    @Configurable("ストップロス")
    public int stopLossPips = 10;
    @Configurable("リミット")
    public int takeProfitPips = 40;
    @Configurable("ブレークイーブン")
    public int breakEvenPips = 5;
    
    private IConsole console;
    private IEngine engine;
    private IHistory history;
    private IOrder order;
    private IChart chart;
    
    IHorizontalLineChartObject breakEvenLine;

    @Override
    public void onStart(IContext context) throws JFException {
        engine = context.getEngine();
        console = context.getConsole();
        history = context.getHistory();
        context.setSubscribedInstruments(java.util.Collections.singleton(instrument), true);
        chart = context.getChart(instrument);
                
        ITick tick = history.getLastTick(instrument);
        double stopLossPrice, takeProfitPrice;
        if(orderCommand.isLong()){
            stopLossPrice = tick.getBid() - stopLossPips * instrument.getPipValue();
            takeProfitPrice = tick.getBid() + takeProfitPips * instrument.getPipValue();
        } else {
            stopLossPrice = tick.getAsk() + stopLossPips * instrument.getPipValue();
            takeProfitPrice = tick.getAsk() - takeProfitPips * instrument.getPipValue();
        }
        // Bid価格でオーダーします(ストップロスとリミット設定条件付き)
        double openPrice = tick.getBid(); 
        order = engine.submitOrder("breakEvenOrder", instrument, orderCommand, amount, openPrice, slippage,  stopLossPrice, takeProfitPrice);
        order.waitForUpdate(2000, IOrder.State.OPENED);
        
        if(chart == null){
            console.getErr().println( instrument + "のチャートが開かれていないので、ブレークイーブンラインを描画出来ません。" );
            context.stop();
            return;
        }
        // ブレークイーブンラインを描画
        double breakEvenPrice = orderCommand.isLong() 
            ? order.getOpenPrice() + breakEvenPips * instrument.getPipValue()
            : order.getOpenPrice() - breakEvenPips * instrument.getPipValue();
        
        breakEvenLine = chart.getChartObjectFactory().createPriceMarker("breakEvenLine", breakEvenPrice);
        breakEvenLine.setColor(Color.RED);
        breakEvenLine.setLineStyle(LineStyle.DASH);
        chart.addToMainChart(breakEvenLine);
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if(instrument != this.instrument || order.getState() != IOrder.State.FILLED || breakEvenLine == null){
            return;
        }
        if((order.isLong() && tick.getBid() >= breakEvenLine.getPrice(0)) ||
                (!order.isLong() && tick.getAsk() <= breakEvenLine.getPrice(0))){
            order.setStopLossPrice(order.getOpenPrice());
            chart.remove(breakEvenLine);
            breakEvenLine = null;
        }
    }

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}

    @Override
    public void onMessage(IMessage message) throws JFException {}

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

    @Override
    public void onStop() throws JFException {}

}






スポンサーリンク

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


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


Top

inserted by FC2 system