Example #1
0
        // returns any closed PL calculated on position basis (not per share)
        /// <summary>
        /// Adjusts the position by applying a new position.
        /// </summary>
        /// <param name="pos">The position adjustment to apply.</param>
        /// <returns></returns>
        public decimal Adjust(Position pos)
        {
            if ((_fullsymbol != "") && (this._fullsymbol != pos._fullsymbol))
            {
                throw new Exception("Failed because adjustment symbol did not match position symbol");
            }
            if (_acct == DefaultSettings.DefaultAccount)
            {
                _acct = pos.Account;
            }
            if (_acct != pos.Account)
            {
                throw new Exception("Failed because adjustment account did not match position account.");
            }
            if ((_fullsymbol == "") && pos.isValid)
            {
                _fullsymbol = pos._fullsymbol;
            }
            if (!pos.isValid)
            {
                throw new Exception("Invalid position adjustment, existing:" + this.ToString() + " adjustment:" + pos.ToString());
            }

            decimal pl = 0;

            if (!pos.isFlat)
            {
                bool oldside = isLong;
                pl = Calc.ClosePL(this, pos.ToTrade());
                if (this.isFlat)
                {
                    this._avgprice = pos.AvgPrice;                                     // if we're leaving flat just copy price
                }
                else if ((pos.isLong && this.isLong) || (!pos.isLong && !this.isLong)) // sides match, adding so adjust price
                {
                    this._avgprice = ((this._avgprice * this._size) + (pos.AvgPrice * pos.Size)) / (pos.Size + this.Size);
                }
                this._size += pos.Size; // adjust the size
                if (oldside != isLong)
                {
                    _avgprice = pos.AvgPrice;                    // this is for when broker allows flipping sides in one trade
                }
                if (this.isFlat)
                {
                    _avgprice = 0; // if we're flat after adjusting, size price back to zero
                }
                _closedpl += pl;   // update running closed pl

                return(pl);
            }

            _openpl = Calc.OpenPL(pos.AvgPrice, this);

            return(pl);
        }
Example #2
0
        // returns any closed PL calculated on position basis (not per share)
        /// <summary>
        /// Adjusts the position by applying a new position.
        /// </summary>
        /// <param name="pos">The position adjustment to apply.</param>
        /// <returns></returns>
        public decimal Adjust(Position pos)
        {
            if ((_fullsymbol!="") && (this._fullsymbol != pos._fullsymbol)) throw new Exception("Failed because adjustment symbol did not match position symbol");
            if (_acct == DefaultSettings.DefaultAccount) _acct = pos.Account;
            if (_acct != pos.Account) throw new Exception("Failed because adjustment account did not match position account.");
            if ((_fullsymbol=="") && pos.isValid) _fullsymbol = pos._fullsymbol;
            if (!pos.isValid) throw new Exception("Invalid position adjustment, existing:" + this.ToString() + " adjustment:" + pos.ToString());

            decimal pl = 0;
            if (! pos.isFlat)
            {
                bool oldside = isLong;
                pl = Calc.ClosePL(this, pos.ToTrade());
                if (this.isFlat) this._avgprice = pos.AvgPrice; // if we're leaving flat just copy price
                else if ((pos.isLong && this.isLong) || (!pos.isLong && !this.isLong)) // sides match, adding so adjust price
                    this._avgprice = ((this._avgprice * this._size) + (pos.AvgPrice * pos.Size)) / (pos.Size + this.Size);
                this._size += pos.Size; // adjust the size
                if (oldside != isLong) _avgprice = pos.AvgPrice; // this is for when broker allows flipping sides in one trade
                if (this.isFlat) _avgprice = 0; // if we're flat after adjusting, size price back to zero
                _closedpl += pl; // update running closed pl

                return pl;
            }

            _openpl = Calc.OpenPL(pos.AvgPrice, this);

            return pl;
        }