After opening an account at SSI for the derivatives market, registering for the API service, investors can experience algorithmic trading at ALGOTRADE LAB with a minimum deposit to open 01 futures contract in their account. The high-level steps are as follows:
Open an account > Sign in > Set up API connection > Algorithmic parameter configuration > Algorithm launch > Algorithm monitoring
Open an Account and Sign In
Submit your account details at www.algotrade.vn/lab > Receive account information by email > Access www.lab.algotrade.vn and login to get started.
The directory structure of the algorithm includes:
-
config.ipynb: to configure the API and configure the parameters of the SMA algorithm;
-
data.ipynb: to store tick values of VN30F1M futures contract and compute SMA(t);
-
db.ipynb: a simple database to store order status, net profit/loss, etc.
-
main.ipynb: implement the algorithm and review execution log in real-time;
-
logs.log: contains the SMA value and all order information the algorithm has traded (only to check algorithm accuracy).
Set Up Api Connections
At the main interface of ALGOTRADE LAB, open file “config.ipynb” > Enter the required information/desired information in the following lines: CONSUMER_ID, CONSUMER_SECRET, PRIVATE_KEY, ACCOUNT, OTP.
Algorithm Parameter Configuration
Under the API connection key input, investors can adjust the algorithm parameters to their wishes. The following parameters can be changed:
-
CUT_LOSS_THRESHOLD: stop loss parameter. When reaching this threshold, the system will automatically close all open positions with market orders. The stop loss value will be derived from the current price and the actual open price, not the theoretical price. The default parameter is -3.
-
TAKE_PROFIT_THRESHOLD: take-profit parameter. The default parameter is 3. The system will automatically take profits when reaching this threshold.
-
MAX_ROUND: the number of times the algorithm can activate. One activation means one open position and one close position. The default parameter is 3, so there are 6 transactions in total. This is to ensure the investor’s experience without excessive trading per day.
-
START_TRADING_TIME: the algorithm start time. Investors can set any time during trading hours from 09:00:00 to 14:30:00. The default is 09:00:00.
After adjusting the configuration, click save (Ctrl + S or Command + S) to update the changes to the system. Here’s an example illustrating an investor who has completed the API connection and parameter configuration:
Algorithm Launch
After connecting the API and configuring algorithm parameters at the main.ipynb file, select ”Kernel” > click “Restart Kernel and Run All Cells” to start the algorithm.
Algorithm Monitoring
At the ALGOTRADE LAB interface, open “main.ipynb” to observe two key parts:
-
“SSI Algorithm Demo”: shows the code of the SMA algorithm.
-
“If you want to show log realtime …”: located under “SSI Algorithm Demo”, it displays real-time trading algorithm status updates.
After initializing the algorithm, the prompt “If you want to show log realtime …” will continuously show updated information.
Stop the Algorithm
In the file main.ipynb, click “Kernel” > “Shut Down Kernel” to stop the algorithm. The status line “KeyboardInterrupt” will display when the algorithm stops successfully.
Adjusting Algorithm Parameters During Operations
If you want to change the algorithm parameters when the system is running, you can do the following:
Open “config.ipynb” > Re-enter new parameters to be changed > Click save (Ctrl + S or Command + S) > In the main interface, click ”Kernel” > click “Restart Kernel and Run All Cells” to restart the algorithm.
Note it’s not necessary to stop the algorithm when changing its parameters.
Source Code
The following is a detailed explanation of the main source code snippets.
Import necessary libraries to run the algorithm, including the internal ALGOTRADE library.
Process position data from SSI, update the opened positions as well as the total profit/loss when the positions are closed. Then update the status of OPEN_POSITION to the initial state (None) to be able to open new positions if the algorithm spots new signals.
Initialize pub/sub redis to receive processed tick data as well as order status placed by the algorithm.
This is the function to open a new position with key information: ACCOUNT is the user account; F1 is the futures contract VN30F1M; side is ‘BUY’ or ‘SELL’; and price is the price to place orders.
This is the main part of the algorithm. After receiving one new tick, the system will compute SMA(N)(t) và SMA(N)(t-1)and check the condition:
-
If Price(t-1) < SMA(N)(t-1) and Price(t) >= SMA(N)(t) then the system will a new long position
-
If Price(t-1) > SMA(N)(t-1)and Price(t) <= SMA(N)(t) then the system will open a new short position
If the unrealized profit/loss (for the opened position) exceeds the profit/loss thresholds defined in the file "config.ipynb", then an opposite position will be opened to take profits or stop loss with a limit order at the ceiling or floor price.
At 14:29:50, if the position is still open then the system will open an opposite position to close it at ATC.