Back

How to loop through my open positions?

Good afternoon,

My trading system places limit orders after market is closed. After the close next day I want to check which positions were executed. How do I loop through the currently open positions?

Entry is based on a ranking from a universe. The system does not liquidate stocks on _changes.RemovedSecurities. The system holds the position until stop loss or profit target is hit. I would like to loop through each stock to set stop and profit target price dependent on the fill price. Any suggestions? Many thanks!

Update Backtest








In Lean/QuantConnect, we differ Order from OrderTicket. While Order is a struct for placing a new trade, an OrderTicket is a single reference to an order for the algorithm to maintain. As the order gets updated this ticket will also get updated. Here we find the fill price, fill quantity, status, etc.

If we want the orders that got filled (open positions), we can use the Transactions object:

// All filled orders
var tickets = Transactions
.GetOrderTickets(x => x.Status == OrderStatus.Filled);

Then we can loop through them to get the information we need (symbol, fill price and quantity) to issue a new order:

foreach (var ticket in tickets)
{
var symbol = ticket.Symbol;
var fillQuantity = ticket.QuantityFilled;
var fillPrice = ticket.AverageFillPrice;
}

 

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.


Let me add a suggestion: instead of looping throught the position, we can use the OnOrderEvent event handler:

public override void OnOrderEvent(OrderEvent orderEvent)
{
if (orderEvent.Status.IsFill())
{
var symbol = orderEvent.Symbol;
var fillQuantity = orderEvent.FillQuantity;
var fillPrice = orderEvent.FillPrice;
}
}

and we can add a new stop and profit target as soon as the order is filled.
 

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.


Many Thanks for your quick reply Alexandre!

Just to double check:

The looping example runs in on OnData() method, so with daily resolution, the stop and profit target orders would be placed after the day's close?

The second example OnOrderEvent can place the stop and profit target orders as soon as the order is filled (intraday)?

0

The daily bar only arrives after market close, so the new orders will be placed on market open. If there is a gap, these orders may never get filled.
When we subscribe to daily resolution data in backtest, "as soon as possible" does not mean intraday, since there is no data flowing to fill the order in a lower resolution.

 

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.


Excellent! Many thanks :-)
0

Alexandre Catarino, do you have any design notes that describe dataflow ? 

 

Such as to place a trade, module X receives trade Order which then calls method Y that is responsible for submitting trade?

 

I found a PDF , however , it doesn't get to this level of depth & breadth of detail.

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