Skip to main content
🎯 Goal
By the end of this, you will have:
- a Python script that reads your trading files
- searches them for relevant text
- sends context to your local model (via Ollama or LM Studio API)
- returns structured analysis of your strategies
🧠 WHAT YOU ARE BUILDING (SIMPLIFIED)
User Question
↓
Search local files (simple keyword match)
↓
Pull relevant strategy/backtest text
↓
Send to local LLM (14B or 20B)
↓
Get structured trading analysis
🧰 STEP 1 — REQUIREMENTS (VERY LIGHT)
You only need:
✔ Python (built into Mac usually)
Check:
python3 --version
✔ Install one package
We’ll use requests for calling the model:
pip3 install requests
⚙️ STEP 2 — CHOOSE YOUR MODEL ENDPOINT
We will use Ollama because it’s easiest for automation.
Make sure Ollama is running:
ollama serve
(or just open the app normally — it runs in background)
Pull a model (if not already):
ollama run qwen:14b
or
ollama run mistral
📁 STEP 3 — YOUR FIRST RAG SCRIPT
Create a file:
rag_query.py
Paste this code:
import os
import requests
# -------- CONFIG --------
MODEL = "qwen:14b" # or mistral / 20b model if installed
BASE_URL = "http://localhost:11434/api/generate"
DATA_FOLDER = "AI_TRADING_SYSTEM"
# -------- SIMPLE FILE SEARCH --------
def search_files(query):
results = []
for root, dirs, files in os.walk(DATA_FOLDER):
for file in files:
if file.endswith(".md") or file.endswith(".txt"):
path = os.path.join(root, file)
try:
with open(path, "r", encoding="utf-8") as f:
content = f.read()
if any(word.lower() in content.lower() for word in query.split()):
results.append(content[:1500]) # limit size
except:
pass
return results[:3] # top 3 matches
# -------- CALL LOCAL LLM --------
def ask_llm(context, question):
prompt = f"""
You are a professional trading analyst.
Use the context below to answer the question.
CONTEXT:
{context}
QUESTION:
{question}
Provide:
1. Summary
2. Strategy analysis
3. Risk notes
4. Recommendation
"""
response = requests.post(BASE_URL, json={
"model": MODEL,
"prompt": prompt,
"stream": False
})
return response.json()["response"]
# -------- MAIN FUNCTION --------
def main():
question = input("Ask your trading question: ")
print("\n🔎 Searching your trading system...\n")
docs = search_files(question)
if not docs:
print("No relevant data found.")
return
context = "\n\n---\n\n".join(docs)
print("\n🧠 Analyzing with local AI...\n")
result = ask_llm(context, question)
print("\n📊 RESULT:\n")
print(result)
if __name__ == "__main__":
main()
🚀 STEP 4 — RUN IT
In terminal:
python3 rag_query.py
Example input:
Which strategy performs best in high volatility regimes?
🧠 WHAT YOU JUST BUILT
You now have:
✔ File-based search
✔ Context injection
✔ Local LLM reasoning
✔ Structured trading output
⚖️ WHAT THIS IS (AND IS NOT)
✔ This IS:
- real RAG (simple version)
- working AI analyst
- your first trading intelligence engine
❌ This is NOT yet:
- vector database
- semantic search
- dashboard UI
- automation system
🧠 WHY THIS VERSION IS IMPORTANT
Because it proves:
AI can already reason over YOUR data, not generic internet knowledge
🔥 NEXT STEP AFTER THIS
Once this works, Phase 3.2 upgrades it to:
- smarter retrieval (embeddings)
- better ranking of documents
- regime-aware filtering
- cleaner structured outputs
🏁 PHASE 3.1 SUCCESS CRITERIA
You are done when:
✔ Script runs without errors
✔ It finds relevant strategy files
✔ It sends context to model
✔ It returns structured analysis
✔ You can ask multiple questions
💡 ONE-LINE TAKEAWAY
Phase 3.1 turns your folders into an AI-readable knowledge base using the simplest possible working pipeline.
