Create your own
Lesson illustration

Building a Reproducible Commodity and Company Evidence Report

Welcome to the final lesson in our module on building a Python and Pine Script research workflow. In our previous lessons, we've journeyed from acquiring and analyzing market data to implementing and, crucially, rigorously evaluating a trading signal. You learned to adopt a skeptical mindset, using walk-forward analysis to protect against common pitfalls like overfitting and look-ahead bias.

This lesson addresses the final, critical step in the research process: communicating your findings. A brilliant analysis is of little use if it's not presented in a clear, coherent, and verifiable way. Your goal is to learn how to generate a reproducible chart-and-data evidence report for a commodity and a related listed company. We will use a powerful tool called Quarto, which allows you to weave together narrative text, executable Python code, and the resulting outputs (like charts and tables) into a single, professional document.

For someone with your extensive development background, this concept should feel very natural. It’s akin to literate programming or generating documentation directly from a codebase—it ensures that your analysis and conclusions are directly and provably linked to the code that produced them. This practice forms the bedrock of a disciplined, evidence-based approach to trading.

1. From Analysis to Artifact: The Power of Reproducible Reports

Before diving into the "how," let's establish the "why." A reproducible report is a document that can be automatically regenerated from its source files (code, data, and text). This isn't just an academic exercise; for a trader, it offers significant advantages:

  • Discipline and Clarity: The process of creating a report forces you to structure your analysis logically. You tell a story with data, starting with a premise and supporting it with evidence.
  • Verification and Trust: Your future self will thank you. When you review a trade idea weeks later, you can re-run the report and know with certainty how you arrived at your conclusions, rather than trying to decipher old, disconnected scripts.
  • Efficiency: Once you have a template, you can rapidly generate the same report for different assets, saving immense amounts of time. Need to update the analysis with the latest data? Simply re-render the document.

The tool we will use to achieve this is Quarto. It's an open-source publishing system that understands Python code blocks within a markdown file, runs the code, and embeds the output seamlessly.

The following short video from Posit, the creators of Quarto, gives a great introduction to what it is and why it's so useful for bridging the gap between analysis and a shareable report.

Quarto Crash Course | Create Professional Reports, Dashboards & Websites w/ Markdown & Python Code!

Watch the initial overview to understand the core concept of Quarto.

Focus on the segment from the introduction, which explains how Quarto turns developer-friendly formats (like code notebooks) into professional, digestible documents.

2. Setting Up Your Reporting Environment with Quarto

