From Backtest to Execution: A Streamlit Trade Alert & Equity Curve Tracker for Gold/Silver (XAUUSD/XAGUSD)

 

Happy New Year everyone. Looking back at 2025, the precious metals market was undoubtedly the "star of the year": silver hit a record high at the end of the year, briefly approaching $80 and even touching $84/oz, before experiencing intraday volatility of around 10%; gold also continued its upward trend, reaching a record high of around $4,550/oz on December 26th, and ending the year around $4,300/oz, representing a year-on-year increase of approximately 65%–66%.


Looking ahead to 2026, many institutions remain bullish. For example, JPMorgan Chase predicts that gold prices could potentially move towards $5,000/oz in the fourth quarter of 2026.


The question is: facing this "trend + high volatility" market, is there an executable strategy to systematically capture these opportunities? This is exactly what my project is researching: comparing Fixed RR, ATR trailing, and the 50/50 scale-out exit framework around XAUUSD/XAGUSD, and then implementing the backtesting findings into a Streamlit trade alert + SQLite equity curve tracking tool.

While the same framework yielded a best-performing annualized return of only about 13% in backtesting for silver, gold is actually more suitable for this type of exit strategy: under my backtesting settings, the best-performing strategy for gold achieved an annualized return of approximately 500%. A comparison with the overall market trend makes this clear—gold's annualized return for 2025 was approximately 65%–66%, reaching a record high of around $4,550/oz around December 26th.


Silver is even more typical: in 2025, the price rose from approximately $30/oz to around $70/oz (an increase of over 140%). Therefore, my strategy's return on silver might actually be lower than the annualized growth of simply holding the stock.


By "achievable," I mean that I currently do not have access to an automated trading API, and all trades must be executed manually. Therefore, I may not be able to catch some signals in the early morning (e.g., 3 AM), and opportunities captured in backtesting may be missed in reality. If automated execution is implemented in the future, the theoretical upper limit for the same rules in backtesting will be even higher (the backtesting system shows a maximum annualized return of over 800%).


At the same time, you'll see that the equity curve in the backtesting is almost unilaterally upward, a near-perfect equity curve. However, this also means it's highly sensitive to execution costs (spreads/slippage/latency) and parameter stability. Therefore, I will later explain "how to use trade alerts + SQLite to record each transaction and generate an equity curve" as a core demo.

If you want to see the code directly, you can visit GitHub link below.

https://github.com/rolzie-7/xau-xag-strategy-monitor

Note: All backtesting results in this article are for research purposes only and do not represent actual returns or future performance; this article does not constitute any investment advice.


All times used in this article are London time GMT+0.


Because I read Jiang Tao's investment philosophy, although I was reading this book before when I did multiple linear regression, it's just too long and the content is too useful, so I'm still slowly reading it bit by bit. The sieve trading method is mentioned by Jiang Tao in a very early chapter. Some people say trading is difficult, but if you strictly follow the sieve trading method, like flipping a coin, heads means buy, tails means sell. This trading strategy can also bring high returns. Taking this as an inspiration, I wondered if I could apply it to precious metals trading? However, a stop-loss needs to be set, otherwise it might amplify drawdowns, so my first strategy is fixed ratio (rr).


The strategy uses a sieve to randomly determine whether to trade long or short today. Then, based on the profit/loss ratio (RR), the take-profit and stop-loss prices are determined.


The order price is the median price of the past 24 hours. If the current price is less than the median price (for long positions), a market buy is executed. Short positions are traded similarly. If no take-profit is achieved that day, a forced market sell is executed at 4:00 PM daily.


Let's look at the equity curve of this strategy. Here, "time" refers to the backtesting parameter I set for "when to start generating trading signals/placing orders each day."


Group 1: Trading starts at 8:30 AM daily – The final return is approximately +300%, but the drawdown is very deep, resulting in significant equity fluctuations. In other words, while the annualized return looks high, it's not a "comfortable" strategy from a risk perspective.


Group 2: Trading starts at 9:00 AM daily – This group is more extreme: it spends most of the time in the loss range, barely breaking even in the end, and the overall performance is not ideal.


Group 3: Trading begins daily at 4:30 AM – this was the best-performing time setting in the backtesting. Although there were still significant drawdowns, the final return was close to +600%, with a stronger overall trend.


These comparisons illustrate that the "sieve strategy" (a framework for randomly determining long/short) is not entirely unusable, but it is very sensitive to "entry timing" (the time window for signal triggering); its biggest drawdown is also obvious – excessive drawdowns and insufficient stability make it difficult to use as a system that inspires long-term confidence.


Therefore, I further tested a second strategy: introducing ATR as a risk and stop-loss/trading stop-loss framework, and instead of using a sieve to randomly determine the direction, triggering trades only when the market exhibits sufficient **momentum**. The core purpose of this approach is to minimize drawdowns and unnecessary noise trading caused by random trading while preserving profit flexibility as much as possible.

 

 

The first chart corresponds to the combination of 11:00 PM + 8:30 AM: place an order at night, and if it's not filled by the next morning, re-order at the latest price. In actual backtesting, this combination almost always resulted in losses.


