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 TBOT on TradingBoat is available under both GPLv3 and Commercial licenses. While TBOT's source code remains the same across both licensing options, the Commercial License offers exclusive access to this Test Framework (Pytest). Pytest for TBOT on TradingBoat is not open source and is only available with a commercial license.

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.

Warning: Pytest never should be run on Live Account of IBKR. It should only run the paper account. All orders and position on the paper account will be modified by Pytest. You should be aware of what you are doing

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.

Paper Trading Account Reset

If you don’t have enough margin, TBOT will receive System Error code 201 as the following screenshot.

Warning: Order rejected - reason:YOUR ORDER IS NOT ACCEPTED. IN ORDER TO OBTAIN THE DESIRED POSITION YOUR NET LIQ [61593.32 GBP] MUST EXCEED THE MARGIN REQ [70777.23 GBP]

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.

 

Cryptocurrency order confirmation

Further details and procedures, I highly recommend referring to the comprehensive Udemy course available here: Link to Udemy Course.

Subscribe to TBOT on TradingBoat

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe