DEV Community

Cover image for TradingView PMax Strategy Optimization: 4 Rounds on Gold
Truong Pham | Moon The Train
Truong Pham | Moon The Train

Posted on Originally published at moonthetrain.com

TradingView PMax Strategy Optimization: 4 Rounds on Gold

This is part 2. Part 1 is here: TradingView PMax Strategy: Best Settings, Repaint, Backtest.

The short version. This is part 2 of my TradingView PMax strategy optimization, and it's about gold only (XAUUSD, H1). In part 1 the original script made a profit factor of 1.37 on gold H1, but the drawdown was large and most of the money came from a few big trades. So I spent four rounds trying to make it better.

  • Two rounds worked, two didn't. Changing the exits made it worse. Position sizing on its own did nothing. Running several versions at once cut the drawdown by 40%, and a market filter roughly doubled the profit per unit of risk.
  • Combined, at the same risk: sized to the same maximum drawdown as the original (about 960 USD), the optimized version made +4,660 USD from January 2020 to September 2026, against +1,635 USD for the original. Both figures are after spread, commission and swap.

My verdict: a real improvement on gold, with the work shown below. It's not finished. It still has to pass data it has never seen.

Where part 1 left off

The original PMax strategy on gold H1, January 2020 to September 2026, Exness Raw Spread ticks, after spread, commission and swap:

Original
Net profit (USD, 0.01 lot in total) +1,635
Max drawdown 961
Yearly profit ÷ max drawdown 0.25
Losing years 2 (2022: −38, 2023: −211)
Worst single day −215 (8 April 2026)
Share of profit from the 5 biggest trades 55%

Two things bothered me. A profit factor of 1.37 on 448 trades isn't very sure of itself: the 95% range runs from 0.98 to 1.89. And more than half the profit came from five trades. One bad stretch, like the short in early April 2026, could undo a year.

The plan: four rounds

Before touching anything, I wrote down four ways to try to make the strategy earn more for the risk it takes, and the order to try them in. Each round attacks a different weakness from part 1.

Round What I changed The weakness it targets
1 Exits: protect a trade once it's open, instead of waiting for the opposite signal PMax gives back a lot of open profit before it flips
2 Position sizing: risk a fixed share of the account on each trade A fixed lot carries far more risk in 2026, with gold above 4,000, than in 2020
3 Diversification: run several versions of the strategy side by side Half the profit came from five trades
4 A market filter: let PMax trade only when conditions suit it Losing streaks when the market doesn't suit a trend follower

The entries stay exactly as in part 1 throughout. Every round is judged the same way, which comes next. Then each round in turn, including the two that didn't work.

How I kept myself honest

Optimizing a strategy is the easiest way to fool yourself, so I set the rules before each round:

  • The pass mark was written down before running anything. Usually: better profit per unit of drawdown than the original in both 2020–2023 and 2024–2026, after swap.
  • Every result was checked with the candles cut at four different times (on the hour, and 15, 30 and 45 minutes past). In part 2 of my SuperTrend test, a beautiful result collapsed under exactly this check, so now it's standard.
  • Neighbouring settings had to agree. If only one exact setting works and its neighbours don't, I treat it as luck.
  • In the first round I went further and picked settings using 2020–2023 only, then looked at 2024–2026 once.

All four rounds use the same PMax entries from part 1, the same data and the same costs.

Round 1: better exits? No.

PMax only exits when the opposite signal fires, so it gives back a lot of open profit. The obvious fix is an exit layer, the usual mix of stops that protect a trade once it's open. I tested 90 combinations, each on all four candle timings.

The winner on 2020–2023 looked great. Its profit factor went from 1.21 to 1.39, and its drawdown from 378 to 151.

Then I opened 2024–2026, which it had never seen:

Same exit layer, gold H1 Original With exits
2020–2023, profit factor 1.21 1.39
2024–2026, profit factor 1.35 1.05
2024–2026, net profit +1,769 +230
Whole period after swap +1,635 +741

Tight exits help when gold goes sideways, which it mostly did until 2023. From 2024 gold trended hard, and the same exits cut the long runs that make PMax money. None of the 89 alternatives earned more than the original in 2024–2026 either. Exits on their own were a dead end, so I dropped them.

Round 2: position sizing? Not on its own.

A fixed 0.01 lot means a trade in 2026, with gold above 4,000, carries several times the risk of a trade in 2020. So I sized every trade to risk a fixed percentage of the account.

With nothing else changed, it was worse. At about 20% maximum drawdown, fixed lots grew the account by 7.9% a year and risk-based sizing by 5.2%. Without a hard limit on each trade's loss, sizing just moves the risk around.

Paired with a loss-cutting rule it beat fixed lots: 10.2% a year against 7.9% at the same 20% drawdown, with the worst year improving from −7.4% to −2.5%. But I found that rule while looking at round 1's test period, so I don't count it as a result yet. It's on my list for the next stage.

