Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Setting Up a Python Project: Virtual Environments and Secure API Keys

In our last session, you mastered a crucial skill: using your AI assistant as a research analyst to identify and evaluate Forex APIs. You now have a chosen API and its documentation, which is the gateway to the market data your bot will need. With that decision made, we can move from planning and research to the first phase of construction.

This lesson focuses on building the foundational skeleton of your project. Before writing any trading logic, a professional software project needs a solid structure. We will use your AI assistant to generate a clean project layout, create an isolated environment for its dependencies, and establish a secure method for handling your API keys. Mastering this setup is a critical step towards building a robust and maintainable trading bot.

1. Generating a Professional Project Structure

Just like an artist prepares a canvas before painting, a developer sets up a project structure before coding. A well-organized project is easier to navigate, debug, and expand. Instead of manually creating folders, we can ask our AI assistant to propose a structure based on best practices.

A common and effective structure for data-driven applications like trading bots is shown below. This will be our target model.

A typical project structure for a machine learning or data-intensive application. It separates source code (`src`), data, configuration (`config`), and tests into distinct directories.

This structure separates concerns logically:

  • src/: Contains your core Python source code (e.g., data connection, strategy logic, execution).
  • data/: For storing any local data, like historical price CSVs.
  • config/: For configuration files.
  • tests/: For code that tests your bot's functionality.
  • Environment Files: Files that manage the project's setup, like requirements.txt for dependencies and .env for secrets.

So, how do we get our AI to suggest a structure like this? We can use a conversational prompting technique where we ask the AI to prompt us for more details.

Essential AI prompts for developers

The "Q&A Strategy" is a powerful way to guide an AI towards a better solution by having it ask you clarifying questions. Watch this clip from the Visual Studio Code channel to see it in action.

Pay close attention to how the developer asks the AI to propose a folder structure and then asks it to ask yes/no questions to refine the recommendation. This interactive process helps the AI understand your specific needs.

Now, apply this technique. Open your AI assistant (like VS Code Copilot Chat or ChatGPT) and use a prompt like this:

Initial Prompt for Project Structure:

"I'm starting a new algorithmic trading bot project in Python. Propose a professional folder and file structure for it. To help you give me the best recommendation, ask me some clarifying questions about the project's requirements first."

The AI will likely ask you questions similar to these:

  • Will the project include unit or integration tests? (Answer: Yes)
  • Will you need to manage separate configuration files for development and production? (Answer: Yes)
  • Will the bot save any data locally? (Answer: Yes, potentially historical data)
  • Will you be connecting to an external API? (Answer: Yes)

By answering these, you guide the AI to generate a comprehensive structure that includes folders like src, tests, config, and files like main.py and requirements.txt, closely mirroring the standard layout we aim for.

2. Setting Up an Isolated Python Environment

Your trading bot will depend on several external Python packages (e.g., for API requests, data analysis). To prevent conflicts with other Python projects on your machine, it's essential to create a virtual environment. This is an isolated sandbox containing only the packages your bot needs.

The process involves three main steps: creating the environment, activating it, and listing your dependencies in a requirements.txt file.

Building a Simple AI Agent With Python and Langchain

This article provides a clear, step-by-step guide for setting up a Python project with a virtual environment. Although the project is different, the setup process is universal.

Follow these steps in the article: Start by creating a requirements.txt file in your project folder, as described in the section "Let's Get Started". For now, you can add python-dotenv to this file. We'll add more packages later. Next, create the virtual environment. In your terminal, from your project's root directory, run the command shown in the article to create the environment. This will create a venv folder in your project. Now, activate it using the appropriate command for your operating system (Mac/Linux or Windows) as detailed in the section right after, under "go into our virtual environment". You'll know it's active because (venv) will appear at the start of your terminal prompt. Finally, install the packages from your requirements.txt file by running the command pip install -r requirements.txt, as shown in the section about installing dependencies.

For a visual walkthrough of this process inside the VS Code editor, which you may be using, the following video is an excellent supplement.

How To Setup A Virtual Environment For Python In Visual Studio Code In 2023

This video from Indently demonstrates creating and activating a virtual environment specifically within VS Code.

Watch these key segments: The explanation of why a virtual environment is necessary for managing packages. The process of creating and activating the environment in the VS Code terminal. The final demonstration of installing a package with pip once the environment is active.

Your project is now running in a clean, isolated environment. Any packages you install will be contained within this venv folder and won't interfere with anything else on your system.

3. Securing Your API Keys

In the last lesson, you researched an API and now have (or will soon get) an API key and secret. These are credentials and must be kept private. Never, ever write them directly into your Python code or commit them to a public repository like GitHub.

The standard practice is to store them in a special file that is ignored by your version control system and load them into your program at runtime.

How to Build a Free Trading Bot with ChatGPT and Alpaca

This guide for building a trading bot with Alpaca's API demonstrates several best practices, including how to handle API keys securely.

In the section on "Converting Pseudocode to Functional Code," find the list of bullet points under "To achieve our objectives...". The article explicitly states the correct approach: "Store your API key in '.env' file". This is the method we will implement.

Here is the full process:

  1. Create a .env file: In the root directory of your project, create a new file named exactly .env.

  2. Add your keys: Inside the .env file, add your API key and secret in the following format. Replace your_actual_api_key and your_actual_api_secret with the credentials from your chosen broker.

    API_KEY="your_actual_api_key"
    API_SECRET="your_actual_api_secret"
    
  3. Ignore the file: Create another file in your root directory named .gitignore. This file tells Git (your version control system) which files to ignore. Add .env to this file to ensure you never accidentally commit your secrets. Your .gitignore should contain at least:

    .env
    venv/
    __pycache__/
    
  4. Load the keys in Python: Use the python-dotenv library (which you added to requirements.txt and installed) to load these variables. You can ask your AI assistant to generate the code for you.

    Prompt for Loading Environment Variables:

    "Using Python, write a code snippet to load environment variables named API_KEY and API_SECRET from a .env file into my program. Use the python-dotenv and os libraries."

    The AI will generate code similar to this:

    import os
    from dotenv import load_dotenv
    
    
    
    
    # Load environment variables from .env file
    load_dotenv()
    
    
    
    
    # Access the variables
    api_key = os.getenv("API_KEY")
    api_secret = os.getenv("API_SECRET")
    
    
    
    
    # Now you can use api_key and api_secret to connect to your broker
    # (For now, you can just print them to verify they loaded correctly)
    # print(f"API Key: {api_key}")
    # print(f"API Secret: {api_secret}")
    

    This snippet can be placed at the beginning of your main script (main.py or bot.py) to make the credentials available to the rest of your application.

Conclusion

Congratulations, you have successfully laid the groundwork for your trading bot. While we haven't written any trading logic yet, you've completed the essential, professional setup that will support the entire project.

Key Takeaways:

  • Prompt for Structure: Use conversational, Q&A-style prompts to have your AI assistant help you design a clean, maintainable project structure.
  • Isolate with Virtual Environments: Always use a virtual environment (venv) to manage your project's dependencies and avoid conflicts.
  • Secure Secrets with .env: Never hard-code API keys. Store them in a .env file, load them with python-dotenv, and add .env to your .gitignore file.

You now have a complete project skeleton, an isolated environment, and a secure way to manage your API credentials. You are fully prepared to start building the functional components of your bot.

In the next lesson, we will put this structure to use. You will prompt your AI assistant to generate the first functional module: a data connector that uses your secured API keys to fetch live Forex data based on the documentation you researched previously.

Can't find a good explanation? Sign up and we'll make it for you

Sign up