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?