public bool HasEnoughMoneyToPlace(Transaction tran) { if (tran.IsFreeOfPlaceMarginCheck()) { return(true); } Account account = tran.Owner; AccountClass.Instrument instrument = account.GetOrCreateInstrument(tran.InstrumentId); decimal placeCheckNecessary = account.CalculatePreCheckNecessary(tran); decimal placeCheckBalance = account.CalculatePreCheckBalance(); decimal credit = account.CalculateCredit(instrument); decimal unclearBalance = account.CaculateUnclearBalance(); TradePolicy tradePolicy = account.Setting.TradePolicy; MarginCheckOption marginCheckOption = tradePolicy.OpenNecessaryPolicy.MarginCheckOption; bool isMarginEnough = true; if (marginCheckOption == MarginCheckOption.Balance || marginCheckOption == MarginCheckOption.All) { decimal necessary = account.SumFund.Balance - unclearBalance - placeCheckBalance + account.Setting.ShortMargin + credit + account.SumFund.InterestNotValued + account.SumFund.StorageNotValued + account.SumFund.TradePLNotValued; isMarginEnough &= placeCheckNecessary <= necessary; } if (marginCheckOption == MarginCheckOption.Equity || marginCheckOption == MarginCheckOption.All) { decimal equity = account.SumFund.Equity - unclearBalance - placeCheckBalance + account.Setting.ShortMargin + credit; isMarginEnough &= placeCheckNecessary <= equity; } return(isMarginEnough); }
private MarginCheckOption GetMarginCheckOption(bool existCloseOrder, TradePolicy tradePolicy) { if (existCloseOrder) { return(tradePolicy.CloseNecessaryPolicy.MarginCheckOption); } else { return(tradePolicy.OpenNecessaryPolicy.MarginCheckOption); } }
internal bool HasEnoughMoneyToFill(AccountClass.Instrument instrument, bool existsCloseOrder, decimal fee, bool isNecessaryFreeOrder, decimal lastEquity, bool isForPayoff, out string errorInfo) { decimal riskCredit = this.CalculateCredit(instrument.Id); decimal unclearBalance = _owner.UnclearDepositManager.Sum(); TradePolicy tradePolicy = _owner.Setting().TradePolicy(null); MarginCheckOption marginCheckOption = this.GetMarginCheckOption(existsCloseOrder, tradePolicy); decimal fillCheckNecessary = existsCloseOrder ? _fund.RiskRawData.NecessaryFillingCloseOrder : _fund.RiskRawData.NecessaryFillingOpenOrder; errorInfo = string.Empty; bool isBalanceEnough = this.CheckBalanceIsEnough(instrument.IsPhysical, marginCheckOption, unclearBalance, fillCheckNecessary, out errorInfo); bool isEquityEnough = this.CheckEquityIsEnough(instrument.IsPhysical, marginCheckOption, unclearBalance, fillCheckNecessary, fee, isForPayoff, riskCredit, isNecessaryFreeOrder, lastEquity, out errorInfo); Logger.Warn(this.BuildLoggerInfo(existsCloseOrder, riskCredit, unclearBalance, marginCheckOption, instrument.Id, instrument.Owner, lastEquity)); return(isBalanceEnough && isEquityEnough); }
private static void Initialize(TradePolicy tradePolicy, DataRow dataRow) { tradePolicy.Id = (Guid)dataRow["ID"]; if (dataRow.Table.Columns.Contains("AlertLevel1")) { tradePolicy.AlertLevel1 = dataRow.GetItemValue <decimal>("AlertLevel1", 0); } if (dataRow.Table.Columns.Contains("AlertLevel2")) { tradePolicy.AlertLevel2 = dataRow.GetItemValue <decimal>("AlertLevel2", 0); } if (dataRow.Table.Columns.Contains("AlertLevel3")) { tradePolicy.AlertLevel3 = dataRow.GetItemValue <decimal>("AlertLevel3", 0); } }
private void CalculateNecessary(ref NecessaryAndQuantity necessaryAndQuantity, DateTime baseTime) { decimal netNecessary = 0, hedgeNecessary = 0; this.CalculateNetAndHedgeNecessary(necessaryAndQuantity.buyNecessarySum, necessaryAndQuantity.sellNecessarySum, necessaryAndQuantity.buyQuantitySum, necessaryAndQuantity.sellQuantitySum, ref necessaryAndQuantity.partialPhysicalNecessarySum, out netNecessary, out hedgeNecessary); _riskData.NetNecessary = netNecessary; _riskData.HedgeNecessary = hedgeNecessary; TradePolicy tradePolicy = _owner.Owner.Setting().TradePolicy(); int decimals = Math.Min(_owner.Currency(null).Decimals, this.GetTradePolicyDetail().NecessaryRound); _riskData.Necessary = Math.Round(netNecessary + hedgeNecessary, decimals, MidpointRounding.AwayFromZero); this.CalculateMinEquitities(netNecessary, hedgeNecessary, baseTime, decimals); _riskData.NecessaryFillingOpenOrder = Math.Round(tradePolicy.OpenNecessaryPolicy.Calculate(netNecessary, hedgeNecessary), decimals, MidpointRounding.AwayFromZero); _riskData.NecessaryFillingCloseOrder = Math.Round(tradePolicy.CloseNecessaryPolicy.Calculate(netNecessary, hedgeNecessary), decimals, MidpointRounding.AwayFromZero); _riskData.PartialPaymentPhysicalNecessary = necessaryAndQuantity.partialPhysicalNecessarySum; }
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); }
internal static bool HasEnoughMoneyToPlace(this Account account, Transaction tran) { if (tran.IsFreeOfPlaceMarginCheck()) { return(true); } AccountClass.Instrument instrument = account.GetOrCreateInstrument(tran.InstrumentId); decimal preCheckNecessaryForBalanceCheckOption = 0; decimal placeCheckNecessary = account.CalculatePreCheckNecessary(tran, out preCheckNecessaryForBalanceCheckOption); decimal placeCheckBalance = account.CalculatePreCheckBalance(); decimal credit = account.CalculateCredit(instrument); decimal unclearBalance = account.CaculateUnclearBalance(); TradePolicy tradePolicy = account.Setting().TradePolicy(null); MarginCheckOption marginCheckOption = tradePolicy.OpenNecessaryPolicy.MarginCheckOption; bool isMarginEnough = true; decimal balanceRemainAmount = account.Balance - unclearBalance - placeCheckBalance + account.Setting().ShortMargin + credit; Logger.InfoFormat("precheck placeCheckNecessary = {0}, placeCheckBalance = {1}, credit = {2}, unclearBalance = {3}, balanceRemainAmount = {4}, ShortMargin = {5} , accountId = {6}, marginCheckOption = {7}, tranId = {8}, isPhysical = {9}, equity = {10}", placeCheckNecessary, placeCheckBalance, credit, unclearBalance, balanceRemainAmount, account.Setting().ShortMargin, account.Id, marginCheckOption, tran.Id, tran.IsPhysical, account.Equity); if (marginCheckOption == MarginCheckOption.Balance || marginCheckOption == MarginCheckOption.All) { if (tran.IsPhysical) { isMarginEnough &= balanceRemainAmount >= 0; } else { isMarginEnough &= preCheckNecessaryForBalanceCheckOption <= balanceRemainAmount; } } if (marginCheckOption == MarginCheckOption.Equity || marginCheckOption == MarginCheckOption.All) { decimal equity = account.Equity - unclearBalance - placeCheckBalance + account.Setting().ShortMargin + credit; isMarginEnough &= placeCheckNecessary <= equity; } return(isMarginEnough); }