コード例 #1
0
        private OrderTransaction CreateTrade(OrderTransaction buytrans, OrderTransaction selltrans)
        {
            var l = new OrderTransaction();

            if (buytrans == null && selltrans == null)
            {
                return(l);
            }

            if (buytrans.Amount >= 0)
            {
                throw new ArgumentException("Buy trans amount >= 0");
            }
            if (selltrans.Amount <= 0)
            {
                throw new ArgumentException("Sell trans amount <= 0");
            }
            if (buytrans.Quantity <= 0)
            {
                throw new ArgumentException("Buy quantity <= 0");
            }
            if (selltrans.Quantity >= 0)
            {
                throw new ArgumentException("Sell quantity >= 0");
            }
            MatchedTrade trade = new MatchedTrade
            {
                Id     = ++tradeId,
                Symbol = buytrans.Symbol,
                DescriptionOfProperty = string.Format("{0} {1}", buytrans.Quantity, buytrans.Symbol),
                DateAcquired          = buytrans.TradeDate,
                DateSoldOrDisposed    = selltrans.TradeDate,
                AdjustmentAmount      = 0,
                ReportedToIrs         = true,
                ReportedToMe          = true,
                Brokerage             = selltrans.Broker,
                BuyOrderId            = buytrans.OrderId,
                SellOrderId           = selltrans.OrderId
            };


            // Buy quantities are positive, sell quantities are negative
            // Buy Amount is negative, sell Amount is positive.
            // commission and fees are always negative
            if (Math.Abs(buytrans.Quantity) == Math.Abs(selltrans.Quantity))
            {
                trade.Quantity    = buytrans.Quantity;
                trade.Proceeds    = Math.Abs(selltrans.Net);
                trade.CostOrBasis = Math.Abs(buytrans.Net);

                //Long Term Short Term
                TimeSpan diff = trade.DateSoldOrDisposed.Subtract(trade.DateAcquired);
                if (diff.TotalDays > 365)
                {
                    trade.LongTermGain = true;
                }

                //if (trade.DateSoldOrDisposed.Year == 2014)
                TotalCommission    += buytrans.Commission;
                TotalCommission    += selltrans.Commission;
                LastTradeCommission = buytrans.Commission + selltrans.Commission;
                TotalProfit        += trade.GainOrLoss;
                //if (Math.Abs(trade.GainOrLoss) > 1000)
                //    throw new Exception("Invalid gain or loss");
                trade.CumulativeProfit = TotalProfit;
                Trades.Add(trade);
            }
            return(l);
        }
コード例 #2
0
        private OrderTransaction CreateTrade(OrderTransaction buytrans, OrderTransaction selltrans)
        {
            var l = new OrderTransaction();
            if (buytrans == null && selltrans == null)
                return l;

            if (buytrans.Amount >= 0)
                throw new ArgumentException("Buy trans amount >= 0");
            if (selltrans.Amount <= 0)
                throw new ArgumentException("Sell trans amount <= 0");
            if (buytrans.Quantity <= 0)
                throw new ArgumentException("Buy quantity <= 0");
            if (selltrans.Quantity >= 0)
                throw new ArgumentException("Sell quantity >= 0");
            MatchedTrade trade = new MatchedTrade
            {
                Id = ++tradeId,
                Symbol = buytrans.Symbol,
                DescriptionOfProperty = string.Format("{0} {1}", buytrans.Quantity, buytrans.Symbol),
                DateAcquired = buytrans.TradeDate,
                DateSoldOrDisposed = selltrans.TradeDate,
                AdjustmentAmount = 0,
                ReportedToIrs = true,
                ReportedToMe = true,
                Brokerage = selltrans.Broker,
                BuyOrderId = buytrans.OrderId,
                SellOrderId = selltrans.OrderId
            };

            // Buy quantities are positive, sell quantities are negative
            // Buy Amount is negative, sell Amount is positive.
            // commission and fees are always negative
            if (Math.Abs(buytrans.Quantity) == Math.Abs(selltrans.Quantity))
            {
                trade.Quantity = buytrans.Quantity;
                trade.Proceeds = Math.Abs(selltrans.Net);
                trade.CostOrBasis = Math.Abs(buytrans.Net);

                //Long Term Short Term
                TimeSpan diff = trade.DateSoldOrDisposed.Subtract(trade.DateAcquired);
                if (diff.TotalDays > 365)
                    trade.LongTermGain = true;

                //if (trade.DateSoldOrDisposed.Year == 2014)
                TotalCommission += buytrans.Commission;
                TotalCommission += selltrans.Commission;
                LastTradeCommission = buytrans.Commission + selltrans.Commission;
                TotalProfit += trade.GainOrLoss;
                //if (Math.Abs(trade.GainOrLoss) > 1000)
                //    throw new Exception("Invalid gain or loss");
                trade.CumulativeProfit = TotalProfit;
                Trades.Add(trade);

            }
            return l;
        }