# How to liquidate all positions before close

**URL:** https://forum.alpaca.markets/t/how-to-liquidate-all-positions-before-close/8219
**Category:** Alpaca Trading
**Created:** [February 2, 2022, 2:48am UTC](https://forum.alpaca.markets/t/how-to-liquidate-all-positions-before-close/8219 "2022-02-02T02:48:03Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Hudson](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/hudson/32/3663_2.png) [@Hudson](https://forum.alpaca.markets/u/Hudson)
#### Post date: [February 2, 2022, 2:48am UTC](https://forum.alpaca.markets/t/how-to-liquidate-all-positions-before-close/8219/1 "2022-02-02T02:48:03Z")

</div>

I have my bot set up and working. I implemented the bot turning itself on and off when the market is open and closed. The one problem I have is figuring out how to get the bot to liquidate all positions let’s say around 5 minutes before market close. Does anyone know the best way to implement this? Thanks!

---

<div class="post-metadata">

### Author: ![Jian\_Lao](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/jian_lao/32/805_2.png) [@Jian\_Lao](https://forum.alpaca.markets/u/Jian_Lao)
#### Post date: [February 2, 2022, 9:12pm UTC](https://forum.alpaca.markets/t/how-to-liquidate-all-positions-before-close/8219/2 "2022-02-02T21:12:29Z")

</div>

Try following on your paper account:

api.close\_all\_positions()

---

<div class="post-metadata">

### Author: ![maxks90](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/maxks90/32/3534_2.png) [@maxks90](https://forum.alpaca.markets/u/maxks90)
#### Post date: [February 3, 2022, 11:28am UTC](https://forum.alpaca.markets/t/how-to-liquidate-all-positions-before-close/8219/3 "2022-02-03T11:28:11Z")

</div>

I just wrote this in Python. Haven’t tested during market hours yet, but works with other times.

```auto
import datetime as dt
import pytz
import pandas as pd

def close_all_positions_end_of_day():
    # First, check if the market is currently open. No point in checking if closed.
    if api.get_clock().is_open:
        # Get the current time (New York time)
        time_now = dt.datetime.now(pytz.timezone('US/Eastern'))

        # Calculate the timestamp for 5 mins before close (15:55) today.
        five_mins_before_end_of_day = pd.Timestamp(year=time_now.year, month=time_now.month, day=time_now.day, hour=15,
                                                   minute=55, second=00, tz='US/Eastern')

        # If the current time is the same as (or after) 15:55, close all positions.
        if time_now.isoformat() >= five_mins_before_end_of_day.isoformat():
            print('Closing all positions...')
            api.close_all_positions()
        else:
            print('Not yet 3:55pm!')
    else:
        print('Market currently closed!')

```

Bear in mind that your bot would need to be regularly/frequently calling this function to check if the current time is at/after the timestamp.
