AI Study Online

How to Use Codex for Quantitative Strategy Research and Backtesting

5 min read
📚 AI Tutorials 🕒 5 min read 📅 Jul 18, 2026 🎯 Intermediate

Introduction

This guide demonstrates how to use Codex to conduct quantitative strategy research and backtesting, covering the complete process from installation to validation. We will use Codex as the primary tool, with khQuant as the underlying command-line quantitative research tool.

1. Install Codex

First, complete the installation of Codex. There are multiple installation methods available online, including official subscription and mirror sites. It is recommended to use the official subscription if possible.

2. Verify khQuant Installation

Before proceeding, confirm that you have installed the V3 version of khQuant on your local machine.

3. Test Configuration with Codex

Ask Codex to check your configuration and run built-in test cases:

On this computer, I have installed the khQuant CLI command set and corresponding skills. Please help me check if the configuration is complete, run the built-in examples using the CLI, and generate a backtest report for me.

After the execution is complete, you will see the backtest report, which includes key metrics such as:

  • Profit and Loss (PnL)
  • Drawdown (DD)
  • Transaction statistics (TRD)

4. Download Data via Codex

You can also use Codex to command khQuant to download and manage data. For example, to download the daily data of the Shanghai 50 Index for the past 5 years, including unadjusted, forward-adjusted, and backward-adjusted versions:

Help me download the daily data of the Shanghai 50 Index for the past 5 years, including unadjusted, forward-adjusted, and backward-adjusted data.

After the download is complete, you can ask Codex to summarize the downloaded data:

Please summarize the data I just downloaded.

5. Check Data in khQuant GUI

You can also open the graphical interface of khQuant and find the database management module to view the downloaded data.

6. Batch Strategy Research

You can use Codex to conduct batch research on strategy parameters. For example, to study the golden cross and death cross strategy for a specific stock, and find the optimal long-term and short-term moving average parameters:

I want to study the golden cross and death cross strategy for a specific stock, but I need to find the optimal long-term and short-term moving average parameters. Please help me call the khQuant skill to generate batch golden cross and death cross strategies, conduct batch backtests, and find the optimal parameters for the past year.

After the automatic strategy writing and backtesting are completed, you will obtain the results of the batch strategy research.

Conclusion

The above is an introductory tutorial on how to use Codex to call khQuant for configuration and strategy research. Once you master this process, you can submit any strategy ideas to the large model for implementation and validation. Note: This tutorial uses Codex as the demonstration example. If you are using Claude Code or other powerful agents, you can also use similar methods to call the toolchain to complete the research.

常见问题

Can I use Claude Code instead of Codex for this workflow?

Yes. The article notes this explicitly. The key is the toolchain (khQuant CLI + Skills), not the specific agent. Claude Code's harness engineering is arguably better suited for this type of multi-step research workflow — it handles long-running tasks, state recovery, and batch operations more robustly. The prompts in this guide work with any agent that can execute CLI commands. If you're using Claude Code, the workflow is identical: install khQuant, configure the skills, and use the same prompts. The batch strategy research section (golden cross optimization) benefits most from Claude Code's stronger planning and multi-step execution.

What is khQuant and where do I get it?

khQuant is a command-line quantitative research tool that provides data download, backtesting, and strategy analysis capabilities. It's the "engine" that Codex controls — Codex handles the natural language interface and orchestration, while khQuant does the actual financial computation. The article assumes you have khQuant V3 installed. Check the original video by Mr. Kanhai (credited at the end of the article) for khQuant installation and setup instructions. The toolchain pattern (AI agent + CLI quant tool) is more important than the specific tools — you can substitute khQuant with any CLI-compatible quant library.

What's a golden cross strategy and why batch-test it?

A golden cross occurs when a short-term moving average (e.g., 50-day) crosses above a long-term moving average (e.g., 200-day) — traditionally a bullish signal. A death cross is the opposite (bearish). The problem: which moving average periods work best? 50/200 is the classic pair, but for a specific stock, 20/100 or 30/150 might perform better. Batch testing means the AI generates and backtests dozens of parameter combinations (short MA from 5 to 60, long MA from 20 to 250) to find the optimal pair for your specific stock and timeframe. This is exactly the kind of tedious optimization work that AI agents excel at — what would take a human days of manual testing takes Codex minutes.

Do I need to know quantitative finance to use this workflow?

You need to understand the concepts (moving averages, golden/death crosses, backtesting, PnL, drawdown) but not the implementation details. The AI handles the coding and parameter optimization. Think of it like driving a car: you need to know where you're going and the basic rules of the road, but you don't need to be a mechanic. Start with simple strategies (single indicator, single stock) to build intuition, then gradually increase complexity. The biggest risk is not understanding what the backtest results actually mean — a 90% win rate with 1 trade is meaningless, while a 55% win rate with 1,000 trades is statistically significant. Learn to read backtest reports critically before deploying real money.

📖 Next Steps

Mastered Codex quant workflow? Go deeper into AI+finance:

Share this article

Related Articles

AI TutorialsIntermediate

Complete Practical Tutorial: Master All Core Operations Inside Claude Code

A hands-on tutorial covering every core Claude Code operation — from CLI installation and project import to file analysis, batch refactoring, automatic debugging, claude.md rules, Git integration, Plan Mode, and permission controls — with executable commands for every step.

5 min read
Claude CodeCLITutorial
AI TutorialsBeginner

WorkBuddy L05: Master the 3-Layer Memory System – Make AI Understand You Better

A deep dive into WorkBuddy's 3-layer memory architecture — Cloud Memory (auto profile + cross-conversation search), User-level Local Memory (MEMORY.md), and Workspace Memory (auto daily logs) — plus how Memory integrates with Skills and Automation to create a self-improving AI assistant.

5 min read
WorkBuddyMemoryAutomation