PROTECTED SOURCE SCRIPT

MartinGale Average Simulator - By Lowis

30
Originality and Utility

This script is not a traditional indicator nor a cosmetic variation of existing tools such as moving averages, oscillators, or common indicator combinations. It is a deterministic averaging and risk modeling engine, specifically designed to simulate, analyze, and validate multi-order averaging (DCA) structures under fully configurable conditions.

The originality of this script lies in the fact that it does not generate trade signals and does not attempt to predict market direction. Instead, it models the mathematical behavior of an entire chained order structure, allowing the user to quantitatively evaluate how an averaging strategy behaves as price evolves.

The script continuously computes and displays:

The dynamic average entry price after each additional order.

The progressive position size growth as orders are added.

The total capital committed, factoring in leverage.

The individual PnL per order and the aggregated PnL of the entire structure, both in absolute and percentage terms.

The real account usage percentage as the averaging sequence progresses.

The sensitivity of the structure to changes in the current or simulated price.

Unlike classic indicators that operate on historical price data to infer probabilities, this tool functions as a deterministic planning and risk-audit system. Its purpose is to help traders answer structural questions that standard indicators do not address, such as:

How much capital is actually committed by the time the Nth order is reached.

The exact resulting average price given a custom percentage distribution across orders.

The structural drawdown required before an averaging strategy becomes profitable.

How changes in leverage, number of orders, or percentage distribution affect overall risk exposure.

The script allows full user control over key parameters, including:

Number of averaging orders.

Custom percentage offsets per order.

Account capital and leverage.

Entry price and current/simulated price.

Decimal precision for price and asset quantity.

Clear visualization through tables and price-level graphics.

This makes the script a trade engineering and risk modeling tool, rather than a signal-based indicator. Its value lies in structural analysis and execution planning, a category not covered by standard open-source scripts in the public library.

The source code is intentionally kept private because the internal averaging, position-sizing, and risk-accumulation engine represents proprietary logic that can be directly reused for commercial products or automated systems. Disclosing this implementation would allow immediate replication without providing additional educational or functional value to the end user, who already has full operational control through exposed inputs.

For these reasons, the script is functionally original, technically useful, and fully justifies closed-source protection, in compliance with TradingView’s publication guidelines.

🔧 How the Indicator Works (Technical Overview)

This indicator implements a deterministic averaging (DCA) simulation engine designed to accurately model the mathematical, financial, and percentage-based behavior of a leveraged position composed of multiple sequential orders.

Unlike traditional indicators that only display static levels or visual signals, this script reconstructs the full internal structure of a position, order by order, allowing the user to analyze its complete evolution under different price scenarios.

📌 Entry Price Calculation per Order

Starting from an initial entry price, the user defines a set of percentage-based distances for each averaging order.
Each new entry price is calculated as:

A percentage deviation relative to the original entry price

Adjusted by trade direction (long or short)

Dynamically rounded according to user-defined price precision

As a result, each order has an independent, deterministic, and reproducible price, without relying on external data or real trade execution.

📌 Position Size and Capital Usage Calculation

For every order, the engine computes:

Asset quantity acquired based on allocated capital and leverage

Actual margin used considering leverage

Progressive accumulation of total deployed capital

Account capital usage percentage per order and in total

This allows the trader to clearly visualize how real account exposure grows as additional averaging orders are added — something that is not evident in standard DCA tools.

📌 Average Entry Price Recalculation

After each new order, the indicator recalculates the weighted average entry price of the entire position by combining:

The previous average price

The new entry price

The updated total asset quantity

This accurately reflects how the real break-even level evolves as the position is averaged, which is critical for aggressive averaging or martingale-style strategies.

📌 Individual and Cumulative PnL Computation

The script supports two evaluation modes:

Current market price

User-defined simulated price, intended for hypothetical or stress-test scenarios

Using the selected price, the indicator calculates:

Individual PnL per order

Total cumulative PnL of the position

PnL percentage relative to the capital used in each order

PnL percentage relative to total deployed capital

Each order maintains its own mathematical identity, avoiding common errors where PnL is diluted or calculated solely against the averaged price.

📌 Structured and Objective Visualization

