Welcome back to the KC-TradingAI blog series. In Part 1, we covered how we processed raw data and encoded financial concepts (SMC) into our system. However, we still don’t have a decision mechanism. We only have an analysis system that says “There is an FVG here” or “RSI is 70.”
Who pulls the trigger?
In the KC-TradingAI project, instead of entrusting the decision mechanism to a single model, I used a “Dual-Brain” architecture:
-
The Analyst (XGBoost): Makes direction predictions by looking at historical data.
-
The Strategist (RL Agent): Manages risk by looking at current conditions and makes the final decision.
In this part, we will examine how the system thinks and, most importantly, how it learns from its mistakes.
The Brain: XGBoost and Reinforcement Learning
1. The Analyst: XGBoost (Supervised Learning)
Our first layer is the XGBoost algorithm, a staple of Kaggle competitions.
We feed the 128-feature dataset created in Part 2 into this model. The model analyzes thousands of past candles to learn complex (non-linear) relationships. For example: “If RVOL is above 2.0, price is in an Order Block zone, and it is the London open; the probability of price rising is 86%.”
XGBoost gives us two outputs:
-
Prediction: BUY, SELL, or HOLD.
-
Confidence Score: How sure is it of its prediction? (e.g., 0.84)
However, XGBoost has a problem: It is static. You train the model once, and it remains the same until you train it again. Yet, the market is a living organism that changes constantly. This is where the second brain comes in.
2. The Strategist: Reinforcement Learning (RL)
This is the most unique part of the project. This Q-Learning based agent, coded in rl_agent.py, learns by gaining experience just like a human.
The RL Agent takes the signal from XGBoost and asks the following questions:
“Okay, the model says ‘BUY’, but is the market too volatile right now? Did we lose 3 times in a row? Are we in the Asian session? What happened when I opened a trade under these conditions before?”
The Agent’s World (State Space) Before making a decision, the agent analyzes the environment (State). We discretized this state in our code as follows:
-
Confidence Level: How strong is the model’s prediction?
-
Volatility Regime: Is the market calm or stormy? (Based on ATR)
-
SMC Signals: Is there an FVG or RBR pattern nearby?
-
Time: Which trading session are we in? (London, New York, Asia)

3. Continuous Learning: Learning from Mistakes
KC-TradingAI is not a “Set and Forget” system; it is a “Set and Grow” system.
The RL agent records the result of every trade (Profit or Loss) into its memory and updates a matrix called the Q-Table.
Example Scenario:
-
XGBoost gives a BUY signal for “GOLD (XAUUSD)” during the Asian session.
-
The RL Agent approves the trade.
-
However, the trade hits the stop loss (Loss).
-
The Learning Moment: The agent runs the
updatefunction: “I lost when I bought Gold during the Asian session in low volatility. I am applying a penalty (negative reward) to this.” -
Result: The next time the same conditions occur, the agent will reject the trade or approach it more cautiously.
4. Dynamic Risk Management: No More Fixed Stop Loss
The reason most bots fail is fixed rules (e.g., 10 pips Stop Loss on every trade). However, the pulse of the market changes constantly.
Our RL Agent determines Dynamic TP/SL (Take Profit / Stop Loss) ratios based on market conditions. The logic works as follows:
-
Low Volatility: Price is moving little. Reduce targets (TP), widen the Stop (SL) slightly (to avoid noise).
-
High Volatility: Price is moving aggressively. Enlarge targets (Catch the big move), tighten the Stop.
-
Winning Streak: If things are going well in that session, the agent gains confidence and increases the profit multiplier.
Python
# Dynamic adaptation based on volatility
if volatility_regime == 'High':
tp_mult *= 1.3 # Increase target by 30%
sl_mult *= 0.9 # Tighten stop by 10%
Operations: Execution, Risk, and Monitoring
Having the best strategy in the world means nothing if your execution infrastructure is poor or your risk management is weak. In this final section, we examine the Execution Layer.
1. Flawless Execution (Execution Pipeline)
When the AI says “BUY”, the job isn’t done. The order must be transmitted to the exchange within seconds, at the right price, and without errors. The “Execution Pipeline” consists of these steps:
-
Type Safety: Python is flexible, but MetaTrader is C++ based. It does not recognize NumPy data types (
float64). Therefore, all price and lot data are passed throughfloat()conversion before the order. This small detail prevents the system from crashing. -
Order Check: A simulation is performed with
mt5.order_check()before the order is sent. Is there enough balance? Is the market open? Is the lot size valid? If we get an error here, the order is never sent. -
Filling Modes: Every broker supports different order types (IOC, FOK, Return). The code automatically detects the mode supported by the broker, reducing the risk of order rejection to zero.
2. The System’s Insurance: Strict Risk Management
A trader’s (or bot’s) number one job is not to make money, but to protect capital. KC-TradingAI uses the advantage of being an emotionless robot to adhere 100% to risk rules.
-
Daily Drawdown Limit: If the bot loses more than a set limit (e.g., -$500) in total that day, the
reset_daily_countersfunction activates and bans trading until the end of the day. -
Frequency Limit: The maximum number of daily trades is limited (e.g., 20 trades) to prevent Overtrading.
-
News Filter: The
news_impact_learnermodule checks the economic calendar. It puts the system on “Hold” before high-volatility events like Non-Farm Payrolls (NFP) or FED interest rate decisions.
3. Smart Trade Management
Entering a trade is easy; exiting is hard. KC-TradingAI monitors the trade continuously (monitor_positions).
-
Breakeven: When price moves in our favor by a certain amount (e.g., 1 ATR), the Stop Loss level is moved to the entry price. There is no longer a possibility of loss from this trade.
-
Trailing Stop: When a trend is caught, the bot drives the Stop Loss level behind the price, locking in profits.
-
Partial Close: When half of the target is reached, 50% of the position is closed to bank profit, leaving the rest to “run.”
4. Telegram Integration
While the bot runs on a server, I want to know what’s happening. The TelegramManagerV2 class allows the bot to talk to me.
-
Chart Signals: When the bot opens a trade, it takes a screenshot of the current chart. It writes the entry point, Stop Loss, Target, and “Why AI opened this trade” (e.g.,
AI-BUY | Confidence: 86% | FVG | Trend Aligned) on it and sends it via Telegram. -
Daily Report: Every midnight, it provides a report summarizing that day’s performance (Win rate, PnL, Best trade).
-
Heartbeat: Is the system working? It sends an “I am here, market is open, everything is fine” message every 4 hours.

Conclusion: From Architecture to Results
In this part, we established the “Dual-Brain” architecture and the safety mechanisms that protect our capital. We now have a bot that thinks like a hedge fund manager and executes like a machine.
However, in algorithmic trading, the most beautiful code is meaningless if it doesn’t generate profit.
Does this complex architecture actually translate to a higher ROI?
In the final installment of this trilogy, we will move from code to Performance. I will share the raw backtest data comparing the Standard Model vs. the RL Agent (spoiler: the RL agent outperforms significantly) and unveil the roadmap for transforming KC-TradingAI into a commercial SaaS product.
Click here to read Part 3: Performance Results & The Road to SaaS
