SMASampleTrade.java

注意事項:
サンプルソースコードには実際にオーダーを発注するものがあります。
サンプルソースコードのストラテジーを起動する場合は、必ずデモ口座で行ってください。



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

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package onBarExample;

import com.dukascopy.api.*;
import java.util.HashSet;
import java.util.Set;


public class SMASampleTrade implements IStrategy { 
    private IEngine        engine;
    private IConsole       console;
    private IHistory       history;
    private IContext       context;
    private IIndicators    indicators;
    private IUserInterface userInterface;
    
    private IBar           previousBar;
    private IOrder         order;
            
    private static final int PREV = 1;
    private static final int SECOND_TO_LAST = 0;
    
    // パラメータを定義します
    @Configurable( value = "通貨ペア" )
    public Instrument myInstrument = Instrument.EURGBP;
    @Configurable( value = "オーダータイプ", obligatory = true )
    public OfferSide myOfferSide = OfferSide.ASK;
    @Configurable( value = "時間軸" )
    public Period myPeriod = Period.TEN_MINS;
    @Configurable( "SMA期間" )
    public int smaTimePeriod = 30;
    
    
    public void onStart(IContext context) throws JFException {
        this.engine        = context.getEngine();
        this.console       = context.getConsole();
        this.history       = context.getHistory();
        this.context       = context;
        this.indicators    = context.getIndicators();
        this.userInterface = context.getUserInterface();
        
        // 対象の通貨ペアを通貨ペアリストに登録します
        Set<Instrument> instruments = new HashSet<Instrument>();
        instruments.add(myInstrument);
        context.setSubscribedInstruments(instruments, true);
    }

    public void onAccount(IAccount account) throws JFException {
    }

    
    // IMessagesからオーダー関連のメッセージをフィルタリングし、Strategyタブへログ出力します。
    public void onMessage(IMessage message) throws JFException {        
        if( message.getOrder() != null )
            console.getOut().println("オーダーラベル: " + message.getOrder().getLabel() + " || メッセージログ: " + message.getContent());
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
    
    
    
    // ここにロジックを追加します。特定の通貨ペアと時間軸でフィルタリングします。
    // オーダー状態を取得し同じラベルのオーダーがあるかチェックし、既にあればそのオーダーをクローズします。
    // そして新しいオーダーを行います。
    
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {            
        
        // 通貨ペアと時間軸でフィルタリング
        if (!instrument.equals(myInstrument) || !period.equals(myPeriod)) {
            return; // 終了
        }
                
        IEngine.OrderCommand myCommand = null;
        int candlesBefore = 2, candlesAfter = 0;
    
        // SMAの最後の確定バーと、最後から2つ目の確定バーを取得
        previousBar = myOfferSide == OfferSide.ASK ? askBar : bidBar;
        long currBarTime = previousBar.getTime();
        double sma[] = indicators.sma(instrument, period, myOfferSide, IIndicators.AppliedPrice.CLOSE,
                smaTimePeriod, Filter.NO_FILTER, candlesBefore, currBarTime, candlesAfter);
        
    
        // 後で結果とチャートを比較出来るようにメッセージ出力します。
        // SMAが上昇トレンドなら買いオーダーを行います。
        // SMAが下降トレンドなら買いオーダーを行います。
    
        printMe( String.format("SMAの値: 2つ前 = %.5f; 1つ前 = %.5f", sma[SECOND_TO_LAST], sma[PREV]));        
        if( sma[PREV] > sma[SECOND_TO_LAST]){
            printMe("SMA上昇"); // インジケータが上昇している
             myCommand = IEngine.OrderCommand.BUY;
        } else if ( sma[PREV] < sma[SECOND_TO_LAST]){
            printMe("SMA下降"); // インジケータが下降している
            myCommand = IEngine.OrderCommand.SELL;
        } else {
            return;
        }
            
    
        // ポジションをチェックしオーダー済みポジションがあるかチェックします。
        // ポジションがある場合はmyCommandと同じ方向のオーダーかチェックします。
        // 同じ方向のオーダーの場合は何もしません。
        // 異なる方向のオーダー(SMAトレンドが変わった)の場合はポジションをクローズして、新たにオーダーします。
    
        order = engine.getOrder("MyStrategyOrder");                      
        if (order != null && engine.getOrders().contains(order) && order.getOrderCommand() != myCommand){
            order.close();
            order.waitForUpdate(IOrder.State.CLOSED); // ポジションがクローズされるまで待ちます
            console.getOut().println( order.getLabel() + " ラベルのポジションをクローズしました");            
        }
    
        // 新規オーダーの場合は、または同じラベルのポジションが存在しない場合は、 新規オーダーを行います。
        if ( order == null || !engine.getOrders().contains(order) ){
            engine.submitOrder("MyStrategyOrder" , instrument, myCommand, 0.001);
        }
    }
    
    private void printMe(String toPrint){
        console.getOut().println(toPrint);
    }
}




スポンサーリンク

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


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


Top

inserted by FC2 system