/// <summary> /// Initializes a new instance of the <see cref="T:B4F.TotalGiro.Accounts.Instructions.InstructionTypeRebalance">InstructionTypeRebalance</see> class. /// </summary> /// <param name="account">The account the withdrawal will belong to</param> /// <param name="executionDate">The date the instruction should execute</param> /// <param name="orderActionType">The type of instruction that is placed on the orders</param> /// <param name="doNotChargeCommission">The instruction is without any charges</param> /// <param name="cashTransfers">The transfers involved</param> internal InstructionTypeRebalance(IAccountTypeCustomer account, DateTime executionDate, OrderActionTypes orderActionType, bool doNotChargeCommission, IList<IJournalEntryLine> cashTransfers) : base(account, executionDate, doNotChargeCommission) { this.OrderActionType = orderActionType; this.status = RebalanceInstructionStati.New; CashTransfers.AddTransfers(cashTransfers); }
/// <summary> /// Constructor for creating a test FeeClient object /// </summary> /// <param name="account"></param> /// <param name="instrument"></param> /// <param name="side"></param> /// <param name="actiontype"></param> /// <param name="transactionDate"></param> /// <param name="issizebased"></param> /// <param name="orderValue"></param> /// <param name="amount"></param> /// <param name="price"></param> /// <param name="ordercurrency"></param> /// <param name="isValueInclComm"></param> public CommClient(IAccountTypeInternal account,IInstrument instrument, Side side, OrderActionTypes actiontype, DateTime transactionDate, bool issizebased, InstrumentSize orderValue, Money amount, Price price, ICurrency ordercurrency, bool isValueInclComm) { if (account == null) throw new ApplicationException("It is not possible to calculate the commission when the account is unknown."); if (instrument == null) throw new ApplicationException("It is not possible to calculate the commission when the instrument value is unknown."); this.account = account; this.instrument = instrument; this.Side = side; this.ActionType = actiontype; this.TransactionDate = transactionDate; this.IsSizeBased = issizebased; this.OriginalOrderType = issizebased ? BaseOrderTypes.SizeBased : BaseOrderTypes.AmountBased; this.Value = orderValue; this.amount = amount; this.Price = price; this.OrderCurrency = ordercurrency; this.IsValueInclComm = isValueInclComm; type = CommClientType.Test; }
/// <summary> /// Constructor /// </summary> /// <param name="account">User account</param> /// <param name="value">Initial value</param> /// <param name="tradedInstrument">Traded instrument</param> /// <param name="isValueInclComm">Is in the value the commission included?</param> /// <param name="feeFactory">Fee factory to use for calculating transaction costs</param> /// <param name="doNotChargeCommission">parameter that decides whether commission should be charged</param> /// <param name="actionType">The action type that created this order.</param> public OrderAmountBased(IAccountTypeInternal account, Money value, IInstrument tradedInstrument, bool isValueInclComm, IFeeFactory feeFactory, bool doNotChargeCommission, OrderActionTypes actionType) : base(account, value, (ITradeableInstrument)tradedInstrument, doNotChargeCommission) { this.ActionType = actionType; this.IsValueInclComm = isValueInclComm; checkInitialValues(); setCommission(feeFactory); // If same currency -> ExRate = 1 exRate = ((ICurrency)Value.Underlying).GetExRate(TradedInstrument.CurrencyNominal, this.Side); }
public static bool CreateRebalanceInstructions(BatchExecutionResults results, int[] accountIds, OrderActionTypes orderActionType, bool noCharges, DateTime execDate, List<RebalanceExclusionDetails> exclusions) { bool retVal = false; if (accountIds == null || accountIds.Count() == 0) throw new B4F.TotalGiro.ApplicationLayer.Common.GridviewNoSelectionException(); using (IDalSession session = NHSessionFactory.CreateSession()) { IList<IAccountTypeCustomer> accounts = AccountMapper.GetAccounts<IAccountTypeCustomer>(session, accountIds); if (accounts != null && accounts.Count > 0) { IList saveAccounts = new ArrayList(); foreach (IAccountTypeCustomer account in accounts) { try { IRebalanceInstruction instruction = (IRebalanceInstruction)account.CreateInstruction(InstructionTypes.Rebalance, orderActionType, execDate, noCharges); if (instruction != null) { if (exclusions != null && exclusions.Count > 0) { foreach (RebalanceExclusionDetails exclusion in exclusions) { switch (exclusion.ComponentType) { case ModelComponentType.Model: if (exclusion.Model == null) exclusion.Model = ModelMapper.GetModel(session, exclusion.ComponentKey); instruction.ExcludedComponents.AddExclusion(exclusion.Model); break; case ModelComponentType.Instrument: if (exclusion.Instrument == null) exclusion.Instrument = InstrumentMapper.GetTradeableInstrument(session, exclusion.ComponentKey); instruction.ExcludedComponents.AddExclusion(exclusion.Instrument); break; } } } saveAccounts.Add(account); results.MarkSuccess(); } } catch (Exception ex) { results.MarkError( new ApplicationException(string.Format("Error creating rebalance instruction for {0}.", account.DisplayNumberWithName), ex)); } } retVal = AccountMapper.UpDateList(session, saveAccounts); } } return retVal; }
public CommRuleSpecific(OrderActionTypes actionType, bool applyToAllAccounts, IAccountType accountType, IModel modelPortfolio, IAccount account, IInstrument instrument, ISecCategory ruleSecCategory, IExchange exchange, string commRuleName, FeeTypes feeType, CommRuleOpenClose openClose, CommRuleBuySell buySell, Fees.Calculations.CommCalc commCalculation) : base(commRuleName, feeType, commCalculation) { this.actionType = actionType; this.ApplyToAllAccounts = applyToAllAccounts; this.AccountType = accountType; this.ModelPortfolio = modelPortfolio; this.Account = account; this.Instrument = instrument; this.RuleSecCategory = ruleSecCategory; this.Exchange = exchange; this.OpenClose = openClose; this.BuySell = buySell; }
/// <summary> /// Constructor /// </summary> /// <param name="account">User account</param> /// <param name="value">Instrument size</param> /// <param name="isClosure">Is this order closing a position.</param> /// <param name="feeFactory">The set of rules to use for calculating transaction costs.</param> /// <param name="doNotChargeCommission">parameter that decides whether commission should be charged</param> /// <param name="actionType">The action type that created this order.</param> public OrderSizeBased(IAccountTypeInternal account, InstrumentSize value, bool isClosure, IFeeFactory feeFactory, bool doNotChargeCommission, OrderActionTypes actionType) : base(account, value, (ITradeableInstrument)value.Underlying, doNotChargeCommission) { this.ActionType = actionType; this.isClosure = isClosure; setCommission(feeFactory); // Accrued Interest for Client Orders if (account.IsAccountTypeCustomer && TradedInstrument.SecCategory.Key == SecCategories.Bond) { IBond bond = (IBond)TradedInstrument; if (bond.DoesPayInterest) { IExchange exchange = bond.DefaultExchange ?? bond.HomeExchange; AccruedInterestDetails calc = bond.AccruedInterest(value, bond.GetSettlementDate(DateTime.Today, exchange), exchange); if (calc.IsRelevant) this.AccruedInterest = calc.AccruedInterest.Abs() * (decimal)this.Side * -1M; } } }
/// <summary> /// Initializes a new instance of the <see cref="T:B4F.TotalGiro.Accounts.Instructions.BuyModelInstruction">BuyModelInstruction</see> class. /// </summary> /// <param name="account">The account the withdrawal will belong to</param> /// <param name="executionDate">The date the instruction should execute</param> /// <param name="orderActionType">The type of instruction that is placed on the orders</param> /// <param name="doNotChargeCommission">The instruction is without any charges</param> /// <param name="cashTransfers">The transfers involved</param> public BuyModelInstruction(IAccountTypeCustomer account, DateTime executionDate, OrderActionTypes orderActionType, bool doNotChargeCommission, IList<IJournalEntryLine> cashTransfers) : base(account, executionDate, orderActionType, doNotChargeCommission, cashTransfers) { //DepositCashPositionDifference = depositCashPositionDifference; }
public ICommRule GetRelevantCommRule(IAccountTypeInternal account, IInstrument instrument, Side side, OrderActionTypes actiontype, DateTime transactionDate, bool isAmountBased, out ICommClient client) { client = new CommClient(account, instrument, side, actiontype, transactionDate, !isAmountBased, null, null, null, null, false); return GetRelevantCommRule(client); }
public static bool BuyModel(int accountId, OrderActionTypes actionType, bool noCharges, decimal depositCashPositionDiff, bool includePrevCash) { return NewPortfoliosAdapter.rebalanceAccount(accountId, InstructionTypes.BuyModel, actionType, noCharges, depositCashPositionDiff, includePrevCash); }
internal static bool rebalanceAccount(int accountId, InstructionTypes instructionType, OrderActionTypes actionType, bool noCharges, decimal depositCashPositionDiff, bool includePrevCash) { bool success = false; Money diff = null; IDalSession session = NHSessionFactory.CreateSession(); IAccountTypeCustomer account = (IAccountTypeCustomer)AccountMapper.GetAccount(session, accountId); if (account.ActiveRebalanceInstructions != null && account.ActiveRebalanceInstructions.Count > 0) throw new ApplicationException(string.Format("The account {0} already has an active rebalance instruction.", account.Number)); IList<IJournalEntryLine> cashTransfers = JournalEntryMapper.GetUnProcessedCashTransfers(session, account); if (cashTransfers == null || cashTransfers.Count == 0) throw new ApplicationException("It is not possible to do this rebalance without cash transfers"); else diff = new Money(depositCashPositionDiff, cashTransfers[0].Currency); if (account.ModelPortfolio == null) throw new ApplicationException(string.Format("The account {0} does not have a model attached.", account.Number)); IInstruction instruction = account.CreateInstruction(instructionType, actionType, DateTime.Now.Date, noCharges, cashTransfers); if (instruction != null) { // check total value of the transfers if (instructionType == InstructionTypes.BuyModel) { IBuyModelInstruction bmi = (IBuyModelInstruction)instruction; if (!(bmi.CashTransfers.TotalTransferAmount.IsGreaterThanZero && account.TotalCash.IsGreaterThanZero)) throw new ApplicationException("It is not possible to do this rebalance with a zero/negative cash transfer amount"); if (!(account.ActiveWithdrawalInstructions.Count == 0 && account.ActiveMoneyTransferOrders.Count == 0)) throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since there are active money transfers/withdrawals.", account.Number)); if (account.OpenOrdersForAccount.NewCollection(x => x.Side == Side.Sell).Count > 0) throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since ther are sell orders.", account.Number)); if (account.OpenOrdersForAccount.Count > 0) { if (diff.IsLessThanZero) throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since the cash difference is negative.", account.Number)); if (account.TotalCash - account.OpenOrderAmount() < bmi.CashTransfers.TotalTransferAmount) throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since the cash is already spent.", account.Number)); } else { if (diff != null && diff.IsNotZero) { // When there is a cash difference (max 15% of the deposit) -> clear it away if (includePrevCash) { if ((bmi.CashTransfers.TotalTransferAmount + diff).IsLessThanZero) throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since there is not enough cash.", account.Number)); bmi.DepositCashPositionDifference = diff; } } } } ICurrency underlying = account.AccountOwner.StichtingDetails.BaseCurrency; InstructionEngineParameters engineParams = InstructionEngineParametersMapper.GetParameters(session); InstructionEngine engine = new InstructionEngine(engineParams); if (engine.ProcessInstruction(instruction)) { session.BeginTransaction(); AccountMapper.Update(session, account); if (instruction.UpdateableOrders != null && instruction.UpdateableOrders.Count > 0) OrderMapper.Insert(session, instruction.UpdateableOrders); success = session.CommitTransaction(); } } session.Close(); return success; }
public static bool Rebalance(int accountId, OrderActionTypes actionType, bool noCharges) { return NewPortfoliosAdapter.rebalanceAccount(accountId, InstructionTypes.Rebalance, actionType, noCharges, 0M, false); }
public bool Edit(OrderActionTypes orderActionType, DateTime executionDate, bool doNotChargeCommission) { if (!IsEditable) throw new ApplicationException("This instruction is not editable"); this.OrderActionType = orderActionType; this.ExecutionDate = executionDate; this.DoNotChargeCommission = doNotChargeCommission; return true; }
/// <summary> /// This is the method where a new rebalance instruction is created for the account. /// </summary> /// <param name="instructionType">The type of instruction, currently we only support rebalance instructions</param> /// <param name="orderActionType">The type of instruction that is placed on the orders</param> /// <param name="executionDate">The date that the rebalance should be executed</param> /// <param name="doNotChargeCommission">Determines whether commission should be charged</param> /// <param name="cashTransfers">The cash transfers involved</param> /// <returns>A new <see cref="T:B4F.TotalGiro.Accounts.Instructions.Instruction">instruction</see></returns> public IInstructionTypeRebalance CreateInstruction(InstructionTypes instructionType, OrderActionTypes orderActionType, DateTime executionDate, bool doNotChargeCommission, IList<IJournalEntryLine> cashTransfers) { IInstructionTypeRebalance instruct = null; switch (instructionType) { case InstructionTypes.Rebalance: instruct = new RebalanceInstruction(this, executionDate, orderActionType, doNotChargeCommission, cashTransfers); break; case InstructionTypes.BuyModel: instruct = new BuyModelInstruction(this, executionDate, orderActionType, doNotChargeCommission, cashTransfers); break; default: throw new ApplicationException("Wrong instruction type for this method"); } if (instruct != null) { AccountInstructions.Add(instruct); ActiveAccountInstructions.Add(instruct); } return instruct; }
/// <summary> /// This is the method where a new rebalance instruction is created for the account. /// </summary> /// <param name="instructionType">The type of instruction, currently we only support rebalance instructions</param> /// <param name="orderActionType">The type of instruction that is placed on the orders</param> /// <param name="executionDate">The date that the rebalance should be executed</param> /// <returns>A new <see cref="T:B4F.TotalGiro.Accounts.Instructions.Instruction">instruction</see></returns> public IInstructionTypeRebalance CreateInstruction(InstructionTypes instructionType, OrderActionTypes orderActionType, DateTime executionDate, bool doNotChargeCommission) { return CreateInstruction(instructionType, orderActionType, executionDate, doNotChargeCommission, null); }