Skip to content

Modifying An Order

Overview

Modifying an order in the forex market means that traders can make adjustments to the specifics of their existing trade orders. These modifications may involve changing the price at which they want to enter or exit a position, altering the quantity of the trade, or extending or shortening the expiration date. By having the flexibility to modify orders, traders can react promptly to shifts in market conditions, ensuring that their trading strategies remain adaptable and responsive.

How to modify an order

In this article you will learn how to modify the details of an existing order: a price, a quantity, or an expiration date. This allows you to adjust your positions in response to changing market conditions. To modify and order, you need to follow these steps:

  1. Login (see the tutorial here)

  2. Create the request

  3. Configure editable order properties

  4. Executing the order change.

  1. Login (see the tutorial here)

  2. Create the request

  3. Configure editable order properties

  4. Executing the order change.

  1. Login (see the tutorial here)

  2. Create the request

  3. Configure editable order properties

  4. Executing the order change.

  5. Creating Listeners

  6. Printing the results in a readable format.

  7. Updating the html to include our new JavaScript files.

  8. The main JavaScript file.

Step 1: Login

Follow the steps in the login tutorial here

Step 2: Create a request to change an order

Create a request

const changeOrderRequestBuilder = ordersManager.getRequestFactory().createChangeOrderRequestBuilder();
changeOrderRequestBuilder.setOrderId(options.orderId);
const changeOrderRequestBuilder = ordersManager.getRequestFactory().createChangeOrderRequestBuilder();
changeOrderRequestBuilder.setOrderId(options.orderId);

In the JavaScript version, we will be creating a worker class that is responsible for passing the results to the classes that will display the results. It is also responsible for creating the request. orders\order-worker.js

class OrderWorker {
    constructor() {
        this.tableBuilder = new OrderTableBuilder();
    }

    #buildRequest(ordersManager, order) {
        let requestFactory = ordersManager.getRequestFactory();
        let requestBuilder = requestFactory.createChangeOrderRequestBuilder();

        return requestBuilder.setOrderId(order.getOrderId())
            .setAmount(20000)
            .build();
    }

    #getDataManagerStateString(state) {
        if (state.isNotLoaded())
            return 'not loaded';

        if (state.isLoading())
            return 'loading';

        if (state.isLoaded())
            return 'loaded';

        return '(has error)'
    }

    changeOrder(ordersManager, order) {
        let request = this.#buildRequest(ordersManager, order);
        ordersManager.changeOrder(request);
    }

    showOrderDetails(order) {
        this.tableBuilder.renderOrderDetailsSubtable(order);
    }

    updateState(state) {
        document.getElementById('state')
            .textContent = this.#getDataManagerStateString(state);
    }
}

Step 3: Configure editable properties

Select properties

changeOrderRequestBuilder.setAmount(10000);
changeOrderRequestBuilder.setRate(1.5214);
changeOrderRequestBuilder.setRateRange(0.0014);
changeOrderRequestBuilder.setTrailingType(FXConnectLite.TrailingStopType.Fixed);
changeOrderRequestBuilder.setTrailingStep(10);
changeOrderRequestBuilder.setAmount(10000);
changeOrderRequestBuilder.setRate(1.5214);
changeOrderRequestBuilder.setRateRange(0.0014);
changeOrderRequestBuilder.setTrailingType(FXConnectLite.TrailingStopType.Fixed);
changeOrderRequestBuilder.setTrailingStep(10);

In the order worker class that we just created, we have a method buildRequest which created the request object for us. Now we configure the changes that we want to make.

#buildRequest(ordersManager, order) {
    let requestFactory = ordersManager.getRequestFactory();
    let requestBuilder = requestFactory.createChangeOrderRequestBuilder();

    return requestBuilder.setOrderId(order.getOrderId())
        .setAmount(20000)
        .build();
}

In the above example, we are configuring amount, rate, rateRange, trailingType and trailingStep.

Step 4: Execute the order change with specified properties

Use the class

ordersManager.changeOrder(changeOrderRequestBuilder.build());
ordersManager.changeOrder(changeOrderRequestBuilder.build());  

In the order worker class we have a method changeOrder where we execute the order change.

changeOrder(ordersManager, order) {
    let request = this.#buildRequest(ordersManager, order);
    ordersManager.changeOrder(request);
}

Step 5: Creating Listeners

First we have the orders\order-change-listener.js
class OrderChangeListener {
    constructor(ordersManager, orderWorker) {
        this.ordersManager = ordersManager;
        this.orderWorker = orderWorker;
    }

    onChange(orderInfo) {
        console.log(`Order ${orderInfo.getOrderId()} changed to status '${orderInfo.getStatus()}'.`);

        let order = this.ordersManager.getOrderById(orderInfo.getOrderId());
        this.orderWorker.showOrderDetails(order);
    }

