예제 #1
0
 public void AddOrderDayHistory(Order order, OrderResetResult resetResult)
 {
     lock (_mutex)
     {
         var model = this.CreateOrderDayHistory(order, resetResult);
         this.Add(model);
     }
 }
예제 #2
0
        private OrderResetResult CreateResetResult(Order order, Dictionary <Guid, TradeDayCommonResult> commonDict, Dictionary <Guid, OpenOrderPLOfCurrentDay> openOrderPLOfCurrentDayDict, Dictionary <Guid, InterestStorage> notValuedPLDict, Dictionary <Guid, InterestStorage> valuedPLDict)
        {
            OrderResetResult     resetResult  = new OrderResetResult();
            TradeDayCommonResult commonResult = null;

            if (commonDict != null)
            {
                commonDict.TryGetValue(order.Id, out commonResult);
            }

            OpenOrderPLOfCurrentDay openOrderPLOfCurrentDay = null;

            if (openOrderPLOfCurrentDayDict != null)
            {
                openOrderPLOfCurrentDayDict.TryGetValue(order.Id, out openOrderPLOfCurrentDay);
            }

            InterestStorage interestStoragePLNotValued = InterestStorage.Empty;

            if (notValuedPLDict != null)
            {
                notValuedPLDict.TryGetValue(order.Id, out interestStoragePLNotValued);
            }

            InterestStorage interestStoragePLValued = InterestStorage.Empty;

            if (valuedPLDict != null)
            {
                valuedPLDict.TryGetValue(order.Id, out interestStoragePLValued);
            }
            resetResult.TradeDay   = _info.TradeDay;
            resetResult.OrderId    = order.Id;
            resetResult.LotBalance = order.LotBalance;
            resetResult.CurrencyId = _info.RateSetting.CurrencyId;
            Price livePrice = order.IsBuy ? _info.Settings.SellPrice : _info.Settings.BuyPrice;

            resetResult.Margin = Calculator.MarginCalculator.CalculateMargin((int)_info.Instrument.MarginFormula, order.LotBalance, order.Owner.ContractSize(_info.TradeDay), order.ExecutePrice, livePrice, _info.RateSetting.RateIn, _info.RateSetting.RateOut, _info.RateSetting.RoundDecimals.Common, _info.RateSetting.RoundDecimals.Instrument);

            resetResult.PerLot = commonResult == null ? InterestStorage.Empty : new InterestStorage(commonResult.InterestPerLot, commonResult.StoragePerLot);

            resetResult.FloatPL = commonResult == null ? InterestStorage.Empty : new InterestStorage(_exchanger.ExchangeByCommonWithInstrumentSourceDecimals(order.LotBalance * commonResult.InterestPerLot), _exchanger.ExchangeByCommonWithInstrumentSourceDecimals(order.LotBalance * commonResult.StoragePerLot));

            resetResult.DayNotValuedPL = openOrderPLOfCurrentDay == null ? InterestStorage.Empty : new InterestStorage(openOrderPLOfCurrentDay.DayNotValued);

            resetResult.NotValuedPL = interestStoragePLNotValued + (openOrderPLOfCurrentDay == null ? InterestStorage.Empty : openOrderPLOfCurrentDay.NotValued);

            resetResult.ValuedPL = interestStoragePLValued + (openOrderPLOfCurrentDay == null ? InterestStorage.Empty : openOrderPLOfCurrentDay.Valued);

            if (_info.Instrument.Category == InstrumentCategory.Physical)
            {
                Physical.PhysicalOrder physicalOrder = (Physical.PhysicalOrder)order;
                resetResult.PhysicalPaidAmount         = physicalOrder.PaidAmount;
                resetResult.PaidPledgeBalance          = physicalOrder.PaidPledgeBalance;
                resetResult.PhysicalOriginValueBalance = physicalOrder.PhysicalOriginValueBalance;
                resetResult.InstalmentInterest         = commonResult == null ? 0m : commonResult.InstalmentInterest;
            }
            return(resetResult);
        }
예제 #3
0
        private static void CalculateOrderRelationPLCommon(Order closeOrder, OrderResetResult resetResult, TradeDayInfo tradeDayInfo)
        {
            PhysicalOrder physicalCloseOrder = closeOrder as PhysicalOrder;

            resetResult.TradeDay                   = tradeDayInfo.TradeDay;
            resetResult.CurrencyId                 = tradeDayInfo.RateSetting.CurrencyId;
            resetResult.OrderId                    = closeOrder.Id;
            resetResult.PhysicalPaidAmount         = physicalCloseOrder != null ? physicalCloseOrder.PaidAmount : 0;
            resetResult.PaidPledgeBalance          = physicalCloseOrder != null ? physicalCloseOrder.PaidPledgeBalance : 0;
            resetResult.PhysicalOriginValueBalance = physicalCloseOrder != null ? physicalCloseOrder.PhysicalOriginValueBalance : 0;
        }
예제 #4
0
        private static OrderResetResult CreateOrderResetResult(Order closeOrder, TradeDayInfo tradeDayInfo, Dictionary <Guid, OrderResetResult> resetOrderDict)
        {
            OrderResetResult resetResult;

            if (!resetOrderDict.TryGetValue(closeOrder.Id, out resetResult))
            {
                resetResult = new OrderResetResult();
                CalculateOrderRelationPLCommon(closeOrder, resetResult, tradeDayInfo);
                resetOrderDict.Add(closeOrder.Id, resetResult);
            }
            return(resetResult);
        }
