How to Automate Position Closures with TradersPost Scripts

Fact checked by
Mike Christensen, CFOA
November 27, 2024
Learn how to automate position closures with the "Close This Position" script in TradersPost, perfect for end-of-day exits or time-based trade management.

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 This Position" script to automate trade exits based on specific times, ensuring streamlined execution and reduced manual intervention.

What Is the Close This Position Script?

The "Close This Position" 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
    • Create an alert in TradingView.
    • Choose the "Close This Position" script as the alert condition.
    • Ensure your webhook URL is correctly configured in the alert’s notification tab.
  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.

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 marker will appear.
  • To verify settings, switch to a resolution matching your time granularity (e.g., 1-minute chart).

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:

  • Mid-session risk management.
  • Closing positions before economic announcements.
  • Custom JSON payloads for advanced strategies.

Conclusion

The "Close This Position" 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.

Ready to automate your trading? Try a free 7-day account:
Try it for free ->