Skip to content

Commit

Permalink
Bug fix: partial closing order has not onComplete event.
Browse files Browse the repository at this point in the history
Upgraded rxJava and JForexAPI
  • Loading branch information
juxeii authored and juxeii committed Apr 5, 2017
1 parent 163f7c1 commit ba954bc
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 6 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,6 +20,8 @@ public class OrderUtilHandler {
private final OrderEventTypeDataFactory orderEventTypeDataFactory;
private final JFHotPublisher<OrderCallRequest> callRequestPublisher;

private final static Logger logger = LogManager.getLogger(OrderUtilHandler.class);

public OrderUtilHandler(final OrderEventGateway orderEventGateway,
final OrderEventTypeDataFactory orderEventTypeDataFactory,
final JFHotPublisher<OrderCallRequest> callRequestPublisher) {
Expand All @@ -27,6 +32,7 @@ public OrderUtilHandler(final OrderEventGateway orderEventGateway,

public Observable<OrderEvent> callObservable(final IOrder orderOfCall,
final OrderCallReason callReason) {
logger.info("Called callObservable");
return Observable
.just(orderOfCall)
.doOnSubscribe(d -> callRequestPublisher.onNext(new OrderCallRequest(orderOfCall, callReason)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/com/jforex/programming/order/task/BasicTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -75,15 +80,26 @@ public Observable<OrderEvent> mergeOrders(final MergeParams mergeParams) {
}

public Observable<OrderEvent> 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,
Expand Down Expand Up @@ -176,6 +192,7 @@ public Observable<OrderEvent> setTakeProfitPrice(final SetTPParams setTPParams)

private final Observable<OrderEvent> orderUtilObservable(final IOrder order,
final OrderCallReason orderCallReason) {
logger.info("Called orderUtilObservable");
return orderUtilHandler.callObservable(order, orderCallReason);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -44,13 +49,15 @@ public Single<IOrder> mergeOrders(final String mergeOrderLabel,

public Completable close(final IOrder order,
final double amount) {
logger.info("Called normal close");
return completable(() -> order.close(amount));
}

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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ba954bc

Please sign in to comment.