Martingale Blow-up Demo — Strategy by mm_mitsuya

By mm_mitsuya

Performance Metrics

Description

This is NOT a strategy to make money. Martingale Blow-up Demo is an educational strategy that shows, with a real backtest, why the martingale method (adding a bigger position every time price moves against you) looks so attractive, and why it eventually blows up. Several default properties are intentionally far riskier than a normal strategy; the reasons are listed under "Default properties".━━━━━━━━━━━━━━━━━━━━WHAT IT DOES━━━━━━━━━━━━━━━━━━━━The strategy opens a long position, buys more each time price falls a fixed percentage, doubles the size of every add, and closes the whole position when price rises a small percentage above the average price. It also simulates the broker's liquidation: if equity falls to the margin requirement, the whole position is closed and the strategy stops trading forever (bankrupt).On the chart it shows the average price, the take-profit line, the next add price, the liquidation price and an optional surrender line. On the bar where liquidation happens, a large label shows the peak equity and the equity left after liquidation. A status table shows the current round, open profit/loss, exposure as a multiple of equity, the deepest round reached, the peak equity and its date, current equity and the liquidation date.Default result (OANDA:XAUUSD, 1 hour, default properties, trading from January 2023, checked with Deep Backtesting):• The Strategy Tester shows 318 trades with 76% profitable (every add counts as a separate trade).• Equity rose slowly and steadily to a peak of 14,365 USD (+43.6%) on January 29, 2026.• Over the next four days gold fell from about 5,400 to about 4,550 USD. The strategy added seven times: 0.13 → 0.26 → 0.52 → 1.04 → 2.08 → 4.16 → 8.32 → 16.64 oz, 33.15 oz in total (about 150,700 USD of exposure).• On February 2, 2026 the position was liquidated at 4,545.7. That single position lost 6,864 USD, erasing three years of profit and a quarter of the starting capital. Equity ended at 7,501 USD (-25.0%), profit factor 0.68, maximum drawdown 47.8%.The equity left after liquidation is only the margin the broker keeps for the open position. A larger first size, a smaller add spacing or a price gap through the liquidation price can leave much less, or a negative balance.━━━━━━━━━━━━━━━━━━━━HOW IT WORKS━━━━━━━━━━━━━━━━━━━━1. First entry. By default the strategy enters long as soon as it is flat ("Always"). You can instead wait for a MACD-style trend shift (a smoothed price above a fast average that is above a slow average), a Stochastic RSI cross up from below 20, or an ATR channel turning positive.2. First size. The first order is a percentage of current equity (default 5%), a fixed cash amount or a fixed quantity. This first size is fixed for the life of the position.3. Adds. Each time the close falls the add spacing (default 1.5%) below the last fill (or below the average price, if selected), the strategy buys again with first size × multiplier to the power of the round number. With the default multiplier of 2 every add doubles.4. Caps. An add is skipped when the maximum number of rounds is reached or when the position after the add would exceed the maximum exposure (default 20 × equity, which is the limit allowed by 5% margin). From then on the strategy can only wait for a bounce, surrender (if enabled) or be liquidated.5. Take profit. When the close is the take-profit percentage (default 1%) above the average price, the whole position is closed. Optionally a ratchet trail starts at that point and closes the position when the close falls a set percentage from its highest close, never below the average price.6. Why it keeps winning. Because each add is larger than everything before it, the average price moves close to the current price. A small bounce is enough to close the whole position in profit, so almost every position ends as a win.7. Liquidation. The liquidation price is the price at which account equity (cash plus open profit/loss) equals 5% of the position's value, the same margin as the strategy properties. A stop order sits at that price at all times. If it is hit, everything is closed and the strategy never trades again. Because the position grows exponentially, one drop that does not bounce reaches this price and wipes out many small wins at once.8. Surrender line (optional). After the cap is reached, the position is closed if price falls a set percentage below the average price. This avoids liquidation but turns into a large realized loss.Signals are evaluated on the bar close. Adds, take profit and surrender are filled at the next bar's open; the liquidation stop can fill inside the bar.━━━━━━━━━━━━━━━━━━━━DEFAULT PROPERTIES━━━━━━━━━━━━━━━━━━━━• Initial capital: 10,000 USD, a typical retail account size.• Order size: calculated by the script. The first order is 5% of equity and every add is twice the previous one. The "Default qty" in the Properties tab (1 contract) is not used because every order sets its own quantity.• Commission: 0.02% per order. OANDA charges gold CFDs through the spread rather than a separate commission; a typical XAUUSD spread is a few tenths of a dollar, about 0.01-0.02% of price. 0.02% per side is a conservative estimate of that cost.• Slippage: 100 ticks = 0.10 USD per fill on market and stop orders (OANDA:XAUUSD ticks are 0.001 USD).• Margin: 5% for long and short (20:1 leverage), the retail limit for gold CFDs under ESMA/FCA rules applied by OANDA's European and UK entities. The liquidation price in the script uses the same 5%. No margin calls occur in the default backtest because the script's liquidation stop closes the position first.• Pyramiding: 20, so that up to 20 rounds (the first entry plus 19 adds) can be open at once. This is required for a martingale.• Recalculate on every tick / after order fills: off. Orders are processed on bar close and filled on the next bar.• Intentional deviations: the risk per position is deliberately extreme. A normal strategy risks a small fraction of equity per trade; this one allows exposure up to 20 × equity and loses almost half of its peak equity in one position. This is the behavior the script exists to show and should never be copied for live trading.━━━━━━━━━━━━━━━━━━━━HOW TO USE━━━━━━━━━━━━━━━━━━━━• Default chart: OANDA:XAUUSD, 1 hour. The defaults start trading on January 1, 2023. If your chart loads less history, the backtest starts at the first loaded bar and the result differs.• Read the Strategy Tester together with the status table: the win rate looks excellent until the single losing position appears.• Settings: – First entry logic: Always, MACD trend shift, Stoch RSI cross-up, ATR channel. – Size mode / First size: % of equity, fixed amount or fixed quantity. – Add spacing (%), Add spacing measured (from last fill or from average price), Size multiplier, Max rounds, Max exposure (× equity). – Take profit (%), Ratchet trail (%). – Use surrender line / Surrender line (%): only active after the cap is reached. – Simulate liquidation: turn the liquidation stop and the bankruptcy stop on or off. – Start / End: the trading period. – Display toggles for the lines, liquidation line and status table.• Things to try: – Raise the first size: equity grows faster and the blow-up comes sooner. – Move the start date: a small shift decides between years of gains and an early exit. – Turn on the surrender line and see what you give up to avoid liquidation. – Lower the multiplier or widen the add spacing: survival improves, profit per position shrinks.━━━━━━━━━━━━━━━━━━━━ORIGINALITY━━━━━━━━━━━━━━━━━━━━Many public martingale and grid scripts show only the attractive part: a high win rate and a smooth equity curve, with liquidation ignored or hidden. This script is built to show the whole life cycle. It simulates liquidation at the broker's margin level with a stop order that recalculates the price after each planned add, stops trading permanently after liquidation, and marks the liquidation bar with the peak equity and the remaining equity side by side. The exposure cap tied to the margin rate, the optional surrender line that only activates after the cap, the ratchet trail and the choice of first-entry filters let you test which changes really reduce the risk and which only delay it.━━━━━━━━━━━━━━━━━━━━LIMITATIONS━━━━━━━━━━━━━━━━━━━━• The liquidation price is an approximation: it treats the whole account as margin for one position (cross margin) and uses a single 5% rate. Real brokers apply their own margin close-out rules, which can close positions earlier, and in a fast market the fill can be far worse than the stop price.• Overnight financing (swap) is not modeled. Martingale positions are often held for days, so real costs would be higher.• Adds and exits are decided on the bar close and filled at the next bar's open, so actual fills can differ from the levels drawn on the chart.• Results depend strongly on the start date and on how much history your chart loads.• The strategy is long only and never uses an offset; it does not repaint.• The JSON alert messages exist only to show how such a strategy would be automated. Do not connect it to a live account.For educational purposes only. Not intended for live trading. Past results do not guarantee future performance, and this is not financial advice.これは「勝つための戦略」ではありません。Martingale Blow-up Demo は、マーチンゲール(価格が逆行するたびに、より大きなポジションを買い増す手法)が、なぜ魅力的に見えて、なぜ最後に破綻するのかを、実際のバックテストで確かめるための教育用ストラテジーです。初期設定のいくつかは、通常のストラテジーよりわざと大きくリスクを取っています。その理由は「初期設定(Default properties)」に書いてあります。■ 何をするか買いポジションを持ち、価格が一定の%下がるたびに買い増し、買い増しのサイズを毎回倍にし、平均建値から少し上がったら全体を決済します。ブローカーの強制決済(清算)もシミュレートします。資産が必要証拠金まで減ったら全ポジションを決済し、以後は一切取引しません(破産)。チャートには、平均建値、利確ライン、次のナンピン価格、清算価格、任意の撤退ラインを表示します。清算した足には、最高資産と清算後に残った資産を並べた大きなラベルを出します。ステータス表には、現在のラウンド、含み損益、資産に対する建玉の倍率、最深ラウンド、最高資産とその日付、現在の資産、清算日を表示します。初期設定での結果(OANDA:XAUUSD・1時間足・初期設定・2023年1月から取引・ディープバックテストで確認):・ストラテジーテスターの取引数は318、勝率は76%です(ナンピン1回ごとを別の取引として数えます)。・資産はゆっくり着実に増え、2026年1月29日に最高 14,365 USD(+43.6%)に達しました。・その後の4日間でゴールドは約5,400 USDから約4,550 USDまで下落。ナンピンは7回:0.13 → 0.26 → 0.52 → 1.04 → 2.08 → 4.16 → 8.32 → 16.64 オンス、合計33.15オンス(建玉は約150,700 USD)。・2026年2月2日、4,545.7で清算。この1回のポジションで 6,864 USD を失い、3年分の利益と元本の4分の1が消えました。最終資産は 7,501 USD(-25.0%)、プロフィットファクター0.68、最大ドローダウン47.8%。清算後に残る資産は、ブローカーが建玉のために確保している証拠金だけです。初回サイズを大きくする、ナンピン間隔を狭くする、清算価格を飛び越える窓が開く、といった条件では、残る資産はもっと少なくなり、マイナスになることもあります。■ 仕組み1. 初回エントリー:初期設定では、ノーポジションになったらすぐに買います(Always)。代わりに、MACD風のトレンド転換(平滑化した価格が速い平均の上にあり、速い平均が遅い平均の上にある)、Stochastic RSI の20以下からの上抜け、ATRチャネルのプラス転換を待つこともできます。2. 初回サイズ:初回の注文は、現在の資産の%(初期値5%)、固定金額、固定数量のいずれかです。初回サイズはそのポジションが終わるまで固定です。3. ナンピン:終値が直前の約定価格(または選んだ場合は平均建値)からナンピン間隔(初期値1.5%)だけ下がるたびに、「初回サイズ × 倍率のラウンド数乗」で買い増します。初期値の倍率2なら、毎回倍になります。4. 上限:最大ラウンド数に達したとき、または買い増し後の建玉が上限(初期値は資産の20倍。5%証拠金で持てる上限)を超えるときは、ナンピンしません。それ以降は、戻りを待つか、撤退する(ONの場合)か、清算されるかしかありません。5. 利確:終値が平均建値から利確%(初期値1%)上に来たら全体を決済します。任意で、そこからラチェット追従を始め、最高の終値から一定%戻した終値で決済することもできます(平均建値より下にはなりません)。6. なぜ勝ち続けるのか:ナンピンのたびにそれまでの合計より大きく買うので、平均建値が現在値に近づきます。少し戻るだけで全体が利益になるため、ほとんどのポジションが勝ちで終わります。7. 清算:清算価格は、口座の資産(現金+含み損益)が建玉の価値の5%(ストラテジー設定と同じ証拠金率)に等しくなる価格です。この価格に常に逆指値を置き、触れたら全決済して以後は取引しません。ポジションは指数関数的に膨らむので、戻らない下落が一度来るだけでこの価格に届き、たくさんの小さな勝ちがまとめて消えます。8. 撤退ライン(任意):上限に達したあと、平均建値から一定%下がったら決済します。清算は避けられますが、大きな損失が確定します。判定は足の終値で行います。ナンピン・利確・撤退は次の足の始値で約定し、清算の逆指値は足の途中でも約定します。■ 初期設定(Default properties)・初期資金:10,000 USD。個人トレーダーの一般的な口座規模です。・注文サイズ:スクリプトが計算します。初回は資産の5%、ナンピンは毎回前回の2倍です。プロパティタブの「既定の数量」(1枚)は、すべての注文が自分で数量を指定するので使いません。・手数料:注文ごとに0.02%。OANDAのゴールドCFDは別途の手数料ではなくスプレッドでコストがかかります。XAUUSDの一般的なスプレッドは数十セントで、価格の約0.01〜0.02%です。片道0.02%は、そのコストを控えめ(多め)に見積もった値です。・スリッページ:100ティック=1回の約定あたり0.10 USD(成行・逆指値)。OANDA:XAUUSDの1ティックは0.001 USDです。・証拠金:買い・売りとも5%(レバレッジ20倍)。OANDAの欧州・英国法人が適用している、ESMA/FCAのゴールドCFDの個人向け上限です。スクリプトの清算価格も同じ5%を使います。スクリプトの清算の逆指値が先に決済するので、初期設定のバックテストではマージンコールは発生しません。・ピラミッディング:20。最大20ラウンド(初回+19回のナンピン)を同時に持てるようにするためで、マーチンゲールには必要です。・ティックごとの再計算/約定後の再計算:OFF。注文は足の確定時に処理し、次の足で約定します。・意図的に外している点:1ポジションあたりのリスクをわざと極端にしています。通常のストラテジーは1回の取引で資産のごく一部しかリスクにさらしませんが、このストラテジーは資産の20倍までの建玉を許し、1回のポジションで最高資産の半分近くを失います。これはこのスクリプトが見せたい挙動そのものであり、実際の取引で真似してはいけません。■ 使い方・初期設定のチャート:OANDA:XAUUSD・1時間足。初期設定では2023年1月1日から取引を始めます。チャートで読み込める履歴がそれより短いと、読み込んだ最初の足から始まり、結果が変わります。・ストラテジーテスターとステータス表を一緒に見てください。たった1回の負けポジションが出るまでは、勝率はとても良く見えます。・設定: - First entry logic(初回エントリーロジック):Always(常時)、MACD trend shift(MACDトレンド転換)、Stoch RSI cross-up(STO RSIクロスアップ)、ATR channel(ATRチャネル)。 - Size mode / First size(サイズの決め方/初回サイズ):資産の%、固定金額、固定数量。 - Add spacing (%)(ナンピン間隔)、Add spacing measured(ナンピン間隔の基準:直前の約定価格から/平均建値から)、Size multiplier(サイズ倍率)、Max rounds(最大ラウンド数)、Max exposure(建玉上限:資産の何倍まで)。 - Take profit (%)(利確)、Ratchet trail (%)(ラチェット追従幅)。 - Use surrender line / Surrender line (%)(撤退ラインを使う/撤退ライン):上限到達後のみ有効。 - Simulate liquidation(清算をシミュレート):清算の逆指値と破産後の停止のON/OFF。 - Start / End(開始/終了):取引期間。 - 各ライン、清算ライン、ステータス表の表示ON/OFF。・試してほしいこと: - 初回サイズを大きくする:資産が早く増え、破綻も早く来ます。 - 開始日を動かす:少しずれるだけで、何年も増え続けるか、早々に退場するかが変わります。 - 撤退ラインをONにして、清算を避ける代わりに何を失うかを見る。 - 倍率を下げる、またはナンピン間隔を広げる:生き残りやすくなる代わりに、1回あたりの利益が小さくなります。■ 独自性公開されているマーチンゲールやグリッドのスクリプトの多くは、高い勝率と滑らかな資産曲線という魅力的な部分だけを見せ、清算を無視したり隠したりしています。このスクリプトは、一連の流れを最後まで見せるために作りました。ブローカーの証拠金水準での清算を、予定しているナンピンごとに価格を計算し直す逆指値でシミュレートし、清算後は取引を永久に止め、清算した足に最高資産と残った資産を並べて表示します。証拠金率に結びつけた建玉上限、上限到達後だけ有効な撤退ライン、ラチェット追従、初回エントリーのフィルター選択を使って、どの変更が本当にリスクを減らし、どの変更が破綻を先送りするだけかを試せます。■ 制限事項・清算価格は概算です。口座全体を1つのポジションの証拠金とみなし(クロスマージン)、5%の一律の率を使います。実際のブローカーは独自の強制決済ルールを使うので、もっと早く決済されることがあり、相場が速いときは逆指値よりかなり悪い価格で約定することもあります。・オーバーナイトの金利(スワップ)は計算していません。マーチンゲールのポジションは何日も持つことが多いので、実際のコストはもっと大きくなります。・ナンピンと決済は足の終値で判断して次の足の始値で約定するので、実際の約定はチャートに描いた水準とずれることがあります。・結果は開始日と、チャートで読み込める履歴の長さに大きく左右されます。・買いのみで、オフセットは使わず、リペイントもしません。・JSONのアラートメッセージは、この種のストラテジーをどう自動化するかを示すためだけのものです。実際の口座につながないでください。教育目的のみ。実際の取引を意図したものではありません。過去の結果は将来の成績を保証せず、投資助言ではありません。

Browse all 5,900+ TradingView Pine Script strategies

View on TradingView