Beispiel #1
0
 internal BfxPosition(BfxPositions positions, BfxPositionsElement pos, BfChildOrderEvent?ev = default)
 {
     Positions           = positions;
     Open                = pos.Open;
     Close               = ev?.EventDate;
     Side                = pos.OpenSize > 0m ? BfTradeSide.Buy : BfTradeSide.Sell;
     OpenPrice           = pos.Price;
     ClosePrice          = ev?.Price;
     Size                = Math.Abs(pos.CurrentSize);
     Commission          = pos.Commission;
     SwapForDifference   = pos.SwapForDifference;
     SwapPointAccumulate = pos.SwapPointAccumulate;
 }
Beispiel #2
0
        internal BfxPositionsElement Split(decimal splitSize)
        {
            var newPos = new BfxPositionsElement
            {
                Open        = this.Open,
                Price       = this.Price,
                OpenSize    = this.OpenSize,
                CurrentSize = -splitSize,
                _commission = this._commission,
                _sfd        = this._sfd,
            };

            CurrentSize += splitSize;
            return(newPos);
        }
Beispiel #3
0
        public BfxPosition[] Update(BfChildOrderEvent ev)
        {
            var executedSize = ev.Side == BfTradeSide.Buy ? ev.Size : -ev.Size;

            if (_q.Count == 0 || Math.Sign(_q.Peek().OpenSize) == Math.Sign(executedSize))
            {
                var pos = new BfxPositionsElement(ev);
                _q.Enqueue(pos);
                return(new BfxPosition[] { new BfxPosition(this, pos) });
            }

            // Process to another side
            var closeSize = executedSize;
            var closedPos = new List <BfxPositionsElement>();

            while (Math.Abs(closeSize) > 0m && _q.Count > 0)
            {
                var pos = _q.Peek();
                if (Math.Abs(closeSize) >= Math.Abs(pos.CurrentSize))
                {
                    closeSize += pos.CurrentSize;
                    closedPos.Add(_q.Dequeue());
                    continue;
                }
                if (Math.Abs(closeSize) < Math.Abs(pos.CurrentSize))
                {
                    closedPos.Add(pos.Split(closeSize));
                    closeSize = 0;
                    break;
                }
            }
            var result = new List <BfxPosition>();

            closedPos.ForEach(e => result.Add(new BfxPosition(this, e, ev)));

            if (closeSize > 0m)
            {
                var pos = new BfxPositionsElement(ev, Math.Abs(closeSize));
                _q.Enqueue(pos);
                result.Add(new BfxPosition(this, pos));
            }

            return(result.ToArray());
        }