-
Notifications
You must be signed in to change notification settings - Fork 10
Submit
This page describes how you can create and submit orders.
Class: OrderParams
/ Package: com.jforex.programming.order
Before submitting a new order one has to specify some mandatory and some optional parameters of the order.
By looking at the IEngine page from Dukascopy
you see that there exists many overloads of the submitOrder
method. It is very hard to remember which method to use and the order of the parameters is also cumbersome to work with.
Here comes the class OrderParams
to the rescue which provides a fluent builder for defining all your order parameters. This class has a static API so you can use directly without an instance.
Here's a minimal example of how you can define an OrderParams
instance:
OrderParams eurusdParams = OrderParams
.forInstrument(Instrument.EURUSD)
.withOrderCommand(OrderCommand.BUY)
.withAmount(0.001)
.withLabel("TestLabel")
.build();
This reads a lot simpler then the native API submitOrder
methods and is also more flexible. This example only specifies the mandatory parameters. The next snippet defines also all optional parameters:
OrderParams eurusdParams = OrderParams
.forInstrument(Instrument.EURUSD)
.withOrderCommand(OrderCommand.BUYLIMIT)
.withAmount(0.001)
.withLabel("TestLabel")
.stopLossPrice(1.1234)
.takeProfitPrice(1.1290)
.goodTillTime(0L)
.price(0.0)
.slippage(2.0)
.comment("Full parameter specified")
.build();
OrderParams audusdParams = eurusdParams
.clone()
.forInstrument(Instrument.AUDUSD)
.build();
After specifying an optional parameter you can always finish the creation of the instance by calling build()
. Another nice feature of OrderParams
is the possibility to clone an existing object and overwrite only that part you want to change(e.g. the instrument as in the above example). This saves a lot of typing and enables good reuse.
Class: SubmitParams
/ Package: com.jforex.programming.order.task.params.basic
After specifying the order parameters we now specify what should happen during the process of the order submission until the order is fully filled. JForexUtils provides you a fluent builder for this task.
Here's an example of specifying the SubmitParams
:
final SubmitParams submitParams = SubmitParams
.withOrderParams(eurusdParams)
.doOnStart(() -> System.out.println("Starting to submit order " + eurusdParams.label()))
.doOnComplete(() -> System.out.println("Order " + eurusdParams.label() + " was submitted."))
.retryOnReject(3, 1500L)
.build();
After specifying the OrderParams
and SubmitParams
we finally submit a new order to the server. All we need to do is calling submitOrder
on the OrderUtil
instance which is directly at your disposal after extending from JForexUtilsStrategy
.
Here's a full example of submitting an order within the onJFStart
method:
@Override
protected void onJFStart(final IContext context) throws JFException {
final OrderParams eurusdParams = OrderParams
.forInstrument(Instrument.EURUSD)
.withOrderCommand(OrderCommand.BUY)
.withAmount(0.001)
.withLabel("TestLabel")
.build();
final SubmitParams submitParams = SubmitParams
.withOrderParams(eurusdParams)
.doOnStart(() -> System.out.println("Starting to submit order " + eurusdParams.label()))
.doOnComplete(() -> System.out.println("Order " + eurusdParams.label() + " was submitted."))
.retryOnReject(3, 1500L)
.build();
orderUtil.submitOrder(submitParams);
}