All calculations are represented through:

Independent horizontal price lines per order

Informational labels anchored to their corresponding price levels

Tabular summaries displaying the exact state of each order, row by row

No classical indicators, predictive signals, or discretionary filters are used.
The system is 100% mathematical, deterministic, and reproducible.

📌 Purpose and Practical Utility

This indicator is designed to:

Evaluate the mathematical viability of averaging schemes

Analyze real leverage-based risk exposure

Compare price scenarios before execution

Understand how small price movements affect large accumulated positions

Identify points where capital usage becomes inefficient or dangerous

It does not execute trades and does not generate trading signals.
Its purpose is structural position analysis, not prediction.


🧭 How to Use the Indicator (Step-by-Step Guide)

This indicator does not require prior trading experience to be used.
It operates entirely through configurable parameters and updates automatically in real time.

1️⃣ Define the Initial Entry Parameters

Start by setting the initial entry price.
This value represents the price at which the first position entry is opened.

Next, define:

Account capital: the total available capital

Entry percentage: the percentage of the account used in the first order

The indicator automatically calculates:

Capital invested

Asset quantity acquired

Real position exposure

2️⃣ Select the Position Direction

Choose whether the position is:

Long (benefits from price increases), or

Short (benefits from price decreases)

This selection automatically adjusts:

Price movement direction

PnL calculations

Averaging percentage behavior

No additional configuration is required.

3️⃣ Configure Leverage and Number of Orders

Set the leverage used for the position.
This value is applied to calculate:

Required margin per order

Total exposure relative to account capital

Then, specify the number of averaging orders.
Each order represents an additional entry that would be placed if price moves against the position.

4️⃣ Define Averaging Percentages

Enter the percentage offsets for each averaging order, separated by commas.

Example:

4, 8, 13, 19, 39, 54


Each value represents how far (in percentage terms) price must move from the original entry before adding a new order.

The indicator automatically calculates:

Exact price level of each order

Updated average entry price

Capital deployed per order

Total capital usage

5️⃣ (Optional) Set a Simulated Price

Optionally, a simulated price can be defined.

This allows users to:

Evaluate hypothetical market scenarios

Analyze deep drawdowns

Simulate price recoveries

Study PnL behavior without waiting for live market movement

Any change to this value instantly recalculates all results.

6️⃣ Interpreting the Results

Once configured, the indicator displays in real time:

Individual order prices

Updated average entry price

Capital invested per order and in total

Individual and cumulative PnL

PnL percentage relative to deployed capital

No buttons or manual refresh are required.
Any parameter change updates the entire simulation automatically.

🎯 Important Notes

The indicator does not execute trades

No buy or sell signals are generated

All calculations are purely mathematical and deterministic

Its purpose is to visualize, analyze, and understand how a multi-entry averaging position behaves under different market conditions.


🔒 Closed-Source Justification

This script is published as closed-source because it implements a custom multi-order position simulation engine that goes beyond standard indicator calculations.

Internally, the script relies on a structured calculation framework that manages:

Order-to-order dependency

Cumulative capital usage across multiple entries

Dynamic average price recalculation

Individual and aggregated PnL modeling

State-aware recalculation logic tied to user-defined parameters

The value of the script resides not in isolated formulas, but in the overall architecture and calculation flow that coordinates these elements into a coherent position analysis model.
Exposing the full source code would effectively reveal the complete framework, making the script trivial to replicate and removing its practical uniqueness.

Despite being closed-source, the script provides full transparency at the output level, allowing users to verify all calculated values directly on the chart through tables, labels, and price-level visualizations.

For this reason, the script is shared as a closed-source publication while still offering complete analytical clarity and educational value to the end user.

⚠️ Disclaimer

This indicator does not provide trade signals, nor does it place or manage real orders.
It is intended strictly for educational, analytical, and risk evaluation purposes.

Haftungsausschluss

Die Informationen und Veröffentlichungen sind nicht als Finanz-, Anlage-, Handels- oder andere Arten von Ratschlägen oder Empfehlungen gedacht, die von TradingView bereitgestellt oder gebilligt werden, und stellen diese nicht dar. Lesen Sie mehr in den Nutzungsbedingungen.