My personal explanation is that the price structure around these two time points may not have "stabilized" yet—even if a trend or momentum emerges in the short term, the signal quality is not high enough, making it easy for a rapid reversal to occur subsequently, leading to frequent stop-loss triggers or being repeatedly shaken out by noise. In other words, this time window is more like a "volatility release/pricing adjustment" phase, rather than a phase where a trend truly unfolds (which also explains why the same logic yields completely different results with a different entry time).


Next, I'll discuss the second strategy: ATR trailing stop. It no longer uses a "sieve" to randomly determine LONG/SHORT, but only enters when clear momentum appears; the exit uses ATR to create a phased dynamic stop-loss, allowing profitable trades to run as long as possible. The specific stop-loss/take-profit mechanism is as follows:


Initial Stop: The stop-loss is fixed at 2.0 × ATR outside the entry price.


Breakeven: When the price moves more than 1.0 × ATR in a favorable direction (or touches TP1), the stop-loss is moved up to the entry price (to ensure no loss).


Trailing Stop: After reaching breakeven, the stop-loss continues to move up with the price, always placed 1.5 × ATR after the previous candlestick's closing price.


The intuitiveness of this mechanism is simple: once the market moves as expected, the stop price will continuously rise, allowing you to capture as much profit as possible while the trend continues; and when the trend weakens or reverses, the trailing stop-loss can lock in profits as much as possible.


The second chart shows the equity curve with 8:00 AM as the order placement time. It can be seen that there is still a very large drawdown. The problem with excessive drawdowns is that even with a positive long-term expected return, it's difficult to achieve stable compounding – because you might not be able to withstand the drawdown psychologically or financially.


Therefore, I started considering a more "trader-friendly" exit strategy: combining the certainty of Fixed RR with the scalability of ATR trailing. In other words, first use Fixed RR to lock in a portion of the profit, and if the market continues to move in the desired direction, then let the remaining position use ATR to "roll over" the profit. This is the 50/50 scale-out strategy I will introduce later.

 

 

In highly volatile precious metals, a practical problem with relying solely on ATR trailing is that pullbacks often precede the trend. Even if you eventually profit from a significant move, deep drawdowns during the process make compounding extremely difficult—you need to withstand prolonged unrealized losses and endure the psychological torment of profits being repeatedly given back.


Therefore, I designed a more balanced exit framework: the 50/50 scale-out. The core idea is simple: first, lock in the "certain profits," then let the "potential major trend" amplify those profits.


The specific approach is:


After opening a position, split the position in half: 50% + 50%


First half (50%): Use a fixed RR (e.g., +2R) to establish a clear TP1 (transaction target), prioritizing the realization of a portion of the profits to reduce overall volatility and drawdown pressure.


Second half (50%): Don't rush to take profits; continue using ATR trailing to track the trend, allowing it to capture as much of the upward (or downward) movement as possible.


This way, even if the market starts to fluctuate or reverse halfway through, you've at least locked in some profits through TP1; and if the market does develop into a one-sided trend, the remaining 50% can be "extended" through ATR trailing. From a risk-reward perspective, it's closer to my desired outcome: more controllable drawdowns, more replicable profits, and easier long-term execution.


Let's look at the equity curve. Figure 1 shows the strategy I will execute, with orders placed at 8:00 AM + 8:30 AM. The drawdown is very small, and the returns are very considerable; I consider it a perfect equity curve. However, the timing of the order placement is also crucial. The next figure shows the equity curve with orders placed at 9:00 AM. Just half an hour later, while it's not a stark contrast, the second one is not the near-perfect, low-drawdown, and stable strategy I'm looking for.

 

 

To make this strategy truly usable, I also created a complementary trade alert and monitoring tool: Streamlit generates manual trade tickets (entry/SL/TP1/TP or trailing stop rules). You only need to input the actual transaction price, and the system will record each transaction in SQLite (trade_db) and continuously update the stop-loss and equity curves. In other words, this isn't just backtesting results, but a complete workflow that connects "strategy → execution → backtesting → equity curve."


Next, I will use a demo to demonstrate how this dashboard generates trade tickets, records transactions, and automatically updates the equity curve.


After running `streamlit run app.py`, wait approximately one minute for the data to be acquired and analyzed.

Select a strategy and view the signals it provides.

After the order is filled, the transaction price and currency are recorded.

This is the price update for the close strategy, which will automatically update the database and the equity curve.


Here you'll realize: the real challenge isn't finding a seemingly profitable curve, but finding a system you can confidently execute long-term.


In precious metals, volatility is high enough to shatter one's mindset—so what I care about more is: whether I can lock in some profits while leaving room for the trend, which is why I use a 50/50 strategy.


I will try to continuously execute this strategy, recording every actual trade, stop-loss changes, and equity curve. A month later, I will update the live trading backtesting results (including execution deviations, cost impacts, and equity curve performance), continuing to iterate the strategy and tools.


All results in this article are from backtesting and personal research, and do not represent actual returns or constitute investment advice. For me, the more important thing is: using tools to record the trading process, forming a backtestable chain of evidence, and then iterating version by version.

评论

此博客中的热门博文

Multi-Timeframe Resonance in Action: Buy point analysis in China A share

多周期共振实战:华发股份与海航控股