예제 #5
0
 private void CalculateOrderFloatPL(Order order, OrderResetResult resetResult)
 {
     if (_info.Settings.BuyPrice != null && _info.Settings.SellPrice != null)
     {
         Price   dayClosePrice = order.IsBuy ? _info.Settings.SellPrice : _info.Settings.BuyPrice;
         Price   buyPrice      = order.IsBuy ? order.ExecutePrice : dayClosePrice;
         Price   sellPrice     = !order.IsBuy ? order.ExecutePrice : dayClosePrice;
         decimal tradePLFloat  = TradePLCalculator.Calculate(_info.Instrument.TradePLFormula, order.LotBalance * order.Owner.ContractSize(_info.TradeDay), (decimal)buyPrice, (decimal)sellPrice, (decimal)dayClosePrice);
         resetResult.DayClosePrice = dayClosePrice;
         resetResult.TradePLFloat  = _exchanger.Exchange(tradePLFloat, _info.RateSetting.RateIn, _info.RateSetting.RateOut, _info.RateSetting.RoundDecimals.Common);
     }
 }
예제 #6
0
 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);
             }
         }
     }
 }
예제 #7
0
        private OrderDayHistory CreateOrderDayHistory(Order order, OrderResetResult resetResult)
        {
            OrderDayHistory result = new OrderDayHistory();

            result.TradeDay = resetResult.TradeDay;
            Debug.Assert(order != null);
            result.OrderID                = order.Id;
            result.InstrumentID           = order.Instrument().Id;
            result.AccountID              = order.AccountId;
            result.DayInterestPLNotValued = resetResult.DayNotValuedPL.Interest;
            result.DayStoragePLNotValued  = resetResult.DayNotValuedPL.Storage;
            result.LotBalance             = order.LotBalance;
            result.StoragePerLot          = resetResult.PerLot.Storage;
            result.InterestPerLot         = resetResult.PerLot.Interest;
            result.InterestPLValued       = resetResult.ValuedPL.Interest;
            result.StoragePLValued        = resetResult.ValuedPL.Storage;
            result.TradePLValued          = resetResult.TradePLValued;
            result.InterestPLFloat        = resetResult.FloatPL.Interest;
            result.StoragePLFloat         = resetResult.FloatPL.Storage;
            result.TradePLFloat           = resetResult.TradePLFloat;
            result.CurrencyID             = resetResult.CurrencyId;
            return(result);
        }
예제 #8
0
        private static void AddResetBills(Account account, OrderResetResult orderResetResult, Order order)
        {
            List <BillValueAndType> valueAndTypeList = new List <BillValueAndType>
            {
                new BillValueAndType(orderResetResult.Margin, ResetBillType.Margin),
                new BillValueAndType(orderResetResult.PerLot.Interest, ResetBillType.InterestPerLot),
                new BillValueAndType(orderResetResult.PerLot.Storage, ResetBillType.StoragePerLot),
                new BillValueAndType(orderResetResult.FloatPL.Interest, ResetBillType.InterestPLFloat),
                new BillValueAndType(orderResetResult.FloatPL.Storage, ResetBillType.StoragePLFloat),
                new BillValueAndType(orderResetResult.TradePLFloat, ResetBillType.TradePLFloat),
                new BillValueAndType(orderResetResult.DayNotValuedPL.Interest, ResetBillType.DayInterestPLNotValued),
                new BillValueAndType(orderResetResult.DayNotValuedPL.Storage, ResetBillType.DayStoragePLNotValued),
                new BillValueAndType(orderResetResult.NotValuedPL.Interest, ResetBillType.InterestPLNotValued),
                new BillValueAndType(orderResetResult.NotValuedPL.Storage, ResetBillType.StoragePLNotValued),
                new BillValueAndType(orderResetResult.TradePLNotValued, ResetBillType.TradePLNotValued),
                new BillValueAndType(orderResetResult.ValuedPL.Interest, ResetBillType.InterestPLValued),
                new BillValueAndType(orderResetResult.ValuedPL.Storage, ResetBillType.StoragePLValued),
                new BillValueAndType(orderResetResult.TradePLValued, ResetBillType.TradePLValued),
                new BillValueAndType(orderResetResult.PhysicalPaidAmount, ResetBillType.PhysicalPaidAmount),
                new BillValueAndType(orderResetResult.PhysicalTradePLValued, ResetBillType.PhysicalTradePLValued),
                new BillValueAndType(orderResetResult.PhysicalTradePLNotValued, ResetBillType.PhysicalTradePLNotValued),
                new BillValueAndType(orderResetResult.PhysicalOriginValueBalance, ResetBillType.PhysicalOriginValueBalance),
                new BillValueAndType(orderResetResult.PaidPledgeBalance, ResetBillType.PaidPledgeBalance),
                new BillValueAndType(orderResetResult.InstalmentInterest, ResetBillType.InstalmentInterest),
                new BillValueAndType(orderResetResult.LotBalance, ResetBillType.LotBalance),
                new BillValueAndType(orderResetResult.FullPaymentCost, ResetBillType.FullPaymentCost),
                new BillValueAndType(orderResetResult.PledgeCost, ResetBillType.PledgeCost),
                new BillValueAndType(order.EstimateCloseCommission, ResetBillType.EstimateCloseCommission),
                new BillValueAndType(order.EstimateCloseLevy, ResetBillType.EstimateCloseLevy),
            };

            foreach (var eachBillValueAndType in valueAndTypeList)
            {
                account.AddResetBill(orderResetResult.OrderId, eachBillValueAndType.Value, eachBillValueAndType.Type, orderResetResult.TradeDay);
            }
        }
예제 #9
0
 public void AddOrderDayHistory(Order order, OrderResetResult orderResetResult)
 {
     _orderDayHistoryRepository.AddOrderDayHistory(order, orderResetResult);
 }