internal TradeDayCalculator(TradeDayInfo data, bool isReset)
 {
     _info              = data;
     _isReset           = isReset;
     _orderPhaseUpdater = new OrderPhaseUpdater(data.CloseOrders, _info.Settings.ResetTime, _info.Instrument.Id, _info.TradeDay);
     this.Balance       = 0m;
     _exchanger         = new Exchanger(data.RateSetting);
 }
Exemple #2
0
 private decimal CalculateValuedPL(decimal value, OrderRelation orderRelation, Exchanger exchanger)
 {
     return(exchanger.Exchange(value, orderRelation.RateIn, orderRelation.RateOut, orderRelation.Decimals));
 }
Exemple #3
0
        internal decimal CalculateValuedCloseOrderPL(List <OrderRelation> valuedOrderRelations, Exchanger exchanger)
        {
            decimal result = 0m;

            foreach (var eachOrderRelation in valuedOrderRelations)
            {
                decimal interestPL = this.CalculateValuedPL(eachOrderRelation.InterestPL, eachOrderRelation, exchanger);
                decimal storagePL  = this.CalculateValuedPL(eachOrderRelation.StoragePL, eachOrderRelation, exchanger);
                decimal tradePL    = this.CalculateValuedPL(eachOrderRelation.TradePL, eachOrderRelation, exchanger);
                result += (interestPL + storagePL + tradePL);
            }
            return(result);
        }
        private static decimal CalculatePhysicalTradePL(PhysicalOrderRelation orderRelation, Exchanger exchanger)
        {
            decimal result = 0m;

            if (orderRelation.PhysicalValueMatureDay == null || orderRelation.RealPhysicalValueMatureDate == orderRelation.PhysicalValueMatureDay)
            {
                result = exchanger.ExchangeByCommonDecimals(orderRelation.TradePL);
            }
            return(result);
        }
 private static void CalculateNotValuedOrderRelationPL(TradeDayInfo tradeDayInfo, Exchanger exchanger, Dictionary <Guid, OrderResetResult> resetOrderDict, List <Guid> affectedOrders)
 {
     foreach (var eachOrderRelation in tradeDayInfo.OrderRelations)
     {
         Order closeOrder = eachOrderRelation.CloseOrder;
         if (!closeOrder.ShouldCalculate(affectedOrders))
         {
             continue;
         }
         PhysicalOrder         physicalCloseOrder    = closeOrder as PhysicalOrder;
         PhysicalOrderRelation physicalOrderRelation = eachOrderRelation as PhysicalOrderRelation;
         if (closeOrder.Phase == OrderPhase.Executed && !closeOrder.IsOpen && closeOrder.ExecuteTime <= tradeDayInfo.Settings.ResetTime && eachOrderRelation.ValueTime == null)
         {
             OrderResetResult resetResult = CreateOrderResetResult(closeOrder, tradeDayInfo, resetOrderDict);
             resetResult.NotValuedPL      += new InterestStorage(exchanger.ExchangeByCommonDecimals(eachOrderRelation.InterestPL), exchanger.ExchangeByCommonDecimals(eachOrderRelation.StoragePL));
             resetResult.TradePLNotValued += exchanger.ExchangeByCommonDecimals(eachOrderRelation.TradePL);
             if (physicalOrderRelation != null)
             {
                 resetResult.PhysicalTradePLNotValued += CalculatePhysicalTradePL(physicalOrderRelation, exchanger);
             }
         }
     }
 }
 private static void CalculateValuedOrderRelationPL(TradeDayInfo tradeDayInfo, Exchanger exchanger, Dictionary <Guid, OrderResetResult> resetOrderDict, List <Guid> affectedOrders)
 {
     foreach (var eachOrderRelation in tradeDayInfo.OrderRelations)
     {
         Order closeOrder = eachOrderRelation.CloseOrder;
         if (!closeOrder.ShouldCalculate(affectedOrders))
         {
             continue;
         }
         if (closeOrder.Phase == OrderPhase.Executed && !closeOrder.IsOpen && closeOrder.ExecuteTime <= tradeDayInfo.Settings.ResetTime && eachOrderRelation.ValueTime > tradeDayInfo.Settings.BeginTime && eachOrderRelation.ValueTime <= tradeDayInfo.Settings.ResetTime)
         {
             var resetResult = CreateOrderResetResult(closeOrder, tradeDayInfo, resetOrderDict);
             var valuedPL    = eachOrderRelation.CalculateValuedPL();
             resetResult.ValuedPL      += new InterestStorage(valuedPL.InterestPL, valuedPL.StoragePL);
             resetResult.TradePLValued += valuedPL.TradePL;
             PhysicalOrderRelation physicalOrderRelation = eachOrderRelation as PhysicalOrderRelation;
             if (physicalOrderRelation != null)
             {
                 resetResult.PhysicalTradePLValued += CalculatePhysicalTradePL(physicalOrderRelation, exchanger);
             }
         }
     }
 }
 internal static void Calculate(TradeDayInfo tradeDayInfo, Exchanger exchanger, Dictionary <Guid, OrderResetResult> resetOrderDict, List <Guid> affectedOrders)
 {
     CalculateNotValuedOrderRelationPL(tradeDayInfo, exchanger, resetOrderDict, affectedOrders);
     CalculateValuedOrderRelationPL(tradeDayInfo, exchanger, resetOrderDict, affectedOrders);
 }
        private void CalculatePL(OrderDayHistory orderDayHistory, Dictionary <Guid, InterestStorage> dict, Exchanger exchanger)
        {
            var             interestPL = exchanger.ExchangeByCommonDecimals(orderDayHistory.DayInterestPLNotValued);
            var             storagePL  = exchanger.ExchangeByCommonDecimals(orderDayHistory.DayStoragePLNotValued);
            InterestStorage interestStoragePL;

            if (!dict.TryGetValue(orderDayHistory.OrderID, out interestStoragePL))
            {
                dict.Add(orderDayHistory.OrderID, new InterestStorage(interestPL, storagePL));
            }
            else
            {
                dict[orderDayHistory.OrderID] = new InterestStorage(interestStoragePL.Interest + interestPL, interestStoragePL.Storage + storagePL);
            }
        }
        internal Dictionary <Guid, InterestStorage> Calculate(Guid accountId, Guid instrumentId, IEnumerable <Order> allExecuteOrders, DateTime tradeDay, Exchanger exchanger, List <Guid> affectedOrders)
        {
            if (!this.ShouldCalculate(instrumentId, tradeDay))
            {
                return(null);
            }
            Dictionary <Guid, InterestStorage> result = new Dictionary <Guid, InterestStorage>();
            var instrumentOpenCloseHistorys           = this.GetInstrumentDayOpenCloseHistorys(instrumentId, tradeDay);
            var orderKeys     = GetOrderKeys(allExecuteOrders, instrumentOpenCloseHistorys);
            var historyOrders = ResetManager.Default.GetOrderDayHistorys(orderKeys);

            if (historyOrders == null || historyOrders.Count == 0)
            {
                return(result);
            }
            foreach (var eachOrder in allExecuteOrders)
            {
                if (!eachOrder.IsOpen || !eachOrder.ShouldCalculate(affectedOrders))
                {
                    continue;
                }
                foreach (var eachInstrument in instrumentOpenCloseHistorys)
                {
                    foreach (var eachOrderDayHistory in historyOrders)
                    {
                        if (this.ShouldAddOrderDayHistoryToPL(eachOrder, eachOrderDayHistory, eachInstrument, tradeDay))
                        {
                            CalculatePL(eachOrderDayHistory, result, exchanger);
                        }
                    }
                }
            }
            return(result);
        }
        internal static Dictionary <Guid, OpenOrderPLOfCurrentDay> Calculate(TradeDayInfo tradeDayInfo, Dictionary <Guid, TradeDayCommonResult> resetCommonResultDict, Exchanger exchanger)
        {
            if (tradeDayInfo.Settings.ValueDate == null && !tradeDayInfo.Settings.UseCompatibleMode)
            {
                return(null);
            }
            Dictionary <Guid, OpenOrderPLOfCurrentDay> result = new Dictionary <Guid, OpenOrderPLOfCurrentDay>();

            foreach (var eachPair in resetCommonResultDict)
            {
                var orderId           = eachPair.Key;
                var commonResetResult = eachPair.Value;
                if (!orderId.ShouldCalculate(tradeDayInfo.AffectedOrders))
                {
                    continue;
                }
                var eachOrder = tradeDayInfo.GetOrder(orderId);
                if (!IsOpenOrderOfCurrentDay(eachOrder, tradeDayInfo))
                {
                    continue;
                }
                OpenOrderPLOfCurrentDay openOrderPLOfCurrentDay = new OpenOrderPLOfCurrentDay();
                int decimals = tradeDayInfo.Settings.IsInterestUseAccountCurrency ? tradeDayInfo.RateSetting.RoundDecimals.Max : tradeDayInfo.RateSetting.RoundDecimals.Instrument;
                var storage  = Math.Round(eachOrder.LotBalance * commonResetResult.StoragePerLot, decimals, MidpointRounding.AwayFromZero);
                var interest = Math.Round(eachOrder.LotBalance * commonResetResult.InterestPerLot, decimals, MidpointRounding.AwayFromZero);
                openOrderPLOfCurrentDay.DayNotValued += tradeDayInfo.Settings.ShouldValueCurrentDayPL ? InterestStorage.Empty : new InterestStorage(interest, storage);

                var exchangeStorage  = exchanger.ExchangeByCommonDecimals(storage);
                var exchangeInterest = exchanger.ExchangeByCommonDecimals(interest);
                openOrderPLOfCurrentDay.NotValued += tradeDayInfo.Settings.ShouldValueCurrentDayPL ? InterestStorage.Empty : new InterestStorage(exchangeInterest, exchangeStorage);

                bool    isInterestValued = (eachOrder.InterestValueDate ?? tradeDayInfo.TradeDay) <= tradeDayInfo.TradeDay;
                decimal interestPLValued = tradeDayInfo.Settings.ShouldValueCurrentDayPL && isInterestValued ? exchangeInterest : 0;
                decimal storagePLValued  = tradeDayInfo.Settings.ShouldValueCurrentDayPL ? exchangeStorage : 0;
                openOrderPLOfCurrentDay.Valued += new InterestStorage(interestPLValued, storagePLValued);

                openOrderPLOfCurrentDay.IsInterestValued = isInterestValued;
                openOrderPLOfCurrentDay.IsStorageValued  = true;
                result.Add(orderId, openOrderPLOfCurrentDay);
            }
            return(result);
        }