public void OrderFilled(decimal filledAmount, decimal filledPrice, DateTime dateFilled, decimal feePercent = 0) { if (this._roundtripStatusId == RoundtripStatus.Exit.Id || this._roundtripStatusId == RoundtripStatus.Entry.Id) { throw new InvestingDomainException("A roundtrip's order should not filled before submitting order."); } if (this._roundtripStatusId == RoundtripStatus.EntryOrderSubmitted.Id) { var fee = Math.Round(filledAmount * feePercent, 8); var buyAmount = filledAmount - fee; this.Transaction = this.Transaction.BuyOrderFilled(buyAmount, filledPrice); this.EntryAt = dateFilled; this._roundtripStatusId = RoundtripStatus.Entry.Id; this.AddDomainEvent(new RoundtripEntryDomainEvent( this.InvestmentId, this.RoundtripId, RoundtripStatus.From(this._roundtripStatusId), this.Market, this.EntryAt ?? throw new InvestingDomainException("Param \"Entry At\" missing."), this.Transaction )); } else if (this._roundtripStatusId == RoundtripStatus.ExitOrderSubmitted.Id) { this.Transaction = this.Transaction.SellOrderFilled(filledAmount, filledPrice); this.ExitAt = dateFilled; var originalBalance = this.EntryBalance - (Math.Round((decimal)this.Transaction.BuyAmount / (1 - feePercent), 8) * this.Transaction.BuyPrice); var fee = Math.Round(filledAmount * filledPrice * feePercent, 8); this.ExitBalance = originalBalance + (filledPrice * filledAmount) - fee; this._roundtripStatusId = RoundtripStatus.Exit.Id; this.AddDomainEvent(new RoundtripExitDomainEvent(this)); } else { throw new InvestingDomainException("Order filled in wrong state at roundtrip."); } }
public void ForceSelling() { if (this._roundtripStatusId == RoundtripStatus.Exit.Id) { throw new InvestingDomainException("Roundtrip already exit, nothing could be selled."); } if (this._roundtripStatusId == RoundtripStatus.ForceExit.Id || this._roundtripStatusId == RoundtripStatus.ForceSell.Id) { throw new InvestingDomainException("Force selling in wrong state ata roundtrip."); } this.AddDomainEvent(new RoundtripForcedSellingDomainEvent( this.RoundtripId, RoundtripStatus.From(this._roundtripStatusId) )); this._roundtripStatusId = RoundtripStatus.ForceSell.Id; }
public Roundtrip(string investmentId, int roundtripNumber, Market market, decimal entryBalance, decimal entryPrice, decimal targetPrice, decimal stopLossPrice, DateTime adviceCreationDate) : this() { this.RoundtripId = Guid.NewGuid().ToString(); this.InvestmentId = investmentId ?? throw new ArgumentNullException(nameof(investmentId)); this.RoundtripNumber = roundtripNumber >= 0 ? roundtripNumber : throw new ArgumentOutOfRangeException(nameof(roundtripNumber)); this._roundtripStatusId = RoundtripStatus.EntryOrderSubmitted.Id; this.Market = market ?? throw new ArgumentNullException(nameof(market)); this.EntryBalance = entryBalance > 0 ? entryBalance : throw new ArgumentNullException(nameof(entryBalance)); this.TargetPrice = targetPrice >= 0 ? targetPrice : throw new ArgumentNullException(nameof(targetPrice)); this.StopLossPrice = stopLossPrice >= 0 ? stopLossPrice : throw new ArgumentNullException(nameof(stopLossPrice)); this.Transaction = new Transaction(); this.AddDomainEvent(new RoundtripEntryOrderSubmittedDomainEvent( this.InvestmentId, this.RoundtripId, RoundtripStatus.From(this._roundtripStatusId), this.Market, this.EntryBalance, entryPrice, adviceCreationDate )); }
public void Exit(decimal exitPrice, DateTime adviceCreationDate) { if (this._roundtripStatusId != RoundtripStatus.Entry.Id) { throw new InvestingDomainException("A roundtrip can only exit in Entry status."); } //Only sell at stop loss price, let the market decide. if (exitPrice > this.StopLossPrice) { return; } this._roundtripStatusId = RoundtripStatus.ExitOrderSubmitted.Id; this.AddDomainEvent(new RoundtripExitOrderSubmittedDomainEvent( this, this.InvestmentId, this.RoundtripId, RoundtripStatus.From(this._roundtripStatusId), this.Market, this.Transaction.BuyAmount ?? throw new InvestingDomainException("Transaction buy amount missing."), exitPrice, adviceCreationDate )); }
public RoundtripStatus GetStatus() { return(RoundtripStatus.From(this._roundtripStatusId)); }