Esempio n. 1
0
        public void SaveOptionTrade(TradeDto dto, DateTime date)
        {
            Trade trade = new Trade();

            if (dto.IsNew)
            {
                trade = dto.MapTo <Trade>();
            }
            else
            {
                trade = this._repository.Get(dto.Id);
                bool exitReasonChanged = dto.ExitReason != trade.ExitReason && dto.ExitReason != TradeExitReasons.None;
                dto.MapTo(trade);

                if (exitReasonChanged)
                {
                    trade.ExitDate = date;
                }
            }

            trade.Reconcile();

            if (dto.IsNew)
            {
                this._repository.Insert(trade);
            }
        }
        public bool Save(TradeDto dto)
        {
            bool  reconcileTradingAccount = false;
            Trade trade = new Trade();

            if (dto.IsNew)
            {
                trade = dto.MapTo <Trade>();
                trade.TradingAccountId = this._tradingAccountAppService.GetActive().Id;
                trade.TradingDayId     = this._tradingDayAppService.Get(trade.EntryDate).Id;

                MarketLogEntry tradeEnterLogEntry = new MarketLogEntry();
                tradeEnterLogEntry.MarketId           = trade.MarketId;
                tradeEnterLogEntry.MarketLogEntryType = MarketLogEntryTypes.TradeEnter;
                if (trade.EntryScreenshotDbId.HasValue)
                {
                    tradeEnterLogEntry.ScreenshotDbId = trade.EntryScreenshotDbId;
                }
                tradeEnterLogEntry.Text             = String.Format("{0} {1} @ {2:C}<br/>{3}", trade.TradeType == TradeTypes.Long ? "Buy" : "Sell", trade.Size, trade.EntryPrice, trade.EntryRemarks);
                tradeEnterLogEntry.TimeStamp        = trade.EntryDate;
                tradeEnterLogEntry.TradingAccountId = trade.TradingAccountId;
                tradeEnterLogEntry.TradingDayId     = trade.TradingDayId;

                this._marketLogEntryRepository.Insert(tradeEnterLogEntry);

                if (trade.ExitReason != TradeExitReasons.None)
                {
                    trade.Market = this._marketRepository.Get(trade.MarketId);
                    trade.Reconcile();
                    reconcileTradingAccount = true;
                }
            }
            else
            {
                trade = this._repository.Get(dto.Id);
                bool exitReasonChanged = dto.ExitReason != trade.ExitReason && dto.ExitReason != TradeExitReasons.None;
                dto.MapTo(trade);

                if (exitReasonChanged)
                {
                    trade.Market = this._marketRepository.Get(trade.MarketId);
                    trade.Reconcile();
                    reconcileTradingAccount = true;
                }
            }

            if (reconcileTradingAccount)
            {
                MarketLogEntry tradeExitLogEntry = new MarketLogEntry();
                tradeExitLogEntry.MarketId           = trade.MarketId;
                tradeExitLogEntry.MarketLogEntryType = MarketLogEntryTypes.TradeExit;
                if (trade.ExitScreenshotDbId.HasValue)
                {
                    tradeExitLogEntry.ScreenshotDbId = trade.ExitScreenshotDbId;
                }
                tradeExitLogEntry.Text             = String.Format("{0}: {1} {2} @ {3:C}, P/L: {4:C}<br/>{5}", trade.ExitReason.GetDisplay(), trade.TradeType == TradeTypes.Long ? "Sell" : "Buy", trade.Size, trade.ExitPrice, trade.ProfitLoss, trade.ExitRemarks);
                tradeExitLogEntry.TimeStamp        = trade.ExitDate.Value;
                tradeExitLogEntry.TradingAccountId = trade.TradingAccountId;
                tradeExitLogEntry.TradingDayId     = trade.TradingDayId;
                this._marketLogEntryRepository.Insert(tradeExitLogEntry);
            }

            if (dto.IsNew)
            {
                this._repository.Insert(trade);
            }

            return(reconcileTradingAccount);
        }