Back

How do I liquidate over time?

Hey everyone,

I'm dealing with somewhat large percentages of daily value in some cases, and to be safe i'd like to liquidate over time instead of instantly to avoid slippage costs on exit.

How can I liquidate over X bars?

For example, over 60 bars, with 1/60th of my initial holdings being liquidated each time.

Thanks,

AS

Update Backtest








If we set the initial quantity, say 600 shares, then we can place sell orders with 100 shares while we hold stock (Portfolio.HoldStock == true) on every OnData call. However look at my solution where we menage the initial quantity with OnOrderEvent method:

public override void OnOrderEvent(OrderEvent orderEvent)
{
if (orderEvent.Status == OrderStatus.Filled)
{
var quantity = orderEvent.FillQuantity;
_initialQuantity = Math.Max(_initialQuantity, quantity);
}
}

The Math.Max function is used to compare the initial quantity against the subsequent sell orders.

Also, we have to take into account that we cannot be short, so the trade that will close the position may not be 1/60 of the initial quantity (note the Math.Min function):

// In OnData
var quantity = (int)Math.Ceiling(_initialQuantity / 60);
Order("SPY", -Math.Min(quantity, Portfolio["SPY"].Quantity));

 

0


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed