Comprehensive Guide to Setting Up and Using Pytest with TBOT on TradingBoat
Comprehensive Guide to Setting Up and Using Pytest with TBOT on TradingBoat
Introduction
This guide details the installation process of Pytest for TBOT on TradingBoat. It assumes prior installation of TBOT on TradingBoat. Note that additional Python packages will be required for Pytest, as TBOT's dependencies for Pytest are not included in the requirements.txt
file.
The Importance of Test Cases
TBOT on TradingBoat supports various order types including Stop Orders, Limit Orders, Market Orders, Attached Orders, and Bracket Orders (Profit Take, Stop Loss) across multiple markets like Stock, Cryptocurrency, Forex, and Futures. When modifying or customizing code, it's crucial to ensure that existing logic remains intact and compatibility is maintained. Running test cases, particularly with Pytest, is an effective way to achieve this.
Setting Up Paper Funding
To effectively run Pytests during trading hours and while placing orders, it's essential to adequately fund your paper account. The following image illustrates how to fund your paper accounts. Ensure that you have sufficient margin to avoid system errors like the one shown below, indicating inadequate funds.
If you don’t have enough margin, TBOT will receive System Error code 201 as the following screenshot.
Why Use Pytest?
- Customization: If you are customizing TBOT on TradingBoat, running Pytest post-modification is crucial to ensure your changes don't disrupt the existing API.
- Understanding TBOT: Pytest helps gain a deeper insight into TBOT's functionalities.
Setting Up Pytest
Prerequisites
- Ensure TBOT is installed on your system.
Installation Steps:
Activate the Python virtual environment for TBOT:
source .venv/bin/activate pip install -r requirements.txt pip install -e .
Install necessary packages:
pip install pytest pip install twisted pip install pytest-twisted pip install pytest-asyncio pip install pytest-orde
Important Test Case
The critical test case is to close all positions and cancel all pending orders. This is to ensure a fresh start for each test session.
Run the test case for closing all positions:
pytest tests/pytest/strategy_close/test_close_all_positions.py
Running the following command automatically handles closing all positions before other test cases:
pytest tests/pytest
Adjusting Delay
Background
Pytest sends Webhook messages to a Web Server (Flask), which places them into a Redis Server. TBOT retrieves these messages, tracks order status using the ib_insync API, and saves the data into databases. This process introduces delays between order placement and result reception.
Controlling Delays:
Logging Level: Reduce logging levels to minimize output:
TBOT_LOGLEVEL=INFO TBOT_IB_LOGLEVEL=WARN
Database Response Time: Adjust waiting times for database updates. These vary depending on the server's setup.
Monitoring IB Gateway
While running Pytest, monitor the IB Gateway or TWS workstation closely. Depending on their versions, they may require manual approval or confirmation for certain actions, such as cryptocurrency order confirmations.
Further details and procedures, I highly recommend referring to the comprehensive Udemy course available here: Link to Udemy Course.