Esempio n. 1
0
        protected override BuySellLot CalculateSameDirectionLotsOfPendingOrders()
        {
            var result = base.CalculateSameDirectionLotsOfPendingOrders();
            var buyDirectionLotBalance = this.CalculateBuyDirectionLotBalance();

            result += new BuySellLot(buyDirectionLotBalance, 0m);
            return(result);
        }
        protected override bool ShouldCheckMargin(Transaction tran, BuySellLot oldLots)
        {
            bool needCheck = !this.IsFreeOfMarginCheck(tran, oldLots);

            foreach (PhysicalOrder eachOrder in tran.Orders)
            {
                needCheck |= eachOrder.IsOpen && eachOrder.PhysicalTradeSide == PhysicalTradeSide.Buy && eachOrder.IsPayoff;
            }
            return(needCheck);
        }
        protected override bool ShouldCheckMargin(Transaction tran, BuySellLot oldLots)
        {
            bool needCheck = !this.IsFreeOfMarginCheck(tran, oldLots);

            foreach (Order order in tran.Orders)
            {
                needCheck |= order.IsOpen;
            }
            return(needCheck);
        }
Esempio n. 4
0
        private Tuple <decimal, decimal> CalculateNetPosition()
        {
            var oldLotBalance = new BuySellLot(_instrument.TotalBuyLotBalance, _instrument.TotalSellLotBalance);

            oldLotBalance += this.CalculateSameDirectionLotsOfPendingOrders();
            bool asRisk         = this.ShouldCalculateTranLotBalanceAsRisk();
            var  tranLotBalance = this.CalculateLotBalanceForPreCheck(asRisk);
            var  oldNetPosition = oldLotBalance.NetPosition;
            var  newNetPosition = oldNetPosition + tranLotBalance.NetPosition;

            return(Tuple.Create(oldNetPosition, newNetPosition));
        }
Esempio n. 5
0
        internal static BuySellLot CalculateLotBalanceForPreCheck(this Transaction tran, bool asRisk = false)
        {
            BuySellLot result = BuySellLot.Empty;

            foreach (var eachOrder in tran.Orders)
            {
                if (tran.ShouldCalculateOrderLotBalance(eachOrder, asRisk))
                {
                    result += eachOrder.CalculateLotBalance();
                }
            }
            return(result);
        }
Esempio n. 6
0
        protected BuySellLot CalculateLotBalanceForPreCheck(bool asRisk = false)
        {
            BuySellLot result = BuySellLot.Empty;

            foreach (var eachOrder in _tran.Orders)
            {
                if (this.ShouldCalculateOrderLotBalance(eachOrder, asRisk))
                {
                    result += this.CalculateOrderLotBalance(eachOrder);
                }
            }
            return(result);
        }
Esempio n. 7
0
        protected virtual BuySellLot CalculateSameDirectionLotsOfPendingOrders()
        {
            if (!_tran.IsPending)
            {
                return(BuySellLot.Empty);
            }
            BuySellLot result = BuySellLot.Empty;

            foreach (Transaction eachTran in _instrument.GetTransactions())
            {
                if (this.ShouldSumPlaceMargin(eachTran))
                {
                    result += this.CalculateLotBalanceForPreCheck();
                }
            }
            return(result);
        }
