Hi there folks!
I have a strategy class, the signals given by the strategy depend on the actual stock position (long sell, or not invested). The actual stock position is an encapsulated field in the strategy class. So, I made an implementation in the OnOrderEvent method that changes the actual stock position in the strategy if the order is filled.
public override void OnOrderEvent(OrderEvent orderEvent)
{
OrderTicket actualOrderTicket = Transactions.GetOrderTickets(t => t.OrderId == orderEvent.OrderId).Last();
StockState newStockState = (actualOrderTicket.Quantity > 0) ? StockState.longPosition : StockState.shortPosition;
switch (actualOrderTicket.Status)
{
case OrderStatus.Filled:
Strategy.Position = newStockState;
break;
default:
break;
}
}
But I have another method in the main algorithm that relies in the stock position to make decisions. So, as far I can understand I have to make locks to avoid problems; isn’t?
I saw how you lock some of the fields in the OrderTicket class and after some search I made this encapsulation in the Strategy class:
public enum StockState
{
shortPosition, // The Portfolio has short position in this bar.
longPosition, // The Portfolio has short position in this bar.
noInvested, // The Portfolio hasn't any position in this bar.
};
public class SomeRandomStrategy
{
#region Fields
private readonly object _positionLock = new object();
private StockState _position = StockState.noInvested;
public StockState Position
{
get
{
lock(_positionLock)
{
return _position;
}
}
set
{
lock(_positionLock)
{
_position = value;
}
}
}
#endregion
#region Constructors
public SomeRandomStrategy()
{
}
#endregion
}
Again, the locking stuff was copied without fully understanding it. And in some places I saw that as right and in others as incorrect. So, I’d like to know if by chance, the code will work. And obviously, any hint will be much appreciated.
Thanks in advance, JJ
P.S.: in the OnOrderEvent implementation I assumed that the OrderEvent.OrderID refers to a unique OrderTicket. Am I right?
Jared Broad
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.
JayJayD
OrderTicket actualOrderTicket = Transactions.GetOrderTickets(t => t.OrderId == orderEvent.OrderId).Last();
The reasoning is that the predicate will return only one OrderTicket (of the subyacent hidden order), so I use the Last() method to retrieve it directly. Without the Last() method, the procedure returns an IEnumerable object.var actualOrderTicket = Transactions.GetOrderTickets(t => t.OrderId == orderEvent.OrderId);
The question is: Is the OrderID unique? Or; Can two different order (different time / symbol / type) have the same ID? Or; Can that predicate return more than one OrderTicket? If the OrderID isn’t unique, I should do a second filter to work with the correct OrderTicket. Best, JJJared Broad
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.
JayJayD
Michael Handschuh
JayJayD
Michael Handschuh
JayJayD
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!