This repository contains an example of a trading bot, demonstrating one of the ways the Capital.com Open API can be used. Please note that the aim of the bot is not to generate profit, but to provide a template for strategy building.
The trading bot strategy is built on the RSI indicator values. The bot opens a long position when RSI is < 30 and a short position when RSI is > 70. It also sets stop-loss and take-profit orders for every trade.
Please note that multiple entries in different directions are not supported by this bot. Therefore, if it receives a signal in the opposite direction, the existing position is closed at market price. It’s highly recommended that you use a separate trading account when testing the bot’s functionality.
Before you begin, ensure you have the following installed on your system:
- JDK 11 or higher
- Maven
- Docker (optional, for containerized deployment)
-
Clone the repository:
git clone https://github.com/capital-com-sv/api-java-samples.git cd api-java-samples -
Import the project into your IDE:
- Open the project in an IDE like IntelliJ IDEA or Eclipse.
- Ensure that Maven dependencies are loaded.
-
Configure your credentials and parameters:
-
Open the
application.ymlfile located in thesrc/main/resourcesdirectory. -
Update the following fields as needed:
capital.user: login: "your_login_here" password: "your_password_here" apiKey: "your_api_key_here" settings: epics: ["NVDA", "TSLA", "GOLD", "SILVER", "NATURALGAS", "OIL_CRUDE"] resolutions: ["HOUR"] quantityPresents: 5 quantityManual: 5 quantityUsingManual: true sltpPresents: 2 sltpPipsFromLowestPrice: 50 sltpUsingPips: true RSIPeriod: 14 finacc: "your_account_id_here"
-
Explanation of parameters:
login,password,apiKey: Your credentials for accessing the API.epics: List of instruments (e.g., "NVDA", "TSLA").resolutions: Timeframes for analysis (e.g., "HOUR").quantityPresents: Trade size as a percentage of available account balance.quantityManual: Fixed trade size.sltpPresents: Stop-loss/take-profit as a percentage of account balance.sltpPipsFromLowestPrice: Stop-loss/take-profit in pips.RSIPeriod: Period for RSI calculation.finacc: Financial account identifier.
-
-
Build the project:
mvn clean install
-
Run from your IDE:
- Locate the
Application.javafile in thesrc/main/java/com/capital/api/java/samplesdirectory. - Run the
mainmethod.
- Locate the
-
Run from the command line:
java -jar target/api-java-samples-1.0-SNAPSHOT.jar
-
Run with Docker (optional):
- Build the Docker image:
docker build -t api-java-samples . - Run the container:
docker run -e API_KEY="your_api_key_here" -e LOGIN="your_login_here" -e PASSWORD="your_password_here" api-java-samples
- Build the Docker image:
For additional testing and API exploration, you can use our Postman collection:
For frequently asked questions and troubleshooting, please visit our API FAQ.
- Capital.com Open API Documentation
- Contact support@capital.com for assistance.