Exemplo n.º 1
0
        public void GenerateReplicaLiquidationChanges()
        {
            //first remove Changes that may not have been processed as we do not want them on
            Changes.Clear();

            //then clear out any remaining position,
            //if there was an opening in the changes it does not matter, we close on realQuantity
            //if there was a close, it will be done anyway!

            if (Replicated)
            {
                PositionChanges change = new PositionChanges
                {
                    PositionId = PositionId,
                    Type       = PositionChanges.ChangeType.Close,
                    //Symbol = Symbol,
                    QuantityBefore = Quantity,
                    QuantityAfter  = 0,
                };
                Changes.Add(change);
                //RealQuantity = 0;
                ReplicationWeight = 0;
                //RealQuantity = 0;
            }
            Replicated = false;
        }
Exemplo n.º 2
0
        internal decimal AddTrade(decimal qty, decimal tradePrice)
        {
            qty = Math.Round(qty, 4);
            if (qty == 0)
            {
                return(0);
            }
            if (Quantity == 0)
            {
                PositionChanges change = new PositionChanges
                {
                    PositionId = PositionId,
                    Type       = PositionChanges.ChangeType.Open,
                    //Symbol = Symbol,
                    QuantityBefore = 0,
                    QuantityAfter  = qty,
                };
                Changes.Add(change);
                Quantity    = qty;
                averageCost = tradePrice;
            }
            else
            {
                var sameDirection   = Math.Sign(Quantity / qty) == 1;
                var replicationIsOn = (RealQuantity != 0);
                if (sameDirection)
                {
                    PositionChanges change = new PositionChanges
                    {
                        PositionId = PositionId,
                        Type       = (replicationIsOn) ? PositionChanges.ChangeType.Increase : PositionChanges.ChangeType.Open,
                        //Symbol = Symbol,
                        QuantityBefore = (replicationIsOn) ? Quantity : 0,
                        QuantityAfter  = Math.Round(Quantity + qty, 4),
                    };
                    Changes.Add(change);

                    averageCost = (averageCost * Quantity + qty * tradePrice) / (Quantity + qty);
                    Quantity   += qty;
                }
                else
                {
                    if (Math.Abs(qty) > Math.Abs(Quantity)) // changing side
                    {
                        var remainder = qty + Quantity;
                        AddTrade(-Quantity, tradePrice);
                        AddTrade(remainder, tradePrice);
                    }
                    else // closing or partially closing
                    {
                        //averagecost does not change
                        // TODO track cash proceeds per position?
                        if (Quantity + qty == 0)
                        {
                            PositionChanges change = new PositionChanges
                            {
                                PositionId = PositionId,
                                Type       = PositionChanges.ChangeType.Close,
                                //Symbol = Symbol,
                                QuantityBefore = Quantity,
                                QuantityAfter  = 0,
                            };
                            Changes.Add(change);
                        }
                        else
                        {
                            PositionChanges change = new PositionChanges
                            {
                                PositionId = PositionId,
                                Type       = (replicationIsOn) ? PositionChanges.ChangeType.Decrease : PositionChanges.ChangeType.Open,

                                //Symbol = Symbol,
                                QuantityBefore = (replicationIsOn) ? Quantity : 0,
                                QuantityAfter  = Math.Round(Quantity + qty, 4),
                            };
                            Changes.Add(change);
                        }

                        Quantity += qty;
                    }
                }
            }

            lastPrice = tradePrice;

            // TODO: create CashImpact method
            return(qty * tradePrice);
        }