diff --git a/build.gradle b/build.gradle index 55050e26..38f130b5 100644 --- a/build.gradle +++ b/build.gradle @@ -49,8 +49,8 @@ publishing { } dependencies { - compile 'io.reactivex.rxjava2:rxjava:2.0.6' - compile 'com.dukascopy.api:JForex-API:2.13.30' + compile 'io.reactivex.rxjava2:rxjava:2.0.8' + compile 'com.dukascopy.api:JForex-API:2.13.34' compile 'com.google.guava:guava:20.0' compile 'org.apache.logging.log4j:log4j-api:2.5' compile 'org.apache.logging.log4j:log4j-core:2.5' diff --git a/src/main/java/com/jforex/programming/order/OrderUtilHandler.java b/src/main/java/com/jforex/programming/order/OrderUtilHandler.java index 51f20891..4f5a0112 100644 --- a/src/main/java/com/jforex/programming/order/OrderUtilHandler.java +++ b/src/main/java/com/jforex/programming/order/OrderUtilHandler.java @@ -1,5 +1,8 @@ package com.jforex.programming.order; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import com.dukascopy.api.IOrder; import com.jforex.programming.order.call.OrderCallReason; import com.jforex.programming.order.call.OrderCallRequest; @@ -17,6 +20,8 @@ public class OrderUtilHandler { private final OrderEventTypeDataFactory orderEventTypeDataFactory; private final JFHotPublisher callRequestPublisher; + private final static Logger logger = LogManager.getLogger(OrderUtilHandler.class); + public OrderUtilHandler(final OrderEventGateway orderEventGateway, final OrderEventTypeDataFactory orderEventTypeDataFactory, final JFHotPublisher callRequestPublisher) { @@ -27,6 +32,7 @@ public OrderUtilHandler(final OrderEventGateway orderEventGateway, public Observable callObservable(final IOrder orderOfCall, final OrderCallReason callReason) { + logger.info("Called callObservable"); return Observable .just(orderOfCall) .doOnSubscribe(d -> callRequestPublisher.onNext(new OrderCallRequest(orderOfCall, callReason))) diff --git a/src/main/java/com/jforex/programming/order/call/OrderCallReason.java b/src/main/java/com/jforex/programming/order/call/OrderCallReason.java index be620c8c..3846dc61 100644 --- a/src/main/java/com/jforex/programming/order/call/OrderCallReason.java +++ b/src/main/java/com/jforex/programming/order/call/OrderCallReason.java @@ -13,6 +13,8 @@ public enum OrderCallReason { MERGE, /** A request for closing an order. */ CLOSE, + /** A request for partial closing an order. */ + PARTIAL_CLOSE, /** A request for changing the stop loss price of an order. */ CHANGE_SL, /** A request for changing the take profit price of an order. */ diff --git a/src/main/java/com/jforex/programming/order/event/OrderEventTypeDataFactory.java b/src/main/java/com/jforex/programming/order/event/OrderEventTypeDataFactory.java index 204c2b3c..4d7cede2 100644 --- a/src/main/java/com/jforex/programming/order/event/OrderEventTypeDataFactory.java +++ b/src/main/java/com/jforex/programming/order/event/OrderEventTypeDataFactory.java @@ -52,6 +52,10 @@ public class OrderEventTypeDataFactory { new OrderEventTypeData(EnumSet.of(CLOSE_OK), EnumSet.of(CLOSE_REJECTED), EnumSet.of(NOTIFICATION, PARTIAL_CLOSE_OK)); + private static final OrderEventTypeData partialCloseData = + new OrderEventTypeData(EnumSet.of(CLOSE_OK, PARTIAL_CLOSE_OK), + EnumSet.of(CLOSE_REJECTED), + EnumSet.of(NOTIFICATION)); private static final OrderEventTypeData setLabelData = new OrderEventTypeData(EnumSet.of(CHANGED_LABEL), EnumSet.of(CHANGE_LABEL_REJECTED), @@ -84,6 +88,7 @@ public OrderEventTypeDataFactory() { .put(OrderCallReason.SUBMIT_CONDITIONAL, submitConditionalData) .put(OrderCallReason.MERGE, mergeData) .put(OrderCallReason.CLOSE, closeData) + .put(OrderCallReason.PARTIAL_CLOSE, partialCloseData) .put(OrderCallReason.CHANGE_LABEL, setLabelData) .put(OrderCallReason.CHANGE_GTT, setGTTData) .put(OrderCallReason.CHANGE_AMOUNT, setAmountData) diff --git a/src/main/java/com/jforex/programming/order/task/BasicTask.java b/src/main/java/com/jforex/programming/order/task/BasicTask.java index de60a1cd..474f4924 100644 --- a/src/main/java/com/jforex/programming/order/task/BasicTask.java +++ b/src/main/java/com/jforex/programming/order/task/BasicTask.java @@ -9,6 +9,9 @@ import java.util.Collection; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import com.dukascopy.api.IOrder; import com.jforex.programming.math.CalculationUtil; import com.jforex.programming.order.OrderParams; @@ -36,6 +39,8 @@ public class BasicTask { private final OrderUtilHandler orderUtilHandler; private final CalculationUtil calculationUtil; + private final static Logger logger = LogManager.getLogger(BasicTask.class); + public BasicTask(final TaskExecutor taskExecutor, final OrderUtilHandler orderUtilHandler, final CalculationUtil calculationUtil) { @@ -75,15 +80,26 @@ public Observable mergeOrders(final MergeParams mergeParams) { } public Observable close(final CloseParams closeParams) { + logger.info("Called close"); return Observable .just(closeParams.order()) .filter(order -> !OrderStaticUtil.isClosed.test(order)) .flatMap(order -> evalCloseParmas(order, closeParams) - .andThen(orderUtilObservable(order, OrderCallReason.CLOSE))); + .andThen(orderUtilObservable(order, callReasonForClose(closeParams, order)))); + } + + private OrderCallReason callReasonForClose(final CloseParams closeParams, + final IOrder orderToClose) { + final double closeAmount = closeParams.partialCloseAmount(); + final double orderAmount = orderToClose.getAmount(); + return closeAmount > 0 && closeAmount < orderAmount + ? OrderCallReason.PARTIAL_CLOSE + : OrderCallReason.CLOSE; } private Completable evalCloseParmas(final IOrder orderToClose, final CloseParams closeParams) { + logger.info("Called evalCloseParmas"); return closeParams.maybePrice().isPresent() ? taskExecutor .close(orderToClose, @@ -176,6 +192,7 @@ public Observable setTakeProfitPrice(final SetTPParams setTPParams) private final Observable orderUtilObservable(final IOrder order, final OrderCallReason orderCallReason) { + logger.info("Called orderUtilObservable"); return orderUtilHandler.callObservable(order, orderCallReason); } } diff --git a/src/main/java/com/jforex/programming/order/task/TaskExecutor.java b/src/main/java/com/jforex/programming/order/task/TaskExecutor.java index b3527a42..c1e44e63 100644 --- a/src/main/java/com/jforex/programming/order/task/TaskExecutor.java +++ b/src/main/java/com/jforex/programming/order/task/TaskExecutor.java @@ -3,6 +3,9 @@ import java.util.Collection; import java.util.concurrent.Callable; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import com.dukascopy.api.IEngine; import com.dukascopy.api.IOrder; import com.dukascopy.api.OfferSide; @@ -18,6 +21,8 @@ public class TaskExecutor { private final StrategyThreadRunner strategyThreadRunner; private final IEngine engine; + private final static Logger logger = LogManager.getLogger(TaskExecutor.class); + public TaskExecutor(final StrategyThreadRunner strategyThreadRunner, final IEngine engine) { this.strategyThreadRunner = strategyThreadRunner; @@ -44,6 +49,7 @@ public Single mergeOrders(final String mergeOrderLabel, public Completable close(final IOrder order, final double amount) { + logger.info("Called normal close"); return completable(() -> order.close(amount)); } @@ -51,6 +57,7 @@ public Completable close(final IOrder order, final double amount, final double price, final double slippage) { + logger.info("Called complex close"); return completable(() -> order.close(amount, price, slippage)); diff --git a/src/test/java/com/jforex/programming/order/event/test/OrderEventTypeDataFactoryTest.java b/src/test/java/com/jforex/programming/order/event/test/OrderEventTypeDataFactoryTest.java index 911f41ef..0c03bd0b 100644 --- a/src/test/java/com/jforex/programming/order/event/test/OrderEventTypeDataFactoryTest.java +++ b/src/test/java/com/jforex/programming/order/event/test/OrderEventTypeDataFactoryTest.java @@ -136,6 +136,22 @@ public void closeEventTypeDataIsCorrect() { PARTIAL_CLOSE_OK)); } + @Test + public void partialCloseEventTypeDataIsCorrect() { + typeData = orderEventTypeDataFactory.forCallReason(OrderCallReason.PARTIAL_CLOSE); + + assertDoneEventTypes(EnumSet.of(PARTIAL_CLOSE_OK, CLOSE_OK)); + assertRejectEventTypes(EnumSet.of(CLOSE_REJECTED)); + assertInfoEventTypes(EnumSet.of(NOTIFICATION)); + assertFinishEventTypes(EnumSet.of(PARTIAL_CLOSE_OK, + CLOSE_OK, + CLOSE_REJECTED)); + assertAllEventTypes(EnumSet.of(NOTIFICATION, + CLOSE_OK, + CLOSE_REJECTED, + PARTIAL_CLOSE_OK)); + } + @Test public void setLabelEventTypeDataIsCorrect() { typeData = orderEventTypeDataFactory.forCallReason(OrderCallReason.CHANGE_LABEL); diff --git a/src/test/java/com/jforex/programming/order/task/test/BasicTaskTest.java b/src/test/java/com/jforex/programming/order/task/test/BasicTaskTest.java index b9f20f79..2cc9aec3 100644 --- a/src/test/java/com/jforex/programming/order/task/test/BasicTaskTest.java +++ b/src/test/java/com/jforex/programming/order/task/test/BasicTaskTest.java @@ -323,7 +323,7 @@ public void verifyTaskExecutorCall() { public class WithPriceSetup { - private final double closeAmount = 0.12; + private final double closeAmount = 0.05; private final double closePrice = 1.1234; private final double closeSlippage = 5.5; @@ -360,14 +360,14 @@ public class OnSubscribe { @Before public void setUp() { orderUtilForTest.setState(orderForTest, IOrder.State.FILLED); - setUpOrderUtilHandlerMock(emptyObservable(), OrderCallReason.CLOSE); + setUpOrderUtilHandlerMock(emptyObservable(), OrderCallReason.PARTIAL_CLOSE); testObserver = observable.test(); } @Test public void orderUtilHandlerIsCalled() { - verifyOrderUtilHandlerMockCall(OrderCallReason.CLOSE); + verifyOrderUtilHandlerMockCall(OrderCallReason.PARTIAL_CLOSE); } @Test