Getting started with Quarto involves installing the command-line interface (CLI) and an extension for your code editor (we'll assume VS Code). Given your background, the installation will be straightforward.

The official Quarto documentation and the crash course video provide clear instructions.

Quarto Crash Course | Create Professional Reports, Dashboards & Websites w/ Markdown & Python Code!

This part of the "Quarto Crash Course" will walk you through setting up your environment.

Follow the instructions for installation and setup. This covers installing the Quarto CLI and the VS Code extension, and creating your first .qmd (Quarto markdown) file.

A Quarto document (.qmd file) has three main components:

  1. A YAML header at the top, enclosed in ---, for metadata like title, author, and output format.
  2. Markdown text for your narrative, explanations, and conclusions.
  3. Executable code chunks, opened with ```{python} and closed with ```, to run your analysis.

3. Building Your First Evidence Report

Let's build a sample report to analyze the relationship between a commodity and a related equity. We'll use LME Copper futures and the mining company Freeport-McMoRan (FCX) as our example.

Step 1: The YAML Header

Start your report.qmd file with a YAML header. This section from the Quarto video explains how to define the document's metadata.

Quarto Crash Course | Create Professional Reports, Dashboards & Websites w/ Markdown & Python Code!

Watch this segment to understand how to structure the document header.

Focus on the explanation of YAML settings. For now, we only need title, author, date, and format. Your header should look like this:

---
title: "Analysis of LME Copper vs. Freeport-McMoRan (FCX)"
author: "Your Name"
date: "today"
format: html
---

Step 2: The Data Gathering Code Block

Now, we'll add our first Python code block to fetch the necessary market data. We'll use the yfinance library, which you've worked with before. The goal is to embed the entire data-gathering process within the report itself.

The following article provides the exact code patterns we need.

How to download market data with yfinance and Python · PythonFinTech

This article from PythonFinTech demonstrates how to use yfinance to download data for multiple tickers at once.

Review the sections on downloading data and multiple tickers. Pay close attention to how yf.download handles a list of tickers and creates a MultiIndex DataFrame.

In your .qmd file, add a code block that imports the necessary libraries and downloads the data for copper futures (HG=F) and Freeport-McMoRan (FCX).

```{python}
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Define tickers and date range
tickers = ["HG=F", "FCX"]
start_date = "2022-01-01"
end_date = "2024-01-01"

# Download data
data = yf.download(
    tickers,
    start=start_date,
    end=end_date,
    group_by="ticker",
    auto_adjust=True
)

# Display the first few rows to confirm
print("Data downloaded successfully:")
data.head()
```

When this report is rendered, Quarto will execute this code. The print statement will output a formatted table directly into your document.

Step 3: The Charting Code Block

The most powerful feature of this workflow is embedding visualizations. Let's create a chart to compare the normalized performance of copper and FCX. This requires adding another code block.

Quarto uses special #| comments to control code chunk options. We'll use this to add a caption and a label for cross-referencing.

Using Python

The official Quarto documentation explains how to create executable code blocks and control their output.

Read the Overview section. Focus on the example showing how a Python code block with #| label: and #| fig-cap: comments is used to generate a cross-referenceable figure.

Now, add the following code block to your report. It normalizes the closing prices and plots them.

```{python}
#| label: fig-price-comparison
#| fig-cap: "Normalized Price Comparison: Copper vs. FCX"

# Extract Close prices into a new DataFrame
close_prices = pd.DataFrame()
close_prices['Copper'] = data['HG=F']['Close']
close_prices['FCX'] = data['FCX']['Close']
close_prices = close_prices.dropna()

# Normalize prices to start at 100
normalized_prices = (close_prices / close_prices.iloc[0] * 100)

# Plot the data
plt.figure(figsize=(12, 6))
sns.lineplot(data=normalized_prices)
plt.title("Normalized Price Comparison: Copper vs. FCX")
plt.ylabel("Normalized Price (Indexed to 100)")
plt.xlabel("Date")
plt.grid(True)
plt.show()
```

Step 4: The Narrative and Rendering

Finally, add some markdown text to tie it all together. You can even refer to your figure using its label, like this: As shown in @fig-price-comparison, there is a clear visual correlation...

To see the final product, you can either use the "Render" button in VS Code (provided by the Quarto extension) or run the command in your terminal: quarto render report.qmd. This will execute all the Python code and generate a self-contained report.html file.

The Quarto video demonstrates this process clearly.

Quarto Crash Course | Create Professional Reports, Dashboards & Websites w/ Markdown & Python Code!

This part of the video shows how code and its outputs are embedded in the final document.

Watch the segment on embedding Python code to see how a code block and its output appear in the rendered document. Then, skip to the HTML report example to see a more complex report with multiple visualizations.

4. A Template for Your Evidence Reports

You can now create a standardized template for all your future trade ideas. This structure ensures you follow a consistent, evidence-based process for every potential trade.

---
title: "Analysis of [Commodity] vs. [Company]"
author: "Your Name"
date: "today"
format: html
execute:
  cache: true # Caches results to speed up rendering
---

## 1. Thesis

*Briefly state the trade idea. E.g., "Investigate the strength of the relationship between copper prices and FCX stock to see if FCX offers a good proxy for a bullish copper view."*

## 2. Data Acquisition & Cleaning

*This section contains the code to download and prepare the data.*

```{python}
# yfinance download code here...
# Data cleaning/alignment code here...
```

## 3. Price Relationship Analysis

### 3.1. Visual Comparison

*Here we plot the data to visually inspect the relationship.*

```{python}
#| label: fig-prices
#| fig-cap: "Normalized price chart for..."
# Plotting code here...
```

As seen in @fig-prices, the prices exhibit a strong/weak visual correlation.

### 3.2. Statistical Summary

*Here we calculate key quantitative metrics from our previous lessons.*

```{python}
# Calculate correlation, rolling sensitivity, volatility, etc.
# Print the results in a pandas DataFrame.
```

The data shows a correlation of X and a rolling beta of Y...

## 4. Conclusion

*Summarize the evidence. Does it support the thesis? What are the key takeaways? E.g., "The high correlation and beta confirm that FCX is highly sensitive to copper prices. The stock appears to be a valid instrument for expressing a view on copper."*

Notice the cache: true option in the YAML header. This is a very useful feature that saves the results of computations, so Quarto doesn't have to re-download the data every time you render the report unless the code changes.

Conclusion

In this lesson, you've learned how to capstone your analytical work by creating a professional, reproducible evidence report. This bridges the gap between raw code and a clear, actionable summary of your findings.

Key Takeaways:

  • Reproducibility is Key: A reproducible report ensures your analysis is verifiable, disciplined, and efficient to update.
  • Quarto Integrates Code and Narrative: Using Quarto with .qmd files allows you to combine markdown text with executable Python code chunks.
  • A Report Tells a Story: Structure your report logically with a thesis, supporting evidence (charts, tables), and a conclusion.
  • Automate Your Documentation: By embedding code directly, your charts and tables are always in sync with your analysis, eliminating copy-paste errors and saving time.

You now have a complete, end-to-end technical workflow: from data retrieval and analysis to signal generation, robust testing, and finally, professional reporting. You are fully equipped with the quantitative toolset to analyze commodity-related trading ideas.

In the next module, we will shift our focus to the bigger picture. We will begin with the very first step of building a trade: writing a falsifiable commodity-market thesis supported by fundamental, company, and price evidence. You'll learn to formulate a strong, testable hypothesis that will serve as the foundation for the entire analytical and execution process we've covered.

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

Sign up