Exemplo n.º 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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// returns absolute return of all positions
        /// both closed and open pl may be included
        /// </summary>
        /// <param name="pdict"></param>
        /// <param name="marketprices"> used to calculate open pl </param>
        /// <param name="countClosedPL"> is closed pl included or not </param>
        /// <returns></returns>
        public static decimal[] AbsoluteReturn(Dictionary <string, Position> pdict, Dictionary <string, decimal> marketprices, bool countClosedPL)
        {
            decimal[] aret        = new decimal[pdict.Count];
            bool      countOpenPL = marketprices.Count >= pdict.Count;
            int       i           = 0;

            foreach (KeyValuePair <string, Position> item in pdict)  // for (int i = 0; i < pdict.Count; i++)
            {
                // get position
                Position p = item.Value;

                if (countOpenPL && marketprices.ContainsKey(item.Key))
                {
                    aret[i] += Calc.OpenPL(marketprices[item.Key], p);
                }
                if (countClosedPL)
                {
                    aret[i] += p.ClosedPL;
                }
                i++;
            }
            return(aret);
        }