Esempio n. 8
0
        internal static bool IsExceedMaxOpenLot(Transaction tran, PlaceContext context, out string errorInfo)
        {
            errorInfo = string.Empty;
            if (!tran.ShouldCheckIsExceedMaxOpenLot)
            {
                return(false);
            }
            TradePolicyDetail tradePolicyDetail = tran.TradePolicyDetail(context.TradeDay);
            var settingAccount = tran.Owner.Setting(context.TradeDay);

            if (settingAccount.MaxOpenLot == null && tradePolicyDetail.AccountMaxOpenLot == null)
            {
                return(false);
            }
            if (!tran.ExistsOpenOrder())
            {
                return(false);
            }
            bool    isBuy = tran.FirstOrder.IsBuy;
            var     placingLotsPerInstrument = CalculateBuySellInfoPerInstrument(tran.Owner.Transactions);
            decimal totalPlacingOpenLot = 0, totalPlacingOpenLotOfTheInstrument = 0;

            foreach (Guid instrumentId in placingLotsPerInstrument.Keys)
            {
                BuySellLot info = placingLotsPerInstrument[instrumentId];
                if (instrumentId == tran.InstrumentId)
                {
                    totalPlacingOpenLotOfTheInstrument = isBuy ? info.BuyLot : info.SellLot;
                    totalPlacingOpenLot += (isBuy ? info.BuyLot : info.SellLot);
                }
                else
                {
                    totalPlacingOpenLot += Math.Max(info.BuyLot, info.SellLot);
                }
            }
            decimal?accountMaxOpenLot = settingAccount.MaxOpenLot;

            if ((accountMaxOpenLot != null && totalPlacingOpenLot > accountMaxOpenLot) ||
                (tradePolicyDetail.AccountMaxOpenLot != null && totalPlacingOpenLotOfTheInstrument > tradePolicyDetail.AccountMaxOpenLot))
            {
                errorInfo = string.Format("totalPlacingOpenLot={0}, totalPlacingOpenLotOfTheInstrument={1}, account.MaxOpenLot={2}, tradePolicyDetail.AccountMaxOpenLot={3}, tranID = {4}, accountID = {5}",
                                          totalPlacingOpenLot, totalPlacingOpenLotOfTheInstrument, accountMaxOpenLot, tradePolicyDetail.AccountMaxOpenLot, tran.Id, tran.AccountId);
                return(true);
            }
            return(false);
        }
 internal bool IsMarginEnoughToExecute(Transaction tran, BuySellLot oldLots, decimal lastEquity, out string errorDetail)
 {
     errorDetail = string.Empty;
     if (this.ShouldCheckMargin(tran, oldLots))
     {
         decimal fee     = SumFee(tran);
         var     account = tran.Owner;
         if (account.IsMultiCurrency)
         {
             fee = tran.CurrencyRate(null).Exchange(fee, Settings.ExchangeDirection.RateOut);
         }
         if (!tran.Owner.HasEnoughMoneyToFill(tran.AccountInstrument, tran.ExistsCloseOrder(), fee, tran.FirstOrder.IsFreeOfNecessaryCheck, lastEquity, out errorDetail))
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 10
0
        internal bool IsFreeOfFillMarginCheck(BuySellLot oldLots, bool existsCloseOrder)
        {
            TradePolicy tradePolicy = this.Owner.Setting().TradePolicy();
            BuySellLot  newLots     = this.GetBuySellLotBalance();

            if (Math.Abs(newLots.NetPosition) <= Math.Abs(oldLots.NetPosition))
            {
                if (tradePolicy.IsFreeOverHedge)
                {
                    return(true);
                }
                if ((newLots.NetPosition * oldLots.NetPosition >= 0) && (existsCloseOrder || tradePolicy.IsFreeHedge))
                {
                    return(true);
                }
            }
            Logger.InfoFormat("IsFreeOfFillMarginCheck, netLots = {0}, oldLots = {1}, accountId = {2}", newLots.ToString(), oldLots.ToString(), _owner.Id);
            return(false);
        }
Esempio n. 11
0
        internal bool IsInAlerting(Instrument instrument, BuySellLot oldLotBalance)
        {
            //0-Unrestricted|1-AlertLevel1|2-AlertLevel2|3-AlertLevel3|4-AlertLevel3 Reduce Position
            if (_account.Setting().ForbiddenAlert < 1 ||
                _account.Setting().ForbiddenAlert < 4 && (int)this.AlertLevel < _account.Setting().ForbiddenAlert ||
                _account.Setting().ForbiddenAlert == 4 && this.AlertLevel < AlertLevel.Cut)
            {
                return(false);
            }
            var currentLotBalance = instrument.GetBuySellLotBalance();

            if (_account.Setting().ForbiddenAlert == 4 && this.AlertLevel == AlertLevel.Cut)
            {
                return(Math.Abs(oldLotBalance.NetPosition) != Math.Abs(currentLotBalance.NetPosition) || oldLotBalance.IsNetPosWithDiffDirection(currentLotBalance));
            }
            else
            {
                return(oldLotBalance.IsAbsNetPosLessThan(currentLotBalance) || oldLotBalance.IsNetPosWithDiffDirection(currentLotBalance));
            }
        }
 protected override bool ShouldCheckMargin(Transaction tran, BuySellLot oldLots)
 {
     return(!this.IsFreeOfMarginCheck(tran, oldLots));
 }
 protected abstract bool ShouldCheckMargin(Transaction tran, BuySellLot oldLots);
        protected bool IsFreeOfMarginCheck(Transaction tran, BuySellLot oldLots)
        {
            bool existsCloseOrder = tran.ExistsCloseOrder();

            return(tran.AccountInstrument.IsFreeOfFillMarginCheck(oldLots, existsCloseOrder));
        }
Esempio n. 15
0
        private void ExecuteOrdersAndVerify(Transaction tran, ExecuteContext context, BuySellLot oldLots)
        {
            decimal lastEquity   = tran.Owner.Equity;
            decimal deltaBalance = tran.ExecuteOrders(context, order =>
            {
                if (order.Phase != OrderPhase.Executed)
                {
                    Logger.WarnFormat("ExecuteOrders orderId = {0} phase = {1} not equal executed", order.Id, order.Phase);
                    return(false);
                }
                return(true);
            });

            tran.Owner.InvalidateInstrumentCache(tran);
            this.VerifyAlertLevelAndNecessary(tran, oldLots, context, lastEquity);
            if (context.IsBook)
            {
                tran.Owner.AddBalance(tran.CurrencyId, -deltaBalance, context.ExecuteTime);
            }
        }