Code Zero Visual Trading for TradingView


Averaging when the market is not moving in your direction.

WARNING This strategy has an increased risk and should be used with an understanding of all the risks.

What is Martingale?

This is a decision-making system in which you increase your bet with each losing result. If you win, you get back everything you lost.

MARTIN GRID STRATEGY is a powerful tool for the TradingView platform that allows you to create and test various trading strategies based on an order grid. This module is especially useful for strategies that utilize the principle of position averaging (Martingale).

With MARTIN GRID STRATEGY you will be able to:

  1. Configure a buy or sell order grid. You will be able to set price levels at which new orders will be opened. Each order can have its own volume, which will increase your position in the desired direction.

  2. Flexibly configure position closing. You will be able to set price levels for closing a position, and you can close a position partially at different levels and with different volumes. This allows you to optimize profits and minimize risks.

  3. Move the Stop-Loss to breakeven. The module allows you to automatically move your Stop-Loss order to the breakeven point to protect your position from potential losses.

  4. Test your strategy on historical data. You will be able to check how your strategy would have worked in the past and find the optimal parameters for the order grid and position closing levels. The module will also help you test your strategy for liquidation risk so that you can manage risks.

MARTIN GRID STRATEGY is a convenient and effective way to create, test, and optimize trading strategies based on an order grid right on the TradingView platform, without the need to use external tools or programming. The configured strategy can be launched for automated trading.

Trading Settings

Direction - This parameter determines the trading direction for your strategy. You can choose LONG (buy) or SHORT (sell). This choice depends on your market analysis and expectations regarding the price movement.

OPEN Signal - This is the signal that initiates opening the first position in your trading strategy. Most often, this signal comes from another module, such as the CONDITION MANAGER Indicator. This indicator analyzes market conditions and generates a signal when certain conditions that you have set are met.

Position size, USD - This is the total amount you want to invest in this trading strategy. This amount will be divided among the various orders in your order grid. The size of each individual order will be determined later when you configure the grid levels.

Set leverage - This is the parameter you set if you are using margin trading. It determines what credit leverage you will use. This parameter is important for correctly calculating the profitability of your strategy when backtesting on historical data.

Your position size will be calculated by the formula:

Position size = Initial Capital * Leverage

This formula shows the relationship between your initial capital, credit leverage, and total position size.

For example, if your initial capital (Initial capital) is $400 and the credit leverage (Leverage) is 4, then your position size (Position size) will be $1600.

These parameters are key to setting up your trading strategy in the MARTIN GRID STRATEGY module. Properly understanding and configuring them will help you create an effective and profitable strategy.

Reinvest - This parameter determines what to do with the profits earned from successful trades. You have two options:

  1. Reinvest profits: If you enable this option, the profits from your successful trades will be added to your position size for subsequent trades. This means that each subsequent trade will be opened with a larger position size as your capital grows. This approach can lead to exponential growth of your profits if the market moves in the direction you have chosen. However, it also increases the risk, as a losing trade can result in larger losses.

  2. Do not reinvest profits: If you disable this option, the profits from your successful trades will not be added to your position size. Each new trade will be opened with the same position size you determined at the start. This is a more conservative approach and helps limit your risks. Your potential profits may be lower, but your potential losses will also be limited.

The choice between these two options depends on your risk tolerance and your overall trading strategy. If you are confident in your strategy and willing to take higher risks for potentially higher profits, reinvesting may be a good choice. But if you prefer a more cautious approach, it's better not to use reinvesting.

Regardless of your choice, always remember risk management and never invest more than you can afford to lose.

Order Grid Configuration

You can configure the deviation from the entry price and the required position volume.

In the MARTIN GRID STRATEGY module, you can configure an order grid that will open at certain price levels. This grid allows you to average your entry price into a position if the market moves against you, and to take profits if the market moves in your direction.

In the example image, we see the following settings:

  • When opening the first order, we invest 6.67% of our total position size.

  • If the price goes against us by 2%, we open an additional order (effectively "buying more") for another 13.34% of our position size. This means we are increasing our position when the price moves unfavorably for us.

  • If the price goes even further against us, by 8%, we open another order for 26.68% of our position size.

  • If the price goes against us by as much as 13%, we open an order for the remaining part of our position.

This strategy allows us to "average" our entry price. If the market eventually reverses and goes in our direction, we will be in profit, despite some of our orders being opened at worse prices.

On the other hand, if the price moves in our favor (i.e., in the direction of our trade) at any of these levels, we can configure taking profits. This allows us to close part or all of our position when we reach a certain profit level.

Remember, the more levels in your grid and the higher percentage of the position you open at each level, the higher your potential profit but also the higher the risk. Always set these parameters according to your risk tolerance and don't forget about capital management rules.

After configuring our order grid for entry, we can also set up conditions for taking profits and risk management using the Trailing Stop feature.

In this example, we see the following settings:

  1. Take Profit 1: When our position reaches 1% profitability, we automatically close 75% of our position. This allows us to lock in most of our profits if the market moves in our direction.

  2. Take Profit 2: After locking in the first part of the profits, we leave the remaining 25% of our position open, hoping the price will continue in our favor. We set a second target of 2% profitability. If the price reaches this level, we close the remaining position, maximizing our profit.

  3. Trailing Stop: To protect our profits after reaching the first take-profit target, we set a trailing stop-loss. In this example, it's set at 0.5% away from the price at which we achieved 1% profitability. This means that if the price starts moving against us after hitting 1% profit and falls by 0.5%, our position will be automatically closed. But if the price continues in our favor, the Trailing Stop will follow the price, keeping a 0.5% distance, allowing us to capture more profit.

