Closed fightthepower closed 5 years ago
Currently there is no such functionality, but it is an interesting use case that we could consider when we are seeking to implement advanced order types and rules-based orders.
In the time being, you could do this with a very simple cron task or even a scheduled cloud-function if you wanted to have it safely executed in a robust hosted environment.
Here is some sample code that you could base something on using the python sdk.
import alpaca_trade_api as tradeapi
api = tradeapi.REST()
orders = api.list_orders(status='open')
positions = api.list_positions()
if orders or positions:
if positions:
print(positions)
if orders:
print("Canceling open orders:")
print([o.id for o in orders])
result = [api.cancel_order(o.id) for o in orders]
print(result)
closed = []
for p in positions:
side = 'sell'
if int(p.qty) < 0:
p.qty = abs(int(p.qty))
side = 'buy'
closed.append(
api.submit_order(p.symbol, qty=p.qty, side=side, type="market", time_in_force="day")
)
if closed:
print("Submitted Orders", closed)
for o in closed:
status = api.get_order(o.id)
if status.status == 'rejected':
print("ORDER FAILED: Your Order was Rejected!!!")
I believe #56 will achieve it.
I want to liquidate all the positions by the end of the day automatically even if my own code didn't initiate it. Is there any way I can tell alpaca to liqudate a position at a particular time of the day (eg: 4.00 PM) while placing an order?