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

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



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

import com.dukascopy.api.*;
import static com.dukascopy.api.IOrder.State.*;
import com.dukascopy.api.IEngine.OrderCommand;

/**
このストラテジーは新規エントリーしてポジションを持ち、そのポジションが無くなるまで様々な方法で部分クローズを行います。
またオーダー状態を管理してオーダーをクローズしたり、キャンセルしたりもします。 */
public class CloseOrderVarious implements IStrategy { private IConsole console; private IEngine engine; private IHistory history; private Instrument instrument = Instrument.EURUSD; @Override public void onStart(IContext context) throws JFException { console = context.getConsole(); engine = context.getEngine(); history = context.getHistory(); context.setSubscribedInstruments(java.util.Collections.singleton(instrument), true); IOrder order = engine.submitOrder("order", instrument, OrderCommand.BUY, 0.01); order.waitForUpdate(2000, FILLED); if (order.getState() != FILLED) { console.getOut().println("オーダー状態がFILLED以外なので、クローズオーダーは出来ません。現在のオーダー状態:" + order.getState()); context.stop(); } // 条件無しの部分クローズ order.close(0.003); order.waitForUpdate(2000); // 条件付き部分クローズ(価格) double lastBid = history.getLastTick(instrument).getBid(); order.close(0.003, lastBid - 100 * instrument.getPipValue()); order.waitForUpdate(2000); // 条件付き部分クローズ(価格とスリップページ) order.close(0.003, lastBid - 100 * instrument.getPipValue(), 5); order.waitForUpdate(2000); // 条件無しの全クローズ order.close(); order.waitForUpdate(2000, CLOSED); if (order.getState() != CLOSED) { console.getErr().println("ポジションが全てクローズされていないので、同じラベルでの新規オーダーは出来ません。現在のオーダー状態:" + order.getState()); context.stop(); } // 即座に条件に引っかからないような価格指定で、条件付きオーダーを送信します double price = history.getLastTick(instrument).getAsk() + 5 * instrument.getPipValue(); order = engine.submitOrder("order", instrument, OrderCommand.BUYSTOP, 0.01, price); order.waitForUpdate(2000, OPENED); if (order.getState() != OPENED) { console.getErr().println("オーダー状態がOPENED以外の時は、オーダーキャンセル出来ません。現在のオーダー状態:" + order.getState()); context.stop(); } order.close(); order.waitForUpdate(2000, CANCELED); // FILLEDとOPENEDの両方のオーダーを作成します price = history.getLastTick(instrument).getAsk(); for(int i = -5; i< 6; i++){ String label = String.format("order_%sAsk%s", Math.abs(i),i < 0 ? "below" : "above"); order = engine.submitOrder(label, instrument, OrderCommand.BUYSTOP, 0.01, price + i*instrument.getPipValue()); order.waitForUpdate(2000, OPENED, FILLED); } for(IOrder o: engine.getOrders()){ console.getNotif().println("アクティブオーダー: " + engine.getOrders()); if (o.getState() == OPENED || o.getState() == FILLED) { o.close(); o.waitForUpdate(2000, CLOSED, CANCELED); } } context.stop(); } @Override public void onMessage(IMessage message) throws JFException { IOrder order = message.getOrder(); if (order == null || message.getType() == IMessage.Type.NOTIFICATION) { return; } console.getOut().println(message); if (message.getType() == IMessage.Type.ORDER_CLOSE_OK) { console.getInfo().println(order + " " + (order.getState() == CANCELED ? "キャンセルしました" : order.getState() == FILLED ? "部分クローズしました" : "全クローズしました")); // CLOSED } } @Override public void onTick(Instrument instrument, ITick tick) throws JFException {} @Override public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) 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