Home » Strategy Functions » Understanding the log.info() Function in Pine Script

Understanding the log.info() Function in Pine Script

Photo of author
Published on

In this article, we’ll delve into the log.info() function, explaining its syntax, usage, and practical examples to enhance your Pine Script debugging skills.

Syntax and Overloads

The log.info() function comes in two forms:

  1. log.info(message) → void
  2. log.info(formatString, arg0, arg1, ...) → void


  • message (series string): The log message to be displayed.
  • formatString, arg0, arg1, ...: A string that contains literal text mixed with one or more placeholders for formatting. Each placeholder corresponds to an additional argument (arg0, arg1, etc.) that will be formatted according to the placeholder’s specifications.

Example Usage

Let’s consider a practical example to illustrate how to use the log.info() function within a trading strategy:

strategy("My Custom Strategy", overlay = true, margin_long = 100, margin_short = 100, process_orders_on_close = true)
bracketTickDistance = input.int(1000, "Stoploss/Take-Profit distance (in ticks)")

buySignal = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
if (buySignal)
    entryPrice = close * 1.01
    log.info("Buy limit order placed at {0}", entryPrice)
    strategy.order("Buy Order", strategy.long, limit = entryPrice)

    log.info("Setting exit orders: Take-profit at {0}, Stop-loss at {1}", close * 1.1, close * 0.9)
    strategy.exit("Exit Order", "Buy Order", profit = bracketTickDistance, loss = bracketTickDistance)

excessiveTrades = strategy.opentrades > 10
if (excessiveTrades)
    log.warning("More than {0} trades opened. Adjust `bracketTickDistance` to manage risk.", strategy.opentrades)

capitalRisk = strategy.initial_capital / 10 > strategy.equity
if (capitalRisk and not capitalRisk[1])
    log.error("Strategy has depleted over 90% of initial capital!")


  • Strategy Definition: The script starts by defining a trading strategy named “My Custom Strategy” with some specific parameters:
    • overlay = true makes the strategy output (like buy/sell markers) overlay on the price chart.
    • margin_long and margin_short are both set to 100, defining the margin requirements for long and short positions respectively.
    • process_orders_on_close set to true means the strategy will process orders at the close of the candle/bar.
  • Input for Stop-Loss/Take-Profit: It introduces an input for the user to specify the distance (in ticks) for both the stop-loss and take-profit orders from the entry price. This is done via input.int(1000, "Stoploss/Take-Profit distance (in ticks)").
  • Buy Signal Logic: It calculates a buy signal based on a simple moving average (SMA) crossover strategy. If the 14-period SMA crosses above the 28-period SMA (ta.crossover(ta.sma(close, 14), ta.sma(close, 28))), it triggers a buy signal.
  • Order Placement on Buy Signal: Upon a buy signal:
    • The entry price for a buy order is set at 1% above the current closing price (entryPrice = close * 1.01).
    • Logs an informational message about the placement of a buy limit order using log.info.
    • Places a buy order with the specified entry price using strategy.order.
  • Exit Orders Setup: For the same buy order, it also sets exit orders (take-profit and stop-loss) at a specified distance (in ticks) from the entry price using strategy.exit.
  • Excessive Trades Warning: It checks if the number of open trades exceeds 10. If so, it logs a warning message suggesting to adjust the bracketTickDistance to manage risk better.
  • Capital Risk Error: The script evaluates if the strategy has lost more than 90% of its initial capital. It uses a condition that compares the strategy’s initial capital divided by 10 to the current equity. If this condition is true and wasn’t true in the previous bar (and not capitalRisk[1]), it logs an error message indicating that the strategy has depleted over 90% of initial capital.

Key Features and Takeaways

  • The log.info() function is essential for debugging by allowing the logging of informational messages in Pine Script.
  • It supports formatted strings, enabling dynamic insertion of variable values into log messages for detailed output.
  • Proper logging aids in tracking the execution flow and understanding the behavior of trading strategies, especially in complex scenarios.

By incorporating the log.info() function into your Pine Script strategies, you can significantly enhance the debugging process, making it easier to refine and optimize your trading algorithms.

Leave a Comment