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); }
internal void Update(XElement necessaryPolicyNode, string prefix) { int prefixLength = prefix.Length; foreach (var attribute in necessaryPolicyNode.Attributes()) { if (!attribute.Name.ToString().StartsWith(prefix)) { continue; } switch (attribute.Name.ToString().Substring(prefixLength)) { case "MarginCheckOption": this.marginCheckOption = (MarginCheckOption)XmlConvert.ToInt32(attribute.Value); break; case "NetFactor": this.netFactor = XmlConvert.ToDecimal(attribute.Value); break; case "HedgeFactor": this.hedgeFactor = XmlConvert.ToDecimal(attribute.Value); break; } } }
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 bool CheckEquityIsEnough(bool isPhysical, MarginCheckOption marginCheckOption, decimal unclearBalance, decimal fillCheckNecessary, decimal fee, bool isForPayoff, decimal riskCredit, bool isNecessaryFreeOrder, decimal lastEquity, out string errorInfo) { errorInfo = string.Empty; if (marginCheckOption == MarginCheckOption.Equity || marginCheckOption == MarginCheckOption.All) { decimal equity = isNecessaryFreeOrder ? _fund.Equity : lastEquity; if (isForPayoff) { equity -= fee; } var equityForCheck = equity - unclearBalance + _owner.ShortMargin + riskCredit; bool result = equityForCheck >= fillCheckNecessary; if (!result) { errorInfo = string.Format("CheckEquityIsEnough failed, equityForCheck = {0}, comparedEquity = {1}, equity = {2}, fee = {3}, lastEquity = {4}, isNecessaryFreeOrder = {5}, balance = {6}, necessary = {7}, accountId = {8}", equityForCheck, fillCheckNecessary, equity, fee, lastEquity, isNecessaryFreeOrder, _owner.Balance, _owner.Necessary, _owner.Id); } return(result); } return(true); }
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); }
private bool CheckBalanceIsEnough(bool isPhysical, MarginCheckOption marginCheckOption, decimal unclearBalance, decimal fillCheckNecessary, out string errorInfo) { errorInfo = string.Empty; if (marginCheckOption == MarginCheckOption.Balance || marginCheckOption == MarginCheckOption.All) { bool result = true; if (isPhysical) { result = this.CheckBalanceIsEnoughForPhysicalOrder(unclearBalance); } else { result = this.CheckBalanceIsEnoughForNormal(unclearBalance, fillCheckNecessary); } if (!result) { errorInfo = this.BuildCheckBalanceFailedInfo(isPhysical, unclearBalance, fillCheckNecessary); } return(result); } return(true); }
private string BuildLoggerInfo(bool existsCloseOrder, decimal riskCredit, decimal unclearBalance, MarginCheckOption marginCheckOption, Guid instrumentId, Account account, decimal lastEquity) { var fund = account.SumFund; StringBuilder sb = Protocal.StringBuilderCache.Acquire(200); sb.Append("HasEnoughMoneyToFill NecessaryFillingCloseOrder ="); sb.Append(fund.RiskRawData.NecessaryFillingCloseOrder); sb.Append(", NecessaryFillingOpenOrder ="); sb.Append(fund.RiskRawData.NecessaryFillingOpenOrder); sb.Append(", existsCloseOrder ="); sb.Append(existsCloseOrder); sb.Append(", riskCredit = "); sb.Append(riskCredit); sb.Append(", unclearBalance = "); sb.Append(unclearBalance); sb.Append(", marginCheckOption ="); sb.Append(marginCheckOption); sb.Append(", shortMargin = "); sb.Append(account.Setting().ShortMargin); sb.Append(", equity = "); sb.Append(account.SumFund.Equity); sb.Append(", lastEquity = "); sb.Append(lastEquity); sb.Append(", instrumentId = "); sb.Append(instrumentId); sb.Append(", accountId = "); sb.Append(account.Id); return(Protocal.StringBuilderCache.GetStringAndRelease(sb)); }
internal NecessaryPolicy(IDBRow necessaryPolicyRow, string prefix) { this.marginCheckOption = (MarginCheckOption)(int)necessaryPolicyRow[prefix + "MarginCheckOption"]; this.netFactor = (decimal)necessaryPolicyRow[prefix + "NetFactor"]; this.hedgeFactor = (decimal)necessaryPolicyRow[prefix + "HedgeFactor"]; }