These take-profit and trailing stop settings allow us to be flexible and adaptive to market conditions. We can lock in part of our profit early on while giving the position a chance to make more profit, but protecting our profit from sudden market reversals.

Remember, you can configure these parameters according to your strategy and preferences. Always test your settings on historical data and adjust as needed.

Moving Stop-Loss to Breakeven

When you trade with multiple take-profit levels, you can choose not to use a trailing stop (as described earlier), but instead move your stop-loss to the breakeven point after reaching the first take-profit level.

Here's how it works:

  1. You set multiple take-profit levels, for example, 1%, 2%, and 3%.

  2. You also set an initial stop-loss to limit your potential losses.

  3. When the price reaches your first take-profit level (1% in our example), your stop-loss is automatically moved to the breakeven point (i.e., your initial entry price level).

  4. This means that if the price moves against you after reaching the first take-profit level, your position will be closed at your initial entry price, thereby protecting you from losses.

  5. However, if the price continues to move in your favor, your position remains open, giving you the opportunity to reach higher take-profit levels and earn greater profits.

This feature is especially useful if you want to give your position a chance to make more profit, but at the same time want to protect yourself from potential losses if the market moves against you after reaching the first target.

Fixed Stop-Loss

In addition to moving the stop-loss to the breakeven point, you also have the option to set a fixed stop-loss for your strategy. This stop-loss is set as a percentage from the average price of your open position.

Here's how it works:

  1. You open a position according to your strategy and order grid settings.

  2. You set a fixed stop-loss, for example, at 5% below the average price of your position.

  3. If the price falls and reaches this stop-loss level, your position is automatically closed, limiting your losses.

  4. This stop-loss remains at the same level (i.e., it does not move with the price like a trailing stop) until you either close the position or your take-profit levels are reached.

A fixed stop-loss is especially useful if you want to limit your potential losses to a certain level. This can be helpful for risk management, especially if you are trading in conditions of high volatility or uncertainty in the market.

However, there are a few things to keep in mind when using a fixed stop-loss:

  1. If you have multiple entries into a position (as in the Martingale strategy), your stop-loss will apply to the average price of all your entries.

  2. A fixed stop-loss does not automatically move to the breakeven point. You must separately set this feature if you want to use it.

  3. If the market is very volatile, a fixed stop-loss may lead to premature closing of your position, even if the overall trend is still in your favor.


When trading with leverage (margin trading), you must be especially careful about risk management. One of the biggest risks is the liquidation of your position.

Liquidation occurs when the price of an asset falls to a certain level (the liquidation price), and your position is automatically closed by the exchange to prevent further losses. This happens because in margin trading, you are essentially borrowing money from the exchange, and the exchange wants to ensure that you can pay back that money.

Here's how you can test and manage liquidation risk in MARTIN GRID STRATEGY:

  1. In the strategy settings, you can set the liquidation price as a percentage from the average price of your open position.

  2. This percentage may vary across different exchanges. For example, on one exchange liquidation may occur when the price falls 20% from your average entry price, while on another it may be a 15% drop.

  3. When you backtest your strategy on historical data, MARTIN GRID STRATEGY will take the liquidation price into account. If the price reaches this level during the backtest, the strategy will stop opening new trades.

  4. This gives you a clear picture of how resilient your strategy is to market volatility. If your strategy frequently reaches the liquidation price during backtests, it means it is too risky and requires adjustment.

  5. You can adapt your strategy by adjusting the order grid parameters, take-profit and stop-loss levels, or decreasing the leverage size to reduce the liquidation risk.

Remember, even if your strategy performs well during backtests, there is always a risk of liquidation when trading in live markets. Therefore, always manage your risks carefully, use moderate position sizes, and choose your leverage size judiciously.

Also, keep in mind that past performance is not a guarantee of future results. Market conditions can change, so always be prepared to adapt your strategy to current market conditions.

Here are the key points about the operating modes of the strategy:

  1. Backtest Mode

In this mode, you test your strategy on historical data. This allows you to see how your strategy would have performed in the past without risking real money.

In backtest mode, you can:

  • Optimize your strategy parameters: Adjust order grid settings, take-profit and stop-loss levels, leverage size, etc. to see how it impacts your strategy's results.

  • Set a test period: Choose a specific time period in the past (e.g. last 6 months) to see how your strategy would have performed during that time.

  • Analyze the results: After running the backtest, you get a detailed report on your strategy's performance, including total profit/loss, number of trades, percentage of winning trades, etc. This provides valuable information for strategy optimization.

  1. Live Trading Mode

After optimizing your strategy in backtest mode, you can move to live trading mode. This is where your strategy will operate in real-time, opening and closing actual trades.

In live trading mode, it's important to:

  • Set start date/time: Specify when your strategy should start operating. This resets any historical trades and lets the strategy start fresh.

  • Set start time 15 minutes ahead: Best practice is to set the start time 15 minutes into the future to allow final checks and set up alerts.

  • Configure alerts: After setting the start time, set up alerts to receive notifications for every trade opened/closed by your strategy.

Remember, transitioning from backtest to live trading is a big step. Always start with small amounts when trading live and closely monitor your strategy's performance. Be prepared to make adjustments if market conditions change.


Last updated