Round 3: run several versions at once? Yes, for risk.

Instead of one set of PMax settings, I ran several versions side by side, each with a smaller share of the same total position size. Different versions flip at different moments, so they don't all get hit by the same bad trade.

After swap Original Several versions
Net profit +1,635 +1,465
Max drawdown 961 565
Yearly profit ÷ max drawdown 0.25 0.38
Share of profit from the 5 biggest trades 55% 8%
Worst single day −215 −125

Profit stayed about the same and the drawdown fell by 40%. That's the point: at the same risk you can trade bigger. It also fixed my worry from part 1. The result no longer hangs on five trades.

Round 4: a market filter? Yes.

The last round adds a filter that decides when PMax is allowed to open a trade at all. I tried four designs:

  • One lost money in 2020–2023 and was dropped.
  • One looked spectacular, with profit per unit of risk up 2.7 times. But only its exact default setting worked. Every neighbouring setting I tried was no better than no filter, and some were worse, so I treat it as luck.
  • One turned out to be the same as another in practice.
  • One held up. I tried six variations of its setting, and every one of them beat the unfiltered strategy at all four candle timings.

The survivor, on the original single set of settings, after swap:

Original With the filter
Net profit +1,635 +1,918
Max drawdown 961 571
Yearly profit ÷ max drawdown, 2020–2023 0.09 0.35
Yearly profit ÷ max drawdown, 2024–2026 0.56 0.95

This is the only round that raised profit and cut drawdown at the same time, in both periods.

Yearly profit divided by maximum drawdown for the original PMax strategy and after each optimization round, XAUUSD H1, Jan 2020 to Sep 2026, Exness ticks, spread, commission and swap included

Rounds 3 and 4 together

After swap, same total lot size Original Optimized
Net profit +1,635 +1,805
Max drawdown 961 372
Yearly profit ÷ max drawdown 0.25 0.72
Profit factor 1.20 1.36
Profit factor 2020–2023 / 2024–2026 1.06 / 1.29 1.15 / 1.49
Losing years 2 1 (2022: −75)
Worst single day −215 −92
Share of profit from the 5 biggest trades 55% 6%

Nearly the same profit with less than 40% of the drawdown. So the fair comparison is at the same risk. Size the optimized version up until its worst drawdown matches the original's 961 USD, and it made +4,660 USD against +1,635 USD, about 2.85 times as much. With fixed lot sizes, profit and drawdown scale together, so this is just the table above multiplied by 2.58.

Closed profit of the original PMax strategy versus the optimized version sized to the same maximum drawdown, XAUUSD H1, Jan 2020 to Sep 2026, Exness ticks, spread, commission and swap included

Does it win every year?

No, and you should know that before trusting any of this. At the same total lot size, the optimized version made less than the original in 2020, 2022, 2024 and 2025, and about the same in 2021. It made more in 2023, where it turned a −211 year into +74, and in 2026. Its advantage comes from much smaller drawdowns, not from making more money every year.

Profit by year for the original and optimized PMax strategy at the same total lot size, XAUUSD H1, 2020 to Sep 2026, after swap

And most of the profit still comes from 2025 and 2026, when gold rallied. The optimized version spreads it better, but it's still a trend follower on a market that trended.

What I haven't verified yet

  • Data it has never seen. Every round used gold from 2020 to 2026. The next step is gold before 2020, and other markets.
  • Position sizing combined with everything else. The numbers above use fixed lots.
  • MetaTrader 5 on real ticks. All of this is my Python port on Exness tick data.
  • Swap on your account. I used the Raw account's swap on gold longs. On a zero-swap account the numbers would be better.
  • Other symbols and timeframes. Everything was tuned on gold H1 only. I wouldn't assume it carries over.

Bottom line

Four rounds of upgrades to the TradingView PMax strategy on gold H1. Two failed and two worked. At the same risk as the original, the optimized version made about 2.85 times the profit after costs and swap, with one losing year instead of two. It's a better trend filter than the one I started with. It isn't proven until it survives data it hasn't seen.

Next: that out-of-sample test. If you want the original numbers first, start with part 1. For the same kind of work on another script, see SuperTrend part 2.

I'm not affiliated with TradingView or with KivancOzbilgic. Past results, including backtests, don't guarantee future results.

Top comments (2)

Collapse
 
stratcorealpha profile image
Arnold Holm •

Your round 2 makes the next account test especially useful: keep the loss-cutting rule fixed, then size each trade from the stop actually available before entry. Include a gap through that stop and a rejected close. Otherwise the smaller historical drawdown can invite a larger position while the account still has the same daily loss limit.

Collapse
 
suppdevbot profile image
DEV SUPPORTS •

You need to verify your account.

Enter fullscreen mode Exit fullscreen mode

tr.ee/dev-to