// 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 (this.hasSymbol && (this.Symbol != pos.Symbol)) { throw new Exception("Invalid Position: Position MUST have a symbol."); } if (!pos.isValid) { throw new Exception("Invalid position adjustment, existing:" + this.ToString() + " adjustment:" + pos.ToString()); } if (pos.Flat) { return(0); // nothing to do } decimal pl = BoxMath.ClosePL(this, pos.ToTrade()); if (this.Flat) { this.price = pos.price; // if we're leaving flat just copy price } else if ((pos.Side && this.Side) || (!pos.Side && !this.Side)) // sides match, adding so adjust price { this.price = ((this.price * this.size) + (pos.price * pos.size)) / (pos.size + this.size); } this.size += pos.size; // adjust the size if (this.Flat) { price = 0; // if we're flat after adjusting, size price back to zero } return(pl); }
/// <summary> /// Gets the closed points (points = PL on per-share basis) for given symbol/account. /// </summary> /// <param name="symbol">The symbol.</param> /// <param name="account">The account.</param> /// <returns>points</returns> public decimal GetClosedPT(string symbol, Account account) { Position pos = new Position(symbol); decimal points = 0; if (!MasterTrades.ContainsKey(account.ID)) { return(points); } foreach (Trade t in MasterTrades[account.ID]) { points += BoxMath.ClosePT(pos, t); pos.Adjust(t); } return(points); }