Recommended resources
The freely available resources this lesson will direct you to.
Welcome to the third lesson in our journey to build an algorithmic trading bot using AI-assisted development. In our previous sessions, we decomposed our bot concept into the Data, Strategy, and Execution components, and then created a project blueprint by defining the interfaces and data flow between them.
Now, with our blueprint in hand, it's time to start generating the actual code. This lesson focuses on the single most important skill for AI-assisted development: formulating a detailed and effective prompt. You will learn how to translate a functional requirement from your blueprint into a precise instruction that guides an AI assistant to write the exact code you need. Mastering this will dramatically accelerate your development process and is the key to getting predictable, high-quality results from tools like GitHub Copilot.
The Anatomy of an Effective Prompt
A prompt is more than just a question; it's a comprehensive specification. A poorly constructed prompt like "write a trading function" will yield generic, unusable code. A well-constructed prompt, however, acts as a detailed work order for your AI assistant.
To understand the essential ingredients of a powerful prompt, let's start with a quick overview of a proven formula.
Master the Perfect ChatGPT Prompt Formula (in just 8 minutes)!
This video, "Master the Perfect ChatGPT Prompt Formula," breaks down any effective prompt into six core components. While the examples aren't all code-related, the principles are universal and provide a fantastic mental checklist.
Watch the entire video (it's only 8 minutes) and focus on these six building blocks: Task (what to do): The specific action you want the AI to perform. It should be clear and start with a verb (e.g., "Generate," "Write," "Analyze"). Context (the background): The background information the AI needs. This is where your project blueprint comes in. Exemplars (examples): Sample inputs, outputs, or structures to follow. This is one of the most powerful ways to improve output quality. Persona (the AI's role): The role you want the AI to adopt (e.g., "expert Python developer"). This primes the AI to respond from a specific point of view. Format (the output structure): The exact structure you want the output in (e.g., a Python function, a JSON object, a table). Tone (the style): The desired style of the response. For code, this often translates to "well-commented," "concise," or "adhering to PEP 8 style." As you watch, think about how you would apply each of these to a request for a function in your trading bot.
From Components to a Coherent Structure
Knowing the components is the first step. The next is assembling them into a coherent structure. Prompt engineering frameworks provide battle-tested templates for this purpose. They turn the "mental checklist" from the video into a fill-in-the-blanks system for consistently creating great prompts.
Prompt Engineering Frameworks: Complete Systems for ...
This article introduces several powerful frameworks for structuring prompts. We will focus on two that are particularly well-suited for code generation.
First, read the introduction to understand the difference between patterns and frameworks. Next, study the RACE Framework (Role, Action, Context, Expectation). This is an excellent, straightforward framework for most coding tasks. Notice how "Expectation" combines the "Format" and "Tone" components we saw earlier. Then, review the RISEN Framework (Role, Instructions, Steps, End Goal, Narrowing). This is a more detailed framework, perfect for complex functions where you need to specify a sequence of operations and strict constraints. "Narrowing" is especially important for preventing the AI from making poor choices or using incompatible libraries.
Practical Prompting Techniques for Code
Now, let's look at how these concepts are applied specifically in the domain of AI-assisted coding. The following table from Addy Osmani, an engineer at Google, summarizes ten essential techniques.

Notice how these techniques operationalize the components:
- Role Prompting directly implements the
PersonaorRole. - Explicit Context Setup is all about providing rich
Context. - Input/Output Examples is a clear use of
Exemplars. - Constraint Anchoring is a powerful way to define the
Format,Narrowing, or other rules the AI must follow.
For a deeper dive into the nuances of prompting for code, the following video offers a wealth of practical tips.
AI Coding 101: Ultimate Prompt Guide (37 tips)
This video, "AI Coding 101: Ultimate Prompt Guide," is packed with tips from a developer who uses AI assistants extensively.
Watch these key sections to pick up some pro tips: Specify Technologies (be specific): Always tell the AI which libraries and frameworks to use (e.g., pandas, requests). This signals your intent and improves code quality. Design First (break it down): This reinforces what we learned in the last lesson. Don't just ask for a feature; give the AI a list of specific implementation steps from your blueprint. Use Examples (provide samples): This confirms the power of exemplars. You can provide sample data formats or even snippets of existing, working code to guide the AI. Naming Matters (be clear): Be deliberate with your naming of functions and variables in your prompt. The AI uses these names as context to understand your intent and maintain consistency.
Putting It All Together: Crafting a Detailed Prompt
Let's apply everything we've learned to write a high-quality prompt for a function in our Moving Average Crossover bot. Our goal is to create the calculate_moving_averages function within the Strategy Engine.
A weak, ineffective prompt would be:
"Write a python function to calculate moving averages for stock data."
This leaves everything to the AI's imagination: What library to use? What is the input data format? What should the output be? The result will be generic and likely incompatible with our blueprint.
Now, let's build a strong, detailed prompt using the RACE framework and our new techniques:
[Role]
You are an expert Python developer with deep expertise in quantitative finance and the pandas library.
[Action]
Write a Python function named calculate_moving_averages.
[Context]
This function is a core part of the Strategy Engine for an algorithmic trading bot that trades Forex pairs. Its purpose is to take in historical price data and add simple moving average (SMA) indicators to it.
- Tech Stack: The project uses Python 3.10 and pandas. All data manipulation must be done using pandas.
- Input Data: The function will receive a pandas DataFrame. This DataFrame is guaranteed to have columns named
timestamp,open,high,low, andclose. The 'close' column should be used for all calculations. - Project Goal: The returned DataFrame will be used by another function to check for moving average crossover signals.
[Expectation]
- Function Signature: The function must accept three arguments:
data(type:pandas.DataFrame): The input price data.short_window(type:int): The period for the short-term SMA.long_window(type:int): The period for the long-term SMA.
- Behavior:
- It must calculate a short-window SMA and add it to the DataFrame in a new column named
SMA_short. - It must calculate a long-window SMA and add it to the DataFrame in a new column named
SMA_long. - It must correctly handle the beginning of the DataFrame where there isn't enough data for a full window calculation (these initial values should be
NaN). - It must return the entire modified DataFrame.
- It must calculate a short-window SMA and add it to the DataFrame in a new column named
- Format & Constraints:
- The code must be a single, self-contained Python function.
- Use type hints for all function arguments and the return value.
- Include a comprehensive docstring explaining the function's purpose, parameters, and return value.
- Do not use any external libraries other than pandas.
This detailed prompt leaves no room for ambiguity. It specifies the persona, the exact task, the project context, the input/output contract, and the stylistic constraints. By providing this level of detail, you are guiding the AI to generate code that is not only functional but also fits perfectly into the architectural blueprint you designed.
Conclusion
In this lesson, you have learned how to move from a high-level blueprint to a low-level implementation request. You now have a systematic process for formulating detailed prompts that produce high-quality, targeted code from an AI assistant.
Key Takeaways:
- Structure is key: Use frameworks like RACE (Role, Action, Context, Expectation) to ensure your prompts are comprehensive.
- Context is king: Provide the AI with the project goal, tech stack, and data formats. This is the information from your blueprint.
- Be explicit in your expectations: Clearly define the function's signature, behavior, and desired output format. Use examples when possible.
- Constraints guide to success: Tell the AI what libraries to use and what patterns to avoid. This prevents it from making poor or incompatible choices.
In our next lesson, we will continue to hone this skill by focusing on how to refine prompts with even more specific constraints, particularly for enforcing library usage, code style, and robust error handling.