Order Cancellation
Overview
Have you ever placed an order on the forex market and then changed your mind? Maybe the market moved against you, or you realized you made a mistake, or you simply wanted to try a different strategy. Whatever the reason, you probably wished you could cancel your order before it was too late.
Well, with the FCLite API, you can do just that. The FCLite API is a simple and powerful tool that allows you to access the forex market data and execute trades programmatically. Unlike other APIs, it is designed for traders who want to focus on their trading logic and not worry about the technical details of the API.
In this article, you will learn how to cancel an order using the FCLite API. Cancelling an order means removing it from the system before it is executed or closed. This can help you avoid unwanted trades, save on commission fees, or adjust your trading strategy according to changing market conditions.
To cancel an order, you will need to:
- Identify the order ID of the order you want to cancel
- Use the removeOrder method to send a request to the API
- Handle the response from the API and check the status of the order By the end of this article, you will be able to cancel any order that you have placed using the FCLite API, as long as it is still in the orders table and has not been executed or closed.
Step 1
Identify the order ID of the order you want to cancel: To do this, you need to use the getOrders method, which returns a list of all orders in the orders table. You can filter the list by using the status parameter, which accepts values such as waiting, pending, open, or closed. You can also use the instrument parameter to filter by the currency pair. For example, if you want to get the order ID of a waiting limit order for EUR/USD, you can use the following code:
// Create a FCLite instance
const fclite = require('fclite');
const fc = new fclite.FCLite();
// Connect to the server
fc.connect('username', 'password', 'demo')
.then(() => {
// Get the list of all orders in the orders table
fc.getOrders()
.then((orders) => {
// Loop through the orders
for (let order of orders) {
// Check if the order is a waiting limit order for EUR/USD
if (order.status == 'waiting' && order.type == 'LIMIT' && order.instrument == 'EUR/USD') {
// Get the order ID
let orderID = order.id;
// Print the order ID to the console
console.log('Order ID: ' + orderID);
// Break the loop
break;
}
}
})
.catch((error) => {
// Handle the error
console.error(error);
});
})
.catch((error) => {
// Handle the error
console.error(error);
});
Step 2
Use the removeOrder method to send a request to the API: To do this, you need to provide the order ID as a parameter to the removeOrder method.
This method sends a request to the API to cancel the order and returns a promise that resolves to the response object.
For example: If you want to cancel the order with the ID of 123456789, you can use the following code:
// Create a FCLite instance
const fclite = require('fclite');
const fc = new fclite.FCLite();
// Connect to the server
fc.connect('username', 'password', 'demo')
.then(() => {
// Set the order ID
let orderID = '123456789';
// Use the removeOrder method to send a request to the API
fc.removeOrder(orderID)
.then((response) => {
// Handle the response
console.log(response);
})
.catch((error) => {
// Handle the error
console.error(error);
});
})
.catch((error) => {
// Handle the error
console.error(error);
});
Step 3
Handle the response from the API and check the status of the order: To do this, you need to check the status code and the body of the response object. If the status code is 200, it means the request was successful and the order was cancelled. If the status code is anything else, it means there was an error and the order was not cancelled. You can use the getLastError method to get the error code and message. You can also use the getOrder method to get the details of the order by providing the order ID as a parameter.
For example: If you want to handle the response and check the status of the order with the ID of 123456789, you can use the following code:
// Create a FCLite instance
const fclite = require('fclite');
const fc = new fclite.FCLite();
// Connect to the server
fc.connect('username', 'password', 'demo')
.then(() => {
// Set the order ID
let orderID = '123456789';
// Use the removeOrder method to send a request to the API
fc.removeOrder(orderID)
.then((response) => {
// Check the status code
if (response.status == 200) {
// The request was successful and the order was cancelled
console.log('The order was cancelled successfully.');
// Use the getOrder method to get the details of the order
fc.getOrder(orderID)
.then((order) => {
// Check the status of the order
if (order.status == 'cancelled') {
// The order is cancelled
console.log('The order status is cancelled.');
} else {
// The order is not cancelled
console.log('The order status is not cancelled.');
}
})
.catch((error) => {
// Handle the error
console.error(error);
});
} else {
// The request failed and the order was not cancelled
console.log('The order was not cancelled.');
// Use the getLastError method to get the error code and message
let errorCode = fc.getLastError().code;
let errorMessage = fc.getLastError().message;
// Print the error code and message to the console
console.error('Error code: ' + errorCode);
console.error('Error message: ' + errorMessage);
}
})
.catch((error) => {
// Handle the error
console.error(error);
});
})
.catch((error) => {
// Handle the error
console.error(error);
});
In conclusion, canceling an order is a simple process that can be done through the FCLite API. However, there are some limitations and conditions that need to be met before an order can be canceled. For example, the order must be in a pending state, and the order ID must be valid and specified. If the order cancellation is successful, the API will return a confirmation message and the order will be removed from the system. If the order cancellation fails, the API will return an error message and the order will remain in the system. Therefore, it is important to check the status and details of the order before attempting to cancel it.
| 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 |