    onAdd(orderInfo) {
        console.log(`Order ${orderInfo.getOrderId()} added.`);
    }

    onDelete(orderInfo) {
        console.log(`Order ${orderInfo.getOrderId()} deleted.`);
    }

    onError(orderInfo) {
        console.log(`Order error: ${orderInfo.getError().getMessage()}`);
    }
}
Next we have the orders\order-state-listener
class OrderStateListener {
    constructor(ordersManager, orderWorker) {
        this.ordersManager = ordersManager;
        this.orderWorker = orderWorker;
    }

    onStateChange(state) {
        this.orderWorker.updateState(state);
        if (!state.isLoaded()) 
            return;

        let orders = this.ordersManager.getOrdersSnapshot();
        if (orders.length === 0) {

            return;
        }

        this.orderWorker.changeOrder(this.ordersManager, orders[0]);
    }
}

Step 6: Printing the results in a readable format.

For printing the results of the command, we have first created a file which will contain our TableBuilder class. Follow the instruction in the Table Builder tutorial here.

Next we create the orders\order-table-builder.js file which will pass our query result to the table printing class.

class OrderTableBuilder {
    constructor() {
        this.builder = new TableBuilder();
    }

    #getOrderDetails(order) {
        return new Map([
            ['Order ID', order.getOrderId()],
            ['Account name', order.getAccountId()],
            ['Account kind', order.getOfferId()],
            ['Offer ID', order.getAmount()],
            ['Amount', order.getRate()],
            ['Buy/Sell', order.getType()],
            ['Open rate', order.getStatus()],
            ['Open time', order.getBuySell()]
        ]);
    }

    #renderOrderTableHeader() {
        let columns = [
            { className: 'col-sm-12', value: 'Entry Order' }
        ];

        return this.builder.createHeaderTableRow(columns);
    }

    renderOrderDetailsSubtable(order) {
        let row = this.#renderOrderTableHeader();

        let details = this.#getOrderDetails(order);
        let columns = this.builder.createDataSubtableColumns(details);

        this.builder.createDataSubtable(row, columns, 'row mb-3 details');

        this.builder.showContainer();
    }
}

Step 7: Updating the html to include our new JavaScript files.

The following lines need to be added to the main html file.
<script src="connection_management/connection-status-change-listener.js"></script>
<script src="orders/order-change-listener.js"></script>
<script src="orders/order-state-listener.js"></script>
<script src="orders/order-table-builder.js"></script>
<script src="orders/order-worker.js"></script>
<script src="common/table-builder.js"></script>
<script src="index.js"></script>

Step 8: The main JavaScript file.

index.js
const sessionFactory = FXConnectLite.FXConnectLiteSessionFactory;
const LoggerFactory = FXConnectLite.LoggerFactory;

document.addEventListener('DOMContentLoaded', () => {
    let session = sessionFactory.create('ModifytOrderSample');
    session.setLogger(LoggerFactory.createLogger(LoggerFactory.LEVEL_DEBUG));

    let orderWorker = new OrderWorker();
    let ordersManager = session.getOrdersManager();
    let orderChangeListener = new OrderChangeListener(ordersManager, orderWorker);
    ordersManager.subscribeOrderChange(orderChangeListener);
    ordersManager.subscribeStateChange(new OrderStateListener(ordersManager, orderWorker));

    let connectionStatusChangeListener = new ConnectionStatusChangeListener(session, ordersManager);
    session.subscribeConnectionStatusChange(connectionStatusChangeListener);

    bindConnectionRelatedEvents(session, () => ordersManager.refresh());
});

When the order is modifying, you can:

  1. Get available instruments.
  2. Subscribe and Unsubscribe to instruments.
  3. Get price updates for an instrument.
  4. Get historical prices for an instrument.
  5. Get open positions.

Conclusion

In this article, you learned how to modify an order using the FCLite API. You can modify the details like a price, a quantity, or an expiration date. This allows you to adjust your positions in response to changing market conditions. Also you learned how to use the changeOrderRequestBuilder to create a request to change an order. Finally, you learned how to use the class with selected properties.

Download the sample JavaScript.

Table of Contents
Order Types
This page will explain each of the different types of orders in the system.
Get All Orders
The article teaches how to get a list of all orders.
Modify an Order
The article teaches how to modify an order
Create a Market Order
The article teaches how to create a market order
Create an Entry Order
API Command used to create an entry order
Create a Close Market Order
The article teaches how to create a close 'market order'
Create an OCO Order
API Command used to create an OCO order
Create a Stop and/or Limit Order
This article provides information about how to add a stop/limit to an order
Cancel an Order
This article provides information about how to cancel an order