OPEN-SOURCE SCRIPT
Updated Hedge Simulation Martingale v1

1. Overview & Strategy Logic
This script implements an automated, multi-position trading strategy that uses a Martingale-inspired approach to manage a series of entries. The core logic is as follows:
Initial Entry: The script enters a trade based on the direction of the previous bar's close. A green bar triggers a Long position; a red bar triggers a Short position.
Profit-Taking: A single, fixed-percentage profit target (Profit Percentage) is set for the entire trade. If reached, all positions are closed for a net profit.
Loss Management (Martingale Logic): If the price moves against the initial position and hits the fixed-percentage stop-loss (Loss Percentage), the script does not exit. Instead, it averages down by adding a new, larger position in the same direction. The size of the new position is determined by multiplying the previous position size by the First Multiplier.
Net Position Management: The script continuously calculates the net average entry price, a new combined profit target, and a new combined stop-loss based on all open positions. The goal is for a single favorable price move to recover all previous losses and hit the profit target.
2. Key Features
Visual Indicators:
Plots the Net Average Entry Price on the chart.
Plots dynamic Profit Target (TP) and Stop-Loss (SL) levels that update as new positions are added.
Displays entry signals (triangles) for the initial Long or Short trade.
Comprehensive Dashboard: A detailed table in the top-right corner shows real-time metrics, including:
Total historical Long/Short volume and PnL.
Current trade's investment, unrealized PnL, and position sizes.
Current position count, direction, and size.
Configurable Parameters:
Profit Percentage: The target profit percentage for the net position.
Loss Percentage: The stop-loss percentage that triggers a new entry.
Initial Position Size: The size of the first position in the series.
First Multiplier: The multiplier applied to the previous position size when averaging down.
Maximum Multiplier: A safety cap (commented out in the code but present) to prevent infinite scaling.
3. Intended Use & Purpose
This script is designed as a position management and tracking tool for traders who are experimenting with or actively using Martingale-style strategies. It is best used to:
Automate the complex calculations of average entry, combined TP/SL, and PnL for multiple entries.
Visually track the status of an ongoing series of positions.
Backtest the viability and risks of such a strategy on historical data.
4. ⚠️ Critical Risk Warning & Disclaimer
THIS STRATEGY CARRIES EXTREME FINANCIAL RISK. USE AT YOUR OWN RISK.
Unlimited Loss Potential: The Martingale strategy is infamous for its potential to generate unlimited losses. By continuously doubling down (or multiplying) on losing positions, a small adverse price move can lead to catastrophic losses that can exceed your account balance.
Margin Calls: The rapidly increasing position size can quickly deplete your margin, leading to a margin call and forced liquidation of all positions at a significant loss.
No Guarantee of Recovery: The assumption that the price will eventually reverse is flawed. A strong, sustained trend can wipe out the entire trading capital.
For Educational/Advanced Use Only: This script is intended for sophisticated traders who fully understand the immense risks involved. It is not a "sure profit" system.
The publisher of this script is not responsible for any financial losses incurred through its use. You are solely responsible for your trading decisions and risk management.
5. How to Use
Apply the Script: Add the script to your chart.
Configure Parameters: Adjust the input parameters according to your risk tolerance and strategy rules. Be extremely cautious with the multiplier and position size.
Monitor the Dashboard: The table will provide all necessary information about the current and historical state of the strategy.
Observe the Levels: Watch the plotted Entry, TP, and SL levels to understand the current market position.
Backtest First: Always test the strategy extensively on historical data before considering it with real capital.
6. Notes
The Maximum Multiplier safety feature is present in the code but is currently commented out. Users are strongly advised to uncomment and set this parameter to act as a final, hard liquidation point.
The script logs key events (trade start, target hit) and export data for further analysis.
This is a complex script and should be thoroughly understood before use.
Release Notes
Bug fixed in calculate benefits.Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.