Features & Settings

How to Automate Position Closures with TradersPost Scripts

Learn how to automate position closures with the "Close All Positions" TradingView script from TradersPost, perfect for end-of-day exits or time-based trade management.

Tom Hartman

Marketing

3 Min Read Reviewed by Mike Christensen Fact-checked by Mike Christensen
BluSky — The Future of Trading. Prop firm futures trading. Sign up at BluSky.pro.
Update history
  1. Correction

    Traders ask how to close part of a position, so the guidance now covers exit quantities and their override, and the script's new name.

Closing open positions at a specific time is a frequent request among traders, especially for strategies that prioritize end-of-day risk management. TradersPost now offers a customizable script to handle this task. This guide explains how to use the "Close All Positions" script to automate trade exits based on specific times, ensuring streamlined execution and reduced manual intervention.

What Is the Close All Positions Script?

The "Close All Positions" script is a tool designed to:

  1. Automate position closures for the specific symbol displayed on your TradingView chart.
  2. Enable time-based closures by defining a specific hour and minute.
  3. Simplify alert creation with preconfigured JSON messages sent to TradersPost via webhooks.

How It Works

Key Features

  • Symbol-Specific Actions: The script applies only to the symbol on your TradingView chart (e.g., ES1!).
  • Action-Based Alerts: Sends an action=exit JSON payload to close all contracts or shares for the selected symbol.
  • Configurable Timing: Users can set the exact time (hour and minute) for the closure within the script’s settings.

Example Workflow

  1. Script Configuration
    • Define the hour, minute, and time zone for the closure.
    • Input the desired JSON message in the script settings, under “Inputs.”
  2. Alert Setup
  3. Execution
    • At the specified time, the alert triggers, sending the JSON payload to TradersPost.
    • TradersPost looks up the current position for the specified symbol and executes the closure.

JSON Message Example

When creating alerts, the script sends a JSON message like this:

{
   "ticker": "ES1!",
   "action": "exit"
}

What This Does

  • ticker: Specifies the symbol (ES1! in this case).
  • action: Tells TradersPost to exit all open positions for the symbol.

If you have three contracts open for ES1!, this message will close all three. To close only part of the position, add a quantity to the exit message: "quantity": 2 exits two contracts, and a quantity larger than the open position exits all of it1. TradersPost uses that quantity only when Allow signal override is checked next to the quantity settings in your strategy subscription; otherwise it exits the full position2.

Important Considerations

Chart Resolution Limitations

The script uses TradingView’s chart resolution to place markers.

  • If the chart resolution doesn’t match the specified time (e.g., a 15-minute chart with a 3:07 PM closure), no alert fires and no marker appears, because the script's source code triggers only on a bar that opens at the chosen hour and minute3.
  • Create the alert on a chart whose resolution matches your time granularity (e.g., a 1-minute chart), so a bar opens at the chosen minute.

Validation Tools

  • Markers on Chart: Visual confirmation that the script will trigger correctly.
  • Pine Logs: Console logs in Pine Script show when the script executes, adding another layer of verification.

Creative Applications

While designed for end-of-day closures, the script’s flexibility allows for creative uses, such as:

Conclusion

The "Close All Positions" script bridges the gap between TradingView and TradersPost, providing an efficient solution for time-based position closures. By leveraging its customizable settings and webhook capabilities, traders can automate critical exit actions, ensuring consistency and reducing manual oversight. If you’re ready to optimize your trade management, this script is a must-try tool.

References

1 docs.traderspost.io, Signal Message Reference
2 docs.traderspost.io, Position Sizing
3 tradingview.com, Close All Positions

  • Post-Only Order: How It Works

    Post-only orders guarantee maker-side execution or cancellation. Learn how they work, why exchanges cancel or reprice them, and when to use one.

  • Difference Between Mark And Last In TOS

    Mark price and last price mean different things on trading platforms. Learn how each is calculated, why they diverge, and which to use for stops.

  • Broker Rules Now Follow the Asset Class

    Learn why a setting disappeared in TradersPost: broker rules for order types, shorting, and time in force now apply per asset class, not per broker.

  • Walk-Forward Optimization Trading Guide

    Master walk-forward optimization trading to test strategies across market regimes, reduce curve fitting, and prepare alerts for disciplined automation.

Start trading at scale today. Sign up for free.

Free 7-day trial

Set-up in 3 minutes

Paper account for testing

By continuing, you agree to the TradersPost Terms of Service and Privacy Policy. This site is protected by reCAPTCHA.

TradersPost operates as a non-custodial automated trading platform, enabling users to connect alerts from their preferred trading platforms to their selected brokerage or exchange accounts. It abstains from the transmission, custody, or management of customer funds, covering both traditional and cryptocurrency assets. Typically, registration requirements set by regulatory entities such as the SEC, FINRA, or FinCEN apply to entities that hold or transmit customer funds. To ensure ongoing compliance, TradersPost regularly engages with regulatory authorities to confirm its adherence to all relevant local and federal laws.

TradersPost does not provide alerts, signals, research, analysis, or trading advice of any kind. It is designed to assist traders and investors in making their own trading decisions based on their alerts. The platform does not offer recommendations regarding securities to buy or sell, nor does it provide trading or investing advice. The platform and its features, capabilities, and tools are provided 'as-is' without any warranty.

Risk Disclosure: The use of automated trading systems involves inherent risks, including the potential for significant financial loss. These systems operate based on predetermined algorithms that may not fully adapt to changing market conditions, possibly making them unsuitable for some investors. Individuals are advised to thoroughly assess their financial situation and risk tolerance before using this platform.

Testimonials appearing on this website may not be representative of other clients or customers and is not a guarantee of future performance or success.

Broker, exchange, trading platform, company names, product names, service marks, trademarks, and logos appearing on this website are the property of their respective owners and are used solely to identify supported connections and technical compatibility. TradersPost is an independent third-party platform and is not affiliated with, endorsed by, sponsored by, or authorized by any of these organizations unless expressly stated. Technical compatibility does not imply a commercial partnership, sponsorship, endorsement, or authorization. See Important Disclosures for trademark information.

© 2026 TradersPost, Inc. All rights reserved.