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

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



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

import java.awt.Color;
import java.text.DecimalFormat;

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

/**
このサンプルでは以下のオーダーを行います

    1.tick受信毎にストップロスとリミットを、現在のマーケット価格から一定価乖離した状態に更新します。
    2.最初のストップロスとリミット価格のラインをチャート上に描画します。
    3.現在のマーケット価格が、最初のストップロスとリミット価格のいずれかを抜けたらログ出力します。

*/
public class SlTpUpdateWithOrigLines implements IStrategy {

    @Configurable("ストップロスとリミット価格との乖離幅")
    public double priceDistance = 0.0003;
    @Configurable("通貨ペア")
    public Instrument instrument = Instrument.EURUSD;
    
    private IEngine engine;
    private IOrder order;
    private IHistory history;
    private IChart chart;
    private IContext context;
    private IConsole console;
    
    private IHorizontalLineChartObject slLine, tpLine;
    private boolean origTpBroken, origSlBroken;
    
    long lastTickTime = 0;
    DecimalFormat df = new DecimalFormat("0.00000");
    
    
    @Override
    public void onStart(IContext context) throws JFException {
        this.context= context;
        engine= context.getEngine();
        history = context.getHistory();
        console = context.getConsole();
        context.setSubscribedInstruments(java.util.Collections.singleton(instrument), true);
        chart = context.getChart(instrument);
        
        double lastPrice = history.getLastTick(instrument).getBid();
        lastTickTime = history.getLastTick(instrument).getTime();

        double slPrice = lastPrice - priceDistance;
        double tpPrice = lastPrice + priceDistance;
        order = engine.submitOrder("order1", instrument, OrderCommand.BUY, 0.001, 0, 5, slPrice, tpPrice);
        
        // 最初のストップロスとリミット価格のラインを描画します
        slLine = chart.getChartObjectFactory().createHorizontalLine();
        tpLine = chart.getChartObjectFactory().createHorizontalLine();
        slLine.setPrice(0, slPrice);
        tpLine.setPrice(0, tpPrice);
        slLine.setColor(Color.RED);
        tpLine.setColor(Color.GREEN);
        slLine.setText("Original SL");
        tpLine.setText("Original TP");
        chart.addToMainChartUnlocked(slLine);
        chart.addToMainChartUnlocked(tpLine);
        
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        // ストップロスとリミットは1秒以内に連続して変更する事は出来ない
        if(instrument != this.instrument || tick.getTime() - lastTickTime < 1000){
            return;
        }
        // Bid価格がストップかリミットのいずれかを抜けてオーダーがクローズされたか、手動でクローズされたかチェック
        if(!engine.getOrders().contains(order)){
            // ストラテジー停止
            context.stop();
            return;
        }
        
        order.setStopLossPrice(tick.getBid() - priceDistance);
        order.setTakeProfitPrice(tick.getBid() + priceDistance);
        
        if(!origSlBroken && tick.getBid() < slLine.getPrice(0)){
            console.getOut().println("最初のストップロス価格を抜けました。Bid価格 = " + df.format(tick.getBid()));
            origSlBroken = true;
        }
        if(!origTpBroken && tick.getBid() > tpLine.getPrice(0)){
            console.getOut().println("最初のリミット価格を抜けました。Bid価格 = " + df.format(tick.getBid()));
            origTpBroken = true;
        }
        
        lastTickTime = tick.getTime();
    }

    @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 {
        chart.removeAll();
        for(IOrder o:engine.getOrders()){
            o.close();
        }
    }

}







スポンサーリンク

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


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


Top

inserted by FC2 system